Skip to content

Commit c487d5d

Browse files
use metrics from internal helpers folder
1 parent 34b4f74 commit c487d5d

8 files changed

Lines changed: 21 additions & 99 deletions

File tree

lambdas/api-handler/src/handlers/get-letter-data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { APIGatewayProxyHandler } from "aws-lambda";
22
import { MetricsLogger, metricScope } from "aws-embedded-metrics";
3+
import { MetricStatus, emitForSingleSupplier } from "@internal/helpers";
34
import { assertNotEmpty } from "../utils/validation";
45
import { extractCommonIds } from "../utils/common-ids";
56
import { ApiErrorDetail } from "../contracts/errors";
67
import { processError } from "../mappers/error-mapper";
78
import ValidationError from "../errors/validation-error";
89
import { getLetterDataUrl } from "../services/letter-operations";
910
import type { Deps } from "../config/deps";
10-
import { MetricStatus, emitForSingleSupplier } from "../utils/metrics";
1111

1212
export default function createGetLetterDataHandler(
1313
deps: Deps,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { APIGatewayProxyHandler } from "aws-lambda";
22
import { MetricsLogger, metricScope } from "aws-embedded-metrics";
3+
import { MetricStatus, emitForSingleSupplier } from "@internal/helpers";
34
import { assertNotEmpty } from "../utils/validation";
45
import { extractCommonIds } from "../utils/common-ids";
56
import ValidationError from "../errors/validation-error";
@@ -8,7 +9,6 @@ import { getLetterById } from "../services/letter-operations";
89
import { processError } from "../mappers/error-mapper";
910
import { mapToGetLetterResponse } from "../mappers/letter-mapper";
1011
import { Deps } from "../config/deps";
11-
import { MetricStatus, emitForSingleSupplier } from "../utils/metrics";
1212

1313
// Get letter data
1414
export default function createGetLetterHandler(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
} from "aws-lambda";
55
import { Logger } from "pino";
66
import { MetricsLogger, metricScope } from "aws-embedded-metrics";
7+
import { MetricStatus, emitForSingleSupplier } from "@internal/helpers";
78
import { getLettersForSupplier } from "../services/letter-operations";
89
import { extractCommonIds } from "../utils/common-ids";
910
import { requireEnvVar } from "../utils/validation";
@@ -12,7 +13,6 @@ import { processError } from "../mappers/error-mapper";
1213
import ValidationError from "../errors/validation-error";
1314
import { mapToGetLettersResponse } from "../mappers/letter-mapper";
1415
import type { Deps } from "../config/deps";
15-
import { MetricStatus, emitForSingleSupplier } from "../utils/metrics";
1616

1717
// The endpoint should only return pending letters for now
1818
const status = "PENDING";

lambdas/api-handler/src/handlers/patch-letter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { APIGatewayProxyHandler } from "aws-lambda";
22
import { MetricsLogger, Unit, metricScope } from "aws-embedded-metrics";
3+
import { MetricStatus } from "@internal/helpers";
34
import { enqueueLetterUpdateRequests } from "../services/letter-operations";
45
import {
56
PatchLetterRequest,
@@ -13,7 +14,6 @@ import { assertNotEmpty } from "../utils/validation";
1314
import { extractCommonIds } from "../utils/common-ids";
1415
import { mapToUpdateCommand } from "../mappers/letter-mapper";
1516
import type { Deps } from "../config/deps";
16-
import { MetricStatus } from "../utils/metrics";
1717

1818
export default function createPatchLetterHandler(
1919
deps: Deps,

lambdas/api-handler/src/handlers/post-mi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { APIGatewayProxyHandler } from "aws-lambda";
22
import { Unit } from "aws-embedded-metrics";
33
import pino from "pino";
4+
import { MetricEntry, MetricStatus, buildEMFObject } from "@internal/helpers";
45
import postMIOperation from "../services/mi-operations";
56
import { ApiErrorDetail } from "../contracts/errors";
67
import ValidationError from "../errors/validation-error";
@@ -10,7 +11,6 @@ import { extractCommonIds } from "../utils/common-ids";
1011
import { PostMIRequest, PostMIRequestSchema } from "../contracts/mi";
1112
import { mapToMI } from "../mappers/mi-mapper";
1213
import { Deps } from "../config/deps";
13-
import { MetricEntry, MetricStatus, buildEMFObject } from "../utils/metrics";
1414

1515
export default function createPostMIHandler(
1616
deps: Deps,

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

Lines changed: 0 additions & 55 deletions
This file was deleted.

lambdas/letter-updates-transformer/src/letter-updates-transformer.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { mapLetterToCloudEvent } from "@nhsdigital/nhs-notify-event-schemas-supp
1414
import { Letter, LetterSchema } from "@internal/datastore";
1515
import { Unit } from "aws-embedded-metrics";
1616
import pino from "pino";
17+
import { MetricEntry, buildEMFObject } from "internal/helpers/src";
1718
import { Deps } from "./deps";
1819

1920
// SNS PublishBatchCommand supports up to 10 messages per batch
@@ -67,28 +68,15 @@ function populateEventTypeMap(cloudEvents: LetterEvent[]) {
6768
}
6869

6970
function emitMetrics(logger: pino.Logger, eventTypeCount: Map<string, number>) {
70-
const namespace =
71-
process.env.AWS_LAMBDA_FUNCTION_NAME || "letter-updates-transformer";
72-
71+
const namespace = "letter-updates-transformer";
7372
for (const [type, count] of eventTypeCount) {
74-
const emf = {
75-
LogGroup: namespace,
76-
ServiceName: namespace,
77-
eventType: type,
78-
_aws: {
79-
Timestamp: Date.now(),
80-
CloudWatchMetrics: [
81-
{
82-
Namespace: namespace,
83-
Dimensions: [["eventType", "ServiceName", "LogGroup"]],
84-
Metrics: [
85-
{ Name: "events published", Value: count, Unit: Unit.Count },
86-
],
87-
},
88-
],
89-
},
90-
"events published": count,
73+
const dimensions: Record<string, string> = { eventType: type };
74+
const metric: MetricEntry = {
75+
key: "Events published",
76+
value: count,
77+
unit: Unit.Count,
9178
};
79+
const emf = buildEMFObject(namespace, dimensions, metric);
9280
logger.info(emf);
9381
}
9482
}

lambdas/mi-updates-transformer/src/mi-updates-transformer.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { MISubmittedEvent } from "@nhsdigital/nhs-notify-event-schemas-supplier-api/src";
1414
import { Unit } from "aws-embedded-metrics";
1515
import pino from "pino";
16+
import { MetricEntry, buildEMFObject } from "internal/helpers/src";
1617
import { mapMIToCloudEvent } from "./mappers/mi-mapper";
1718
import { Deps } from "./deps";
1819

@@ -52,27 +53,15 @@ function extractMIData(record: DynamoDBRecord): MI {
5253
}
5354

5455
function emitMetrics(logger: pino.Logger, eventTypeCount: Map<string, number>) {
55-
const namespace =
56-
process.env.AWS_LAMBDA_FUNCTION_NAME || "mi-updates-transformer";
56+
const namespace = "mi-updates-transformer";
5757
for (const [type, count] of eventTypeCount) {
58-
const emf = {
59-
LogGroup: namespace,
60-
ServiceName: namespace,
61-
eventType: type,
62-
_aws: {
63-
Timestamp: Date.now(),
64-
CloudWatchMetrics: [
65-
{
66-
Namespace: namespace,
67-
Dimensions: [["LogGroup", "ServiceName", "eventType"]],
68-
Metrics: [
69-
{ Name: "events published", Value: count, Unit: Unit.Count },
70-
],
71-
},
72-
],
73-
},
74-
"events published": count,
58+
const dimensions: Record<string, string> = { eventType: type };
59+
const metric: MetricEntry = {
60+
key: "Events published",
61+
value: count,
62+
unit: Unit.Count,
7563
};
64+
const emf = buildEMFObject(namespace, dimensions, metric);
7665
logger.info(emf);
7766
}
7867
}

0 commit comments

Comments
 (0)