Skip to content

Commit b1511ec

Browse files
committed
opentelemetry-instrumentation: expose a way to init autoinstrumentation
1 parent 789bf86 commit b1511ec

File tree

5 files changed

+119
-28
lines changed

5 files changed

+119
-28
lines changed

opentelemetry-instrumentation/README.rst

+14
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ 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+
auto-instrumentation
134+
--------------------
135+
136+
::
137+
138+
from opentelemetry.instrumentation import autoinstrumentation
139+
autoinstrumenttion.initialize()
140+
141+
142+
If you are in an environment where you cannot use opentelemetry-instrument to inject auto-instrumentation you can do so manually with
143+
the code above.
144+
145+
146+
133147
References
134148
----------
135149

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

+22
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,19 @@ 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+
environ["PYTHONPATH"] = _python_path_without_directory(
125+
environ.get("PYTHONPATH", ""), dirname(abspath(__file__)), pathsep
126+
)
127+
128+
try:
129+
distro = _load_distro()
130+
distro.configure()
131+
_load_configurators()
132+
_load_instrumentors(distro)
133+
except Exception: # pylint: disable=broad-except
134+
_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,55 @@
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, getcwd
17+
from os.path import 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+
@patch.dict("os.environ", {}, clear=True)
28+
@patch("opentelemetry.instrumentation.auto_instrumentation._logger")
29+
def test_handles_pythonpath_not_set(self, logger_mock):
30+
auto_instrumentation.initialize()
31+
self.assertEqual(
32+
environ["PYTHONPATH"],
33+
pathsep.join([self.auto_instrumentation_path, getcwd()]),
34+
)
35+
logger_mock.exception.assert_not_called()
36+
37+
@patch.dict("os.environ", {"PYTHONPATH": "."})
38+
@patch("opentelemetry.instrumentation.auto_instrumentation._logger")
39+
def test_handles_pythonpath_set(self, logger_mock):
40+
auto_instrumentation.initialize()
41+
self.assertEqual(
42+
environ["PYTHONPATH"],
43+
pathsep.join([self.auto_instrumentation_path, getcwd()]),
44+
)
45+
46+
logger_mock.exception.assert_not_called()
47+
48+
@patch("opentelemetry.instrumentation.auto_instrumentation._logger")
49+
@patch("opentelemetry.instrumentation.auto_instrumentation._load_distro")
50+
def test_handles_exceptions(self, load_distro_mock, logger_mock):
51+
load_distro_mock.side_effect = ValueError
52+
auto_instrumentation.initialize()
53+
logger_mock.exception.assert_called_once_with(
54+
"Failed to auto initialize OpenTelemetry"
55+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
@patch("opentelemetry.instrumentation.auto_instrumentation.initialize")
22+
def test_sitecustomize_side_effects(self, initialize_mock):
23+
initialize_mock.assert_not_called()
24+
25+
import opentelemetry.instrumentation.auto_instrumentation.sitecustomize # NOQA
26+
27+
initialize_mock.assert_called_once()

0 commit comments

Comments
 (0)