4
4
import logging
5
5
from abc import ABC , abstractmethod
6
6
from datetime import datetime
7
- from typing import Any , Dict , List , Optional
7
+ from typing import Any , Dict , List , Optional , Union
8
+
9
+ from typing_extensions import TypedDict
10
+
11
+ from samtranslator .internal .deprecation_control import deprecated
8
12
9
13
LOG = logging .getLogger (__name__ )
10
14
@@ -25,6 +29,7 @@ def publish(self, namespace: str, metrics: List["MetricDatum"]) -> None:
25
29
class CWMetricsPublisher (MetricsPublisher ):
26
30
BATCH_SIZE = 20
27
31
32
+ @deprecated ()
28
33
def __init__ (self , cloudwatch_client ) -> None : # type: ignore[no-untyped-def]
29
34
"""
30
35
Constructor
@@ -66,7 +71,7 @@ class DummyMetricsPublisher(MetricsPublisher):
66
71
def __init__ (self ) -> None :
67
72
MetricsPublisher .__init__ (self )
68
73
69
- def publish (self , namespace , metrics ): # type: ignore[no-untyped-def]
74
+ def publish (self , namespace : str , metrics : List [ "MetricDatum" ]) -> None :
70
75
"""Do not publish any metric, this is a dummy publisher used for offline use."""
71
76
LOG .debug (f"Dummy publisher ignoring { len (metrics )} metrices" )
72
77
@@ -90,7 +95,14 @@ class MetricDatum:
90
95
Class to hold Metric data.
91
96
"""
92
97
93
- def __init__ (self , name , value , unit , dimensions = None , timestamp = None ) -> None : # type: ignore[no-untyped-def]
98
+ def __init__ (
99
+ self ,
100
+ name : str ,
101
+ value : Union [int , float ],
102
+ unit : str ,
103
+ dimensions : Optional [List ["MetricDimension" ]] = None ,
104
+ timestamp : Optional [datetime ] = None ,
105
+ ) -> None :
94
106
"""
95
107
Constructor
96
108
@@ -116,6 +128,11 @@ def get_metric_data(self) -> Dict[str, Any]:
116
128
}
117
129
118
130
131
+ class MetricDimension (TypedDict ):
132
+ Name : str
133
+ Value : Any
134
+
135
+
119
136
class Metrics :
120
137
def __init__ (
121
138
self , namespace : str = "ServerlessTransform" , metrics_publisher : Optional [MetricsPublisher ] = None
@@ -138,7 +155,14 @@ def __del__(self) -> None:
138
155
)
139
156
self .publish ()
140
157
141
- def _record_metric (self , name , value , unit , dimensions = None , timestamp = None ): # type: ignore[no-untyped-def]
158
+ def _record_metric (
159
+ self ,
160
+ name : str ,
161
+ value : Union [int , float ],
162
+ unit : str ,
163
+ dimensions : Optional [List ["MetricDimension" ]] = None ,
164
+ timestamp : Optional [datetime ] = None ,
165
+ ) -> None :
142
166
"""
143
167
Create and save metric object in internal cache.
144
168
@@ -150,7 +174,13 @@ def _record_metric(self, name, value, unit, dimensions=None, timestamp=None): #
150
174
"""
151
175
self .metrics_cache .setdefault (name , []).append (MetricDatum (name , value , unit , dimensions , timestamp ))
152
176
153
- def record_count (self , name , value , dimensions = None , timestamp = None ): # type: ignore[no-untyped-def]
177
+ def record_count (
178
+ self ,
179
+ name : str ,
180
+ value : int ,
181
+ dimensions : Optional [List ["MetricDimension" ]] = None ,
182
+ timestamp : Optional [datetime ] = None ,
183
+ ) -> None :
154
184
"""
155
185
Create metric with unit Count.
156
186
@@ -160,9 +190,15 @@ def record_count(self, name, value, dimensions=None, timestamp=None): # type: i
160
190
:param dimensions: array of dimensions applied to the metric
161
191
:param timestamp: timestamp of metric (datetime.datetime object)
162
192
"""
163
- self ._record_metric (name , value , Unit .Count , dimensions , timestamp ) # type: ignore[no-untyped-call]
193
+ self ._record_metric (name , value , Unit .Count , dimensions , timestamp )
164
194
165
- def record_latency (self , name , value , dimensions = None , timestamp = None ): # type: ignore[no-untyped-def]
195
+ def record_latency (
196
+ self ,
197
+ name : str ,
198
+ value : Union [int , float ],
199
+ dimensions : Optional [List ["MetricDimension" ]] = None ,
200
+ timestamp : Optional [datetime ] = None ,
201
+ ) -> None :
166
202
"""
167
203
Create metric with unit Milliseconds.
168
204
@@ -172,7 +208,7 @@ def record_latency(self, name, value, dimensions=None, timestamp=None): # type:
172
208
:param dimensions: array of dimensions applied to the metric
173
209
:param timestamp: timestamp of metric (datetime.datetime object)
174
210
"""
175
- self ._record_metric (name , value , Unit .Milliseconds , dimensions , timestamp ) # type: ignore[no-untyped-call]
211
+ self ._record_metric (name , value , Unit .Milliseconds , dimensions , timestamp )
176
212
177
213
def publish (self ) -> None :
178
214
"""Calls publish method from the configured metrics publisher to publish metrics"""
@@ -184,7 +220,7 @@ def publish(self) -> None:
184
220
self .metrics_publisher .publish (self .namespace , all_metrics )
185
221
self .metrics_cache = {}
186
222
187
- def get_metric (self , name ): # type: ignore[no-untyped-def]
223
+ def get_metric (self , name : str ) -> List [ MetricDatum ]:
188
224
"""
189
225
Returns a list of metrics from the internal cache for a metric name
190
226
0 commit comments