-
Notifications
You must be signed in to change notification settings - Fork 83
Description
Version
ALL
Steps and/or minimal code example to reproduce
I noticed that treatMissingData is silently ignored when (incorrectly) creating custom monitoring, smth like this:
const snsMetricGroup = {
metrics: [
{
metric: snsMetric,
addAlarm: {
Warning: {
...
treatMissingData: TreatMissingData.NOT_BREACHING,
comparisonOperator: ComparisonOperator.LESS_THAN_THRESHOLD,
}
}
}
],
};
this.monitoring.monitorCustom({
...
metricGroups: [snsMetricGroup]
});
The interesting part is that comparisonOperator is not silently ignored.
This happens because:
- The object we create for the
Warningrecord is of typeCustomThreshold. CustomThresholdis an interface that defines thecomparisonOperatorfield and extends another interfaceCustomAlarmThreshold, see here.CustomAlarmThresholddefines two fields:comparisonOperatorOverrideandtreatMissingDataOverride.
Now if we look at the implementation of CustomAlarmFactory::addCustomAlarm() which is being called by monitorCustom() from my code snippet, we see this non-uniform behavior:
cdk-monitoring-constructs/lib/common/monitoring/alarms/CustomAlarmFactory.ts
Lines 29 to 42 in a9d1dfc
| return this.alarmFactory.addAlarm(metric, { | |
| ...props, | |
| disambiguator, | |
| treatMissingData: | |
| props.treatMissingDataOverride ?? TreatMissingData.MISSING, | |
| threshold: props.threshold, | |
| comparisonOperator: | |
| props.comparisonOperatorOverride ?? props.comparisonOperator, | |
| alarmDedupeStringSuffix: props.dedupeString, | |
| alarmNameSuffix, | |
| alarmDescription: | |
| props.additionalDescription ?? | |
| `Threshold of ${props.threshold} has been breached.`, | |
| }); |
- Notice how
comparisonOperatorcan be any ofprops.comparisonOperatorOverrideandprops.comparisonOperatordefined by the user. - Notice how
treatMissingDatacan only be overriden byprops.treatMissingDataOverride, otherwise it defaults to the constantMISSING.
This is not a bug per se, but this is something that is easy to get wrong. It would make sense for developers (for readability) to allow only two options:
- both
comparisonOperatorandtreatMissingData - both
comparisonOperatorOverrideandtreatMissingDataOverride - but not a mix of
comparisonOperatorandtreatMissingDataOverride
Looking at git blame, I see that this inconsistency was introduced when this public GitHub repo was created: 8d7ce03. So this happens in all versions of CDK Monitoring Constructs.
Expected behavior
I would argue that the easiest fix would be to add the treatMissingData to CustomThreshold interface, and modify the CustomAlarmFactory::addCustomAlarm() implementation to fall back to this new field if no treatMissingDataOverride is set.
Actual behavior
Silent bug in our alarms, as discussed above (the alarms used TreatMissingData.MISSING instead of the requested TreatMissingData.NOT_BREACHING).
Other details
Slightly related issue: #669