Skip to content

Commit 0c09679

Browse files
author
toshke
committed
Added support for reporting boolean for matching status code
1 parent 0f0c0da commit 0c09679

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ their namespace, defaults to 'HttpCheck'
3434
metric to be published as well, with value determined by success of matching response body against
3535
regular expression contained within this option
3636

37+
`STATUS_CODE_MATCH` - report whether http status code is equal to given status code or not. If this option
38+
is not present, it won't be reported upon. Defaults to empty
3739

3840
## Outputs
3941

@@ -50,6 +52,9 @@ is 2 minutes for http requests.
5052

5153
`ResponseBody` - Optional, by default this won't be reported
5254

55+
`ResponseBodyRegexMatch` - Optional, if `BODY_REGEX_MATCH` option is provided
56+
57+
`StatusCodeMatch` - Optional, if `STATUS_CODE_MATCH` options is provided
5358

5459
## Dependencies
5560

@@ -71,6 +76,9 @@ server timeout
7176
- `ResponseBodyRegexMatch` - **optional** this will report 1 or 0 if `BODY_REGEX_MATCH` option is specified. 1 is reported
7277
if response body matches regex provided, or 0 otherwise.
7378

79+
- `StatusCodeMatch` - **optional*& this will report 1 or 0 if `STATUS_CODE_MATCH` options is specified. 1 is reported
80+
if response status code matches code provided, or 0 otherwise
81+
7482
## Deployment
7583

7684
You can either deploy Lambda manually, or through [serverless](serverless.com) project.

handler.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Config:
2222
CW_METRICS_NAMESPACE = 'CW_METRICS_NAMESPACE'
2323
CW_METRICS_METRIC_NAME = 'CW_METRICS_METRIC_NAME'
2424
BODY_REGEX_MATCH = 'BODY_REGEX_MATCH'
25+
STATUS_CODE_MATCH = 'STATUS_CODE_MATCH'
2526

2627
def __init__(self, event):
2728
self.event = event
@@ -34,7 +35,8 @@ def __init__(self, event):
3435
self.REPORT_AS_CW_METRICS: '1',
3536
self.CW_METRICS_NAMESPACE: 'HttpCheck',
3637
self.HEADERS: '',
37-
self.BODY_REGEX_MATCH: None
38+
self.BODY_REGEX_MATCH: None,
39+
self.STATUS_CODE_MATCH: None
3840
}
3941

4042
def __get_property(self, property_name):
@@ -83,6 +85,10 @@ def headers(self):
8385
@property
8486
def bodyregexmatch(self):
8587
return self.__get_property(self.BODY_REGEX_MATCH)
88+
89+
@property
90+
def statuscodematch(self):
91+
return self.__get_property(self.STATUS_CODE_MATCH)
8692

8793
@property
8894
def cwoptions(self):
@@ -102,6 +108,7 @@ def __init__(self, config):
102108
self.payload = config.payload
103109
self.headers = config.headers
104110
self.bodyregexmatch = config.bodyregexmatch
111+
self.statuscodematch = config.statuscodematch
105112

106113
def execute(self):
107114
url = urlparse(self.endpoint)
@@ -145,6 +152,9 @@ def execute(self):
145152
regex = re.compile(self.bodyregexmatch)
146153
value = 1 if regex.match(response_body) else 0
147154
result['ResponseBodyRegexMatch'] = value
155+
156+
if self.statuscodematch is not None:
157+
result['StatusCodeMatch'] = int(int(response_data.status) == int(self.statuscodematch))
148158

149159
# return structure with data
150160
return result
@@ -189,15 +199,16 @@ def report(self, result):
189199
'Unit': 'None',
190200
'Value': int(result['StatusCode'])
191201
})
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+
})
201212

202213
result = cloudwatch.put_metric_data(
203214
MetricData=metric_data,

test/ipify.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"REPORT_RESPONSE_BODY":"1",
66
"REPORT_AS_CW_METRICS":"1",
77
"CW_METRICS_NAMESPACE":"HttpCheckTestNamespace1",
8-
"BODY_REGEX_MATCH":"\\{\"ip\":(.*)\\}"
8+
"BODY_REGEX_MATCH":"\\{\"ip\":(.*)\\}",
9+
"STATUS_CODE_MATCH": 200
910
}

test/postRequest.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"PAYLOAD":"{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}",
66
"REPORT_RESPONSE_BODY":"1",
77
"REPORT_AS_CW_METRICS":"1",
8-
"CW_METRICS_NAMESPACE":"HttpCheckTestNamespace"
8+
"CW_METRICS_NAMESPACE":"HttpCheckTestNamespace",
9+
"STATUS_CODE_MATCH": 201
910
}

0 commit comments

Comments
 (0)