Skip to content

Commit 189566d

Browse files
committed
opentelemetry-instrumentation: make it easier to use bootstrap with custom values (open-telemetry#2918)
1 parent 6f1ca05 commit 189566d

File tree

2 files changed

+66
-21
lines changed

2 files changed

+66
-21
lines changed

Diff for: opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap.py

+27-11
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
SubprocessError,
2323
check_call,
2424
)
25+
from typing import Optional
2526

2627
from packaging.requirements import Requirement
2728

2829
from opentelemetry.instrumentation.bootstrap_gen import (
29-
default_instrumentations,
30-
libraries,
30+
default_instrumentations as gen_default_instrumentations,
31+
)
32+
from opentelemetry.instrumentation.bootstrap_gen import (
33+
libraries as gen_libraries,
3134
)
3235
from opentelemetry.instrumentation.version import __version__
3336
from opentelemetry.util._importlib_metadata import (
@@ -75,7 +78,7 @@ def _sys_pip_install(package):
7578
print(error)
7679

7780

78-
def _pip_check():
81+
def _pip_check(libraries):
7982
"""Ensures none of the instrumentations have dependency conflicts.
8083
Clean check reported as:
8184
'No broken requirements found.'
@@ -113,7 +116,7 @@ def _is_installed(req):
113116
return True
114117

115118

116-
def _find_installed_libraries():
119+
def _find_installed_libraries(default_instrumentations, libraries):
117120
for lib in default_instrumentations:
118121
yield lib
119122

@@ -122,18 +125,25 @@ def _find_installed_libraries():
122125
yield lib["instrumentation"]
123126

124127

125-
def _run_requirements():
128+
def _run_requirements(default_instrumentations, libraries):
126129
logger.setLevel(logging.ERROR)
127-
print("\n".join(_find_installed_libraries()))
130+
print(
131+
"\n".join(
132+
_find_installed_libraries(default_instrumentations, libraries)
133+
)
134+
)
128135

129136

130-
def _run_install():
131-
for lib in _find_installed_libraries():
137+
def _run_install(default_instrumentations, libraries):
138+
for lib in _find_installed_libraries(default_instrumentations, libraries):
132139
_sys_pip_install(lib)
133-
_pip_check()
140+
_pip_check(libraries)
134141

135142

136-
def run() -> None:
143+
def run(
144+
default_instrumentations: Optional[list] = None,
145+
libraries: Optional[list] = None,
146+
) -> None:
137147
action_install = "install"
138148
action_requirements = "requirements"
139149

@@ -163,8 +173,14 @@ def run() -> None:
163173
)
164174
args = parser.parse_args()
165175

176+
if libraries is None:
177+
libraries = gen_libraries
178+
179+
if default_instrumentations is None:
180+
default_instrumentations = gen_default_instrumentations
181+
166182
cmd = {
167183
action_install: _run_install,
168184
action_requirements: _run_requirements,
169185
}[args.action]
170-
cmd()
186+
cmd(default_instrumentations, libraries)

Diff for: opentelemetry-instrumentation/tests/test_bootstrap.py

+39-10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
from unittest.mock import call, patch
2020

2121
from opentelemetry.instrumentation import bootstrap
22-
from opentelemetry.instrumentation.bootstrap_gen import libraries
22+
from opentelemetry.instrumentation.bootstrap_gen import (
23+
default_instrumentations,
24+
libraries,
25+
)
2326

2427

2528
def sample_packages(packages, rate):
@@ -56,15 +59,15 @@ def setUpClass(cls):
5659
"opentelemetry.instrumentation.bootstrap._pip_check",
5760
)
5861

59-
cls.pkg_patcher.start()
60-
cls.mock_pip_install = cls.pip_install_patcher.start()
61-
cls.mock_pip_check = cls.pip_check_patcher.start()
62+
def setUp(self):
63+
super().setUp()
64+
self.mock_pip_check = self.pip_check_patcher.start()
65+
self.mock_pip_install = self.pip_install_patcher.start()
6266

63-
@classmethod
64-
def tearDownClass(cls):
65-
cls.pip_check_patcher.start()
66-
cls.pip_install_patcher.start()
67-
cls.pkg_patcher.stop()
67+
def tearDown(self):
68+
super().tearDown()
69+
self.pip_check_patcher.stop()
70+
self.pip_install_patcher.stop()
6871

6972
@patch("sys.argv", ["bootstrap", "-a", "pipenv"])
7073
def test_run_unknown_cmd(self):
@@ -73,18 +76,44 @@ def test_run_unknown_cmd(self):
7376

7477
@patch("sys.argv", ["bootstrap", "-a", "requirements"])
7578
def test_run_cmd_print(self):
79+
self.pkg_patcher.start()
7680
with patch("sys.stdout", new=StringIO()) as fake_out:
7781
bootstrap.run()
7882
self.assertEqual(
7983
fake_out.getvalue(),
8084
"\n".join(self.installed_libraries) + "\n",
8185
)
86+
self.pkg_patcher.stop()
8287

8388
@patch("sys.argv", ["bootstrap", "-a", "install"])
8489
def test_run_cmd_install(self):
90+
self.pkg_patcher.start()
8591
bootstrap.run()
8692
self.mock_pip_install.assert_has_calls(
8793
[call(i) for i in self.installed_libraries],
8894
any_order=True,
8995
)
90-
self.assertEqual(self.mock_pip_check.call_count, 1)
96+
self.mock_pip_check.assert_called_once()
97+
self.pkg_patcher.stop()
98+
99+
@patch("sys.argv", ["bootstrap", "-a", "install"])
100+
def test_can_override_available_libraries(self):
101+
bootstrap.run(libraries=[])
102+
self.mock_pip_install.assert_has_calls(
103+
[call(i) for i in default_instrumentations],
104+
any_order=True,
105+
)
106+
self.mock_pip_check.assert_called_once()
107+
108+
@patch("sys.argv", ["bootstrap", "-a", "install"])
109+
def test_can_override_available_default_instrumentations(self):
110+
with patch(
111+
"opentelemetry.instrumentation.bootstrap._is_installed",
112+
return_value=True,
113+
):
114+
bootstrap.run(default_instrumentations=[])
115+
self.mock_pip_install.assert_has_calls(
116+
[call(i) for i in self.installed_libraries],
117+
any_order=True,
118+
)
119+
self.mock_pip_check.assert_called_once()

0 commit comments

Comments
 (0)