forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
71 lines (54 loc) · 2.01 KB
/
functions.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
from __future__ import annotations
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.exceptions import (
MetricResolutionError,
MetricUnitError,
)
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.metric_properties import MetricResolution, MetricUnit
from aws_lambda_powertools.shared.types import List
def extract_cloudwatch_metric_resolution_value(metric_resolutions: List, resolution: int | MetricResolution) -> int:
"""Return metric value from CloudWatch metric unit whether that's str or MetricResolution enum
Parameters
----------
unit : Union[int, MetricResolution]
Metric resolution
Returns
-------
int
Metric resolution value must be 1 or 60
Raises
------
MetricResolutionError
When metric resolution is not supported by CloudWatch
"""
if isinstance(resolution, MetricResolution):
return resolution.value
if isinstance(resolution, int) and resolution in metric_resolutions:
return resolution
raise MetricResolutionError(
f"Invalid metric resolution '{resolution}', expected either option: {metric_resolutions}", # noqa: E501
)
def extract_cloudwatch_metric_unit_value(metric_units: List, metric_valid_options: List, unit: str | MetricUnit) -> str:
"""Return metric value from CloudWatch 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 metric_valid_options:
unit = MetricUnit[unit].value
if unit not in metric_units:
raise MetricUnitError(
f"Invalid metric unit '{unit}', expected either option: {metric_valid_options}",
)
if isinstance(unit, MetricUnit):
unit = unit.value
return unit