File tree 2 files changed +34
-1
lines changed
aws_embedded_metrics/serializers
2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -67,15 +67,19 @@ def create_body() -> Dict[str, Any]:
67
67
68
68
# Track batch number to know where to slice metric data
69
69
i = 0
70
-
70
+ complete_metrics = set ()
71
71
while remaining_data :
72
72
remaining_data = False
73
73
current_body = create_body ()
74
74
75
75
for metric_name , metric in context .metrics .items ():
76
+ # ensure we don't add duplicates of metrics we already completed
77
+ if metric_name in complete_metrics :
78
+ continue
76
79
77
80
if len (metric .values ) == 1 :
78
81
current_body [metric_name ] = metric .values [0 ]
82
+ complete_metrics .add (metric_name )
79
83
else :
80
84
# Slice metric data as each batch cannot contain more than
81
85
# MAX_DATAPOINTS_PER_METRIC entries for a given metric
@@ -87,6 +91,8 @@ def create_body() -> Dict[str, Any]:
87
91
# of the metric value list
88
92
if len (metric .values ) > end_index :
89
93
remaining_data = True
94
+ else :
95
+ complete_metrics .add (metric_name )
90
96
91
97
metric_body = {"Name" : metric_name , "Unit" : metric .unit }
92
98
if metric .storage_resolution == StorageResolution .HIGH :
Original file line number Diff line number Diff line change @@ -248,6 +248,33 @@ def test_serialize_with_more_than_100_metrics_and_datapoints():
248
248
assert metric_results == expected_results
249
249
250
250
251
+ def test_serialize_no_duplication_bug ():
252
+ """
253
+ A bug existed where metrics with lots of values have to be broken up
254
+ but single value metrics got duplicated across each section.
255
+ This test verifies the fix to ensure no duplication.
256
+ """
257
+ context = get_context ()
258
+ single_expected_result = 1
259
+ single_found_result = 0
260
+
261
+ # create a metric with a single value
262
+ single_key = "Metric-single"
263
+ context .put_metric (single_key , single_expected_result )
264
+ # add a lot of another metric so the log batches must be broken up
265
+ for i in range (1000 ):
266
+ context .put_metric ("Metric-many" , 0 )
267
+
268
+ results = serializer .serialize (context )
269
+
270
+ # count up all values for the single metric to ensure no duplicates
271
+ for batch in results :
272
+ for metric_key , value in json .loads (batch ).items ():
273
+ if metric_key == single_key :
274
+ single_found_result += value
275
+ assert single_expected_result == single_found_result
276
+
277
+
251
278
def test_serialize_with_multiple_metrics ():
252
279
# arrange
253
280
metrics = 2
You can’t perform that action at this time.
0 commit comments