9
9
import warnings
10
10
from collections import defaultdict
11
11
from contextlib import contextmanager
12
- from typing import Any , Callable , Dict , Generator , List , Optional , Union
12
+ from typing import TYPE_CHECKING , Any , Callable , Generator
13
13
14
14
from aws_lambda_powertools .metrics .exceptions import (
15
15
MetricResolutionError ,
24
24
from aws_lambda_powertools .metrics .provider .cold_start import (
25
25
reset_cold_start_flag , # noqa: F401 # backwards compatibility
26
26
)
27
- from aws_lambda_powertools .metrics .types import MetricNameUnitResolution
28
27
from aws_lambda_powertools .shared import constants
29
28
from aws_lambda_powertools .shared .functions import resolve_env_var_choice
30
29
30
+ if TYPE_CHECKING :
31
+ from aws_lambda_powertools .metrics .types import MetricNameUnitResolution
32
+
31
33
logger = logging .getLogger (__name__ )
32
34
33
35
# Maintenance: alias due to Hyrum's law
@@ -66,10 +68,10 @@ class MetricManager:
66
68
67
69
def __init__ (
68
70
self ,
69
- metric_set : Dict [str , Any ] | None = None ,
70
- dimension_set : Dict | None = None ,
71
+ metric_set : dict [str , Any ] | None = None ,
72
+ dimension_set : dict | None = None ,
71
73
namespace : str | None = None ,
72
- metadata_set : Dict [str , Any ] | None = None ,
74
+ metadata_set : dict [str , Any ] | None = None ,
73
75
service : str | None = None ,
74
76
):
75
77
self .metric_set = metric_set if metric_set is not None else {}
@@ -110,11 +112,11 @@ def add_metric(
110
112
----------
111
113
name : str
112
114
Metric name
113
- unit : Union[ MetricUnit, str]
115
+ unit : MetricUnit | str
114
116
`aws_lambda_powertools.helper.models.MetricUnit`
115
117
value : float
116
118
Metric value
117
- resolution : Union[ MetricResolution, int]
119
+ resolution : MetricResolution | int
118
120
`aws_lambda_powertools.helper.models.MetricResolution`
119
121
120
122
Raises
@@ -129,7 +131,7 @@ def add_metric(
129
131
130
132
unit = self ._extract_metric_unit_value (unit = unit )
131
133
resolution = self ._extract_metric_resolution_value (resolution = resolution )
132
- metric : Dict = self .metric_set .get (name , defaultdict (list ))
134
+ metric : dict = self .metric_set .get (name , defaultdict (list ))
133
135
metric ["Unit" ] = unit
134
136
metric ["StorageResolution" ] = resolution
135
137
metric ["Value" ].append (float (value ))
@@ -147,19 +149,19 @@ def add_metric(
147
149
148
150
def serialize_metric_set (
149
151
self ,
150
- metrics : Dict | None = None ,
151
- dimensions : Dict | None = None ,
152
- metadata : Dict | None = None ,
153
- ) -> Dict :
152
+ metrics : dict | None = None ,
153
+ dimensions : dict | None = None ,
154
+ metadata : dict | None = None ,
155
+ ) -> dict :
154
156
"""Serializes metric and dimensions set
155
157
156
158
Parameters
157
159
----------
158
- metrics : Dict , optional
160
+ metrics : dict , optional
159
161
Dictionary of metrics to serialize, by default None
160
- dimensions : Dict , optional
162
+ dimensions : dict , optional
161
163
Dictionary of dimensions to serialize, by default None
162
- metadata: Dict , optional
164
+ metadata: dict , optional
163
165
Dictionary of metadata to serialize, by default None
164
166
165
167
Example
@@ -172,7 +174,7 @@ def serialize_metric_set(
172
174
173
175
Returns
174
176
-------
175
- Dict
177
+ dict
176
178
Serialized metrics following EMF specification
177
179
178
180
Raises
@@ -206,8 +208,8 @@ def serialize_metric_set(
206
208
#
207
209
# In case using high-resolution metrics, add StorageResolution field
208
210
# Example: [ { "Name": "metric_name", "Unit": "Count", "StorageResolution": 1 } ] # noqa ERA001
209
- metric_definition : List [MetricNameUnitResolution ] = []
210
- metric_names_and_values : Dict [str , float ] = {} # { "metric_name": 1.0 }
211
+ metric_definition : list [MetricNameUnitResolution ] = []
212
+ metric_names_and_values : dict [str , float ] = {} # { "metric_name": 1.0 }
211
213
212
214
for metric_name in metrics :
213
215
metric : dict = metrics [metric_name ]
@@ -354,10 +356,10 @@ def flush_metrics(self, raise_on_empty_metrics: bool = False) -> None:
354
356
355
357
def log_metrics (
356
358
self ,
357
- lambda_handler : Callable [[Dict , Any ], Any ] | Optional [ Callable [[Dict , Any , Optional [ Dict ]] , Any ]] = None ,
359
+ lambda_handler : Callable [[dict , Any ], Any ] | Callable [[dict , Any , dict | None ] , Any ] | None = None ,
358
360
capture_cold_start_metric : bool = False ,
359
361
raise_on_empty_metrics : bool = False ,
360
- default_dimensions : Dict [str , str ] | None = None ,
362
+ default_dimensions : dict [str , str ] | None = None ,
361
363
):
362
364
"""Decorator to serialize and publish metrics at the end of a function execution.
363
365
@@ -385,7 +387,7 @@ def handler(event, context):
385
387
captures cold start metric, by default False
386
388
raise_on_empty_metrics : bool, optional
387
389
raise exception if no metrics are emitted, by default False
388
- default_dimensions: Dict [str, str], optional
390
+ default_dimensions: dict [str, str], optional
389
391
metric dimensions as key=value that will always be present
390
392
391
393
Raises
@@ -420,12 +422,12 @@ def decorate(event, context, *args, **kwargs):
420
422
421
423
return decorate
422
424
423
- def _extract_metric_resolution_value (self , resolution : Union [ int , MetricResolution ] ) -> int :
425
+ def _extract_metric_resolution_value (self , resolution : int | MetricResolution ) -> int :
424
426
"""Return metric value from metric unit whether that's str or MetricResolution enum
425
427
426
428
Parameters
427
429
----------
428
- unit : Union[ int, MetricResolution]
430
+ unit : int | MetricResolution
429
431
Metric resolution
430
432
431
433
Returns
@@ -448,12 +450,12 @@ def _extract_metric_resolution_value(self, resolution: Union[int, MetricResoluti
448
450
f"Invalid metric resolution '{ resolution } ', expected either option: { self ._metric_resolutions } " , # noqa: E501
449
451
)
450
452
451
- def _extract_metric_unit_value (self , unit : Union [ str , MetricUnit ] ) -> str :
453
+ def _extract_metric_unit_value (self , unit : str | MetricUnit ) -> str :
452
454
"""Return metric value from metric unit whether that's str or MetricUnit enum
453
455
454
456
Parameters
455
457
----------
456
- unit : Union[ str, MetricUnit]
458
+ unit : str | MetricUnit
457
459
Metric unit
458
460
459
461
Returns
@@ -566,7 +568,7 @@ def single_metric(
566
568
value : float ,
567
569
resolution : MetricResolution | int = 60 ,
568
570
namespace : str | None = None ,
569
- default_dimensions : Dict [str , str ] | None = None ,
571
+ default_dimensions : dict [str , str ] | None = None ,
570
572
) -> Generator [SingleMetric , None , None ]:
571
573
"""Context manager to simplify creation of a single metric
572
574
@@ -604,7 +606,7 @@ def single_metric(
604
606
Metric value
605
607
namespace: str
606
608
Namespace for metrics
607
- default_dimensions: Dict [str, str], optional
609
+ default_dimensions: dict [str, str], optional
608
610
Metric dimensions as key=value that will always be present
609
611
610
612
@@ -624,7 +626,7 @@ def single_metric(
624
626
SchemaValidationError
625
627
When metric object fails EMF schema validation
626
628
""" # noqa: E501
627
- metric_set : Dict | None = None
629
+ metric_set : dict | None = None
628
630
try :
629
631
metric : SingleMetric = SingleMetric (namespace = namespace )
630
632
metric .add_metric (name = name , unit = unit , value = value , resolution = resolution )
0 commit comments