1
1
from parameterized import parameterized , param
2
+ from datetime import datetime
2
3
from unittest import TestCase
3
4
from unittest .mock import MagicMock , call , ANY
4
5
from samtranslator .metrics .metrics import (
@@ -30,25 +31,43 @@ class TestMetrics(TestCase):
30
31
"CountMetric" ,
31
32
12 ,
32
33
[{"Name" : "SAM" , "Value" : "Dim1" }, {"Name" : "SAM" , "Value" : "Dim2" }],
34
+ None ,
33
35
),
34
36
param (
35
37
"DummyNamespace" ,
36
38
"IAMError" ,
37
39
59 ,
38
40
[{"Name" : "SAM" , "Value" : "Dim1" }, {"Name" : "SAM" , "Value" : "Dim2" }],
41
+ None ,
42
+ ),
43
+ param (
44
+ "DummyNamespace" ,
45
+ "MyCount" ,
46
+ 77 ,
47
+ [{"Name" : "SAM" , "Value" : "Dim1" }, {"Name" : "SAM" , "Value" : "Dim2" }],
48
+ datetime (2022 , 8 , 11 , 0 , 0 , 0 ),
39
49
),
40
50
]
41
51
)
42
- def test_publishing_count_metric (self , namespace , name , value , dimensions ):
52
+ def test_publishing_count_metric (self , namespace , name , value , dimensions , timestamp ):
43
53
mock_metrics_publisher = MetricPublisherTestHelper ()
44
54
metrics = Metrics (namespace , mock_metrics_publisher )
45
- metrics .record_count (name , value , dimensions )
55
+ kwargs = {
56
+ "name" : name ,
57
+ "value" : value ,
58
+ "dimensions" : dimensions ,
59
+ }
60
+ if timestamp is not None :
61
+ kwargs ["timestamp" ] = timestamp
62
+ metrics .record_count (** kwargs )
46
63
metrics .publish ()
47
64
self .assertEqual (len (mock_metrics_publisher .metrics_cache ), 1 )
48
65
published_metric = mock_metrics_publisher .metrics_cache [0 ].get_metric_data ()
49
66
self .assertEqual (published_metric ["MetricName" ], name )
50
67
self .assertEqual (published_metric ["Dimensions" ], dimensions )
51
68
self .assertEqual (published_metric ["Value" ], value )
69
+ if timestamp is not None :
70
+ self .assertEqual (published_metric ["Timestamp" ], timestamp )
52
71
53
72
@parameterized .expand (
54
73
[
@@ -57,25 +76,43 @@ def test_publishing_count_metric(self, namespace, name, value, dimensions):
57
76
"SARLatency" ,
58
77
1200 ,
59
78
[{"Name" : "SAM" , "Value" : "Dim1" }, {"Name" : "SAM" , "Value" : "Dim2" }],
79
+ None ,
60
80
),
61
81
param (
62
82
"DummyNamespace" ,
63
83
"IAMLatency" ,
64
84
400 ,
65
85
[{"Name" : "SAM" , "Value" : "Dim1" }, {"Name" : "SAM" , "Value" : "Dim2" }],
86
+ None ,
87
+ ),
88
+ param (
89
+ "DummyNamespace" ,
90
+ "OtherLatency" ,
91
+ 400000 ,
92
+ [{"Name" : "SAM" , "Value" : "Dim1" }, {"Name" : "SAM" , "Value" : "Dim2" }],
93
+ datetime (2021 , 9 , 20 , 12 , 0 , 0 ),
66
94
),
67
95
]
68
96
)
69
- def test_publishing_latency_metric (self , namespace , name , value , dimensions ):
97
+ def test_publishing_latency_metric (self , namespace , name , value , dimensions , timestamp ):
70
98
mock_metrics_publisher = MetricPublisherTestHelper ()
71
99
metrics = Metrics (namespace , mock_metrics_publisher )
72
- metrics .record_latency (name , value , dimensions )
100
+ kwargs = {
101
+ "name" : name ,
102
+ "value" : value ,
103
+ "dimensions" : dimensions ,
104
+ }
105
+ if timestamp is not None :
106
+ kwargs ["timestamp" ] = timestamp
107
+ metrics .record_latency (** kwargs )
73
108
metrics .publish ()
74
109
self .assertEqual (len (mock_metrics_publisher .metrics_cache ), 1 )
75
110
published_metric = mock_metrics_publisher .metrics_cache [0 ].get_metric_data ()
76
111
self .assertEqual (published_metric ["MetricName" ], name )
77
112
self .assertEqual (published_metric ["Dimensions" ], dimensions )
78
113
self .assertEqual (published_metric ["Value" ], value )
114
+ if timestamp is not None :
115
+ self .assertEqual (published_metric ["Timestamp" ], timestamp )
79
116
80
117
@parameterized .expand (
81
118
[
@@ -153,41 +190,52 @@ class TestCWMetricPublisher(TestCase):
153
190
12 ,
154
191
Unit .Count ,
155
192
[{"Name" : "SAM" , "Value" : "Dim1" }, {"Name" : "SAM" , "Value" : "Dim2" }],
193
+ None ,
156
194
),
157
195
param (
158
196
"DummyNamespace" ,
159
197
"IAMError" ,
160
198
59 ,
161
199
Unit .Count ,
162
200
[{"Name" : "SAM" , "Value" : "Dim1" }, {"Name" : "SAM" , "Value" : "Dim2" }],
201
+ datetime (2022 , 8 , 8 , 8 , 8 , 8 ),
163
202
),
164
203
param (
165
204
"DummyNamespace" ,
166
205
"SARLatency" ,
167
206
1200 ,
168
207
Unit .Milliseconds ,
169
208
[{"Name" : "SAM" , "Value" : "Dim1" }, {"Name" : "SAM" , "Value" : "Dim2" }],
209
+ None ,
170
210
),
171
211
param (
172
212
"DummyNamespace" ,
173
213
"IAMLatency" ,
174
214
400 ,
175
215
Unit .Milliseconds ,
176
216
[{"Name" : "SAM" , "Value" : "Dim1" }, {"Name" : "SAM" , "Value" : "Dim2" }],
217
+ datetime (2021 , 9 , 10 , 10 , 10 , 10 ),
177
218
),
178
219
]
179
220
)
180
- def test_publish_metric (self , namespace , name , value , unit , dimensions ):
221
+ def test_publish_metric (self , namespace , name , value , unit , dimensions , timestamp ):
181
222
mock_cw_client = MagicMock ()
182
223
metric_publisher = CWMetricsPublisher (mock_cw_client )
183
- metric_datum = MetricDatum (name , value , unit , dimensions )
224
+ metric_datum = MetricDatum (name , value , unit , dimensions , timestamp )
184
225
metrics = [metric_datum ]
185
226
metric_publisher .publish (namespace , metrics )
227
+ expected_timestamp = timestamp if timestamp is not None else ANY
186
228
mock_cw_client .put_metric_data .assert_has_calls (
187
229
[
188
230
call (
189
231
MetricData = [
190
- {"Dimensions" : dimensions , "Unit" : unit , "Value" : value , "MetricName" : name , "Timestamp" : ANY }
232
+ {
233
+ "Dimensions" : dimensions ,
234
+ "Unit" : unit ,
235
+ "Value" : value ,
236
+ "MetricName" : name ,
237
+ "Timestamp" : expected_timestamp ,
238
+ }
191
239
],
192
240
Namespace = namespace ,
193
241
)
0 commit comments