-
Notifications
You must be signed in to change notification settings - Fork 254
Support for CloudWatch Metric Math Alarms #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -233,12 +233,33 @@ var handleCloudWatch = function(event, context) { | |
| var region = event.Records[0].EventSubscriptionArn.split(":")[3]; | ||
| var subject = "AWS CloudWatch Notification"; | ||
| var alarmName = message.AlarmName; | ||
| var metricName = message.Trigger.MetricName; | ||
| var trigger = message.Trigger; | ||
| var alarmExpression; | ||
| if (typeof trigger.MetricName === "undefined") { | ||
| // This is a CloudWatch metric math alarm. Instead of a MetricName and | ||
| // statistic there is an array of Metrics where the first element is the | ||
| // math expression. | ||
|
|
||
| // first build a dictionary mapping metric ids to metric name and statistic | ||
| var sourceMetrics = {}; | ||
| for (const metric of trigger.Metrics.slice(1)) { | ||
|
||
| sourceMetrics[metric.Id] = metric.MetricStat.Stat + ":" | ||
| + metric.MetricStat.Metric.MetricName; | ||
| } | ||
|
|
||
| // now replace each instance of the metric id in the alarm expression | ||
| alarmExpression = trigger.Metrics[0].Expression; | ||
| for (var metricid in sourceMetrics) { | ||
|
||
| alarmExpression = alarmExpression.replace(new RegExp(metricid,"g"),sourceMetrics[metricid]); | ||
|
||
| } | ||
| } else { | ||
| // This is a standard CloudWatch alarm on a single metric | ||
| alarmExpression = trigger.Statistic + ":" + trigger.MetricName; | ||
| } | ||
| var oldState = message.OldStateValue; | ||
| var newState = message.NewStateValue; | ||
| var alarmDescription = message.AlarmDescription; | ||
| var alarmReason = message.NewStateReason; | ||
| var trigger = message.Trigger; | ||
| var color = "warning"; | ||
|
|
||
| if (message.NewStateValue === "ALARM") { | ||
|
|
@@ -257,8 +278,7 @@ var handleCloudWatch = function(event, context) { | |
| { "title": "Alarm Description", "value": alarmDescription, "short": false}, | ||
| { | ||
| "title": "Trigger", | ||
| "value": trigger.Statistic + " " | ||
| + metricName + " " | ||
| "value": alarmExpression + " " | ||
| + trigger.ComparisonOperator + " " | ||
| + trigger.Threshold + " for " | ||
| + trigger.EvaluationPeriods + " period(s) of " | ||
|
|
@@ -364,7 +384,7 @@ var processEvent = function(event, context) { | |
| try { | ||
| eventSnsMessage = JSON.parse(eventSnsMessageRaw); | ||
| } | ||
| catch (e) { | ||
| catch (e) { | ||
| } | ||
|
|
||
| if(eventSubscriptionArn.indexOf(config.services.codepipeline.match_text) > -1 || eventSnsSubject.indexOf(config.services.codepipeline.match_text) > -1 || eventSnsMessageRaw.indexOf(config.services.codepipeline.match_text) > -1){ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "Records": [ | ||
| { | ||
| "EventSource": "aws:sns", | ||
| "EventVersion": "1.0", | ||
| "EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789123:cloudwatch-alarms:00000000-0000-0000-0000-000000000000", | ||
| "Sns": { | ||
| "Type": "Notification", | ||
| "MessageId": "00000000-0000-0000-0000-000000000000", | ||
| "TopicArn": "arn:aws:sns:us-east-1:123456789123:cloudwatch-alarms", | ||
| "Subject": "ALARM: \"Sample Metric Math Alert\" in US East (N. Virginia)", | ||
| "Message": "{\"AlarmName\":\"Sample Metric Math Alert\",\"AlarmDescription\":null,\"AWSAccountId\":\"946184294613\",\"NewStateValue\":\"ALARM\",\"NewStateReason\":\"Threshold Crossed: 1 out of the last 1 datapoints [8133.333333333447 (07/08/19 18:43:00)] was greater than the threshold (1000.0) (minimum 1 datapoint for OK -> ALARM transition).\",\"StateChangeTime\":\"2019-08-07T18:45:24.269+0000\",\"Region\":\"US East (N. Virginia)\",\"OldStateValue\":\"OK\",\"Trigger\":{\"Period\":60,\"EvaluationPeriods\":1,\"ComparisonOperator\":\"GreaterThanThreshold\",\"Threshold\":1000.0,\"TreatMissingData\":\"- TreatMissingData: missing\",\"EvaluateLowSampleCountPercentile\":\"\",\"Metrics\":[{\"Expression\":\"m1*100*m2 - MIN(m1)\",\"Id\":\"e2\",\"Label\":\"Expression2\",\"ReturnData\":true},{\"Id\":\"m1\",\"MetricStat\":{\"Metric\":{\"Dimensions\":[{\"value\":\"i-0a5c8ea70646e7b05\",\"name\":\"InstanceId\"}],\"MetricName\":\"CPUUtilization\",\"Namespace\":\"AWS/EC2\"},\"Period\":60,\"Stat\":\"Average\"},\"ReturnData\":false},{\"Id\":\"m2\",\"MetricStat\":{\"Metric\":{\"Dimensions\":[{\"value\":\"i-0a5c8ea70646e7b05\",\"name\":\"InstanceId\"}],\"MetricName\":\"NetworkOut\",\"Namespace\":\"AWS/EC2\"},\"Period\":60,\"Stat\":\"Average\"},\"ReturnData\":false}]}}", | ||
| "Timestamp": "2019-08-07T18:45:24.355Z", | ||
| "MessageAttributes": {} | ||
| } | ||
| } | ||
| ] | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
trigger.MetricNameandtrigger.Metricsare mutually exclusive, it may improve readability to say "If this trigger has multiple metrics, use them" instead of "If this trigger doesn't have a single metric, use multiple."if (typeof trigger.Metrics === 'object')will determine iftrigger.Metricsis an array.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's also
Array.isArray(trigger.Metrics)instead of checkingtypeof. While it is a part of the ES5 spec, older browser has spotty implementations. NodeJS should not have a problem with it. If you find that ES6 is acceptable for this file,Array.isArrayshould be acceptable as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to if (typeof trigger.Metrics === 'object')