Skip to content

Commit

Permalink
feat(alarm): Make AlarmNamingStrategy customizable (#316)
Browse files Browse the repository at this point in the history
Making the AlarmNamingStrategy configurable. My team wants to add the disambiguator to the alarm dedupe string. This requires custom logic for generating the deduplication string, which is why we want to be able to override this class's functionality.

_By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
  • Loading branch information
Mitchell-Cook authored Jan 27, 2023
1 parent cb2bca9 commit 9002122
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 16 deletions.
83 changes: 82 additions & 1 deletion API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 16 additions & 6 deletions lib/common/alarm/AlarmFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
IAlarmAnnotationStrategy,
} from "./IAlarmAnnotationStrategy";
import { IAlarmDedupeStringProcessor } from "./IAlarmDedupeStringProcessor";
import { IAlarmNamingStrategy } from "./IAlarmNamingStrategy";
import { noopAction } from "./NoopAlarmActionStrategy";
import {
MetricFactoryDefaults,
Expand Down Expand Up @@ -403,6 +404,13 @@ export interface AlarmFactoryDefaults {
*/
readonly dedupeStringProcessor?: IAlarmDedupeStringProcessor;

/**
* Custom strategy to name alarms
*
* @default - default behaviour (no change)
*/
readonly alarmNamingStrategy?: IAlarmNamingStrategy;

/**
* Number of breaches required to transition into an ALARM state.
*
Expand Down Expand Up @@ -460,17 +468,19 @@ export class AlarmFactory {
protected readonly alarmScope: Construct;
protected readonly globalAlarmDefaults: AlarmFactoryDefaults;
protected readonly globalMetricDefaults: MetricFactoryDefaults;
protected readonly alarmNamingStrategy: AlarmNamingStrategy;
protected readonly alarmNamingStrategy: IAlarmNamingStrategy;

constructor(alarmScope: Construct, props: AlarmFactoryProps) {
this.alarmScope = alarmScope;
this.globalAlarmDefaults = props.globalAlarmDefaults;
this.globalMetricDefaults = props.globalMetricDefaults;
this.alarmNamingStrategy = new AlarmNamingStrategy(
props.globalAlarmDefaults.alarmNamePrefix,
props.localAlarmNamePrefix,
props.globalAlarmDefaults.dedupeStringProcessor
);
this.alarmNamingStrategy =
props.globalAlarmDefaults.alarmNamingStrategy ??
new AlarmNamingStrategy(
props.globalAlarmDefaults.alarmNamePrefix,
props.localAlarmNamePrefix,
props.globalAlarmDefaults.dedupeStringProcessor
);
}

addAlarm(
Expand Down
11 changes: 2 additions & 9 deletions lib/common/alarm/AlarmNamingStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@ import {
DoNotModifyDedupeString,
IAlarmDedupeStringProcessor,
} from "./IAlarmDedupeStringProcessor";
import { AlarmNamingInput, IAlarmNamingStrategy } from "./IAlarmNamingStrategy";

const AlarmNamePartSeparator = "-";
const AlarmLabelPartSeparator = " ";

export interface AlarmNamingInput {
readonly alarmNameSuffix: string;
readonly alarmNameOverride?: string;
readonly alarmDedupeStringSuffix?: string;
readonly dedupeStringOverride?: string;
readonly disambiguator?: string;
}

export class AlarmNamingStrategy {
export class AlarmNamingStrategy implements IAlarmNamingStrategy {
protected readonly globalPrefix: string;
protected readonly localPrefix: string;
protected readonly dedupeStringStrategy: IAlarmDedupeStringProcessor;
Expand Down
31 changes: 31 additions & 0 deletions lib/common/alarm/IAlarmNamingStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export interface AlarmNamingInput {
readonly alarmNameSuffix: string;
readonly alarmNameOverride?: string;
readonly alarmDedupeStringSuffix?: string;
readonly dedupeStringOverride?: string;
readonly disambiguator?: string;
}

/**
* Strategy used to name alarms, their widgets, and their dedupe strings.
*/
export interface IAlarmNamingStrategy {
/**
* How to generate the name of an alarm.
*
* @param props AlarmNamingInput
*/
getName(props: AlarmNamingInput): string;

/**
* How to generate the label for the alarm displayed on a widget.
*
* @param props AlarmNamingInput
*/
getWidgetLabel(props: AlarmNamingInput): string;

/**
* How to generate the deduplication string for an alarm.
*/
getDedupeString(props: AlarmNamingInput): string | undefined;
}
1 change: 1 addition & 0 deletions lib/common/alarm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./CustomAlarmThreshold";
export * from "./IAlarmActionStrategy";
export * from "./IAlarmAnnotationStrategy";
export * from "./IAlarmDedupeStringProcessor";
export * from "./IAlarmNamingStrategy";
export * from "./MultipleAlarmActionStrategy";
export * from "./NoopAlarmActionStrategy";
export * from "./OpsItemAlarmActionStrategy";
Expand Down
32 changes: 32 additions & 0 deletions test/common/alarm/AlarmFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import {
AddAlarmProps,
AlarmFactory,
AlarmFactoryDefaults,
AlarmNamingInput,
CompositeAlarmOperator,
IAlarmNamingStrategy,
MetricFactoryDefaults,
multipleActions,
noopAction,
Expand Down Expand Up @@ -409,3 +411,33 @@ test("addAlarm: disambigatorAction takes precedence over default action", () =>

expect(alarm.action).toStrictEqual(snsAction);
});

test("addAlarm: custom alarm naming strategy", () => {
const alarmName = "alarmName";
const alarmLabel = "alarmLabel";
const alarmDedupe = "alarmDedupe";
const disambiguator = "Critical";
const stack = new Stack();
const customNamingStrategy: IAlarmNamingStrategy = {
getName: (props: AlarmNamingInput) => `${alarmName}-${props.disambiguator}`,
getWidgetLabel: (props: AlarmNamingInput) =>
`${alarmLabel}-${props.disambiguator}`,
getDedupeString: (props: AlarmNamingInput) =>
`${alarmDedupe}-${props.disambiguator}`,
};
const factory = new AlarmFactory(stack, {
globalMetricDefaults,
globalAlarmDefaults: {
...globalAlarmDefaultsWithDisambiguator,
alarmNamingStrategy: customNamingStrategy,
},
localAlarmNamePrefix: "prefix",
});
const alarm = factory.addAlarm(metric, {
...props,
disambiguator,
});
expect(alarm.alarmName).toBe(`${alarmName}-${disambiguator}`);
expect(alarm.alarmLabel).toBe(`${alarmLabel}-${disambiguator}`);
expect(alarm.dedupeString).toBe(`${alarmDedupe}-${disambiguator}`);
});

0 comments on commit 9002122

Please sign in to comment.