Skip to content

Commit 3bd2b37

Browse files
feat(alarm): add minSampleCountToEvaluateDatapoint to CustomMonitoring (#458)
Fixes #452 Follow up to #453: * (feat) Exposing the new `minSampleCountToEvaluateDatapoint` through CustomMonitoring * (fix) Fixing the `minSampleCountToEvaluateDatapoint` MathExpression's period as it defaults to 5 minutes. This didn't come up during testing as I tested it using 5 minute period. Apparently, if we don't set the period on MathExpression explicitly, it overrides all child metrics to 5 minute, [reference](https://github.com/aws/aws-cdk/blob/db21fefc2dc76eb4ff306fa41652ab6a6cc95e42/packages/aws-cdk-lib/aws-cloudwatch/lib/metric.ts#L606). To avoid similar situations in the future, extended the unit test to cover custom periods. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 59e7fa9 commit 3bd2b37

File tree

6 files changed

+1465
-66
lines changed

6 files changed

+1465
-66
lines changed

API.md

Lines changed: 1400 additions & 56 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/common/alarm/AlarmFactory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ export class AlarmFactory {
587587
alarmMetric = new MathExpression({
588588
label: `${adjustedMetric}`,
589589
expression: `IF(sampleCount > ${props.minSampleCountToEvaluateDatapoint}, metric)`,
590+
period: adjustedMetric.period,
590591
usingMetrics: {
591592
metric: adjustedMetric,
592593
sampleCount: metricSampleCount,

lib/common/alarm/CustomAlarmThreshold.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ export interface CustomAlarmThreshold {
3939
*/
4040
readonly alarmDescriptionOverride?: string;
4141

42+
/**
43+
* Specifies how many samples (N) of the metric is needed in a datapoint to be evaluated for alarming.
44+
* If this property is specified, your metric will be subject to MathExpression that will add an IF condition
45+
* to your metric to make sure that each datapoint is evaluated only if it has sufficient number of samples.
46+
* If the number of samples is not sufficient, the datapoint will be treated as missing data and will be evaluated
47+
* according to the treatMissingData parameter.
48+
* If specified, deprecated minMetricSamplesToAlarm has no effect.
49+
*
50+
* @default - default behaviour - no condition on sample count will be used
51+
*/
52+
readonly minSampleCountToEvaluateDatapoint?: number;
53+
4254
/**
4355
* Specifies how many samples (N) of the metric is needed to trigger the alarm.
4456
* If this property is specified, a composite alarm is created of the following:
@@ -48,6 +60,9 @@ export interface CustomAlarmThreshold {
4860
* </ul>
4961
* This composite alarm will be returned as a result and uses the specified alarm actions.
5062
* @default - default behaviour - no condition on sample count will be added to the alarm
63+
* @deprecated Use minSampleCountToEvaluateDatapoint instead. minMetricSamplesAlarm uses different evaluation
64+
* period for its child alarms, so it doesn't guarantee that each datapoint in the evaluation period has
65+
* sufficient number of samples
5166
*/
5267
readonly minMetricSamplesToAlarm?: number;
5368

test/common/alarm/AlarmFactory.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,15 @@ test("addAlarm: check created alarms when minMetricSamplesToAlarm is used", () =
285285
alarmNameSuffix: "none",
286286
comparisonOperator: ComparisonOperator.LESS_THAN_THRESHOLD,
287287
minMetricSamplesToAlarm: 42,
288+
period: Duration.minutes(15),
288289
});
289290

290291
const template = Template.fromStack(stack);
291292
template.hasResourceProperties("AWS::CloudWatch::Alarm", {
292293
AlarmName: "DummyServiceAlarms-prefix-none",
293294
MetricName: "DummyMetric1",
294295
Statistic: "Average",
296+
Period: 900,
295297
});
296298
template.hasResourceProperties("AWS::CloudWatch::Alarm", {
297299
AlarmName: "DummyServiceAlarms-prefix-none-NoSamples",
@@ -304,6 +306,7 @@ test("addAlarm: check created alarms when minMetricSamplesToAlarm is used", () =
304306
Statistic: "SampleCount",
305307
Threshold: 42,
306308
TreatMissingData: "breaching",
309+
Period: 900,
307310
});
308311

309312
const alarmRuleCapture = new Capture();
@@ -344,6 +347,7 @@ test("addAlarm: check created alarms when minSampleCountToEvaluateDatapoint is u
344347
comparisonOperator: ComparisonOperator.LESS_THAN_THRESHOLD,
345348
minSampleCountToEvaluateDatapoint: 42,
346349
minMetricSamplesToAlarm: 55, // not used if minSampleCountToEvaluateDatapoint defined
350+
period: Duration.minutes(15),
347351
});
348352

349353
const template = Template.fromStack(stack);
@@ -365,7 +369,7 @@ test("addAlarm: check created alarms when minSampleCountToEvaluateDatapoint is u
365369
Metric: Match.objectLike({
366370
MetricName: "DummyMetric1",
367371
}),
368-
Period: 300,
372+
Period: 900,
369373
Stat: "Average",
370374
},
371375
ReturnData: false,
@@ -376,7 +380,7 @@ test("addAlarm: check created alarms when minSampleCountToEvaluateDatapoint is u
376380
Metric: Match.objectLike({
377381
MetricName: "DummyMetric1",
378382
}),
379-
Period: 300,
383+
Period: 900,
380384
Stat: "SampleCount",
381385
},
382386
ReturnData: false,

test/monitoring/custom/CustomMonitoring.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ test("snapshot test", () => {
9090
Warning: {
9191
threshold: 10,
9292
comparisonOperator: ComparisonOperator.GREATER_THAN_THRESHOLD,
93+
minSampleCountToEvaluateDatapoint: 15,
9394
},
9495
Critical: {
9596
threshold: 50,

test/monitoring/custom/__snapshots__/CustomMonitoring.test.ts.snap

Lines changed: 42 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)