1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-24 17:51:14 +02:00

feat: infinity representation serializable

This commit is contained in:
kwasniew 2025-09-22 11:16:57 +02:00
parent 2b9c580a29
commit 4a2b4dbd49
No known key found for this signature in database
GPG Key ID: 43A7CBC24C119560
2 changed files with 37 additions and 12 deletions

View File

@ -26,7 +26,7 @@ describe('BatchHistogram', () => {
{ le: 1, count: 7 }, { le: 1, count: 7 },
{ le: 2.5, count: 9 }, { le: 2.5, count: 9 },
{ le: 5, count: 10 }, { le: 5, count: 10 },
{ le: Number.POSITIVE_INFINITY, count: 10 }, { le: '+Inf', count: 10 },
], ],
}, },
); );
@ -71,7 +71,7 @@ describe('BatchHistogram', () => {
{ le: 1, count: 4 }, { le: 1, count: 4 },
{ le: 2.5, count: 5 }, { le: 2.5, count: 5 },
{ le: 5, count: 5 }, { le: 5, count: 5 },
{ le: Number.POSITIVE_INFINITY, count: 5 }, { le: '+Inf', count: 5 },
], ],
}, },
); );
@ -87,7 +87,7 @@ describe('BatchHistogram', () => {
{ le: 1, count: 2 }, { le: 1, count: 2 },
{ le: 2.5, count: 3 }, { le: 2.5, count: 3 },
{ le: 5, count: 3 }, { le: 5, count: 3 },
{ le: Number.POSITIVE_INFINITY, count: 3 }, { le: '+Inf', count: 3 },
], ],
}, },
); );
@ -125,7 +125,7 @@ describe('BatchHistogram', () => {
sum: 1.5, sum: 1.5,
buckets: [ buckets: [
{ le: 1, count: 2 }, { le: 1, count: 2 },
{ le: Number.POSITIVE_INFINITY, count: 3 }, { le: '+Inf', count: 3 },
], ],
}, },
); );
@ -137,7 +137,7 @@ describe('BatchHistogram', () => {
sum: 3.0, sum: 3.0,
buckets: [ buckets: [
{ le: 1, count: 1 }, { le: 1, count: 1 },
{ le: Number.POSITIVE_INFINITY, count: 2 }, { le: '+Inf', count: 2 },
], ],
}, },
); );
@ -170,4 +170,27 @@ describe('BatchHistogram', () => {
/test_histogram_count{app="my_app",service="web"} 2/, /test_histogram_count{app="my_app",service="web"} 2/,
); );
}); });
test('should handle "+Inf" string from SDK serialization', async () => {
histogram.recordBatch(
{ client: 'sdk' },
{
count: 5,
sum: 12.3,
buckets: [
{ le: 1, count: 3 },
{ le: '+Inf', count: 5 }, // String instead of Infinity
],
},
);
const metrics = await registry.metrics();
expect(metrics).toMatch(/test_histogram_bucket{client="sdk",le="1"} 3/);
expect(metrics).toMatch(
/test_histogram_bucket{client="sdk",le="\+Inf"} 5/,
);
expect(metrics).toMatch(/test_histogram_sum{client="sdk"} 12\.3/);
expect(metrics).toMatch(/test_histogram_count{client="sdk"} 5/);
});
}); });

View File

@ -1,7 +1,7 @@
import type { Registry } from 'prom-client'; import type { Registry } from 'prom-client';
interface BucketData { interface BucketData {
le: number; le: number | '+Inf';
count: number; count: number;
} }
@ -22,7 +22,7 @@ export class BatchHistogram {
{ {
count: number; count: number;
sum: number; sum: number;
buckets: Map<number, number>; buckets: Map<number | '+Inf', number>;
} }
> = new Map(); > = new Map();
@ -86,15 +86,17 @@ export class BatchHistogram {
for (const [le, cumulativeCount] of Array.from( for (const [le, cumulativeCount] of Array.from(
data.buckets.entries(), data.buckets.entries(),
).sort((a, b) => a[0] - b[0])) { ).sort((a, b) => {
// Sort buckets: numbers first (ascending), then '+Inf' last
if (a[0] === '+Inf') return 1;
if (b[0] === '+Inf') return -1;
return (a[0] as number) - (b[0] as number);
})) {
values.push({ values.push({
value: cumulativeCount, value: cumulativeCount,
labels: { labels: {
...labels, ...labels,
le: le: le.toString(),
le === Number.POSITIVE_INFINITY
? '+Inf'
: le.toString(),
}, },
metricName: `${this.name}_bucket`, metricName: `${this.name}_bucket`,
}); });