@@ -16,10 +16,29 @@ def format_dimensions(dimensions: Mapping[str, str]) -> List[Mapping[str, str]]:
16
16
return [{"Name" : key , "Value" : value } for key , value in dimensions .items ()]
17
17
18
18
19
- class MetricPublisher :
20
- def __init__ (self , session : SessionProxy , namespace : str ) -> None :
21
- self .client = session .client ("cloudwatch" )
22
- self .namespace = namespace
19
+ class MetricsPublisher :
20
+ """A cloudwatch based metric publisher.\
21
+ Given a resource type and session, \
22
+ this publisher will publish metrics to CloudWatch.\
23
+ Can be used with the MetricsPublisherProxy.
24
+
25
+ Functions:
26
+ ----------
27
+ __init__: Initializes metric publisher with given session and resource type
28
+
29
+ publish_exception_metric: Publishes an exception based metric
30
+
31
+ publish_invocation_metric: Publishes a metric related to invocations
32
+
33
+ publish_duration_metric: Publishes an duration metric
34
+
35
+ publish_log_delivery_exception_metric: Publishes an log delivery exception metric
36
+ """
37
+
38
+ def __init__ (self , session : SessionProxy , resource_type : str ) -> None :
39
+ self ._client = session .client ("cloudwatch" )
40
+ self ._resource_type = resource_type
41
+ self ._namespace = self ._make_namespace (self ._resource_type )
23
42
24
43
def publish_metric ( # pylint: disable-msg=too-many-arguments
25
44
self ,
@@ -30,8 +49,8 @@ def publish_metric( # pylint: disable-msg=too-many-arguments
30
49
timestamp : datetime .datetime ,
31
50
) -> None :
32
51
try :
33
- self .client .put_metric_data (
34
- Namespace = self .namespace ,
52
+ self ._client .put_metric_data (
53
+ Namespace = self ._namespace ,
35
54
MetricData = [
36
55
{
37
56
"MetricName" : metric_name .name ,
@@ -46,84 +65,125 @@ def publish_metric( # pylint: disable-msg=too-many-arguments
46
65
except ClientError as e :
47
66
LOG .error ("An error occurred while publishing metrics: %s" , str (e ))
48
67
49
-
50
- class MetricsPublisherProxy :
51
- @staticmethod
52
- def _make_namespace (resource_type : str ) -> str :
53
- suffix = resource_type .replace ("::" , "/" )
54
- return f"{ METRIC_NAMESPACE_ROOT } /{ suffix } "
55
-
56
- def __init__ (self , resource_type : str ) -> None :
57
- self .namespace = self ._make_namespace (resource_type )
58
- self .resource_type = resource_type
59
- self ._publishers : List [MetricPublisher ] = []
60
-
61
- def add_metrics_publisher (self , session : Optional [SessionProxy ]) -> None :
62
- if session :
63
- self ._publishers .append (MetricPublisher (session , self .namespace ))
64
-
65
68
def publish_exception_metric (
66
69
self , timestamp : datetime .datetime , action : Action , error : Any
67
70
) -> None :
68
71
dimensions : Mapping [str , str ] = {
69
72
"DimensionKeyActionType" : action .name ,
70
73
"DimensionKeyExceptionType" : str (type (error )),
71
- "DimensionKeyResourceType" : self .resource_type ,
74
+ "DimensionKeyResourceType" : self ._resource_type ,
72
75
}
73
- for publisher in self ._publishers :
74
- publisher .publish_metric (
75
- metric_name = MetricTypes .HandlerException ,
76
- dimensions = dimensions ,
77
- unit = StandardUnit .Count ,
78
- value = 1.0 ,
79
- timestamp = timestamp ,
80
- )
76
+ self .publish_metric (
77
+ metric_name = MetricTypes .HandlerException ,
78
+ dimensions = dimensions ,
79
+ unit = StandardUnit .Count ,
80
+ value = 1.0 ,
81
+ timestamp = timestamp ,
82
+ )
81
83
82
84
def publish_invocation_metric (
83
85
self , timestamp : datetime .datetime , action : Action
84
86
) -> None :
85
87
dimensions = {
86
88
"DimensionKeyActionType" : action .name ,
87
- "DimensionKeyResourceType" : self .resource_type ,
89
+ "DimensionKeyResourceType" : self ._resource_type ,
88
90
}
89
- for publisher in self ._publishers :
90
- publisher .publish_metric (
91
- metric_name = MetricTypes .HandlerInvocationCount ,
92
- dimensions = dimensions ,
93
- unit = StandardUnit .Count ,
94
- value = 1.0 ,
95
- timestamp = timestamp ,
96
- )
91
+ self .publish_metric (
92
+ metric_name = MetricTypes .HandlerInvocationCount ,
93
+ dimensions = dimensions ,
94
+ unit = StandardUnit .Count ,
95
+ value = 1.0 ,
96
+ timestamp = timestamp ,
97
+ )
97
98
98
99
def publish_duration_metric (
99
100
self , timestamp : datetime .datetime , action : Action , milliseconds : float
100
101
) -> None :
101
102
dimensions = {
102
103
"DimensionKeyActionType" : action .name ,
103
- "DimensionKeyResourceType" : self .resource_type ,
104
+ "DimensionKeyResourceType" : self ._resource_type ,
104
105
}
105
- for publisher in self . _publishers :
106
- publisher .publish_metric (
107
- metric_name = MetricTypes .HandlerInvocationDuration ,
108
- dimensions = dimensions ,
109
- unit = StandardUnit .Milliseconds ,
110
- value = milliseconds ,
111
- timestamp = timestamp ,
112
- )
106
+
107
+ self .publish_metric (
108
+ metric_name = MetricTypes .HandlerInvocationDuration ,
109
+ dimensions = dimensions ,
110
+ unit = StandardUnit .Milliseconds ,
111
+ value = milliseconds ,
112
+ timestamp = timestamp ,
113
+ )
113
114
114
115
def publish_log_delivery_exception_metric (
115
116
self , timestamp : datetime .datetime , error : Any
116
117
) -> None :
117
118
dimensions = {
118
119
"DimensionKeyActionType" : "ProviderLogDelivery" ,
119
120
"DimensionKeyExceptionType" : str (type (error )),
120
- "DimensionKeyResourceType" : self .resource_type ,
121
+ "DimensionKeyResourceType" : self ._resource_type ,
121
122
}
123
+ self .publish_metric (
124
+ metric_name = MetricTypes .HandlerException ,
125
+ dimensions = dimensions ,
126
+ unit = StandardUnit .Count ,
127
+ value = 1.0 ,
128
+ timestamp = timestamp ,
129
+ )
130
+
131
+ @staticmethod
132
+ def _make_namespace (resource_type : str ) -> str :
133
+ suffix = resource_type .replace ("::" , "/" )
134
+ return f"{ METRIC_NAMESPACE_ROOT } /{ suffix } "
135
+
136
+
137
+ class MetricsPublisherProxy :
138
+ """A proxy for publishing metrics to multiple publishers. \
139
+ Iterates over available publishers and publishes.
140
+
141
+ Functions:
142
+ ----------
143
+ add_metrics_publisher: Adds a metrics publisher to the list of publishers
144
+
145
+ publish_exception_metric: \
146
+ Publishes an exception based metric to the list of publishers
147
+
148
+ publish_invocation_metric: \
149
+ Publishes a metric related to invocations to the list of publishers
150
+
151
+ publish_duration_metric: Publishes a duration metric to the list of publishers
152
+
153
+ publish_log_delivery_exception_metric: \
154
+ Publishes a log delivery exception metric to the list of publishers
155
+ """
156
+
157
+ def __init__ (self ) -> None :
158
+ self ._publishers : List [MetricsPublisher ] = []
159
+
160
+ def add_metrics_publisher (
161
+ self , session : Optional [SessionProxy ], type_name : Optional [str ]
162
+ ) -> None :
163
+ if session and type_name :
164
+ publisher = MetricsPublisher (session , type_name )
165
+ self ._publishers .append (publisher )
166
+
167
+ def publish_exception_metric (
168
+ self , timestamp : datetime .datetime , action : Action , error : Any
169
+ ) -> None :
122
170
for publisher in self ._publishers :
123
- publisher .publish_metric (
124
- metric_name = MetricTypes .HandlerException ,
125
- dimensions = dimensions ,
126
- unit = StandardUnit .Count ,
127
- value = 1.0 ,
128
- timestamp = timestamp ,
129
- )
171
+ publisher .publish_exception_metric (timestamp , action , error )
172
+
173
+ def publish_invocation_metric (
174
+ self , timestamp : datetime .datetime , action : Action
175
+ ) -> None :
176
+ for publisher in self ._publishers :
177
+ publisher .publish_invocation_metric (timestamp , action )
178
+
179
+ def publish_duration_metric (
180
+ self , timestamp : datetime .datetime , action : Action , milliseconds : float
181
+ ) -> None :
182
+ for publisher in self ._publishers :
183
+ publisher .publish_duration_metric (timestamp , action , milliseconds )
184
+
185
+ def publish_log_delivery_exception_metric (
186
+ self , timestamp : datetime .datetime , error : Any
187
+ ) -> None :
188
+ for publisher in self ._publishers :
189
+ publisher .publish_log_delivery_exception_metric (timestamp , error )
0 commit comments