Skip to content

Commit 271be72

Browse files
committed
improve logging and monitoring of notification lambda
1 parent 6a64656 commit 271be72

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

ab-testing/cdk/lib/__snapshots__/notificationLambda.test.ts.snap

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,11 @@ exports[`The AB testing notification lambda stack > matches the CODE snapshot 1`
309309
},
310310
"Environment": {
311311
"Variables": {
312+
"STAGE": "CODE",
312313
"EMAIL_DOMAIN": {
313314
"Ref": "EmailIdentityAbtestingnotificationlambdaE053C648"
314315
},
315316
"STACK": "frontend",
316-
"STAGE": "CODE",
317317
"APP": "ab-testing-notification-lambda"
318318
}
319319
},
@@ -389,7 +389,7 @@ exports[`The AB testing notification lambda stack > matches the CODE snapshot 1`
389389
{
390390
"Ref": "AbTestingNotificationLambda36F153E8"
391391
},
392-
" exceeded 1% error rate"
392+
" exceeded 0% error rate"
393393
]
394394
]
395395
},
@@ -483,7 +483,7 @@ exports[`The AB testing notification lambda stack > matches the CODE snapshot 1`
483483
"Value": "CODE"
484484
}
485485
],
486-
"Threshold": 1,
486+
"Threshold": 0,
487487
"TreatMissingData": "notBreaching"
488488
}
489489
}
@@ -630,6 +630,16 @@ exports[`The AB testing notification lambda stack > matches the PROD snapshot 1`
630630
]
631631
}
632632
},
633+
"ABTestingNotificationErrors7CB687D5": {
634+
"Type": "AWS::SNS::Subscription",
635+
"Properties": {
636+
"Endpoint": "[email protected]",
637+
"Protocol": "email",
638+
"TopicArn": {
639+
"Ref": "AbTestingNotificationSnsTopicB3559144"
640+
}
641+
}
642+
},
633643
"AbTestingNotificationLambdaServiceRole529D80CF": {
634644
"Type": "AWS::IAM::Role",
635645
"Properties": {
@@ -809,11 +819,11 @@ exports[`The AB testing notification lambda stack > matches the PROD snapshot 1`
809819
},
810820
"Environment": {
811821
"Variables": {
822+
"STAGE": "PROD",
812823
"EMAIL_DOMAIN": {
813824
"Ref": "EmailIdentityAbtestingnotificationlambdaE053C648"
814825
},
815826
"STACK": "frontend",
816-
"STAGE": "PROD",
817827
"APP": "ab-testing-notification-lambda"
818828
}
819829
},
@@ -949,7 +959,7 @@ exports[`The AB testing notification lambda stack > matches the PROD snapshot 1`
949959
{
950960
"Ref": "AbTestingNotificationLambda36F153E8"
951961
},
952-
" exceeded 1% error rate"
962+
" exceeded 0% error rate"
953963
]
954964
]
955965
},
@@ -1043,7 +1053,7 @@ exports[`The AB testing notification lambda stack > matches the PROD snapshot 1`
10431053
"Value": "PROD"
10441054
}
10451055
],
1046-
"Threshold": 1,
1056+
"Threshold": 0,
10471057
"TreatMissingData": "notBreaching"
10481058
}
10491059
}

ab-testing/cdk/lib/notificationLambda.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { App } from "aws-cdk-lib";
88
import { Schedule } from "aws-cdk-lib/aws-events";
99
import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
1010
import { Runtime } from "aws-cdk-lib/aws-lambda";
11-
import { Topic } from "aws-cdk-lib/aws-sns";
11+
import { Subscription, SubscriptionProtocol, Topic } from "aws-cdk-lib/aws-sns";
1212

1313
const lambdaFunctionName = "ab-testing-notification-lambda";
1414

@@ -39,6 +39,16 @@ export class AbTestingNotificationLambda extends GuStack {
3939

4040
const snsTopic = new Topic(this, "AbTestingNotificationSnsTopic");
4141

42+
// Notify teams in PROD if this lambda errors
43+
if (props.stage === "PROD") {
44+
new Subscription(this, "ABTestingNotificationErrors", {
45+
topic: snsTopic,
46+
// TODO: Change this to [email protected] after testing for a while
47+
endpoint: "[email protected]",
48+
protocol: SubscriptionProtocol.EMAIL,
49+
});
50+
}
51+
4252
const lambda = new GuScheduledLambda(
4353
this,
4454
"AbTestingNotificationLambda",
@@ -49,10 +59,11 @@ export class AbTestingNotificationLambda extends GuStack {
4959
rules: this.stage === "PROD" ? [runDailyRule] : [],
5060
monitoringConfiguration: {
5161
snsTopicName: snsTopic.topicName,
52-
toleratedErrorPercentage: 1,
62+
toleratedErrorPercentage: 0,
5363
},
5464
runtime: Runtime.NODEJS_22_X,
5565
environment: {
66+
STAGE: props.stage,
5667
EMAIL_DOMAIN: emailIdentity.emailIdentityName,
5768
},
5869
},

ab-testing/notification-lambda/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ export const handler = async (): Promise<void> => {
66
const expiringAbTestsByOwner =
77
getExpiringAbTestsGroupedByOwner(activeABtests);
88

9+
// Check if any test owners have expiring tests
10+
// Early return if there are no results
11+
if (!Object.keys(expiringAbTestsByOwner).length) {
12+
console.log("No owners found with expiring tests");
13+
Promise.resolve();
14+
}
15+
16+
// Sending emails to owners with expiring tests
917
await Promise.all(
1018
Object.entries(expiringAbTestsByOwner).map(([owner, abTests]) =>
1119
sendEmail(owner, abTests),

ab-testing/notification-lambda/src/lib/email.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ export const sendEmail = async (
129129
Source: `AB Testing <notifications@${process.env.EMAIL_DOMAIN}>`,
130130
Destination: {
131131
ToAddresses: [recipient],
132+
CcAddresses:
133+
// Only add CC addresses in prod
134+
process.env.STAGE === "PROD"
135+
136+
: [],
132137
},
133138
Message: {
134139
Subject: {
@@ -150,10 +155,15 @@ export const sendEmail = async (
150155
)
151156
.then(() =>
152157
console.log(
153-
`Sent AB test expiry reminder email to ${recipient}`,
158+
`Sent AB test expiry reminder email to ${recipient} for tests
159+
${Object.values(expiringAbTests)
160+
.flat()
161+
.map((test) => test.name)
162+
.join(", ")}`,
154163
),
155164
);
156165
} catch (error) {
157-
console.log(`Error sending email to ${recipient}`, error);
166+
console.error(`Error sending email to ${recipient}`, error);
167+
throw error;
158168
}
159169
};

ab-testing/notification-lambda/src/run.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212
import { handler } from "./index.ts";
1313

14+
process.env.STAGE = `LOCAL`;
1415
process.env.EMAIL_DOMAIN = `abtesting.code.dev-gutools.co.uk`;
1516

16-
console.log(`Running lambda handler locally\n`);
17-
console.log(`EMAIL_DOMAIN=${process.env.EMAIL_DOMAIN}\n`);
17+
console.debug(`Running lambda handler locally\n`);
18+
console.debug(`EMAIL_DOMAIN=${process.env.EMAIL_DOMAIN}\n`);
1819

1920
void (async () => {
2021
try {

0 commit comments

Comments
 (0)