Skip to content

Commit 38006e8

Browse files
xrmxemdnetolzchen
authored
opentelemetry-instrumentation: expose a way to init autoinstrumentation programmatically (#3273)
* opentelemetry-instrumentation: expose a way to init autoinstrumentation * Please pylint * Add changelog * Fix example * Fix whitespace in README * Add a note aboout ordering of initialization vs imports * Don't touch PYTHONPATH if not set * Update opentelemetry-instrumentation/README.rst Co-authored-by: Emídio Neto <[email protected]> * Update CHANGELOG.md * Update opentelemetry-instrumentation/README.rst Co-authored-by: Leighton Chen <[email protected]> --------- Co-authored-by: Emídio Neto <[email protected]> Co-authored-by: Leighton Chen <[email protected]>
1 parent b1f714e commit 38006e8

File tree

6 files changed

+128
-28
lines changed

6 files changed

+128
-28
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
([#3266](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3266))
2222
- `opentelemetry-instrumentation-botocore` Add support for GenAI choice events
2323
([#3275](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3275))
24+
- `opentelemetry-instrumentation` make it simpler to initialize auto-instrumentation programmatically
25+
([#3273](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3273))
2426

2527
### Fixed
2628

opentelemetry-instrumentation/README.rst

+13
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ start celery with the rest of the arguments.
130130
The above command will configure the global trace provider to use the Random IDs Generator, and then
131131
pass ``--port=3000`` to ``flask run``.
132132

133+
Programmatic Auto-instrumentation
134+
--------------------
135+
136+
::
137+
138+
from opentelemetry.instrumentation import auto_instrumentation
139+
auto_instrumentation.initialize()
140+
141+
142+
If you are in an environment where you cannot use opentelemetry-instrument to inject auto-instrumentation you can do so programmatically with
143+
the code above. Please note that some instrumentations may require the ``initialize()`` method to be called before the library they
144+
instrument is imported.
145+
133146
References
134147
----------
135148

opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/__init__.py

+23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
from re import sub
2020
from shutil import which
2121

22+
from opentelemetry.instrumentation.auto_instrumentation._load import (
23+
_load_configurators,
24+
_load_distro,
25+
_load_instrumentors,
26+
)
27+
from opentelemetry.instrumentation.utils import _python_path_without_directory
2228
from opentelemetry.instrumentation.version import __version__
2329
from opentelemetry.util._importlib_metadata import entry_points
2430

@@ -110,3 +116,20 @@ def run() -> None:
110116

111117
executable = which(args.command)
112118
execl(executable, executable, *args.command_args)
119+
120+
121+
def initialize():
122+
"""Setup auto-instrumentation, called by the sitecustomize module"""
123+
# prevents auto-instrumentation of subprocesses if code execs another python process
124+
if "PYTHONPATH" in environ:
125+
environ["PYTHONPATH"] = _python_path_without_directory(
126+
environ["PYTHONPATH"], dirname(abspath(__file__)), pathsep
127+
)
128+
129+
try:
130+
distro = _load_distro()
131+
distro.configure()
132+
_load_configurators()
133+
_load_instrumentors(distro)
134+
except Exception: # pylint: disable=broad-except
135+
_logger.exception("Failed to auto initialize OpenTelemetry")

opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py

+1-28
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from logging import getLogger
16-
from os import environ
17-
from os.path import abspath, dirname, pathsep
18-
19-
from opentelemetry.instrumentation.auto_instrumentation._load import (
20-
_load_configurators,
21-
_load_distro,
22-
_load_instrumentors,
23-
)
24-
from opentelemetry.instrumentation.utils import _python_path_without_directory
25-
26-
logger = getLogger(__name__)
27-
28-
29-
def initialize():
30-
# prevents auto-instrumentation of subprocesses if code execs another python process
31-
environ["PYTHONPATH"] = _python_path_without_directory(
32-
environ["PYTHONPATH"], dirname(abspath(__file__)), pathsep
33-
)
34-
35-
try:
36-
distro = _load_distro()
37-
distro.configure()
38-
_load_configurators()
39-
_load_instrumentors(distro)
40-
except Exception: # pylint: disable=broad-except
41-
logger.exception("Failed to auto initialize opentelemetry")
42-
15+
from opentelemetry.instrumentation.auto_instrumentation import initialize
4316

4417
initialize()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# type: ignore
15+
16+
from os import environ
17+
from os.path import abspath, dirname, pathsep
18+
from unittest import TestCase
19+
from unittest.mock import patch
20+
21+
from opentelemetry.instrumentation import auto_instrumentation
22+
23+
# TODO: convert to assertNoLogs instead of mocking logger when 3.10 is baseline
24+
25+
26+
class TestInitialize(TestCase):
27+
auto_instrumentation_path = dirname(abspath(auto_instrumentation.__file__))
28+
29+
@patch.dict("os.environ", {}, clear=True)
30+
@patch("opentelemetry.instrumentation.auto_instrumentation._logger")
31+
def test_handles_pythonpath_not_set(self, logger_mock):
32+
auto_instrumentation.initialize()
33+
self.assertNotIn("PYTHONPATH", environ)
34+
logger_mock.exception.assert_not_called()
35+
36+
@patch.dict("os.environ", {"PYTHONPATH": "."})
37+
@patch("opentelemetry.instrumentation.auto_instrumentation._logger")
38+
def test_handles_pythonpath_set(self, logger_mock):
39+
auto_instrumentation.initialize()
40+
self.assertEqual(environ["PYTHONPATH"], ".")
41+
logger_mock.exception.assert_not_called()
42+
43+
@patch.dict(
44+
"os.environ",
45+
{"PYTHONPATH": auto_instrumentation_path + pathsep + "foo"},
46+
)
47+
@patch("opentelemetry.instrumentation.auto_instrumentation._logger")
48+
def test_clears_auto_instrumentation_path(self, logger_mock):
49+
auto_instrumentation.initialize()
50+
self.assertEqual(environ["PYTHONPATH"], "foo")
51+
logger_mock.exception.assert_not_called()
52+
53+
@patch("opentelemetry.instrumentation.auto_instrumentation._logger")
54+
@patch("opentelemetry.instrumentation.auto_instrumentation._load_distro")
55+
def test_handles_exceptions(self, load_distro_mock, logger_mock):
56+
# pylint:disable=no-self-use
57+
load_distro_mock.side_effect = ValueError
58+
auto_instrumentation.initialize()
59+
logger_mock.exception.assert_called_once_with(
60+
"Failed to auto initialize OpenTelemetry"
61+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# type: ignore
15+
16+
from unittest import TestCase
17+
from unittest.mock import patch
18+
19+
20+
class TestSiteCustomize(TestCase):
21+
# pylint:disable=import-outside-toplevel,unused-import,no-self-use
22+
@patch("opentelemetry.instrumentation.auto_instrumentation.initialize")
23+
def test_sitecustomize_side_effects(self, initialize_mock):
24+
initialize_mock.assert_not_called()
25+
26+
import opentelemetry.instrumentation.auto_instrumentation.sitecustomize # NOQA
27+
28+
initialize_mock.assert_called_once()

0 commit comments

Comments
 (0)