@@ -57,6 +57,7 @@ def serialize_single_metric(
57
57
dimension : Dict ,
58
58
namespace : str ,
59
59
metadata : Dict | None = None ,
60
+ timestamp : int | datetime .datetime | None = None ,
60
61
) -> CloudWatchEMFOutput :
61
62
"""Helper function to build EMF object from a given metric, dimension and namespace"""
62
63
my_metrics = AmazonCloudWatchEMFProvider (namespace = namespace )
@@ -66,6 +67,9 @@ def serialize_single_metric(
66
67
if metadata is not None :
67
68
my_metrics .add_metadata (** metadata )
68
69
70
+ if timestamp :
71
+ my_metrics .set_timestamp (timestamp )
72
+
69
73
return my_metrics .serialize_metric_set ()
70
74
71
75
@@ -143,6 +147,28 @@ def test_single_metric_default_dimensions(capsys, metric, dimension, namespace):
143
147
assert expected == output
144
148
145
149
150
+ def test_single_metric_with_custom_timestamp (capsys , metric , dimension , namespace ):
151
+ # GIVEN we provide a custom timestamp
152
+ # WHEN using single_metric context manager
153
+
154
+ default_dimensions = {dimension ["name" ]: dimension ["value" ]}
155
+
156
+ timestamp = int ((datetime .datetime .now () - datetime .timedelta (days = 2 )).timestamp () * 1000 )
157
+ with single_metric (
158
+ namespace = namespace ,
159
+ default_dimensions = default_dimensions ,
160
+ ** metric ,
161
+ timestamp = timestamp ,
162
+ ) as my_metric :
163
+ my_metric .add_metric (name = "second_metric" , unit = "Count" , value = 1 )
164
+
165
+ output = capture_metrics_output (capsys )
166
+ expected = serialize_single_metric (metric = metric , dimension = dimension , namespace = namespace , timestamp = timestamp )
167
+
168
+ # THEN we should have custom timestamp added to the metric
169
+ assert expected == output
170
+
171
+
146
172
def test_single_metric_default_dimensions_inherit (capsys , metric , dimension , namespace ):
147
173
# GIVEN we provide Metrics default dimensions
148
174
# WHEN using single_metric context manager
0 commit comments