1111
1212LOG = logging .getLogger (__name__ )
1313
14+ METRIC_NAMESPACE_ROOT = "AWS/CloudFormation"
15+
1416
1517def format_dimensions (dimensions : Mapping [str , str ]) -> List [Mapping [str , str ]]:
1618 return [{"Name" : key , "Value" : value } for key , value in dimensions .items ()]
1719
1820
1921class MetricPublisher :
20- def __init__ (self , namespace : str , session : Session ) -> None :
21- self .namespace = namespace
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
2226 self .client = session .client ("cloudwatch" )
2327
24- def _publish_metric ( # pylint: disable-msg=too-many-arguments
28+ def publish_metric ( # pylint: disable-msg=too-many-arguments
2529 self ,
2630 metric_name : MetricTypes ,
2731 dimensions : Mapping [str , str ],
@@ -34,9 +38,9 @@ def _publish_metric( # pylint: disable-msg=too-many-arguments
3438 Namespace = self .namespace ,
3539 MetricData = [
3640 {
37- "MetricName" : metric_name ,
41+ "MetricName" : metric_name . name ,
3842 "Dimensions" : format_dimensions (dimensions ),
39- "Unit" : unit ,
43+ "Unit" : unit . name ,
4044 "Timestamp" : str (timestamp ),
4145 "Value" : value ,
4246 }
@@ -46,51 +50,76 @@ def _publish_metric( # pylint: disable-msg=too-many-arguments
4650 except ClientError as e :
4751 LOG .error ("An error occurred while publishing metrics: %s" , str (e ))
4852
53+
54+ class MetricsPublisherProxy :
55+ def __init__ (self ) -> None :
56+ self ._publishers : List [MetricPublisher ] = []
57+
58+ def add_metrics_publisher (self , publisher : MetricPublisher ) -> None :
59+ self ._publishers .append (publisher )
60+
4961 def publish_exception_metric (
5062 self , timestamp : datetime .datetime , action : Action , error : Any
5163 ) -> None :
52- dimensions : Mapping [ str , str ] = {
53- "DimensionKeyActionType" : action ,
54- "DimensionKeyExceptionType " : str ( type ( error )) ,
55- "DimensionKeyResourceType " : self . namespace ,
56- }
57-
58- self . _publish_metric (
59- metric_name = MetricTypes .HandlerException ,
60- dimensions = dimensions ,
61- unit = StandardUnit .Count ,
62- value = 1.0 ,
63- timestamp = timestamp ,
64- )
64+ 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+ publisher . publish_metric (
71+ metric_name = MetricTypes .HandlerException ,
72+ dimensions = dimensions ,
73+ unit = StandardUnit .Count ,
74+ value = 1.0 ,
75+ timestamp = timestamp ,
76+ )
6577
6678 def publish_invocation_metric (
6779 self , timestamp : datetime .datetime , action : Action
6880 ) -> None :
69- dimensions = {
70- "DimensionKeyActionType" : action ,
71- "DimensionKeyResourceType " : self . namespace ,
72- }
73-
74- self . _publish_metric (
75- metric_name = MetricTypes .HandlerInvocationCount ,
76- dimensions = dimensions ,
77- unit = StandardUnit .Count ,
78- value = 1.0 ,
79- timestamp = timestamp ,
80- )
81+ for publisher in self . _publishers :
82+ dimensions = {
83+ "DimensionKeyActionType " : action . name ,
84+ "DimensionKeyResourceType" : publisher . resource_type ,
85+ }
86+ publisher . publish_metric (
87+ metric_name = MetricTypes .HandlerInvocationCount ,
88+ dimensions = dimensions ,
89+ unit = StandardUnit .Count ,
90+ value = 1.0 ,
91+ timestamp = timestamp ,
92+ )
8193
8294 def publish_duration_metric (
8395 self , timestamp : datetime .datetime , action : Action , milliseconds : float
8496 ) -> None :
85- dimensions = {
86- "DimensionKeyActionType" : action ,
87- "DimensionKeyResourceType" : self .namespace ,
88- }
89-
90- self ._publish_metric (
91- metric_name = MetricTypes .HandlerInvocationDuration ,
92- dimensions = dimensions ,
93- unit = StandardUnit .Milliseconds ,
94- value = milliseconds ,
95- timestamp = timestamp ,
96- )
97+ for publisher in self ._publishers :
98+ dimensions = {
99+ "DimensionKeyActionType" : action .name ,
100+ "DimensionKeyResourceType" : publisher .resource_type ,
101+ }
102+ publisher .publish_metric (
103+ metric_name = MetricTypes .HandlerInvocationDuration ,
104+ dimensions = dimensions ,
105+ unit = StandardUnit .Milliseconds ,
106+ value = milliseconds ,
107+ timestamp = timestamp ,
108+ )
109+
110+ def publish_log_delivery_exception_metric (
111+ self , timestamp : datetime .datetime , error : Any
112+ ) -> None :
113+ 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+ publisher .publish_metric (
120+ metric_name = MetricTypes .HandlerException ,
121+ dimensions = dimensions ,
122+ unit = StandardUnit .Count ,
123+ value = 1.0 ,
124+ timestamp = timestamp ,
125+ )
0 commit comments