cdklabs / cdk-monitoring-constructs

Easy-to-use CDK constructs for monitoring your AWS infrastructure
https://constructs.dev/packages/cdk-monitoring-constructs
Apache License 2.0
480 stars 70 forks source link

"Threshold metric expression must use ANOMALY_DETECTION_BAND function" when creating anomaly detection alarm on math expression #340

Closed Lykr closed 1 year ago

Lykr commented 1 year ago

Version

v3.0.0

Steps and/or minimal code example to reproduce

  1. Invoke .monitorCustom() function to monitor a custom metric.
  2. Pass a math expression as a metric in metricGroup.
  3. Set addAlarmOnAnomaly props for it.
  4. Deploy the monitoring stack.
  5. Failed to deploy with following error:

    Threshold metric expression must use ANOMALY_DETECTION_BAND function.

Expected behavior

Deploy successfully.

Actual behavior

Failed to deploy.

Other details

No response

ntvy95 commented 1 year ago

I ran into the same error on v5.0.0.

ntvy95 commented 1 year ago

The root cause seems to be because of this code line: https://github.com/cdklabs/cdk-monitoring-constructs/blob/0d79e94c89dba85e5532a355717c199000505fec/lib/common/metric/AnomalyDetectionMathExpression.ts#L42.

All math expresion will have ReturnData to be true, while the CDK only expect one of them to be true and the others should be false per this doc: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudwatch-alarm-metricdataquery.html#cfn-cloudwatch-alarm-metricdataquery-returndata.

Laxenade commented 1 year ago

I just logged a similar bug https://github.com/cdklabs/cdk-monitoring-constructs/issues/425. Should have checked issues before cutting one but anyway.

I don't think ReturnData is the problem here, I was able to create an anomaly detection on a normal metric, despite ReturnData is true on both the expression and the metric, CFN and CW didn't complain. Though, I agree that when there are multiple expressions, only one should be marked as true.

"Properties": {
   "Metrics": [
      {
         "Expression": "ANOMALY_DETECTION_BAND(alarm_a20137860f588,3)",
         "Id": "expr_1",
         "Label": "Band (stdev 3)",
         "ReturnData": true
      },
      {
         "Id": "alarm_a20137860f588",
         "ReturnData": true,
          ...Other props omitted
      }
   ],
   "ThresholdMetricId": "expr_1",
   "TreatMissingData": "breaching"
}