@@ -82,6 +82,11 @@ def a_hundred_metrics() -> List[Dict[str, str]]:
8282 return [{"name" : f"metric_{ i } " , "unit" : "Count" , "value" : 1 } for i in range (100 )]
8383
8484
85+ @pytest .fixture
86+ def a_hundred_metric_values () -> List [Dict [str , str ]]:
87+ return [{"name" : "metric" , "unit" : "Count" , "value" : i } for i in range (100 )]
88+
89+
8590def serialize_metrics (
8691 metrics : List [Dict ], dimensions : List [Dict ], namespace : str , metadatas : List [Dict ] = None
8792) -> Dict :
@@ -229,6 +234,37 @@ def test_metrics_spillover(monkeypatch, capsys, metric, dimension, namespace, a_
229234 assert serialized_101th_metric == expected_101th_metric
230235
231236
237+ def test_metric_values_spillover (monkeypatch , capsys , dimension , namespace , a_hundred_metric_values ):
238+ # GIVEN Metrics is initialized and we have over a hundred metric values to add
239+ my_metrics = Metrics (namespace = namespace )
240+ my_metrics .add_dimension (** dimension )
241+ metric = a_hundred_metric_values [0 ]
242+
243+ # WHEN we add 100 metric values
244+ for _metric in a_hundred_metric_values :
245+ my_metrics .add_metric (** _metric )
246+
247+ # THEN it should serialize and flush the metric at the 100th value
248+ # and clear all metrics and dimensions from memory
249+ output = capture_metrics_output (capsys )
250+ spillover_values = output [metric ["name" ]]
251+ assert my_metrics .metric_set == {}
252+ assert len (spillover_values ) == 100
253+
254+ # GIVEN we add the 101st metric
255+ # WHEN we already had a Metric class instance
256+ # with an existing dimension set from the previous 100th metric batch
257+ my_metrics .add_metric (** metric )
258+
259+ # THEN serializing the 101st value should
260+ # create a new EMF object with a single value in it (101st)
261+ # and contain the same dimension we previously added
262+ serialized_101st_metric = my_metrics .serialize_metric_set ()
263+ expected_101st_metric = serialize_single_metric (metric = metric , dimension = dimension , namespace = namespace )
264+ remove_timestamp (metrics = [serialized_101st_metric , expected_101st_metric ])
265+ assert serialized_101st_metric == expected_101st_metric
266+
267+
232268def test_log_metrics_decorator_call_decorated_function (metric , namespace , service ):
233269 # GIVEN Metrics is initialized
234270 my_metrics = Metrics (service = service , namespace = namespace )
0 commit comments