Skip to content

Commit 4083a58

Browse files
authored
feat: populate impact metrics with default values (#803)
1 parent b20061d commit 4083a58

File tree

2 files changed

+80
-6
lines changed

2 files changed

+80
-6
lines changed

src/impact-metrics/metric-types.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ class CounterImpl implements Counter {
7878

7979
this.values.clear();
8080

81+
if (samples.length === 0) {
82+
samples.push({
83+
labels: {},
84+
value: 0,
85+
});
86+
}
87+
8188
return {
8289
name: this.opts.name,
8390
help: this.opts.help,
@@ -199,6 +206,18 @@ class HistogramImpl implements Histogram {
199206

200207
this.values.clear();
201208

209+
if (samples.length === 0) {
210+
samples.push({
211+
labels: {},
212+
count: 0,
213+
sum: 0,
214+
buckets: this.buckets.map((le) => ({
215+
le: le === Infinity ? '+Inf' : le,
216+
count: 0,
217+
})),
218+
});
219+
}
220+
202221
return {
203222
name: this.opts.name,
204223
help: this.opts.help,

src/test/impact-metrics/metric-types.test.ts

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,28 @@ test('Gauge tracks values separately per label set', () => {
116116
});
117117
});
118118

119-
test('collect returns empty array when all metrics are empty', () => {
119+
test('collect returns counter with zero value when counter is empty', () => {
120120
const registry = new InMemoryMetricRegistry();
121121
registry.counter({ name: 'noop_counter', help: 'noop' });
122122
registry.gauge({ name: 'noop_gauge', help: 'noop' });
123123

124124
const result = registry.collect();
125-
expect(result).toStrictEqual([]);
125+
expect(result).toStrictEqual([
126+
{
127+
name: 'noop_counter',
128+
help: 'noop',
129+
type: 'counter',
130+
samples: [
131+
{
132+
labels: {},
133+
value: 0,
134+
},
135+
],
136+
},
137+
]);
126138
});
127139

128-
test('collect returns empty array after flushing previous values', () => {
140+
test('collect returns counter with zero value after flushing previous values', () => {
129141
const registry = new InMemoryMetricRegistry();
130142
const counter = registry.counter({ name: 'flush_test', help: 'flush' });
131143

@@ -135,7 +147,19 @@ test('collect returns empty array after flushing previous values', () => {
135147
expect(first).toHaveLength(1);
136148

137149
const second = registry.collect();
138-
expect(second).toStrictEqual([]);
150+
expect(second).toStrictEqual([
151+
{
152+
name: 'flush_test',
153+
help: 'flush',
154+
type: 'counter',
155+
samples: [
156+
{
157+
labels: {},
158+
value: 0,
159+
},
160+
],
161+
},
162+
]);
139163
});
140164

141165
test('restore reinserts collected metrics into the registry', () => {
@@ -149,7 +173,19 @@ test('restore reinserts collected metrics into the registry', () => {
149173
expect(flushed).toHaveLength(1);
150174

151175
const afterFlush = registry.collect();
152-
expect(afterFlush).toStrictEqual([]);
176+
expect(afterFlush).toStrictEqual([
177+
{
178+
name: 'restore_test',
179+
help: 'testing restore',
180+
type: 'counter',
181+
samples: [
182+
{
183+
labels: {},
184+
value: 0,
185+
},
186+
],
187+
},
188+
]);
153189

154190
registry.restore(flushed);
155191

@@ -277,7 +313,26 @@ test('Histogram restoration preserves exact data', () => {
277313
expect(firstCollect).toHaveLength(1);
278314

279315
const emptyCollect = registry.collect();
280-
expect(emptyCollect).toStrictEqual([]);
316+
expect(emptyCollect).toStrictEqual([
317+
{
318+
name: 'restore_histogram',
319+
help: 'testing histogram restore',
320+
type: 'histogram',
321+
samples: [
322+
{
323+
labels: {},
324+
count: 0,
325+
sum: 0,
326+
buckets: [
327+
{ le: 0.1, count: 0 },
328+
{ le: 1, count: 0 },
329+
{ le: 10, count: 0 },
330+
{ le: '+Inf', count: 0 },
331+
],
332+
},
333+
],
334+
},
335+
]);
281336

282337
registry.restore(firstCollect);
283338

0 commit comments

Comments
 (0)