Skip to content

Commit 34b4f74

Browse files
move metrics to internal folder
1 parent 9fdf330 commit 34b4f74

6 files changed

Lines changed: 457 additions & 1035 deletions

File tree

internal/helpers/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"dependencies": {
3+
"aws-embedded-metrics": "^4.2.1",
34
"pino": "^10.3.0",
45
"zod": "^4.1.11"
56
},

internal/helpers/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from "./version";
88
export { default as $Environment } from "./environment";
99
export * from "./id-ref";
1010
export * from "./logger";
11+
export * from "./metrics";

internal/helpers/src/metrics.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { MetricsLogger, Unit } from "aws-embedded-metrics";
2+
3+
export function emitForSingleSupplier(
4+
metrics: MetricsLogger,
5+
functionName: string,
6+
supplierId: string,
7+
count: number,
8+
message: string,
9+
dimensions?: Record<string, string>,
10+
) {
11+
metrics.setNamespace(process.env.AWS_LAMBDA_FUNCTION_NAME || functionName);
12+
metrics.putDimensions({
13+
...dimensions,
14+
Supplier: supplierId,
15+
});
16+
metrics.putMetric(message, count, Unit.Count);
17+
}
18+
19+
export enum MetricStatus {
20+
Success = "success",
21+
Failure = "failure",
22+
}
23+
24+
export 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: [
48+
{ Name: metric.key, Value: metric.value, Unit: metric.unit },
49+
],
50+
},
51+
],
52+
},
53+
[metric.key]: metric.value,
54+
};
55+
}

lambdas/api-handler/src/handlers/post-letters.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 type { Deps } from "../config/deps";
56
import { ApiErrorDetail } from "../contracts/errors";
67
import {
@@ -14,7 +15,6 @@ import { mapToUpdateCommands } from "../mappers/letter-mapper";
1415
import { enqueueLetterUpdateRequests } from "../services/letter-operations";
1516
import { extractCommonIds } from "../utils/common-ids";
1617
import { assertNotEmpty, requireEnvVar } from "../utils/validation";
17-
import { MetricEntry, MetricStatus, buildEMFObject } from "../utils/metrics";
1818

1919
function duplicateIdsExist(postLettersRequest: PostLettersRequest) {
2020
const ids = postLettersRequest.data.map((item) => item.id);

0 commit comments

Comments
 (0)