1
1
import datetime
2
2
import logging
3
- from typing import Any , List , Mapping
4
-
5
- # boto3 doesn't have stub files
6
- from boto3 .session import Session # type: ignore
3
+ from typing import Any , List , Mapping , Optional
7
4
8
5
from botocore .exceptions import ClientError # type: ignore
9
6
7
+ from .boto3_proxy import SessionProxy
10
8
from .interface import Action , MetricTypes , StandardUnit
11
9
12
10
LOG = logging .getLogger (__name__ )
@@ -19,11 +17,9 @@ def format_dimensions(dimensions: Mapping[str, str]) -> List[Mapping[str, str]]:
19
17
20
18
21
19
class MetricPublisher :
22
- def __init__ (self , account_id : str , resource_type : str , session : Session ) -> None :
23
- suffix = resource_type .replace ("::" , "/" )
24
- self .namespace = f"{ METRIC_NAMESPACE_ROOT } /{ account_id } /{ suffix } "
25
- self .resource_type = resource_type
20
+ def __init__ (self , session : SessionProxy , namespace : str ) -> None :
26
21
self .client = session .client ("cloudwatch" )
22
+ self .namespace = namespace
27
23
28
24
def publish_metric ( # pylint: disable-msg=too-many-arguments
29
25
self ,
@@ -52,21 +48,29 @@ def publish_metric( # pylint: disable-msg=too-many-arguments
52
48
53
49
54
50
class MetricsPublisherProxy :
55
- def __init__ (self ) -> None :
51
+ @staticmethod
52
+ def _make_namespace (account_id : str , resource_type : str ) -> str :
53
+ suffix = resource_type .replace ("::" , "/" )
54
+ return f"{ METRIC_NAMESPACE_ROOT } /{ account_id } /{ suffix } "
55
+
56
+ def __init__ (self , account_id : str , resource_type : str ) -> None :
57
+ self .namespace = self ._make_namespace (account_id , resource_type )
58
+ self .resource_type = resource_type
56
59
self ._publishers : List [MetricPublisher ] = []
57
60
58
- def add_metrics_publisher (self , publisher : MetricPublisher ) -> None :
59
- self ._publishers .append (publisher )
61
+ def add_metrics_publisher (self , session : Optional [SessionProxy ]) -> None :
62
+ if session :
63
+ self ._publishers .append (MetricPublisher (session , self .namespace ))
60
64
61
65
def publish_exception_metric (
62
66
self , timestamp : datetime .datetime , action : Action , error : Any
63
67
) -> None :
68
+ dimensions : Mapping [str , str ] = {
69
+ "DimensionKeyActionType" : action .name ,
70
+ "DimensionKeyExceptionType" : str (type (error )),
71
+ "DimensionKeyResourceType" : self .resource_type ,
72
+ }
64
73
for publisher in self ._publishers :
65
- dimensions : Mapping [str , str ] = {
66
- "DimensionKeyActionType" : action .name ,
67
- "DimensionKeyExceptionType" : str (type (error )),
68
- "DimensionKeyResourceType" : publisher .resource_type ,
69
- }
70
74
publisher .publish_metric (
71
75
metric_name = MetricTypes .HandlerException ,
72
76
dimensions = dimensions ,
@@ -78,11 +82,11 @@ def publish_exception_metric(
78
82
def publish_invocation_metric (
79
83
self , timestamp : datetime .datetime , action : Action
80
84
) -> None :
85
+ dimensions = {
86
+ "DimensionKeyActionType" : action .name ,
87
+ "DimensionKeyResourceType" : self .resource_type ,
88
+ }
81
89
for publisher in self ._publishers :
82
- dimensions = {
83
- "DimensionKeyActionType" : action .name ,
84
- "DimensionKeyResourceType" : publisher .resource_type ,
85
- }
86
90
publisher .publish_metric (
87
91
metric_name = MetricTypes .HandlerInvocationCount ,
88
92
dimensions = dimensions ,
@@ -94,11 +98,11 @@ def publish_invocation_metric(
94
98
def publish_duration_metric (
95
99
self , timestamp : datetime .datetime , action : Action , milliseconds : float
96
100
) -> None :
101
+ dimensions = {
102
+ "DimensionKeyActionType" : action .name ,
103
+ "DimensionKeyResourceType" : self .resource_type ,
104
+ }
97
105
for publisher in self ._publishers :
98
- dimensions = {
99
- "DimensionKeyActionType" : action .name ,
100
- "DimensionKeyResourceType" : publisher .resource_type ,
101
- }
102
106
publisher .publish_metric (
103
107
metric_name = MetricTypes .HandlerInvocationDuration ,
104
108
dimensions = dimensions ,
@@ -110,12 +114,12 @@ def publish_duration_metric(
110
114
def publish_log_delivery_exception_metric (
111
115
self , timestamp : datetime .datetime , error : Any
112
116
) -> None :
117
+ dimensions = {
118
+ "DimensionKeyActionType" : "ProviderLogDelivery" ,
119
+ "DimensionKeyExceptionType" : str (type (error )),
120
+ "DimensionKeyResourceType" : self .resource_type ,
121
+ }
113
122
for publisher in self ._publishers :
114
- dimensions : Mapping [str , str ] = {
115
- "DimensionKeyActionType" : "ProviderLogDelivery" ,
116
- "DimensionKeyExceptionType" : str (type (error )),
117
- "DimensionKeyResourceType" : publisher .resource_type ,
118
- }
119
123
publisher .publish_metric (
120
124
metric_name = MetricTypes .HandlerException ,
121
125
dimensions = dimensions ,
0 commit comments