Skip to content
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

fix: make anomaly detection alarm work on math expression #426

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/common/metric/AnomalyDetectionMathExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,29 @@ export class AnomalyDetectionMathExpression extends MathExpression {
createAlarm(scope: Construct, id: string, props: CreateAlarmOptions): Alarm {
const alarm = super.createAlarm(scope, id, props);

// `usingMetrics` of an anomaly detection alarm can only ever have one entry.
// Should the entry be a math expression, the math expression can have its own `usingMetrics`.
const finalExpressionId = Object.keys(this.usingMetrics)[0];

// https://github.com/aws/aws-cdk/issues/10540#issuecomment-725222564
const cfnAlarm = alarm.node.defaultChild as CfnAlarm;
cfnAlarm.addPropertyDeletionOverride("Threshold");
(cfnAlarm.metrics as CfnAlarm.MetricDataQueryProperty[]).forEach(
(metric, index) => {
if (metric.expression) {
// To create an anomaly detection alarm, returned data should be set to true on two MetricDataQueryProperty(s):
// 1. The metric or math expression that is being evaluated for anomaly detection (eg. expr_1)
// 2. The actual expression of anomaly detection (eg. ANOMALY_DETECTION_BAND(expr_1, 1))
let returnData = false;

if (metric.expression?.includes("ANOMALY_DETECTION_BAND")) {
// thresholdMetricId is the ID of the ANOMALY_DETECTION_BAND function used as the threshold for the alarm.
cfnAlarm.thresholdMetricId = metric.id;
returnData = true;
} else if (metric.id === finalExpressionId) {
returnData = true;
}
cfnAlarm.addPropertyOverride(`Metrics.${index}.ReturnData`, true);

cfnAlarm.addPropertyOverride(`Metrics.${index}.ReturnData`, returnData);
}
);

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.