Skip to content

Commit a5a9e32

Browse files
committed
opentelemetry-instrumentation: make it easier to use bootstrap with custom values
Pass the available instrumentation libraries and the default libraries from bootstrap_gen to run() (and down to all the functions) instead of relying on the globals. This way it's easier to reuse run with custom values. While at it fix mock handling in tests.
1 parent e4ece57 commit a5a9e32

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def _sys_pip_install(package):
7575
print(error)
7676

7777

78-
def _pip_check():
78+
def _pip_check(libraries):
7979
"""Ensures none of the instrumentations have dependency conflicts.
8080
Clean check reported as:
8181
'No broken requirements found.'
@@ -113,7 +113,7 @@ def _is_installed(req):
113113
return True
114114

115115

116-
def _find_installed_libraries():
116+
def _find_installed_libraries(default_instrumentations, libraries):
117117
for lib in default_instrumentations:
118118
yield lib
119119

@@ -122,18 +122,24 @@ def _find_installed_libraries():
122122
yield lib["instrumentation"]
123123

124124

125-
def _run_requirements():
125+
def _run_requirements(default_instrumentations, libraries):
126126
logger.setLevel(logging.ERROR)
127-
print("\n".join(_find_installed_libraries()))
127+
print(
128+
"\n".join(
129+
_find_installed_libraries(default_instrumentations, libraries)
130+
)
131+
)
128132

129133

130-
def _run_install():
131-
for lib in _find_installed_libraries():
134+
def _run_install(default_instrumentations, libraries):
135+
for lib in _find_installed_libraries(default_instrumentations, libraries):
132136
_sys_pip_install(lib)
133137
_pip_check()
134138

135139

136-
def run() -> None:
140+
def run(
141+
default_instrumentations=default_instrumentations, libraries=libraries
142+
) -> None:
137143
action_install = "install"
138144
action_requirements = "requirements"
139145

@@ -167,4 +173,4 @@ def run() -> None:
167173
action_install: _run_install,
168174
action_requirements: _run_requirements,
169175
}[args.action]
170-
cmd()
176+
cmd(default_instrumentations, libraries)

opentelemetry-instrumentation/tests/test_bootstrap.py

Lines changed: 39 additions & 10 deletions
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,17 @@ 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()
66+
self.pkg_patcher.start()
6267

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

6974
@patch("sys.argv", ["bootstrap", "-a", "pipenv"])
7075
def test_run_unknown_cmd(self):
@@ -87,4 +92,28 @@ def test_run_cmd_install(self):
8792
[call(i) for i in self.installed_libraries],
8893
any_order=True,
8994
)
90-
self.assertEqual(self.mock_pip_check.call_count, 1)
95+
self.mock_pip_check.assert_called_once()
96+
97+
@patch("sys.argv", ["bootstrap", "-a", "install"])
98+
def test_can_override_available_libraries(self):
99+
self.pkg_patcher.stop()
100+
bootstrap.run(libraries=[])
101+
self.mock_pip_install.assert_has_calls(
102+
[call(i) for i in default_instrumentations],
103+
any_order=True,
104+
)
105+
self.mock_pip_check.assert_called_once()
106+
107+
@patch("sys.argv", ["bootstrap", "-a", "install"])
108+
def test_can_override_available_default_instrumentations(self):
109+
self.pkg_patcher.stop()
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)