Skip to content

Commit 22f8b9a

Browse files
authored
fix(metrics): flush upon a single metric 100th data point (#1046)
1 parent 51ff350 commit 22f8b9a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

Diff for: aws_lambda_powertools/metrics/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def add_metric(self, name: str, unit: Union[MetricUnit, str], value: float) -> N
127127
logger.debug(f"Adding metric: {name} with {metric}")
128128
self.metric_set[name] = metric
129129

130-
if len(self.metric_set) == MAX_METRICS:
130+
if len(self.metric_set) == MAX_METRICS or len(metric["Value"]) == MAX_METRICS:
131131
logger.debug(f"Exceeded maximum of {MAX_METRICS} metrics - Publishing existing metric set")
132132
metrics = self.serialize_metric_set()
133133
print(json.dumps(metrics))

Diff for: tests/functional/test_metrics.py

+36
Original file line numberDiff line numberDiff line change
@@ -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+
8590
def 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+
232268
def 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

Comments
 (0)