@@ -33,6 +33,7 @@ class TestLoad(TestCase):
33
33
"opentelemetry.instrumentation.auto_instrumentation._load.iter_entry_points"
34
34
)
35
35
def test_load_configurators (self , iter_mock ):
36
+ # Add multiple entry points but only specify the 2nd in the environment variable.
36
37
ep_mock1 = Mock ()
37
38
ep_mock1 .name = "custom_configurator1"
38
39
configurator_mock1 = Mock ()
@@ -65,6 +66,7 @@ def test_load_configurators_no_ep(
65
66
iter_mock ,
66
67
):
67
68
iter_mock .return_value = ()
69
+ # Confirm method does not crash if not entry points exist.
68
70
_load ._load_configurators ()
69
71
70
72
@patch .dict (
@@ -74,6 +76,7 @@ def test_load_configurators_no_ep(
74
76
"opentelemetry.instrumentation.auto_instrumentation._load.iter_entry_points"
75
77
)
76
78
def test_load_configurators_error (self , iter_mock ):
79
+ # Add multiple entry points but only specify the 2nd in the environment variable.
77
80
ep_mock1 = Mock ()
78
81
ep_mock1 .name = "custom_configurator1"
79
82
configurator_mock1 = Mock ()
@@ -89,6 +92,7 @@ def test_load_configurators_error(self, iter_mock):
89
92
ep_mock3 .load .return_value = configurator_mock3
90
93
91
94
iter_mock .return_value = (ep_mock1 , ep_mock2 , ep_mock3 )
95
+ # Confirm failed configuration raises exception.
92
96
self .assertRaises (Exception , _load ._load_configurators )
93
97
94
98
@patch .dict ("os.environ" , {OTEL_PYTHON_DISTRO : "custom_distro2" })
@@ -99,6 +103,7 @@ def test_load_configurators_error(self, iter_mock):
99
103
"opentelemetry.instrumentation.auto_instrumentation._load.iter_entry_points"
100
104
)
101
105
def test_load_distros (self , iter_mock , isinstance_mock ):
106
+ # Add multiple entry points but only specify the 2nd in the environment variable.
102
107
ep_mock1 = Mock ()
103
108
ep_mock1 .name = "custom_distro1"
104
109
distro_mock1 = Mock ()
@@ -113,6 +118,7 @@ def test_load_distros(self, iter_mock, isinstance_mock):
113
118
ep_mock3 .load .return_value = distro_mock3
114
119
115
120
iter_mock .return_value = (ep_mock1 , ep_mock2 , ep_mock3 )
121
+ # Mock entry points to be instances of BaseDistro.
116
122
isinstance_mock .return_value = True
117
123
self .assertEqual (
118
124
_load ._load_distros (),
@@ -132,6 +138,7 @@ def test_load_distros(self, iter_mock, isinstance_mock):
132
138
def test_load_distros_not_distro (
133
139
self , iter_mock , default_distro_mock , isinstance_mock
134
140
):
141
+ # Add multiple entry points but only specify the 2nd in the environment variable.
135
142
ep_mock1 = Mock ()
136
143
ep_mock1 .name = "custom_distro1"
137
144
distro_mock1 = Mock ()
@@ -146,6 +153,7 @@ def test_load_distros_not_distro(
146
153
ep_mock3 .load .return_value = distro_mock3
147
154
148
155
iter_mock .return_value = (ep_mock1 , ep_mock2 , ep_mock3 )
156
+ # Confirm default distro is used if specified entry point is not a BaseDistro
149
157
isinstance_mock .return_value = False
150
158
self .assertEqual (
151
159
_load ._load_distros (),
@@ -161,6 +169,7 @@ def test_load_distros_not_distro(
161
169
)
162
170
def test_load_distros_no_ep (self , iter_mock , default_distro_mock ):
163
171
iter_mock .return_value = ()
172
+ # Confirm default distro is used if there are no entry points.
164
173
self .assertEqual (
165
174
_load ._load_distros (),
166
175
default_distro_mock (),
@@ -190,6 +199,7 @@ def test_load_distros_error(self, iter_mock, isinstance_mock):
190
199
191
200
iter_mock .return_value = (ep_mock1 , ep_mock2 , ep_mock3 )
192
201
isinstance_mock .return_value = True
202
+ # Confirm method raises exception if it fails to load a distro.
193
203
self .assertRaises (Exception , _load ._load_distros )
194
204
195
205
@patch .dict (
@@ -203,6 +213,7 @@ def test_load_distros_error(self, iter_mock, isinstance_mock):
203
213
"opentelemetry.instrumentation.auto_instrumentation._load.iter_entry_points"
204
214
)
205
215
def test_load_instrumentors (self , iter_mock , dep_mock ):
216
+ # Mock opentelemetry_pre_instrument entry points
206
217
pre_ep_mock1 = Mock ()
207
218
pre_ep_mock1 .name = "pre1"
208
219
pre_mock1 = Mock ()
@@ -213,12 +224,7 @@ def test_load_instrumentors(self, iter_mock, dep_mock):
213
224
pre_mock2 = Mock ()
214
225
pre_ep_mock2 .load .return_value = pre_mock2
215
226
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
222
228
ep_mock1 = Mock ()
223
229
ep_mock1 .name = "instr1"
224
230
@@ -231,6 +237,7 @@ def test_load_instrumentors(self, iter_mock, dep_mock):
231
237
ep_mock4 = Mock ()
232
238
ep_mock4 .name = "instr4"
233
239
240
+ # Mock opentelemetry_instrumentor entry points
234
241
post_ep_mock1 = Mock ()
235
242
post_ep_mock1 .name = "post1"
236
243
post_mock1 = Mock ()
@@ -243,23 +250,28 @@ def test_load_instrumentors(self, iter_mock, dep_mock):
243
250
244
251
distro_mock = Mock ()
245
252
253
+ # Mock entry points in order
246
254
iter_mock .side_effect = [
247
255
(pre_ep_mock1 , pre_ep_mock2 ),
248
256
(ep_mock1 , ep_mock2 , ep_mock3 , ep_mock4 ),
249
257
(post_ep_mock1 , post_ep_mock2 ),
250
258
]
259
+ # No dependency conflict
251
260
dep_mock .return_value = None
252
261
_load ._load_instrumentors (distro_mock )
262
+ # All opentelemetry_pre_instrument entry points should be loaded
253
263
pre_mock1 .assert_called_once ()
254
264
pre_mock2 .assert_called_once ()
255
265
self .assertEqual (iter_mock .call_count , 3 )
266
+ # Only non-disabled instrumentations should be loaded
256
267
distro_mock .load_instrumentor .assert_has_calls (
257
268
[
258
269
call (ep_mock2 , skip_dep_check = True ),
259
270
call (ep_mock4 , skip_dep_check = True ),
260
271
]
261
272
)
262
273
self .assertEqual (distro_mock .load_instrumentor .call_count , 2 )
274
+ # All opentelemetry_post_instrument entry points should be loaded
263
275
post_mock1 .assert_called_once ()
264
276
post_mock2 .assert_called_once ()
265
277
@@ -289,6 +301,7 @@ def test_load_instrumentors_dep_conflict(self, iter_mock, dep_mock):
289
301
distro_mock = Mock ()
290
302
291
303
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.
292
305
dep_mock .side_effect = [None , "DependencyConflict" ]
293
306
_load ._load_instrumentors (distro_mock )
294
307
distro_mock .load_instrumentor .assert_has_calls (
0 commit comments