-
Notifications
You must be signed in to change notification settings - Fork 2
/
Metrics.ts
41 lines (33 loc) · 1.1 KB
/
Metrics.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
// Type definition for metrics scraping
export type Definition = {
name: string;
help?: string|undefined;
type?: "gauge"|"untyped"|"counter"|undefined;
value?: number;
labels?: {[id:string]: string};
};
export function format(metrics: Definition[]) {
let ret: string[] = [];
for(const metric of metrics) {
if (metric.help) {
ret.push(`# HELP ${metric.name} ${metric.help}\n`);
}
if (metric.type) {
ret.push(`# TYPE ${metric.name} ${metric.type}\n`);
}
if (Object.prototype.hasOwnProperty.call(metric, 'value')) {
let v = metric.value;
if (v === undefined) {
v = NaN;
}
let labelObj = metric.labels || {};
let labels:string[] = [];
for(const key of Object.keys(labelObj)) {
labels.push(`${key}=${JSON.stringify(labelObj[key])}`);
}
const labelStr = labels.length ? `{${labels.join(',')}}` : '';
ret.push(`${metric.name}${labelStr} ${v}\n`);
}
}
return ret.join('');
}