1717from unittest .mock import Mock , call , patch
1818
1919from opentelemetry .instrumentation .auto_instrumentation import _load
20- from opentelemetry .instrumentation .dependencies import (
21- DependencyConflict ,
22- DependencyConflictError ,
23- )
2420from opentelemetry .instrumentation .environment_variables import (
2521 OTEL_PYTHON_CONFIGURATOR ,
2622 OTEL_PYTHON_DISABLED_INSTRUMENTATIONS ,
2925from opentelemetry .instrumentation .version import __version__
3026from opentelemetry .util ._importlib_metadata import EntryPoint , entry_points
3127
32- # TODO: Revert changes from 6d5a5149d02fdec50d8053767b55bc91d80a9228
28+
3329class TestLoad (TestCase ):
3430 @patch .dict (
3531 "os.environ" , {OTEL_PYTHON_CONFIGURATOR : "custom_configurator2" }
@@ -204,12 +200,6 @@ def test_load_distro_error(self, iter_mock, isinstance_mock):
204200 # Confirm method raises exception if it fails to load a distro.
205201 self .assertRaises (Exception , _load ._load_distro )
206202
207- # @staticmethod
208- # def _instrumentation_failed_to_load_call(entry_point, dependency_conflict):
209- # return call(
210- # "Skipping instrumentation %s: %s", entry_point, dependency_conflict
211- # )
212-
213203 @patch .dict (
214204 "os.environ" ,
215205 {OTEL_PYTHON_DISABLED_INSTRUMENTATIONS : " instr1 , instr3 " },
@@ -277,11 +267,6 @@ def test_load_instrumentors(self, iter_mock, dep_mock):
277267 [
278268 call (ep_mock2 , skip_dep_check = True ),
279269 call (ep_mock4 , skip_dep_check = True ),
280- # call(ep_mock2, raise_exception_on_conflict=True),
281- # call(ep_mock4, raise_exception_on_conflict=True),
282- # TODO: maybe merge
283- # call(ep_mock2, skip_dep_check=True, raise_exception_on_conflict=True),
284- # call(ep_mock4, skip_dep_check=True, raise_exception_on_conflict=True),
285270 ]
286271 )
287272 self .assertEqual (distro_mock .load_instrumentor .call_count , 2 )
@@ -293,11 +278,13 @@ def test_load_instrumentors(self, iter_mock, dep_mock):
293278 "os.environ" ,
294279 {OTEL_PYTHON_DISABLED_INSTRUMENTATIONS : " instr1 , instr3 " },
295280 )
296- @patch ("opentelemetry.instrumentation.auto_instrumentation._load._logger" )
281+ @patch (
282+ "opentelemetry.instrumentation.auto_instrumentation._load.get_dist_dependency_conflicts"
283+ )
297284 @patch (
298285 "opentelemetry.instrumentation.auto_instrumentation._load.entry_points"
299286 )
300- def test_load_instrumentors_dep_conflict (self , iter_mock , mock_logger ): # pylint: disable=no-self-use
287+ def test_load_instrumentors_dep_conflict (self , iter_mock , dep_mock ): # pylint: disable=no-self-use
301288 ep_mock1 = Mock ()
302289 ep_mock1 .name = "instr1"
303290
@@ -310,39 +297,27 @@ def test_load_instrumentors_dep_conflict(self, iter_mock, mock_logger): # pylin
310297 ep_mock4 = Mock ()
311298 ep_mock4 .name = "instr4"
312299
313- dependency_conflict = DependencyConflict ("1.2.3" , None )
314-
315300 distro_mock = Mock ()
316- distro_mock .load_instrumentor .side_effect = [
317- None ,
318- DependencyConflictError (dependency_conflict ),
319- ]
320301
321- # If a dependency conflict is raised, that instrumentation should not be loaded, but others still should.
322- # In this case, ep_mock4 will fail to load and ep_mock2 will succeed. (ep_mock1 and ep_mock3 are disabled)
323302 iter_mock .return_value = (ep_mock1 , ep_mock2 , ep_mock3 , ep_mock4 )
303+ # If a dependency conflict is raised, that instrumentation should not be loaded, but others still should.
304+ dep_mock .side_effect = [None , "DependencyConflict" ]
324305 _load ._load_instrumentors (distro_mock )
325306 distro_mock .load_instrumentor .assert_has_calls (
326307 [
327- call (ep_mock2 , raise_exception_on_conflict = True ),
328- call (ep_mock4 , raise_exception_on_conflict = True ),
329- ]
330- )
331- assert distro_mock .load_instrumentor .call_count == 2
332- mock_logger .debug .assert_has_calls (
333- [
334- self ._instrumentation_failed_to_load_call (
335- ep_mock4 .name , dependency_conflict
336- )
308+ call (ep_mock2 , skip_dep_check = True ),
337309 ]
338310 )
311+ distro_mock .load_instrumentor .assert_called_once ()
339312
340- @patch ("opentelemetry.instrumentation.auto_instrumentation._load._logger" )
313+ @patch (
314+ "opentelemetry.instrumentation.auto_instrumentation._load.get_dist_dependency_conflicts"
315+ )
341316 @patch (
342317 "opentelemetry.instrumentation.auto_instrumentation._load.entry_points"
343318 )
344319 def test_load_instrumentors_import_error_does_not_stop_everything (
345- self , iter_mock , mock_logger
320+ self , iter_mock , dep_mock
346321 ):
347322 ep_mock1 = Mock (name = "instr1" )
348323 ep_mock2 = Mock (name = "instr2" )
@@ -356,28 +331,25 @@ def test_load_instrumentors_import_error_does_not_stop_everything(
356331 (ep_mock1 , ep_mock2 ),
357332 (),
358333 ]
334+ dep_mock .return_value = None
359335
360336 _load ._load_instrumentors (distro_mock )
361337
362338 distro_mock .load_instrumentor .assert_has_calls (
363339 [
364- call (ep_mock1 , raise_exception_on_conflict = True ),
365- call (ep_mock2 , raise_exception_on_conflict = True ),
340+ call (ep_mock1 , skip_dep_check = True ),
341+ call (ep_mock2 , skip_dep_check = True ),
366342 ]
367343 )
368344 self .assertEqual (distro_mock .load_instrumentor .call_count , 2 )
369- # TODO: re-enable
370- # mock_logger.exception.assert_any_call(
371- # "Importing of %s failed, skipping it",
372- # ep_mock1.name,
373- # )
374-
375- # mock_logger.debug.assert_any_call("Instrumented %s", ep_mock2.name)
376345
346+ @patch (
347+ "opentelemetry.instrumentation.auto_instrumentation._load.get_dist_dependency_conflicts"
348+ )
377349 @patch (
378350 "opentelemetry.instrumentation.auto_instrumentation._load.entry_points"
379351 )
380- def test_load_instrumentors_raises_exception (self , iter_mock ):
352+ def test_load_instrumentors_raises_exception (self , iter_mock , dep_mock ):
381353 ep_mock1 = Mock (name = "instr1" )
382354 ep_mock2 = Mock (name = "instr2" )
383355
@@ -390,58 +362,18 @@ def test_load_instrumentors_raises_exception(self, iter_mock):
390362 (ep_mock1 , ep_mock2 ),
391363 (),
392364 ]
365+ dep_mock .return_value = None
393366
394367 with self .assertRaises (ValueError ):
395368 _load ._load_instrumentors (distro_mock )
396369
397370 distro_mock .load_instrumentor .assert_has_calls (
398371 [
399- call (ep_mock1 , raise_exception_on_conflict = True ),
372+ call (ep_mock1 , skip_dep_check = True ),
400373 ]
401374 )
402375 self .assertEqual (distro_mock .load_instrumentor .call_count , 1 )
403376
404- # TODO: re-enable
405- # @patch("opentelemetry.instrumentation.auto_instrumentation._load._logger")
406- # @patch(
407- # "opentelemetry.instrumentation.auto_instrumentation._load.entry_points"
408- # )
409- # def test_load_instrumentors_module_not_found_error(
410- # self, iter_mock, mock_logger
411- # ):
412- # ep_mock1 = Mock()
413- # ep_mock1.name = "instr1"
414-
415- # ep_mock2 = Mock()
416- # ep_mock2.name = "instr2"
417-
418- # distro_mock = Mock()
419-
420- # distro_mock.load_instrumentor.side_effect = [
421- # ModuleNotFoundError("No module named 'fake_module'"),
422- # None,
423- # ]
424-
425- # iter_mock.side_effect = [(), (ep_mock1, ep_mock2), ()]
426-
427- # _load._load_instrumentors(distro_mock)
428-
429- # distro_mock.load_instrumentor.assert_has_calls(
430- # [
431- # call(ep_mock1, raise_exception_on_conflict=True),
432- # call(ep_mock2, raise_exception_on_conflict=True),
433- # ]
434- # )
435- # self.assertEqual(distro_mock.load_instrumentor.call_count, 2)
436-
437- # mock_logger.debug.assert_any_call(
438- # "Skipping instrumentation %s: %s",
439- # "instr1",
440- # "No module named 'fake_module'",
441- # )
442-
443- # mock_logger.debug.assert_any_call("Instrumented %s", ep_mock2.name)
444-
445377 def test_load_instrumentors_no_entry_point_mocks (self ):
446378 distro_mock = Mock ()
447379 _load ._load_instrumentors (distro_mock )
@@ -474,4 +406,4 @@ def test_entry_point_dist_finder(self):
474406 entry_point_without_dist
475407 )
476408 # dist are not comparable, being truthy is enough
477- self .assertTrue (new_entry_point_dist )
409+ self .assertTrue (new_entry_point_dist )
0 commit comments