-
Notifications
You must be signed in to change notification settings - Fork 1
/
MetricsGenerator.ts
47 lines (41 loc) · 1.35 KB
/
MetricsGenerator.ts
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
import type { BaseType, LabelList, MetricsConfiguration } from "./types"
import { metrics } from "./MetricsModel"
function object2label(obj: LabelList) {
return Object.entries(obj)
.map(([key, val]) => `${key}="${val}"`)
.join(", ")
}
function createMetric(
config: MetricsConfiguration,
value: string | number,
labels: Record<string, string | number> = {}
) {
const labelInfo = object2label(labels)
const entry = labelInfo ? config.name + "{" + labelInfo + "}" : config.name
return (
`# HELP ${config.name} ${config.description}\n` +
`# TYPE ${config.name} ${config.type}\n` +
`${entry} ${value}\n\n`
)
}
function getLabels(config: MetricsConfiguration) {
return Object.fromEntries(
Object.entries(config.labels || {}).map(([key, path]) => [
key,
metrics[config.topic][path as string],
])
) as LabelList
}
function getValue(config: MetricsConfiguration) {
return metrics[config.topic][config.path as string] as BaseType
}
export default function getMetrics(config: MetricsConfiguration[]) {
config
.map((config) => config.topic)
.filter((topic) => !metrics[topic])
.forEach((topic) => console.warn(`path '${topic}' not found in metrics`))
return config
.filter((config) => metrics[config.topic])
.map((config) => createMetric(config, getValue(config), getLabels(config)))
.join("")
}