9
9
class TestStarlettePatch (TestCase ):
10
10
"""Test the Starlette instrumentation patches."""
11
11
12
+ @patch ("amazon.opentelemetry.distro.patches._starlette_patches.AGENT_OBSERVABILITY_ENABLED" , True )
12
13
@patch ("amazon.opentelemetry.distro.patches._starlette_patches._logger" )
13
14
def test_starlette_patch_applied_successfully (self , mock_logger ):
14
15
"""Test that the Starlette instrumentation patch is applied successfully."""
15
16
# Create a mock StarletteInstrumentor class
16
17
mock_instrumentor_class = MagicMock ()
17
18
mock_instrumentor_class .__name__ = "StarletteInstrumentor"
18
19
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
20
28
mock_starlette_module = MagicMock ()
21
29
mock_starlette_module .StarletteInstrumentor = mock_instrumentor_class
22
30
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
+ ):
25
42
# Apply the patch
26
43
_apply_starlette_instrumentation_patches ()
27
44
@@ -33,6 +50,18 @@ def test_starlette_patch_applied_successfully(self, mock_logger):
33
50
result = mock_instrumentor_class .instrumentation_dependencies (mock_instance )
34
51
self .assertEqual (result , ("starlette >= 0.13" ,))
35
52
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
+
36
65
# Verify logging
37
66
mock_logger .debug .assert_called_once_with (
38
67
"Successfully patched Starlette instrumentation_dependencies method"
0 commit comments