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 = () => ({
|
||||
counters: new Map<string, { value: number; help: string }>(),
|
||||
gauges: new Map<string, { value: number; help: string }>(),
|
||||
import type { IImpactMetricsResolver } from '../../lib/types/index.js';
|
||||
|
||||
defineCounter(name: string, help: string) {
|
||||
this.counters.set(name, { value: 0, help });
|
||||
},
|
||||
export const fakeImpactMetricsResolver = () => {
|
||||
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) {
|
||||
this.gauges.set(name, { value: 0, help });
|
||||
},
|
||||
const resolver: IImpactMetricsResolver = {
|
||||
defineCounter(name: string, help: string) {
|
||||
counters.set(name, { value: 0, help });
|
||||
},
|
||||
|
||||
incrementCounter(name: string, value: number = 1) {
|
||||
const counter = this.counters.get(name);
|
||||
defineGauge(name: string, help: string) {
|
||||
gauges.set(name, { value: 0, help });
|
||||
},
|
||||
|
||||
if (!counter) {
|
||||
return;
|
||||
}
|
||||
defineHistogram(name: string, help: string, buckets?: number[]) {
|
||||
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;
|
||||
this.counters.set(name, counter);
|
||||
},
|
||||
incrementCounter(name: string, value: number = 1) {
|
||||
const counter = counters.get(name);
|
||||
|
||||
updateGauge(name: string, value: number) {
|
||||
const gauge = this.gauges.get(name);
|
||||
if (!counter) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!gauge) {
|
||||
return;
|
||||
}
|
||||
counter.value += value;
|
||||
counters.set(name, counter);
|
||||
},
|
||||
|
||||
gauge.value = value;
|
||||
this.gauges.set(name, gauge);
|
||||
},
|
||||
});
|
||||
updateGauge(name: string, value: number) {
|
||||
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