Skip to content

Commit 1883e14

Browse files
committed
refactor(report): Simplify low 2XX alarm creation
Loop over a list of alarm configurations to keep DRY.
1 parent 139ad05 commit 1883e14

File tree

3 files changed

+40
-51
lines changed

3 files changed

+40
-51
lines changed

cdk/bin/cdk.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'source-map-support/register';
2-
import { App } from 'aws-cdk-lib';
2+
import { App, Duration } from 'aws-cdk-lib';
33
import type { NotificationProps } from '../lib/notification';
44
import { Notification } from '../lib/notification';
55
import type { RegistrationProps } from '../lib/registration';
@@ -31,8 +31,10 @@ export const registrationCodeProps: RegistrationProps = {
3131
env: { region: 'eu-west-1' },
3232
instanceMetricGranularity: '5Minute',
3333
minAsgSize: 0,
34-
low2xxIn30MinutesThreshold: 0,
35-
low2xxIn24HoursThreshold: 1,
34+
low2xxAlarmConfiguration: [
35+
{ period: Duration.minutes(30), threshold: 0 },
36+
{ period: Duration.hours(24), threshold: 1 },
37+
],
3638
cloudFormationStackName: 'mobile-notifications-registration-CODE',
3739
};
3840

@@ -44,8 +46,10 @@ export const registrationProdProps: RegistrationProps = {
4446
instanceMetricGranularity: '1Minute',
4547
maxAsgSize: 0,
4648
minAsgSize: 0,
47-
low2xxIn30MinutesThreshold: 1_000,
48-
low2xxIn24HoursThreshold: 150_000,
49+
low2xxAlarmConfiguration: [
50+
{ period: Duration.minutes(30), threshold: 1_000 },
51+
{ period: Duration.hours(24), threshold: 150_000 },
52+
],
4953
cloudFormationStackName: 'mobile-notifications-registration-PROD',
5054
};
5155

cdk/lib/__snapshots__/registration.test.ts.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ exports[`The Registration stack matches the snapshot for CODE 1`] = `
11221122
},
11231123
"Type": "AWS::ElasticLoadBalancing::LoadBalancer",
11241124
},
1125-
"Low2XXIn24Hours04FC9138": {
1125+
"Low2XXIn1dayE630C03B": {
11261126
"Properties": {
11271127
"ActionsEnabled": false,
11281128
"AlarmActions": [
@@ -1139,7 +1139,7 @@ exports[`The Registration stack matches the snapshot for CODE 1`] = `
11391139
],
11401140
},
11411141
],
1142-
"AlarmDescription": "Triggers if load balancer in CODE does not have enough 200s in a whole day. <<<Runbook|https://docs.google.com/document/d/1aJMytnPGeWH8YLpD2_66doxqyr8dPvAVonYIOG-zmOA>>>",
1142+
"AlarmDescription": "Triggers if load balancer in CODE does not have enough 200s in 1 day. <<<Runbook|https://docs.google.com/document/d/1aJMytnPGeWH8YLpD2_66doxqyr8dPvAVonYIOG-zmOA>>>",
11431143
"ComparisonOperator": "LessThanThreshold",
11441144
"Dimensions": [
11451145
{
@@ -1198,7 +1198,7 @@ exports[`The Registration stack matches the snapshot for CODE 1`] = `
11981198
},
11991199
"Type": "AWS::CloudWatch::Alarm",
12001200
},
1201-
"Low2XXIn30Minutes4275AF7F": {
1201+
"Low2XXIn30minutesE2E53BDD": {
12021202
"Properties": {
12031203
"ActionsEnabled": false,
12041204
"AlarmActions": [
@@ -1215,7 +1215,7 @@ exports[`The Registration stack matches the snapshot for CODE 1`] = `
12151215
],
12161216
},
12171217
],
1218-
"AlarmDescription": "Triggers if load balancer in CODE does not have enough 200s in half an hour. <<<Runbook|https://docs.google.com/document/d/1aJMytnPGeWH8YLpD2_66doxqyr8dPvAVonYIOG-zmOA>>>",
1218+
"AlarmDescription": "Triggers if load balancer in CODE does not have enough 200s in 30 minutes. <<<Runbook|https://docs.google.com/document/d/1aJMytnPGeWH8YLpD2_66doxqyr8dPvAVonYIOG-zmOA>>>",
12191219
"ComparisonOperator": "LessThanThreshold",
12201220
"Dimensions": [
12211221
{
@@ -3055,7 +3055,7 @@ exports[`The Registration stack matches the snapshot for PROD 1`] = `
30553055
},
30563056
"Type": "AWS::ElasticLoadBalancing::LoadBalancer",
30573057
},
3058-
"Low2XXIn24Hours04FC9138": {
3058+
"Low2XXIn1dayE630C03B": {
30593059
"Properties": {
30603060
"ActionsEnabled": false,
30613061
"AlarmActions": [
@@ -3072,7 +3072,7 @@ exports[`The Registration stack matches the snapshot for PROD 1`] = `
30723072
],
30733073
},
30743074
],
3075-
"AlarmDescription": "Triggers if load balancer in PROD does not have enough 200s in a whole day. <<<Runbook|https://docs.google.com/document/d/1aJMytnPGeWH8YLpD2_66doxqyr8dPvAVonYIOG-zmOA>>>",
3075+
"AlarmDescription": "Triggers if load balancer in PROD does not have enough 200s in 1 day. <<<Runbook|https://docs.google.com/document/d/1aJMytnPGeWH8YLpD2_66doxqyr8dPvAVonYIOG-zmOA>>>",
30763076
"ComparisonOperator": "LessThanThreshold",
30773077
"Dimensions": [
30783078
{
@@ -3131,7 +3131,7 @@ exports[`The Registration stack matches the snapshot for PROD 1`] = `
31313131
},
31323132
"Type": "AWS::CloudWatch::Alarm",
31333133
},
3134-
"Low2XXIn30Minutes4275AF7F": {
3134+
"Low2XXIn30minutesE2E53BDD": {
31353135
"Properties": {
31363136
"ActionsEnabled": false,
31373137
"AlarmActions": [
@@ -3148,7 +3148,7 @@ exports[`The Registration stack matches the snapshot for PROD 1`] = `
31483148
],
31493149
},
31503150
],
3151-
"AlarmDescription": "Triggers if load balancer in PROD does not have enough 200s in half an hour. <<<Runbook|https://docs.google.com/document/d/1aJMytnPGeWH8YLpD2_66doxqyr8dPvAVonYIOG-zmOA>>>",
3151+
"AlarmDescription": "Triggers if load balancer in PROD does not have enough 200s in 30 minutes. <<<Runbook|https://docs.google.com/document/d/1aJMytnPGeWH8YLpD2_66doxqyr8dPvAVonYIOG-zmOA>>>",
31523152
"ComparisonOperator": "LessThanThreshold",
31533153
"Dimensions": [
31543154
{

cdk/lib/registration.ts

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { AccessScope } from '@guardian/cdk/lib/constants';
44
import type { GuStackProps } from '@guardian/cdk/lib/constructs/core';
55
import { GuStack, GuStringParameter } from '@guardian/cdk/lib/constructs/core';
66
import { GuAllowPolicy } from '@guardian/cdk/lib/constructs/iam';
7-
import { type App, Duration, Tags } from 'aws-cdk-lib';
7+
import type { Duration } from 'aws-cdk-lib';
8+
import { type App, Tags } from 'aws-cdk-lib';
89
import {
910
ComparisonOperator,
1011
TreatMissingData,
@@ -26,8 +27,7 @@ export interface RegistrationProps extends GuStackProps {
2627
instanceMetricGranularity: '1Minute' | '5Minute';
2728
minAsgSize: number;
2829
maxAsgSize?: number;
29-
low2xxIn30MinutesThreshold: number;
30-
low2xxIn24HoursThreshold: number;
30+
low2xxAlarmConfiguration: Array<{ period: Duration; threshold: number }>;
3131
}
3232

3333
export class Registration extends GuStack {
@@ -51,8 +51,7 @@ export class Registration extends GuStack {
5151
instanceMetricGranularity,
5252
minAsgSize,
5353
maxAsgSize,
54-
low2xxIn30MinutesThreshold,
55-
low2xxIn24HoursThreshold,
54+
low2xxAlarmConfiguration,
5655
} = props;
5756

5857
const { account, region } = this;
@@ -134,39 +133,25 @@ export class Registration extends GuStack {
134133
);
135134
const snsAction = new SnsAction(alarmTopic);
136135

137-
const low2xxIn30Minutes = loadBalancer.metrics
138-
.httpCodeTarget(HttpCodeTarget.TARGET_2XX_COUNT, {
139-
period: Duration.minutes(30),
140-
statistic: 'Sum',
141-
})
142-
.createAlarm(this, 'Low2XXIn30Minutes', {
143-
actionsEnabled: false,
144-
alarmDescription: `Triggers if load balancer in ${stage} does not have enough 200s in half an hour. ${runbookCopy}`,
145-
comparisonOperator: ComparisonOperator.LESS_THAN_THRESHOLD,
146-
evaluationPeriods: 1,
147-
threshold: low2xxIn30MinutesThreshold,
148-
treatMissingData: TreatMissingData.BREACHING,
149-
});
150-
151-
low2xxIn30Minutes.addAlarmAction(snsAction);
152-
low2xxIn30Minutes.addOkAction(snsAction);
153-
154-
const low2xxIn24Hours = loadBalancer.metrics
155-
.httpCodeTarget(HttpCodeTarget.TARGET_2XX_COUNT, {
156-
period: Duration.hours(24),
157-
statistic: 'Sum',
158-
})
159-
.createAlarm(this, 'Low2XXIn24Hours', {
160-
actionsEnabled: false,
161-
alarmDescription: `Triggers if load balancer in ${stage} does not have enough 200s in a whole day. ${runbookCopy}`,
162-
comparisonOperator: ComparisonOperator.LESS_THAN_THRESHOLD,
163-
evaluationPeriods: 1,
164-
threshold: low2xxIn24HoursThreshold,
165-
treatMissingData: TreatMissingData.BREACHING,
166-
});
167-
168-
low2xxIn24Hours.addAlarmAction(snsAction);
169-
low2xxIn24Hours.addOkAction(snsAction);
136+
low2xxAlarmConfiguration.forEach(({ period, threshold }) => {
137+
const humanPeriod = period.toHumanString();
138+
const alarm = loadBalancer.metrics
139+
.httpCodeTarget(HttpCodeTarget.TARGET_2XX_COUNT, {
140+
period,
141+
statistic: 'Sum',
142+
})
143+
.createAlarm(this, `Low2XXIn${humanPeriod}`, {
144+
actionsEnabled: false,
145+
alarmDescription: `Triggers if load balancer in ${stage} does not have enough 200s in ${humanPeriod}. ${runbookCopy}`,
146+
comparisonOperator: ComparisonOperator.LESS_THAN_THRESHOLD,
147+
evaluationPeriods: 1,
148+
threshold,
149+
treatMissingData: TreatMissingData.BREACHING,
150+
});
151+
152+
alarm.addAlarmAction(snsAction);
153+
alarm.addOkAction(snsAction);
154+
});
170155

171156
adjustCloudformationParameters(this);
172157
}

0 commit comments

Comments
 (0)