Skip to content

Commit e84bf1c

Browse files
log EMF object for letter-status-update failure
1 parent a1c77ae commit e84bf1c

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

lambdas/api-handler/src/handlers/get-letters.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { mapToGetLettersResponse } from "../mappers/letter-mapper";
1414
import type { Deps } from "../config/deps";
1515
import { MetricStatus, emitForSingleSupplier } from "../utils/metrics";
1616

17-
// List letters Handlers
1817
// The endpoint should only return pending letters for now
1918
const status = "PENDING";
2019

lambdas/api-handler/src/handlers/letter-status-update.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { SQSEvent, SQSHandler } from "aws-lambda";
1+
import { SQSEvent, SQSHandler, SQSRecord } from "aws-lambda";
2+
import { Unit } from "aws-embedded-metrics";
3+
import pino from "pino";
24
import {
35
UpdateLetterCommand,
46
UpdateLetterCommandSchema,
57
} from "../contracts/letters";
68
import { Deps } from "../config/deps";
79
import { mapToUpdateLetter } from "../mappers/letter-mapper";
10+
import { buildEMFObject } from "../utils/metrics";
811

912
export default function createLetterStatusUpdateHandler(
1013
deps: Deps,
@@ -27,9 +30,21 @@ export default function createLetterStatusUpdateHandler(
2730
},
2831
"Error processing letter status update",
2932
);
33+
// create metric object
34+
emitAndFlushMetricLog(message, deps.logger);
3035
}
3136
});
3237

3338
await Promise.all(tasks);
3439
};
3540
}
41+
42+
function emitAndFlushMetricLog(message: SQSRecord, logger: pino.Logger) {
43+
const metric = {
44+
key: "statusUpdateFailed",
45+
value: 1,
46+
unit: Unit.Count,
47+
};
48+
const emf = buildEMFObject("letter-status-update", {}, metric);
49+
logger.info(emf);
50+
}

lambdas/api-handler/src/utils/metrics.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,34 @@ export enum MetricStatus {
2020
Success = "success",
2121
Failure = "failure",
2222
}
23+
24+
interface MetricEntry {
25+
key: string;
26+
value: number;
27+
unit: Unit;
28+
}
29+
30+
// build EMF object
31+
export function buildEMFObject(
32+
functionName: string,
33+
dimensions: Record<string, string>,
34+
metric: MetricEntry,
35+
) {
36+
const namespace = process.env.AWS_LAMBDA_FUNCTION_NAME || functionName;
37+
return {
38+
LogGroup: namespace,
39+
ServiceName: namespace,
40+
...dimensions,
41+
_aws: {
42+
Timestamp: Date.now(),
43+
CloudWatchMetrics: [
44+
{
45+
Namespace: namespace,
46+
Dimensions: [[...Object.keys(dimensions), "ServiceName", "LogGroup"]],
47+
Metrics: [{ Name: metric.key, Unit: metric.value }],
48+
},
49+
],
50+
},
51+
[metric.key]: metric.value,
52+
};
53+
}

0 commit comments

Comments
 (0)