Skip to content

Commit 4a800f4

Browse files
voltbitSimenB
authored andcommitted
fix: correct exemplar formating (#556)
1 parent a38aa2b commit 4a800f4

File tree

5 files changed

+97
-16
lines changed

5 files changed

+97
-16
lines changed

lib/registry.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,18 @@ class Registry {
6969
const flattenedShared = flattenSharedLabels(sharedLabels);
7070
const labelParts = [...formattedLabels, flattenedShared].filter(Boolean);
7171
const labelsString = labelParts.length ? `{${labelParts.join(',')}}` : '';
72-
values.push(
73-
`${metricName}${labelsString} ${getValueAsString(val.value)}`,
74-
);
72+
let fullMetricLine = `${metricName}${labelsString} ${getValueAsString(
73+
val.value,
74+
)}`;
7575

7676
const { exemplar } = val;
7777
if (exemplar && this.contentType === Registry.OPENMETRICS_CONTENT_TYPE) {
7878
const formattedExemplars = formatLabels(exemplar.labelSet);
79-
values.push(
80-
` # {${formattedExemplars.join(',')}} ${getValueAsString(
81-
exemplar.value,
82-
)} ${exemplar.timestamp}`,
83-
);
79+
fullMetricLine += ` # {${formattedExemplars.join(
80+
',',
81+
)}} ${getValueAsString(exemplar.value)} ${exemplar.timestamp}`;
8482
}
83+
values.push(fullMetricLine);
8584
}
8685

8786
return values.join('\n');
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Exemplars with OpenMetrics registry should make histogram with exemplars on multiple buckets 1`] = `
4+
"# HELP counter_exemplar_test help
5+
# TYPE counter_exemplar_test counter
6+
counter_exemplar_test_total{method="get",code="200"} 2 # {traceId="trace_id_test",spanId="span_id_test"} 2 1678654679
7+
# HELP histogram_exemplar_test test
8+
# TYPE histogram_exemplar_test histogram
9+
histogram_exemplar_test_bucket{le="0.005",method="get",code="200"} 0
10+
histogram_exemplar_test_bucket{le="0.01",method="get",code="200"} 1 # {traceId="trace_id_test_1",spanId="span_id_test_1"} 0.007 1678654679
11+
histogram_exemplar_test_bucket{le="0.025",method="get",code="200"} 1
12+
histogram_exemplar_test_bucket{le="0.05",method="get",code="200"} 1
13+
histogram_exemplar_test_bucket{le="0.1",method="get",code="200"} 1
14+
histogram_exemplar_test_bucket{le="0.25",method="get",code="200"} 1
15+
histogram_exemplar_test_bucket{le="0.5",method="get",code="200"} 2 # {traceId="trace_id_test_2",spanId="span_id_test_2"} 0.4 1678654679
16+
histogram_exemplar_test_bucket{le="1",method="get",code="200"} 2
17+
histogram_exemplar_test_bucket{le="2.5",method="get",code="200"} 2
18+
histogram_exemplar_test_bucket{le="5",method="get",code="200"} 2
19+
histogram_exemplar_test_bucket{le="10",method="get",code="200"} 2
20+
histogram_exemplar_test_bucket{le="+Inf",method="get",code="200"} 3 # {traceId="trace_id_test_3",spanId="span_id_test_3"} 11 1678654679
21+
histogram_exemplar_test_sum{method="get",code="200"} 11.407
22+
histogram_exemplar_test_count{method="get",code="200"} 3
23+
# EOF
24+
"
25+
`;

test/__snapshots__/registerTest.js.snap

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ exports[`Register with OpenMetrics type should not output all initialized metric
1313
"
1414
`;
1515

16+
exports[`Register with OpenMetrics type should not output all initialized metrics at value 0 if labels and exemplars enabled 1`] = `
17+
"# HELP counter help
18+
# TYPE counter counter
19+
# HELP gauge help
20+
# TYPE gauge gauge
21+
# HELP histogram help
22+
# TYPE histogram histogram
23+
# HELP summary help
24+
# TYPE summary summary
25+
# EOF
26+
"
27+
`;
28+
1629
exports[`Register with OpenMetrics type should output all initialized metrics at value 0 1`] = `
1730
"# HELP counter help
1831
# TYPE counter counter

test/exemplarsTest.js

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const globalRegistry = require('../index').register;
55
const Histogram = require('../index').Histogram;
66
const Counter = require('../index').Counter;
77

8+
Date.now = jest.fn(() => 1678654679000);
9+
810
describe('Exemplars', () => {
911
it('should throw when using with Prometheus registry', async () => {
1012
globalRegistry.setContentType(Registry.PROMETHEUS_CONTENT_TYPE);
@@ -96,6 +98,8 @@ describe('Exemplars', () => {
9698
getValuesByLabel('+Inf', vals)[0].exemplar.labelSet.traceId,
9799
).toEqual('trace_id_test_3');
98100
expect(getValuesByLabel('+Inf', vals)[0].exemplar.value).toEqual(11);
101+
102+
expect(await globalRegistry.metrics()).toMatchSnapshot();
99103
});
100104

101105
it('should throw if exemplar is too long', async () => {

test/registerTest.js

+48-8
Original file line numberDiff line numberDiff line change
@@ -223,24 +223,64 @@ describe('Register', () => {
223223
}
224224
});
225225

226-
it('should output all initialized metrics at value 0', async () => {
227-
new Counter({ name: 'counter', help: 'help' });
228-
new Gauge({ name: 'gauge', help: 'help' });
229-
new Histogram({ name: 'histogram', help: 'help' });
230-
new Summary({ name: 'summary', help: 'help' });
226+
if (regType === Registry.OPENMETRICS_CONTENT_TYPE) {
227+
it('should output all initialized metrics at value 0', async () => {
228+
new Counter({ name: 'counter', help: 'help', enableExemplars: true });
229+
new Gauge({ name: 'gauge', help: 'help' });
230+
new Histogram({
231+
name: 'histogram',
232+
help: 'help',
233+
enableExemplars: true,
234+
});
235+
new Summary({ name: 'summary', help: 'help' });
231236

232-
expect(await register.metrics()).toMatchSnapshot();
233-
});
237+
expect(await register.metrics()).toMatchSnapshot();
238+
});
239+
} else {
240+
it('should output all initialized metrics at value 0', async () => {
241+
new Counter({ name: 'counter', help: 'help' });
242+
new Gauge({ name: 'gauge', help: 'help' });
243+
new Histogram({ name: 'histogram', help: 'help' });
244+
new Summary({ name: 'summary', help: 'help' });
245+
246+
expect(await register.metrics()).toMatchSnapshot();
247+
});
248+
}
234249

235250
it('should not output all initialized metrics at value 0 if labels', async () => {
236251
new Counter({ name: 'counter', help: 'help', labelNames: ['label'] });
237252
new Gauge({ name: 'gauge', help: 'help', labelNames: ['label'] });
238-
new Histogram({ name: 'histogram', help: 'help', labelNames: ['label'] });
253+
new Histogram({
254+
name: 'histogram',
255+
help: 'help',
256+
labelNames: ['label'],
257+
});
239258
new Summary({ name: 'summary', help: 'help', labelNames: ['label'] });
240259

241260
expect(await register.metrics()).toMatchSnapshot();
242261
});
243262

263+
if (regType === Registry.OPENMETRICS_CONTENT_TYPE) {
264+
it('should not output all initialized metrics at value 0 if labels and exemplars enabled', async () => {
265+
new Counter({
266+
name: 'counter',
267+
help: 'help',
268+
labelNames: ['label'],
269+
enableExemplars: true,
270+
});
271+
new Gauge({ name: 'gauge', help: 'help', labelNames: ['label'] });
272+
new Histogram({
273+
name: 'histogram',
274+
help: 'help',
275+
labelNames: ['label'],
276+
enableExemplars: true,
277+
});
278+
new Summary({ name: 'summary', help: 'help', labelNames: ['label'] });
279+
280+
expect(await register.metrics()).toMatchSnapshot();
281+
});
282+
}
283+
244284
describe('should escape', () => {
245285
let escapedResult;
246286
beforeEach(async () => {

0 commit comments

Comments
 (0)