Skip to content
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8e845cf
change to create a PR
Vlasis-Perdikidis Jan 20, 2026
b51d290
add my first EMF metrics to upsert letter lambda
Vlasis-Perdikidis Jan 21, 2026
6764866
correct handler function and lint errors
Vlasis-Perdikidis Jan 21, 2026
9e96b04
add cloudwatch:PutMetricData permission
Vlasis-Perdikidis Jan 21, 2026
53db977
set custom namespace
Vlasis-Perdikidis Jan 21, 2026
2f1513d
remove cloudwatch client declaration
Vlasis-Perdikidis Jan 21, 2026
c1a9bc2
remove oddOrEven from dimensions
Vlasis-Perdikidis Jan 22, 2026
9f9d1cf
add oddOrEven from dimensions
Vlasis-Perdikidis Jan 22, 2026
b2f012b
remove operationType from messageProcessed
Vlasis-Perdikidis Jan 23, 2026
32b30a1
log environment variables
Vlasis-Perdikidis Jan 23, 2026
ba37764
log letterEvent and operation
Vlasis-Perdikidis Jan 26, 2026
0233eb8
fix linting errors
Vlasis-Perdikidis Jan 26, 2026
6198984
emit metrics per supplier and with correct count
Vlasis-Perdikidis Jan 26, 2026
e965c60
correct emitMetrics function
Vlasis-Perdikidis Jan 26, 2026
038d755
add metrics for rest of Lambda functions
Vlasis-Perdikidis Jan 29, 2026
f022bf9
fix failing unit tests
Vlasis-Perdikidis Jan 30, 2026
f413b15
add extra test to check that statuses map is populated
Vlasis-Perdikidis Jan 30, 2026
a185e5f
address PR comments
Vlasis-Perdikidis Feb 3, 2026
1d7367e
fix npm vulnerabilities
Vlasis-Perdikidis Feb 3, 2026
ceff734
fix typeckeck and lint errors
Vlasis-Perdikidis Feb 3, 2026
9da31ac
remove comment
Vlasis-Perdikidis Feb 3, 2026
e46fdb7
Merge branch 'main' into feature/CCM-13610-Expose-key-metrics-to-Clou…
masl2 Feb 5, 2026
495657e
regen lock after merge
masl2 Feb 5, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,5 @@ Deployments can be made of any [release](https://github.com/NHSDigital/nhs-notif
Unless stated otherwise, the codebase is released under the MIT License. This covers both the codebase and any sample code in the documentation.

Any HTML or Markdown documentation is [© Crown Copyright](https://www.nationalarchives.gov.uk/information-management/re-using-public-sector-information/uk-government-licensing-framework/crown-copyright/) and available under the terms of the [Open Government Licence v3.0](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/).

to open a PR
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,16 @@ data "aws_iam_policy_document" "upsert_letter_lambda" {
module.sqs_letter_updates.sqs_queue_arn
]
}

statement {
sid = "AllowCloudWatchMetrics"
effect = "Allow"

actions = [
"cloudwatch:PutMetricData"
]

resources = ["*"]
}

}
2 changes: 2 additions & 0 deletions lambdas/upsert-letter/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"dependencies": {
"@aws-sdk/client-cloudwatch": "^3.972.0",
"@aws-sdk/client-dynamodb": "^3.858.0",
"@aws-sdk/lib-dynamodb": "^3.858.0",
"@internal/datastore": "*",
"@nhsdigital/nhs-notify-event-schemas-letter-rendering": "^2.0.1",
"@nhsdigital/nhs-notify-event-schemas-letter-rendering-v1": "npm:@nhsdigital/nhs-notify-event-schemas-letter-rendering@^1.1.5",
"@nhsdigital/nhs-notify-event-schemas-supplier-api": "^1.0.8",
"@types/aws-lambda": "^8.10.148",
"aws-embedded-metrics": "^4.2.1",
"aws-lambda": "^1.0.7",
"esbuild": "^0.27.2",
"pino": "^9.7.0",
Expand Down
83 changes: 54 additions & 29 deletions lambdas/upsert-letter/src/handler/upsert-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
LetterRequestPreparedEventV2,
} from "@nhsdigital/nhs-notify-event-schemas-letter-rendering";
import z from "zod";
import { Unit, metricScope } from "aws-embedded-metrics";
import { Deps } from "../config/deps";

type SupplierSpec = { supplierId: string; specId: string };
Expand Down Expand Up @@ -153,33 +154,57 @@
}

export default function createUpsertLetterHandler(deps: Deps): SQSHandler {
return async (event: SQSEvent) => {
const batchItemFailures: SQSBatchItemFailure[] = [];

const tasks = event.Records.map(async (record) => {
try {
const message: string = parseSNSNotification(record);

const snsEvent = JSON.parse(message);

const letterEvent: unknown = removeEventBridgeWrapper(snsEvent);

const type = getType(letterEvent);

const operation = getOperationFromType(type);

await runUpsert(operation, letterEvent, deps);
} catch (error) {
deps.logger.error(
{ err: error, message: record.body },
`Error processing upsert of record ${record.messageId}`,
);
batchItemFailures.push({ itemIdentifier: record.messageId });
}
});

await Promise.all(tasks);

return { batchItemFailures };
};
return metricScope((metrics) => {
return async (event: SQSEvent) => {
console.log(
"the environment variables:",
JSON.stringify(process.env, null, 2),
Comment thread Fixed
);
console.log("The SQSEvent:", event);
const batchItemFailures: SQSBatchItemFailure[] = [];
metrics.setNamespace("vlasis_upsertLetter");

const tasks = event.Records.map(async (record) => {
try {
const message: string = parseSNSNotification(record);
console.log("the message:", message);

const snsEvent = JSON.parse(message);
console.log("the snsEvent:", snsEvent);

const letterEvent: unknown = removeEventBridgeWrapper(snsEvent);

const type = getType(letterEvent);

const operation = getOperationFromType(type);
metrics.putDimensions({
FunctionName: "upsertLambda",
// eslint-disable-next-line sonarjs/pseudo-random
OddOrEven: `${Math.floor(Math.random() * 10) % 2}`,
});

await runUpsert(operation, letterEvent, deps);
metrics.setProperty("operation", operation.name);

metrics.putMetric("MessagesProcessed", 1, Unit.Count);
} catch (error) {
deps.logger.error(
{ err: error, message: record.body },
`Error processing upsert of record ${record.messageId}`,
);
metrics.putDimensions({
FunctionName: "upsertLambda",
// eslint-disable-next-line sonarjs/pseudo-random
OddOrEven: `${Math.floor(Math.random() * 10) % 2}`,
});
metrics.putMetric("MessageFailed", 1, Unit.Count);
batchItemFailures.push({ itemIdentifier: record.messageId });
}
});

await Promise.all(tasks);

return { batchItemFailures };
};
});
}
Loading
Loading