99class TestStarlettePatch (TestCase ):
1010 """Test the Starlette instrumentation patches."""
1111
12+ @patch ("amazon.opentelemetry.distro.patches._starlette_patches.AGENT_OBSERVABILITY_ENABLED" , True )
1213 @patch ("amazon.opentelemetry.distro.patches._starlette_patches._logger" )
1314 def test_starlette_patch_applied_successfully (self , mock_logger ):
1415 """Test that the Starlette instrumentation patch is applied successfully."""
1516 # Create a mock StarletteInstrumentor class
1617 mock_instrumentor_class = MagicMock ()
1718 mock_instrumentor_class .__name__ = "StarletteInstrumentor"
1819
19- # Create a mock module
20+ class MockMiddleware :
21+ def __init__ (self , app , ** kwargs ):
22+ pass
23+
24+ mock_middleware_class = MockMiddleware
25+ original_init = mock_middleware_class .__init__
26+
27+ # Create mock modules
2028 mock_starlette_module = MagicMock ()
2129 mock_starlette_module .StarletteInstrumentor = mock_instrumentor_class
2230
23- # Mock the import
24- with patch .dict ("sys.modules" , {"opentelemetry.instrumentation.starlette" : mock_starlette_module }):
31+ mock_asgi_module = MagicMock ()
32+ mock_asgi_module .OpenTelemetryMiddleware = mock_middleware_class
33+
34+ # Mock the imports
35+ with patch .dict (
36+ "sys.modules" ,
37+ {
38+ "opentelemetry.instrumentation.starlette" : mock_starlette_module ,
39+ "opentelemetry.instrumentation.asgi" : mock_asgi_module ,
40+ },
41+ ):
2542 # Apply the patch
2643 _apply_starlette_instrumentation_patches ()
2744
@@ -33,6 +50,18 @@ def test_starlette_patch_applied_successfully(self, mock_logger):
3350 result = mock_instrumentor_class .instrumentation_dependencies (mock_instance )
3451 self .assertEqual (result , ("starlette >= 0.13" ,))
3552
53+ self .assertNotEqual (mock_middleware_class .__init__ , original_init )
54+
55+ # Test middleware patching sets exclude flags
56+ mock_middleware_instance = MagicMock ()
57+ mock_middleware_instance .exclude_receive_span = False
58+ mock_middleware_instance .exclude_send_span = False
59+
60+ mock_middleware_class .__init__ (mock_middleware_instance , "app" )
61+
62+ self .assertTrue (mock_middleware_instance .exclude_receive_span )
63+ self .assertTrue (mock_middleware_instance .exclude_send_span )
64+
3665 # Verify logging
3766 mock_logger .debug .assert_called_once_with (
3867 "Successfully patched Starlette instrumentation_dependencies method"
0 commit comments