Open alextriaca opened 4 years ago
Hello, I'm working on a fix for this now, a PR should be open shortly.
Absolute legend! Thanks @rrhodes 😄
This will be my first contribution to CDK, so bear with me, I suspect regular contributors will have valuable feedback for me. :)
In the meantime, If you really need this feature you can configure it using escape hatch:
const anomalyDetectionExp = new cloudwatch.MathExpression({
expression: 'ANOMALY_DETECTION_BAND(m1,2)',
usingMetrics: {
m1: metricA,
},
label: 'Anomaly detection',
period: cdk.Duration.minutes(1),
});
const alarm = anomalyDetectionExp.createAlarm(this, 'MyAlarm', {
evaluationPeriods: 1,
threshold: 5, // dummy value will be removed below
comparisonOperator: ComparisonOperator.LESS_THAN_LOWER_OR_GREATER_THAN_UPPER_THRESHOLD,
});
const cfnAlarm = alarm.node.defaultChild as CfnAlarm;
cfnAlarm.addPropertyDeletionOverride('Threshold');
(alarm.node.defaultChild as CfnAlarm).thresholdMetricId = 'expr_1'; // The id will always be 'expr_1' if there is one expression
An L2 solution for this problem remains outstanding. Initial work to embed support for threshold metric IDs into the existing Alarm
and Metric
functionality left the code in a less than desirable state for maintenance going forward, notably because any metric using thresholdMetricId
cannot possess a threshold
property. The PR for that approach is now closed but remains available for future reference.
@NetaNir suggested the following, which I agree would be the best approach at this time:
Since there are several such subtleties in the anomaly detection alarm (e.g the allowed operators, annotation, returnData), it probably makes more sense to implement a createAnomalyDetectionAlarm.
any update so far?
Is this still both p1 and in progress? Sounds like the preferred approach is a factory function that produces an Alarm
with thresholdMetricId
populated and other invariants respected? Is there any other guidance for a contributor?
👋 A status update on this would be highly appreciated!
Hi, I see that a patch is in progress, any update on it ?
Got the exact same problem with this code :
const alarm = new cloudwatch.Alarm(this, 'Alarm', {
alarmName: 'Alarm',
actionsEnabled: true,
metric: new Metric({
namespace: 'AWS/ApiGateway',
metricName: 'Count',
dimensions: {
'ApiName': 'The API',
'Resource': '/an/endpoint',
'Stage': 'prod',
'Method': 'GET'
},
statistic: 'Sum'
}),
comparisonOperator: cloudwatch.ComparisonOperator.LESS_THAN_LOWER_THRESHOLD,
threshold: 1,
evaluationPeriods: 1,
datapointsToAlarm: 1,
treatMissingData: cloudwatch.TreatMissingData.BREACHING
});
Is there any hope for this?
I'm willing to contribute this but would like to have context of what's left to align on so we can have few back and forwards on the PR. Recent changes to the rendering logic made on the crossAccount and crossRegion do the workaround mentioned above impossible now.
Is this being worked upon?
Any update on this?
Run into this issue trying to use .monitorBilling() from cdk-monitoring-constructs
module. Any updates?
To give a cocurrently working example using escape hatch:
export function addLogAlarm(logGroup: LogGroup, metricNamespace: string, alarmName: string, alarmSnsTopic: ITopic, filterPattern = '%ERROR|WARNING%') {
const metricFilter = logGroup.addMetricFilter(`${alarmName}-metric-filter`, {
filterPattern: FilterPattern.literal(filterPattern),
metricName: `${alarmName}-metric`,
metricNamespace,
defaultValue: 0,
unit: Unit.COUNT,
});
const anomalyExpression = new MathExpression({
expression: 'ANOMALY_DETECTION_BAND(m1,2)',
usingMetrics: {
m1: metricFilter.metric({statistic: "sum"}),
},
period: Duration.minutes(1),
});
const logsAlarm = new Alarm(logGroup.stack, alarmName, {
alarmName,
metric: anomalyExpression,
evaluationPeriods: 10,
datapointsToAlarm: 1,
threshold: 5, // dummy value will be removed below
comparisonOperator: ComparisonOperator.GREATER_THAN_UPPER_THRESHOLD,
});
const cfnAlarm = logsAlarm.node.defaultChild as CfnAlarm;
cfnAlarm.addPropertyDeletionOverride('Threshold');
(logsAlarm.node.defaultChild as CfnAlarm).thresholdMetricId = 'expr_1'; // The id will always be 'expr_1' if there is one expression
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const metrics = cfnAlarm.metrics as Record<string, any>[];
metrics[0].returnData = true;
metrics[1].returnData = true;
addAlarmActions(alarmSnsTopic, logsAlarm);
}
This also gets triggered by threshold: 0
When creating Cloudwatch Alarm that makes use of anomaly detection the following error is raised at deploy time:
Reproduction Steps
The Alarm construct works great for any of the other ComparisonOperators that do not rely on anomaly detection i.e.
GreaterThanThreshold
.Environment
This is :bug: Bug Report