-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmetrics.ts
More file actions
55 lines (51 loc) · 1.25 KB
/
metrics.ts
File metadata and controls
55 lines (51 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { MetricsLogger, Unit } from "aws-embedded-metrics";
export function emitForSingleSupplier(
metrics: MetricsLogger,
functionName: string,
supplierId: string,
count: number,
message: string,
dimensions?: Record<string, string>,
) {
metrics.setNamespace(process.env.AWS_LAMBDA_FUNCTION_NAME || functionName);
metrics.putDimensions({
...dimensions,
Supplier: supplierId,
});
metrics.putMetric(message, count, Unit.Count);
}
export enum MetricStatus {
Success = "success",
Failure = "failure",
}
export interface MetricEntry {
key: string;
value: number;
unit: Unit;
}
// build EMF object
export function buildEMFObject(
functionName: string,
dimensions: Record<string, string>,
metric: MetricEntry,
) {
const namespace = process.env.AWS_LAMBDA_FUNCTION_NAME || functionName;
return {
LogGroup: namespace,
ServiceName: namespace,
...dimensions,
_aws: {
Timestamp: Date.now(),
CloudWatchMetrics: [
{
Namespace: namespace,
Dimensions: [[...Object.keys(dimensions), "ServiceName", "LogGroup"]],
Metrics: [
{ Name: metric.key, Value: metric.value, Unit: metric.unit },
],
},
],
},
[metric.key]: metric.value,
};
}