-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.ts
More file actions
35 lines (32 loc) · 838 Bytes
/
metrics.ts
File metadata and controls
35 lines (32 loc) · 838 Bytes
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
import { Metrics } from '@aws-lambda-powertools/metrics'
const registry: Record<string, Metrics> = {}
export type AddMetricsFn = (...args: Parameters<Metrics['addMetric']>) => void
const metricsEnabled = process.env.DISABLE_METRICS !== '1'
console.debug(`[Metrics]`, metricsEnabled ? `Enabled` : `Disabled`)
/**
* Manages the instantiation of a Metrics object (unless Metrics are disabled)
*/
export const metricsForComponent = (
component: string,
namespace = 'hello-nrfcloud-backend',
): {
metrics: Metrics
track: AddMetricsFn
} => {
if (registry[component] === undefined) {
registry[component] = new Metrics({
namespace,
serviceName: component,
})
}
const metrics = registry[component]
return {
metrics,
track: (...args) => {
if (!metricsEnabled) {
return
}
metrics.addMetric(...args)
},
}
}