@@ -22,6 +22,7 @@ class Config:
22
22
CW_METRICS_NAMESPACE = 'CW_METRICS_NAMESPACE'
23
23
CW_METRICS_METRIC_NAME = 'CW_METRICS_METRIC_NAME'
24
24
BODY_REGEX_MATCH = 'BODY_REGEX_MATCH'
25
+ STATUS_CODE_MATCH = 'STATUS_CODE_MATCH'
25
26
26
27
def __init__ (self , event ):
27
28
self .event = event
@@ -34,7 +35,8 @@ def __init__(self, event):
34
35
self .REPORT_AS_CW_METRICS : '1' ,
35
36
self .CW_METRICS_NAMESPACE : 'HttpCheck' ,
36
37
self .HEADERS : '' ,
37
- self .BODY_REGEX_MATCH : None
38
+ self .BODY_REGEX_MATCH : None ,
39
+ self .STATUS_CODE_MATCH : None
38
40
}
39
41
40
42
def __get_property (self , property_name ):
@@ -83,6 +85,10 @@ def headers(self):
83
85
@property
84
86
def bodyregexmatch (self ):
85
87
return self .__get_property (self .BODY_REGEX_MATCH )
88
+
89
+ @property
90
+ def statuscodematch (self ):
91
+ return self .__get_property (self .STATUS_CODE_MATCH )
86
92
87
93
@property
88
94
def cwoptions (self ):
@@ -102,6 +108,7 @@ def __init__(self, config):
102
108
self .payload = config .payload
103
109
self .headers = config .headers
104
110
self .bodyregexmatch = config .bodyregexmatch
111
+ self .statuscodematch = config .statuscodematch
105
112
106
113
def execute (self ):
107
114
url = urlparse (self .endpoint )
@@ -145,6 +152,9 @@ def execute(self):
145
152
regex = re .compile (self .bodyregexmatch )
146
153
value = 1 if regex .match (response_body ) else 0
147
154
result ['ResponseBodyRegexMatch' ] = value
155
+
156
+ if self .statuscodematch is not None :
157
+ result ['StatusCodeMatch' ] = int (int (response_data .status ) == int (self .statuscodematch ))
148
158
149
159
# return structure with data
150
160
return result
@@ -189,15 +199,16 @@ def report(self, result):
189
199
'Unit' : 'None' ,
190
200
'Value' : int (result ['StatusCode' ])
191
201
})
192
- if 'ResponseBodyRegexMatch' in result :
193
- metric_data .append ({
194
- 'MetricName' : 'ResponseBodyRegexMatch' ,
195
- 'Dimensions' : [
196
- {'Name' : 'Endpoint' , 'Value' : self .endpoint }
197
- ],
198
- 'Unit' : 'None' ,
199
- 'Value' : int (result ['ResponseBodyRegexMatch' ])
200
- })
202
+ for additional_metric in ['ResponseBodyRegexMatch' , 'StatusCodeMatch' ]:
203
+ if additional_metric in result :
204
+ metric_data .append ({
205
+ 'MetricName' : additional_metric ,
206
+ 'Dimensions' : [
207
+ {'Name' : 'Endpoint' , 'Value' : self .endpoint }
208
+ ],
209
+ 'Unit' : 'None' ,
210
+ 'Value' : int (result [additional_metric ])
211
+ })
201
212
202
213
result = cloudwatch .put_metric_data (
203
214
MetricData = metric_data ,
0 commit comments