-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathbase.py
308 lines (251 loc) · 10.4 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import datetime
import json
import logging
import numbers
import os
from collections import defaultdict
from enum import Enum
from typing import Any, Dict, List, Optional, Union
from ..shared import constants
from ..shared.functions import resolve_env_var_choice
from .exceptions import MetricUnitError, MetricValueError, SchemaValidationError
logger = logging.getLogger(__name__)
MAX_METRICS = 100
MAX_DIMENSIONS = 9
class MetricUnit(Enum):
Seconds = "Seconds"
Microseconds = "Microseconds"
Milliseconds = "Milliseconds"
Bytes = "Bytes"
Kilobytes = "Kilobytes"
Megabytes = "Megabytes"
Gigabytes = "Gigabytes"
Terabytes = "Terabytes"
Bits = "Bits"
Kilobits = "Kilobits"
Megabits = "Megabits"
Gigabits = "Gigabits"
Terabits = "Terabits"
Percent = "Percent"
Count = "Count"
BytesPerSecond = "Bytes/Second"
KilobytesPerSecond = "Kilobytes/Second"
MegabytesPerSecond = "Megabytes/Second"
GigabytesPerSecond = "Gigabytes/Second"
TerabytesPerSecond = "Terabytes/Second"
BitsPerSecond = "Bits/Second"
KilobitsPerSecond = "Kilobits/Second"
MegabitsPerSecond = "Megabits/Second"
GigabitsPerSecond = "Gigabits/Second"
TerabitsPerSecond = "Terabits/Second"
CountPerSecond = "Count/Second"
class MetricManager:
"""Base class for metric functionality (namespace, metric, dimension, serialization)
MetricManager creates metrics asynchronously thanks to CloudWatch Embedded Metric Format (EMF).
CloudWatch EMF can create up to 100 metrics per EMF object
and metrics, dimensions, and namespace created via MetricManager
will adhere to the schema, will be serialized and validated against EMF Schema.
**Use `aws_lambda_powertools.metrics.metrics.Metrics` or
`aws_lambda_powertools.metrics.metric.single_metric` to create EMF metrics.**
Environment variables
---------------------
POWERTOOLS_METRICS_NAMESPACE : str
metric namespace to be set for all metrics
POWERTOOLS_SERVICE_NAME : str
service name used for default dimension
Raises
------
MetricUnitError
When metric metric isn't supported by CloudWatch
MetricValueError
When metric value isn't a number
SchemaValidationError
When metric object fails EMF schema validation
"""
def __init__(
self,
metric_set: Optional[Dict[str, Any]] = None,
dimension_set: Optional[Dict] = None,
namespace: Optional[str] = None,
metadata_set: Optional[Dict[str, Any]] = None,
service: Optional[str] = None,
):
self.metric_set = metric_set if metric_set is not None else {}
self.dimension_set = dimension_set if dimension_set is not None else {}
self.namespace = resolve_env_var_choice(choice=namespace, env=os.getenv(constants.METRICS_NAMESPACE_ENV))
self.service = resolve_env_var_choice(choice=service, env=os.getenv(constants.SERVICE_NAME_ENV))
self._metric_units = [unit.value for unit in MetricUnit]
self._metric_unit_options = list(MetricUnit.__members__)
self.metadata_set = metadata_set if metadata_set is not None else {}
def add_metric(self, name: str, unit: Union[MetricUnit, str], value: float) -> None:
"""Adds given metric
Example
-------
**Add given metric using MetricUnit enum**
metric.add_metric(name="BookingConfirmation", unit=MetricUnit.Count, value=1)
**Add given metric using plain string as value unit**
metric.add_metric(name="BookingConfirmation", unit="Count", value=1)
Parameters
----------
name : str
Metric name
unit : Union[MetricUnit, str]
`aws_lambda_powertools.helper.models.MetricUnit`
value : float
Metric value
Raises
------
MetricUnitError
When metric unit is not supported by CloudWatch
"""
if not isinstance(value, numbers.Number):
raise MetricValueError(f"{value} is not a valid number")
unit = self.__extract_metric_unit_value(unit=unit)
metric: Dict = self.metric_set.get(name, defaultdict(list))
metric["Unit"] = unit
metric["Value"].append(float(value))
logger.debug(f"Adding metric: {name} with {metric}")
self.metric_set[name] = metric
if len(self.metric_set) == MAX_METRICS or len(metric["Value"]) == MAX_METRICS:
logger.debug(f"Exceeded maximum of {MAX_METRICS} metrics - Publishing existing metric set")
metrics = self.serialize_metric_set()
print(json.dumps(metrics))
# clear metric set only as opposed to metrics and dimensions set
# since we could have more than 100 metrics
self.metric_set.clear()
def serialize_metric_set(
self, metrics: Optional[Dict] = None, dimensions: Optional[Dict] = None, metadata: Optional[Dict] = None
) -> Dict:
"""Serializes metric and dimensions set
Parameters
----------
metrics : Dict, optional
Dictionary of metrics to serialize, by default None
dimensions : Dict, optional
Dictionary of dimensions to serialize, by default None
metadata: Dict, optional
Dictionary of metadata to serialize, by default None
Example
-------
**Serialize metrics into EMF format**
metrics = MetricManager()
# ...add metrics, dimensions, namespace
ret = metrics.serialize_metric_set()
Returns
-------
Dict
Serialized metrics following EMF specification
Raises
------
SchemaValidationError
Raised when serialization fail schema validation
"""
if metrics is None: # pragma: no cover
metrics = self.metric_set
if dimensions is None: # pragma: no cover
dimensions = self.dimension_set
if metadata is None: # pragma: no cover
metadata = self.metadata_set
if self.service and not self.dimension_set.get("service"):
# self.service won't be a float
self.add_dimension(name="service", value=self.service) # type: ignore[arg-type]
if len(metrics) == 0:
raise SchemaValidationError("Must contain at least one metric.")
if self.namespace is None:
raise SchemaValidationError("Must contain a metric namespace.")
logger.debug({"details": "Serializing metrics", "metrics": metrics, "dimensions": dimensions})
metric_names_and_units: List[Dict[str, str]] = [] # [ { "Name": "metric_name", "Unit": "Count" } ]
metric_names_and_values: Dict[str, float] = {} # { "metric_name": 1.0 }
for metric_name in metrics:
metric: dict = metrics[metric_name]
metric_value: int = metric.get("Value", 0)
metric_unit: str = metric.get("Unit", "")
metric_names_and_units.append({"Name": metric_name, "Unit": metric_unit})
metric_names_and_values.update({metric_name: metric_value})
return {
"_aws": {
"Timestamp": int(datetime.datetime.now().timestamp() * 1000), # epoch
"CloudWatchMetrics": [
{
"Namespace": self.namespace, # "test_namespace"
"Dimensions": [list(dimensions.keys())], # [ "service" ]
"Metrics": metric_names_and_units,
}
],
},
**dimensions, # "service": "test_service"
**metadata, # "username": "test"
**metric_names_and_values, # "single_metric": 1.0
}
def add_dimension(self, name: str, value: str) -> None:
"""Adds given dimension to all metrics
Example
-------
**Add a metric dimensions**
metric.add_dimension(name="operation", value="confirm_booking")
Parameters
----------
name : str
Dimension name
value : str
Dimension value
"""
logger.debug(f"Adding dimension: {name}:{value}")
if len(self.dimension_set) == 9:
raise SchemaValidationError(
f"Maximum number of dimensions exceeded ({MAX_DIMENSIONS}): Unable to add dimension {name}."
)
# Cast value to str according to EMF spec
# Majority of values are expected to be string already, so
# checking before casting improves performance in most cases
self.dimension_set[name] = value if isinstance(value, str) else str(value)
def add_metadata(self, key: str, value: Any) -> None:
"""Adds high cardinal metadata for metrics object
This will not be available during metrics visualization.
Instead, this will be searchable through logs.
If you're looking to add metadata to filter metrics, then
use add_dimensions method.
Example
-------
**Add metrics metadata**
metric.add_metadata(key="booking_id", value="booking_id")
Parameters
----------
key : str
Metadata key
value : any
Metadata value
"""
logger.debug(f"Adding metadata: {key}:{value}")
# Cast key to str according to EMF spec
# Majority of keys are expected to be string already, so
# checking before casting improves performance in most cases
if isinstance(key, str):
self.metadata_set[key] = value
else:
self.metadata_set[str(key)] = value
def __extract_metric_unit_value(self, unit: Union[str, MetricUnit]) -> str:
"""Return metric value from metric unit whether that's str or MetricUnit enum
Parameters
----------
unit : Union[str, MetricUnit]
Metric unit
Returns
-------
str
Metric unit value (e.g. "Seconds", "Count/Second")
Raises
------
MetricUnitError
When metric unit is not supported by CloudWatch
"""
if isinstance(unit, str):
if unit in self._metric_unit_options:
unit = MetricUnit[unit].value
if unit not in self._metric_units:
raise MetricUnitError(
f"Invalid metric unit '{unit}', expected either option: {self._metric_unit_options}"
)
if isinstance(unit, MetricUnit):
unit = unit.value
return unit