1
+ from __future__ import annotations
2
+
1
3
import datetime
2
4
import functools
3
5
import json
7
9
import warnings
8
10
from collections import defaultdict
9
11
from contextlib import contextmanager
10
- from enum import Enum
11
12
from typing import Any , Callable , Dict , Generator , List , Optional , Union
12
13
13
- from ..shared import constants
14
- from ..shared .functions import resolve_env_var_choice
15
- from .exceptions import (
14
+ from aws_lambda_powertools .metrics .exceptions import (
16
15
MetricResolutionError ,
17
16
MetricUnitError ,
18
17
MetricValueError ,
19
18
SchemaValidationError ,
20
19
)
21
- from .types import MetricNameUnitResolution
20
+ from aws_lambda_powertools .metrics .provider .cloudwatch_emf import cold_start
21
+ from aws_lambda_powertools .metrics .provider .cloudwatch_emf .cold_start import (
22
+ reset_cold_start_flag , # noqa: F401 # backwards compatibility
23
+ )
24
+ from aws_lambda_powertools .metrics .provider .cloudwatch_emf .constants import MAX_DIMENSIONS , MAX_METRICS
25
+ from aws_lambda_powertools .metrics .provider .cloudwatch_emf .metric_properties import MetricResolution , MetricUnit
26
+ from aws_lambda_powertools .metrics .types import MetricNameUnitResolution
27
+ from aws_lambda_powertools .shared import constants
28
+ from aws_lambda_powertools .shared .functions import resolve_env_var_choice
22
29
23
30
logger = logging .getLogger (__name__ )
24
31
25
- MAX_METRICS = 100
26
- MAX_DIMENSIONS = 29
27
-
28
- is_cold_start = True
29
-
30
-
31
- class MetricResolution (Enum ):
32
- Standard = 60
33
- High = 1
34
-
35
-
36
- class MetricUnit (Enum ):
37
- Seconds = "Seconds"
38
- Microseconds = "Microseconds"
39
- Milliseconds = "Milliseconds"
40
- Bytes = "Bytes"
41
- Kilobytes = "Kilobytes"
42
- Megabytes = "Megabytes"
43
- Gigabytes = "Gigabytes"
44
- Terabytes = "Terabytes"
45
- Bits = "Bits"
46
- Kilobits = "Kilobits"
47
- Megabits = "Megabits"
48
- Gigabits = "Gigabits"
49
- Terabits = "Terabits"
50
- Percent = "Percent"
51
- Count = "Count"
52
- BytesPerSecond = "Bytes/Second"
53
- KilobytesPerSecond = "Kilobytes/Second"
54
- MegabytesPerSecond = "Megabytes/Second"
55
- GigabytesPerSecond = "Gigabytes/Second"
56
- TerabytesPerSecond = "Terabytes/Second"
57
- BitsPerSecond = "Bits/Second"
58
- KilobitsPerSecond = "Kilobits/Second"
59
- MegabitsPerSecond = "Megabits/Second"
60
- GigabitsPerSecond = "Gigabits/Second"
61
- TerabitsPerSecond = "Terabits/Second"
62
- CountPerSecond = "Count/Second"
32
+ # Maintenance: alias due to Hyrum's law
33
+ is_cold_start = cold_start .is_cold_start
63
34
64
35
65
36
class MetricManager :
@@ -94,11 +65,11 @@ class MetricManager:
94
65
95
66
def __init__ (
96
67
self ,
97
- metric_set : Optional [ Dict [str , Any ]] = None ,
98
- dimension_set : Optional [ Dict ] = None ,
99
- namespace : Optional [ str ] = None ,
100
- metadata_set : Optional [ Dict [str , Any ]] = None ,
101
- service : Optional [ str ] = None ,
68
+ metric_set : Dict [str , Any ] | None = None ,
69
+ dimension_set : Dict | None = None ,
70
+ namespace : str | None = None ,
71
+ metadata_set : Dict [str , Any ] | None = None ,
72
+ service : str | None = None ,
102
73
):
103
74
self .metric_set = metric_set if metric_set is not None else {}
104
75
self .dimension_set = dimension_set if dimension_set is not None else {}
@@ -112,9 +83,9 @@ def __init__(
112
83
def add_metric (
113
84
self ,
114
85
name : str ,
115
- unit : Union [ MetricUnit , str ] ,
86
+ unit : MetricUnit | str ,
116
87
value : float ,
117
- resolution : Union [ MetricResolution , int ] = 60 ,
88
+ resolution : MetricResolution | int = 60 ,
118
89
) -> None :
119
90
"""Adds given metric
120
91
@@ -173,9 +144,9 @@ def add_metric(
173
144
174
145
def serialize_metric_set (
175
146
self ,
176
- metrics : Optional [ Dict ] = None ,
177
- dimensions : Optional [ Dict ] = None ,
178
- metadata : Optional [ Dict ] = None ,
147
+ metrics : Dict | None = None ,
148
+ dimensions : Dict | None = None ,
149
+ metadata : Dict | None = None ,
179
150
) -> Dict :
180
151
"""Serializes metric and dimensions set
181
152
@@ -355,10 +326,10 @@ def flush_metrics(self, raise_on_empty_metrics: bool = False) -> None:
355
326
356
327
def log_metrics (
357
328
self ,
358
- lambda_handler : Union [ Callable [[Dict , Any ], Any ], Optional [Callable [[Dict , Any , Optional [Dict ]], Any ] ]] = None ,
329
+ lambda_handler : Callable [[Dict , Any ], Any ] | Optional [Callable [[Dict , Any , Optional [Dict ]], Any ]] = None ,
359
330
capture_cold_start_metric : bool = False ,
360
331
raise_on_empty_metrics : bool = False ,
361
- default_dimensions : Optional [ Dict [str , str ]] = None ,
332
+ default_dimensions : Dict [str , str ] | None = None ,
362
333
):
363
334
"""Decorator to serialize and publish metrics at the end of a function execution.
364
335
@@ -537,9 +508,9 @@ class SingleMetric(MetricManager):
537
508
def add_metric (
538
509
self ,
539
510
name : str ,
540
- unit : Union [ MetricUnit , str ] ,
511
+ unit : MetricUnit | str ,
541
512
value : float ,
542
- resolution : Union [ MetricResolution , int ] = 60 ,
513
+ resolution : MetricResolution | int = 60 ,
543
514
) -> None :
544
515
"""Method to prevent more than one metric being created
545
516
@@ -565,9 +536,9 @@ def single_metric(
565
536
name : str ,
566
537
unit : MetricUnit ,
567
538
value : float ,
568
- resolution : Union [ MetricResolution , int ] = 60 ,
569
- namespace : Optional [ str ] = None ,
570
- default_dimensions : Optional [ Dict [str , str ]] = None ,
539
+ resolution : MetricResolution | int = 60 ,
540
+ namespace : str | None = None ,
541
+ default_dimensions : Dict [str , str ] | None = None ,
571
542
) -> Generator [SingleMetric , None , None ]:
572
543
"""Context manager to simplify creation of a single metric
573
544
@@ -622,7 +593,7 @@ def single_metric(
622
593
SchemaValidationError
623
594
When metric object fails EMF schema validation
624
595
""" # noqa: E501
625
- metric_set : Optional [ Dict ] = None
596
+ metric_set : Dict | None = None
626
597
try :
627
598
metric : SingleMetric = SingleMetric (namespace = namespace )
628
599
metric .add_metric (name = name , unit = unit , value = value , resolution = resolution )
@@ -635,9 +606,3 @@ def single_metric(
635
606
metric_set = metric .serialize_metric_set ()
636
607
finally :
637
608
print (json .dumps (metric_set , separators = ("," , ":" )))
638
-
639
-
640
- def reset_cold_start_flag ():
641
- global is_cold_start
642
- if not is_cold_start :
643
- is_cold_start = True
0 commit comments