77import pytest
88
99from aws_lambda_powertools import Metrics
10- from aws_lambda_powertools .utilities .typing import DurableContextProtocol
1110
1211# Reset cold start flag before each test
1312from aws_lambda_powertools .metrics .provider import cold_start
13+ from aws_lambda_powertools .utilities .typing import DurableContextProtocol
1414
1515
1616def capture_metrics_output (capsys ):
@@ -66,22 +66,22 @@ def lambda_handler(evt, context):
6666
6767 # THEN metrics should be emitted successfully
6868 output = capture_metrics_output (capsys )
69-
69+
7070 assert output ["test_metric" ] == [1.0 ]
7171 assert output ["service" ] == service
7272
7373
7474def test_log_metrics_capture_cold_start_with_durable_context (capsys , namespace , service ):
7575 """Test that capture_cold_start_metric works with DurableContext."""
7676 reset_cold_start_flag ()
77-
77+
7878 # GIVEN Metrics is initialized
7979 my_metrics = Metrics (service = service , namespace = namespace )
80-
80+
8181 # Create a DurableContext with embedded Lambda context
8282 LambdaContext = namedtuple ("LambdaContext" , "function_name" )
8383 lambda_ctx = LambdaContext ("durable_test_function" )
84-
84+
8585 durable_ctx = Mock (spec = DurableContextProtocol )
8686 durable_ctx .lambda_context = lambda_ctx
8787 durable_ctx .state = Mock (operations = [{"id" : "op1" }])
@@ -95,27 +95,25 @@ def lambda_handler(evt, context):
9595
9696 # THEN ColdStart metric should be captured with the function name from unwrapped context
9797 outputs = capture_metrics_output_multiple_emf_objects (capsys )
98-
98+
9999 # Cold start is in a separate EMF blob
100100 cold_start_output = outputs [0 ]
101101 assert cold_start_output ["ColdStart" ] == [1.0 ]
102102 assert cold_start_output ["function_name" ] == "durable_test_function"
103103 assert cold_start_output ["service" ] == service
104104
105105
106- def test_log_metrics_capture_cold_start_with_durable_context_explicit_function_name (
107- capsys , namespace , service
108- ):
106+ def test_log_metrics_capture_cold_start_with_durable_context_explicit_function_name (capsys , namespace , service ):
109107 """Test capture_cold_start_metric with explicit function_name and DurableContext."""
110108 reset_cold_start_flag ()
111-
109+
112110 # GIVEN Metrics is initialized with explicit function_name
113111 my_metrics = Metrics (service = service , namespace = namespace , function_name = "explicit_function" )
114-
112+
115113 # Create a DurableContext
116114 LambdaContext = namedtuple ("LambdaContext" , "function_name" )
117115 lambda_ctx = LambdaContext ("context_function" )
118-
116+
119117 durable_ctx = Mock (spec = DurableContextProtocol )
120118 durable_ctx .lambda_context = lambda_ctx
121119 durable_ctx .state = Mock (operations = [{"id" : "op1" }])
@@ -129,7 +127,7 @@ def lambda_handler(evt, context):
129127
130128 # THEN explicit function_name should take priority
131129 output = capture_metrics_output (capsys )
132-
130+
133131 assert output .get ("function_name" ) == "explicit_function"
134132
135133
@@ -147,18 +145,18 @@ def lambda_handler(evt, context):
147145
148146 # THEN metrics should be emitted successfully
149147 output = capture_metrics_output (capsys )
150-
148+
151149 assert output ["regression_test" ] == [42.0 ]
152150 assert output ["service" ] == service
153151
154152
155153def test_log_metrics_capture_cold_start_standard_context_still_works (capsys , namespace , service ):
156154 """Test that capture_cold_start_metric with standard context still works (regression test)."""
157155 reset_cold_start_flag ()
158-
156+
159157 # GIVEN Metrics is initialized
160158 my_metrics = Metrics (service = service , namespace = namespace )
161-
159+
162160 LambdaContext = namedtuple ("LambdaContext" , "function_name" )
163161 standard_context = LambdaContext ("standard_function" )
164162
@@ -171,6 +169,6 @@ def lambda_handler(evt, context):
171169
172170 # THEN ColdStart metric should be captured
173171 output = capture_metrics_output (capsys )
174-
172+
175173 assert "ColdStart" in output or output .get ("ColdStart" ) == [1.0 ]
176174 assert output .get ("function_name" ) == "standard_function"
0 commit comments