Skip to content

Commit f9d48ed

Browse files
committed
Added comments to tests
1 parent 35e9eb7 commit f9d48ed

File tree

2 files changed

+31
-17
lines changed
  • opentelemetry-instrumentation
    • src/opentelemetry/instrumentation/auto_instrumentation
    • tests/auto_instrumentation

2 files changed

+31
-17
lines changed

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

+12-11
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,28 @@
2828
)
2929
from opentelemetry.instrumentation.version import __version__
3030

31-
logger = getLogger(__name__)
31+
_logger = getLogger(__name__)
3232

3333

3434
def _load_distros() -> BaseDistro:
3535
distro_name = environ.get(OTEL_PYTHON_DISTRO, None)
3636
for entry_point in iter_entry_points("opentelemetry_distro"):
3737
try:
38+
# If no distro is specified, use first to come up.
3839
if distro_name is None or distro_name == entry_point.name:
3940
distro = entry_point.load()()
4041
if not isinstance(distro, BaseDistro):
41-
logger.debug(
42+
_logger.debug(
4243
"%s is not an OpenTelemetry Distro. Skipping",
4344
entry_point.name,
4445
)
4546
continue
46-
logger.debug(
47+
_logger.debug(
4748
"Distribution %s will be configured", entry_point.name
4849
)
4950
return distro
5051
except Exception as exc: # pylint: disable=broad-except
51-
logger.exception(
52+
_logger.exception(
5253
"Distribution %s configuration failed", entry_point.name
5354
)
5455
raise exc
@@ -67,15 +68,15 @@ def _load_instrumentors(distro):
6768

6869
for entry_point in iter_entry_points("opentelemetry_instrumentor"):
6970
if entry_point.name in package_to_exclude:
70-
logger.debug(
71+
_logger.debug(
7172
"Instrumentation skipped for library %s", entry_point.name
7273
)
7374
continue
7475

7576
try:
7677
conflict = get_dist_dependency_conflicts(entry_point.dist)
7778
if conflict:
78-
logger.debug(
79+
_logger.debug(
7980
"Skipping instrumentation %s: %s",
8081
entry_point.name,
8182
conflict,
@@ -84,9 +85,9 @@ def _load_instrumentors(distro):
8485

8586
# tell instrumentation to not run dep checks again as we already did it above
8687
distro.load_instrumentor(entry_point, skip_dep_check=True)
87-
logger.debug("Instrumented %s", entry_point.name)
88+
_logger.debug("Instrumented %s", entry_point.name)
8889
except Exception as exc: # pylint: disable=broad-except
89-
logger.exception("Instrumenting of %s failed", entry_point.name)
90+
_logger.exception("Instrumenting of %s failed", entry_point.name)
9091
raise exc
9192

9293
for entry_point in iter_entry_points("opentelemetry_post_instrument"):
@@ -98,7 +99,7 @@ def _load_configurators():
9899
configured = None
99100
for entry_point in iter_entry_points("opentelemetry_configurator"):
100101
if configured is not None:
101-
logger.warning(
102+
_logger.warning(
102103
"Configuration of %s not loaded, %s already loaded",
103104
entry_point.name,
104105
configured,
@@ -112,12 +113,12 @@ def _load_configurators():
112113
entry_point.load()().configure(auto_instrumentation_version=__version__) # type: ignore
113114
configured = entry_point.name
114115
else:
115-
logger.warning(
116+
_logger.warning(
116117
"Configuration of %s not loaded because %s is set by %s",
117118
entry_point.name,
118119
configurator_name,
119120
OTEL_PYTHON_CONFIGURATOR,
120121
)
121122
except Exception as exc: # pylint: disable=broad-except
122-
logger.exception("Configuration of %s failed", entry_point.name)
123+
_logger.exception("Configuration of %s failed", entry_point.name)
123124
raise exc

opentelemetry-instrumentation/tests/auto_instrumentation/test_load.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class TestLoad(TestCase):
3333
"opentelemetry.instrumentation.auto_instrumentation._load.iter_entry_points"
3434
)
3535
def test_load_configurators(self, iter_mock):
36+
# Add multiple entry points but only specify the 2nd in the environment variable.
3637
ep_mock1 = Mock()
3738
ep_mock1.name = "custom_configurator1"
3839
configurator_mock1 = Mock()
@@ -65,6 +66,7 @@ def test_load_configurators_no_ep(
6566
iter_mock,
6667
):
6768
iter_mock.return_value = ()
69+
# Confirm method does not crash if not entry points exist.
6870
_load._load_configurators()
6971

7072
@patch.dict(
@@ -74,6 +76,7 @@ def test_load_configurators_no_ep(
7476
"opentelemetry.instrumentation.auto_instrumentation._load.iter_entry_points"
7577
)
7678
def test_load_configurators_error(self, iter_mock):
79+
# Add multiple entry points but only specify the 2nd in the environment variable.
7780
ep_mock1 = Mock()
7881
ep_mock1.name = "custom_configurator1"
7982
configurator_mock1 = Mock()
@@ -89,6 +92,7 @@ def test_load_configurators_error(self, iter_mock):
8992
ep_mock3.load.return_value = configurator_mock3
9093

9194
iter_mock.return_value = (ep_mock1, ep_mock2, ep_mock3)
95+
# Confirm failed configuration raises exception.
9296
self.assertRaises(Exception, _load._load_configurators)
9397

9498
@patch.dict("os.environ", {OTEL_PYTHON_DISTRO: "custom_distro2"})
@@ -99,6 +103,7 @@ def test_load_configurators_error(self, iter_mock):
99103
"opentelemetry.instrumentation.auto_instrumentation._load.iter_entry_points"
100104
)
101105
def test_load_distros(self, iter_mock, isinstance_mock):
106+
# Add multiple entry points but only specify the 2nd in the environment variable.
102107
ep_mock1 = Mock()
103108
ep_mock1.name = "custom_distro1"
104109
distro_mock1 = Mock()
@@ -113,6 +118,7 @@ def test_load_distros(self, iter_mock, isinstance_mock):
113118
ep_mock3.load.return_value = distro_mock3
114119

115120
iter_mock.return_value = (ep_mock1, ep_mock2, ep_mock3)
121+
# Mock entry points to be instances of BaseDistro.
116122
isinstance_mock.return_value = True
117123
self.assertEqual(
118124
_load._load_distros(),
@@ -132,6 +138,7 @@ def test_load_distros(self, iter_mock, isinstance_mock):
132138
def test_load_distros_not_distro(
133139
self, iter_mock, default_distro_mock, isinstance_mock
134140
):
141+
# Add multiple entry points but only specify the 2nd in the environment variable.
135142
ep_mock1 = Mock()
136143
ep_mock1.name = "custom_distro1"
137144
distro_mock1 = Mock()
@@ -146,6 +153,7 @@ def test_load_distros_not_distro(
146153
ep_mock3.load.return_value = distro_mock3
147154

148155
iter_mock.return_value = (ep_mock1, ep_mock2, ep_mock3)
156+
# Confirm default distro is used if specified entry point is not a BaseDistro
149157
isinstance_mock.return_value = False
150158
self.assertEqual(
151159
_load._load_distros(),
@@ -161,6 +169,7 @@ def test_load_distros_not_distro(
161169
)
162170
def test_load_distros_no_ep(self, iter_mock, default_distro_mock):
163171
iter_mock.return_value = ()
172+
# Confirm default distro is used if there are no entry points.
164173
self.assertEqual(
165174
_load._load_distros(),
166175
default_distro_mock(),
@@ -190,6 +199,7 @@ def test_load_distros_error(self, iter_mock, isinstance_mock):
190199

191200
iter_mock.return_value = (ep_mock1, ep_mock2, ep_mock3)
192201
isinstance_mock.return_value = True
202+
# Confirm method raises exception if it fails to load a distro.
193203
self.assertRaises(Exception, _load._load_distros)
194204

195205
@patch.dict(
@@ -203,6 +213,7 @@ def test_load_distros_error(self, iter_mock, isinstance_mock):
203213
"opentelemetry.instrumentation.auto_instrumentation._load.iter_entry_points"
204214
)
205215
def test_load_instrumentors(self, iter_mock, dep_mock):
216+
# Mock opentelemetry_pre_instrument entry points
206217
pre_ep_mock1 = Mock()
207218
pre_ep_mock1.name = "pre1"
208219
pre_mock1 = Mock()
@@ -213,12 +224,7 @@ def test_load_instrumentors(self, iter_mock, dep_mock):
213224
pre_mock2 = Mock()
214225
pre_ep_mock2.load.return_value = pre_mock2
215226

216-
ep_mock3 = Mock()
217-
ep_mock3.name = "instr3"
218-
219-
ep_mock4 = Mock()
220-
ep_mock4.name = "instr4"
221-
227+
# Mock opentelemetry_instrumentor entry points
222228
ep_mock1 = Mock()
223229
ep_mock1.name = "instr1"
224230

@@ -231,6 +237,7 @@ def test_load_instrumentors(self, iter_mock, dep_mock):
231237
ep_mock4 = Mock()
232238
ep_mock4.name = "instr4"
233239

240+
# Mock opentelemetry_instrumentor entry points
234241
post_ep_mock1 = Mock()
235242
post_ep_mock1.name = "post1"
236243
post_mock1 = Mock()
@@ -243,23 +250,28 @@ def test_load_instrumentors(self, iter_mock, dep_mock):
243250

244251
distro_mock = Mock()
245252

253+
# Mock entry points in order
246254
iter_mock.side_effect = [
247255
(pre_ep_mock1, pre_ep_mock2),
248256
(ep_mock1, ep_mock2, ep_mock3, ep_mock4),
249257
(post_ep_mock1, post_ep_mock2),
250258
]
259+
# No dependency conflict
251260
dep_mock.return_value = None
252261
_load._load_instrumentors(distro_mock)
262+
# All opentelemetry_pre_instrument entry points should be loaded
253263
pre_mock1.assert_called_once()
254264
pre_mock2.assert_called_once()
255265
self.assertEqual(iter_mock.call_count, 3)
266+
# Only non-disabled instrumentations should be loaded
256267
distro_mock.load_instrumentor.assert_has_calls(
257268
[
258269
call(ep_mock2, skip_dep_check=True),
259270
call(ep_mock4, skip_dep_check=True),
260271
]
261272
)
262273
self.assertEqual(distro_mock.load_instrumentor.call_count, 2)
274+
# All opentelemetry_post_instrument entry points should be loaded
263275
post_mock1.assert_called_once()
264276
post_mock2.assert_called_once()
265277

@@ -289,6 +301,7 @@ def test_load_instrumentors_dep_conflict(self, iter_mock, dep_mock):
289301
distro_mock = Mock()
290302

291303
iter_mock.return_value = (ep_mock1, ep_mock2, ep_mock3, ep_mock4)
304+
# If a dependency conflict is raised, that instrumentation should not be loaded, but others still should.
292305
dep_mock.side_effect = [None, "DependencyConflict"]
293306
_load._load_instrumentors(distro_mock)
294307
distro_mock.load_instrumentor.assert_has_calls(

0 commit comments

Comments
 (0)