-
Notifications
You must be signed in to change notification settings - Fork 0
/
prom-client-serializer.js
69 lines (65 loc) · 1.87 KB
/
prom-client-serializer.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { Histogram } from "../indexer-metrics-collector/prometheus.js";
/**
* @typedef SerializedHistogram
* @property {"histogram"} type
* @property {string} name
* @property {string} help
* @property {string[]} labelNames
* @property {number[]} buckets
* @property {string} aggregator
* @property {Record<string,{ labels: Record<string,string>, bucketValues: Record<string,number>, sum: number, count: number }>} hashToBuckets
*/
/**
* Object to encode/decde a prom-client Histogram to/from an object that can be JSON-stringified.
* One use case for this is to encode the Histogram into something that can be stored in a k/v store
*/
export class HistogramSerializer {
/**
* @param {Histogram<string>} metric
* @returns {Promise<SerializedHistogram>}
*/
static async serialize(metric) {
const got = await metric.get();
const labelNames = metric.labelNames;
const buckets = metric.buckets;
const hashToBuckets = metric.hashMap;
const { name, help, type, aggregator } = got;
/** @type {SerializedHistogram} */
const serialized = {
labelNames,
buckets,
name,
help,
type,
aggregator,
hashToBuckets,
};
return serialized;
}
/**
* @param {SerializedHistogram} serialized
* @param {Array<import('prom-client').Registry>} registers
* @returns {Histogram<string>}
*/
static deserialize(serialized, registers) {
const histogram = new Histogram({
name: serialized.name,
help: serialized.help,
buckets: serialized.buckets,
registers,
});
histogram.hashMap = serialized.hashToBuckets;
return histogram;
}
}
/**
* @param {Histogram<string>} histogram
* @returns {number}
*/
export function countHistogramEntries(histogram) {
let total = 0;
for (const entry of Object.entries(histogram.hashMap)) {
total += entry[1].count;
}
return total;
}