-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathmodels.py
132 lines (108 loc) · 3.87 KB
/
models.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
"""Collection of classes as models and builder functions
that provide classes as data representation for
key data used in more than one place.
"""
from enum import Enum
from typing import Union
class LambdaContextModel:
"""A handful of Lambda Runtime Context fields
Full Lambda Context object: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html
NOTE
----
Originally, memory_size is `int` but we cast to `str` in this model
due to aws_lambda_logging library use of `%` during formatting
Ref: https://gitlab.com/hadrien/aws_lambda_logging/blob/master/aws_lambda_logging.py#L47
Parameters
----------
function_name: str
Lambda function name, by default "UNDEFINED"
e.g. "test"
function_memory_size: str
Lambda function memory in MB, by default "UNDEFINED"
e.g. "128"
casting from int to str due to aws_lambda_logging using `%` when enumerating fields
function_arn: str
Lambda function ARN, by default "UNDEFINED"
e.g. "arn:aws:lambda:eu-west-1:809313241:function:test"
function_request_id: str
Lambda function unique request id, by default "UNDEFINED"
e.g. "52fdfc07-2182-154f-163f-5f0f9a621d72"
"""
def __init__(
self,
function_name: str = "UNDEFINED",
function_memory_size: str = "UNDEFINED",
function_arn: str = "UNDEFINED",
function_request_id: str = "UNDEFINED",
):
self.function_name = function_name
self.function_memory_size = function_memory_size
self.function_arn = function_arn
self.function_request_id = function_request_id
def build_lambda_context_model(context: object) -> LambdaContextModel:
"""Captures Lambda function runtime info to be used across all log statements
Parameters
----------
context : object
Lambda context object
Returns
-------
LambdaContextModel
Lambda context only with select fields
"""
context = {
"function_name": context.function_name,
"function_memory_size": str(context.memory_limit_in_mb),
"function_arn": context.invoked_function_arn,
"function_request_id": context.aws_request_id,
}
return LambdaContextModel(**context)
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"
def build_metric_unit_from_str(unit: Union[str, MetricUnit]) -> MetricUnit:
"""Builds correct metric unit value from string or return Count as default
Parameters
----------
unit : str, MetricUnit
metric unit
Returns
-------
MetricUnit
Metric Unit enum from string value or MetricUnit.Count as a default
"""
if isinstance(unit, MetricUnit):
return unit
if isinstance(unit, str):
unit = unit.lower().capitalize()
metric_unit = None
try:
metric_unit = MetricUnit[unit]
except (TypeError, KeyError):
metric_units = [units for units, _ in MetricUnit.__members__.items()]
raise ValueError(f"Invalid Metric Unit - Received {unit}. Value Metric Units are {metric_units}")
return metric_unit