mirror of
https://github.com/Unleash/unleash.git
synced 2025-10-27 11:02:16 +01:00
feat: fake impact metrics with histograms
This commit is contained in:
parent
a902c7d866
commit
b830ddb3bb
86
src/test/fixtures/fake-impact-metrics.ts
vendored
86
src/test/fixtures/fake-impact-metrics.ts
vendored
@ -1,34 +1,68 @@
|
|||||||
export const fakeImpactMetricsResolver = () => ({
|
import type { IImpactMetricsResolver } from '../../lib/types/index.js';
|
||||||
counters: new Map<string, { value: number; help: string }>(),
|
|
||||||
gauges: new Map<string, { value: number; help: string }>(),
|
|
||||||
|
|
||||||
defineCounter(name: string, help: string) {
|
export const fakeImpactMetricsResolver = () => {
|
||||||
this.counters.set(name, { value: 0, help });
|
const counters = new Map<string, { value: number; help: string }>();
|
||||||
},
|
const gauges = new Map<string, { value: number; help: string }>();
|
||||||
|
const histograms = new Map<
|
||||||
|
string,
|
||||||
|
{ count: number; sum: number; help: string; buckets: number[] }
|
||||||
|
>();
|
||||||
|
|
||||||
defineGauge(name: string, help: string) {
|
const resolver: IImpactMetricsResolver = {
|
||||||
this.gauges.set(name, { value: 0, help });
|
defineCounter(name: string, help: string) {
|
||||||
},
|
counters.set(name, { value: 0, help });
|
||||||
|
},
|
||||||
|
|
||||||
incrementCounter(name: string, value: number = 1) {
|
defineGauge(name: string, help: string) {
|
||||||
const counter = this.counters.get(name);
|
gauges.set(name, { value: 0, help });
|
||||||
|
},
|
||||||
|
|
||||||
if (!counter) {
|
defineHistogram(name: string, help: string, buckets?: number[]) {
|
||||||
return;
|
const defaultBuckets = buckets || [
|
||||||
}
|
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10,
|
||||||
|
];
|
||||||
|
histograms.set(name, {
|
||||||
|
count: 0,
|
||||||
|
sum: 0,
|
||||||
|
help,
|
||||||
|
buckets: defaultBuckets,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
counter.value += value;
|
incrementCounter(name: string, value: number = 1) {
|
||||||
this.counters.set(name, counter);
|
const counter = counters.get(name);
|
||||||
},
|
|
||||||
|
|
||||||
updateGauge(name: string, value: number) {
|
if (!counter) {
|
||||||
const gauge = this.gauges.get(name);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!gauge) {
|
counter.value += value;
|
||||||
return;
|
counters.set(name, counter);
|
||||||
}
|
},
|
||||||
|
|
||||||
gauge.value = value;
|
updateGauge(name: string, value: number) {
|
||||||
this.gauges.set(name, gauge);
|
const gauge = gauges.get(name);
|
||||||
},
|
|
||||||
});
|
if (!gauge) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
gauge.value = value;
|
||||||
|
gauges.set(name, gauge);
|
||||||
|
},
|
||||||
|
|
||||||
|
observeHistogram(name: string, value: number) {
|
||||||
|
const histogram = histograms.get(name);
|
||||||
|
|
||||||
|
if (!histogram) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
histogram.count++;
|
||||||
|
histogram.sum += value;
|
||||||
|
histograms.set(name, histogram);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
return { ...resolver, counters, gauges, histograms };
|
||||||
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user