-
Notifications
You must be signed in to change notification settings - Fork 79
feat(alarm): add minSampleCountToEvaluateDatapoint #453
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import { | |
CompositeAlarm, | ||
HorizontalAnnotation, | ||
IAlarmRule, | ||
MathExpression, | ||
TreatMissingData, | ||
} from "aws-cdk-lib/aws-cloudwatch"; | ||
import { Construct } from "constructs"; | ||
|
@@ -186,6 +187,18 @@ export interface AddAlarmProps { | |
*/ | ||
readonly evaluateLowSampleCountPercentile?: boolean; | ||
|
||
/** | ||
* Specifies how many samples (N) of the metric is needed in a datapoint to be evaluated for alarming. | ||
* If this property is specified, your metric will be subject to MathExpression that will add an IF condition | ||
* to your metric to make sure that each datapoint is evaluated only if it has sufficient number of samples. | ||
* If the number of samples is not sufficient, the datapoint will be treated as missing data and will be evaluated | ||
* according to the treatMissingData parameter. | ||
* If specified, deprecated minMetricSamplesToAlarm has no effect. | ||
* | ||
* @default - default behaviour - no condition on sample count will be used | ||
*/ | ||
readonly minSampleCountToEvaluateDatapoint?: number; | ||
|
||
/** | ||
* Specifies how many samples (N) of the metric is needed to trigger the alarm. | ||
* If this property is specified, an artificial composite alarm is created of the following: | ||
|
@@ -195,6 +208,9 @@ export interface AddAlarmProps { | |
* </ul> | ||
* The newly created composite alarm will be returned as a result, and it will take the original alarm actions. | ||
* @default - default behaviour - no condition on sample count will be added to the alarm | ||
* @deprecated Use minSampleCountToEvaluateDatapoint instead. minMetricSamplesAlarm uses different evaluation | ||
* period for its child alarms, so it doesn't guarantee that each datapoint in the evaluation period has | ||
* sufficient number of samples | ||
*/ | ||
readonly minMetricSamplesToAlarm?: number; | ||
|
||
|
@@ -511,6 +527,9 @@ export class AlarmFactory { | |
props | ||
); | ||
|
||
// metric that will be ultimately used to create the alarm | ||
let alarmMetric: MetricWithAlarmSupport = adjustedMetric; | ||
|
||
// prepare primary alarm properties | ||
|
||
const actionsEnabled = this.determineActionsEnabled( | ||
|
@@ -549,32 +568,58 @@ export class AlarmFactory { | |
); | ||
} | ||
|
||
// apply metric math for minimum metric samples | ||
|
||
if (props.minSampleCountToEvaluateDatapoint) { | ||
if (adjustedMetric instanceof MathExpression) { | ||
throw new Error( | ||
"minSampleCountToEvaluateDatapoint is not supported for MathExpressions. " + | ||
"If you already use MathExpression, you can extend your expression to evaluate " + | ||
"the sample count using IF statement, e.g. IF(sampleCount > X, mathExpression)." | ||
); | ||
} | ||
|
||
const metricSampleCount = adjustedMetric.with({ | ||
statistic: MetricStatistic.N, | ||
label: "Sample count", | ||
}); | ||
|
||
alarmMetric = new MathExpression({ | ||
label: `${adjustedMetric}`, | ||
expression: `IF(sampleCount > ${props.minSampleCountToEvaluateDatapoint}, metric)`, | ||
usingMetrics: { | ||
metric: adjustedMetric, | ||
sampleCount: metricSampleCount, | ||
}, | ||
}); | ||
} | ||
|
||
// create primary alarm | ||
|
||
const primaryAlarm = adjustedMetric.createAlarm( | ||
this.alarmScope, | ||
const primaryAlarm = alarmMetric.createAlarm(this.alarmScope, alarmName, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adjustedMetric changed to alarmMetric; the rest was automatically reformatted by |
||
alarmName, | ||
{ | ||
alarmName, | ||
alarmDescription, | ||
threshold: props.threshold, | ||
comparisonOperator: props.comparisonOperator, | ||
treatMissingData: props.treatMissingData, | ||
// default value (undefined) means "evaluate" | ||
evaluateLowSampleCountPercentile: evaluateLowSampleCountPercentile | ||
? undefined | ||
: "ignore", | ||
datapointsToAlarm, | ||
evaluationPeriods, | ||
actionsEnabled, | ||
} | ||
); | ||
alarmDescription, | ||
threshold: props.threshold, | ||
comparisonOperator: props.comparisonOperator, | ||
treatMissingData: props.treatMissingData, | ||
// default value (undefined) means "evaluate" | ||
evaluateLowSampleCountPercentile: evaluateLowSampleCountPercentile | ||
? undefined | ||
: "ignore", | ||
datapointsToAlarm, | ||
evaluationPeriods, | ||
actionsEnabled, | ||
}); | ||
|
||
let alarm: AlarmBase = primaryAlarm; | ||
|
||
// create composite alarm for min metric samples (if defined) | ||
// deprecated in favour of minSampleCountToEvaluateDatapoint | ||
|
||
if (props.minMetricSamplesToAlarm) { | ||
if ( | ||
!props.minSampleCountToEvaluateDatapoint && | ||
props.minMetricSamplesToAlarm | ||
) { | ||
const metricSampleCount = adjustedMetric.with({ | ||
statistic: MetricStatistic.N, | ||
}); | ||
|
@@ -627,6 +672,8 @@ export class AlarmFactory { | |
datapointsToAlarm, | ||
dedupeString, | ||
minMetricSamplesToAlarm: props.minMetricSamplesToAlarm, | ||
minSampleCountToEvaluateDatapoint: | ||
props.minSampleCountToEvaluateDatapoint, | ||
fillAlarmRange: props.fillAlarmRange ?? false, | ||
overrideAnnotationColor: props.overrideAnnotationColor, | ||
overrideAnnotationLabel: props.overrideAnnotationLabel, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I wonder if it's worth abstracting that at some point anyway just for convenience?