Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct exemplar formating #556

Merged
merged 2 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,18 @@ class Registry {
? `{${formattedLabels.join(',')}}`
: '';

values.push(
`${metricName}${labelsString} ${getValueAsString(val.value)}`,
);
let fullMetricLine = `${metricName}${labelsString} ${getValueAsString(
val.value,
)}`;

const { exemplar } = val;
if (exemplar && this.contentType === Registry.OPENMETRICS_CONTENT_TYPE) {
const formattedExemplars = formatLabels(exemplar.labelSet);
values.push(
` # {${formattedExemplars.join(',')}} ${getValueAsString(
exemplar.value,
)} ${exemplar.timestamp}`,
);
fullMetricLine += ` # {${formattedExemplars.join(
',',
)}} ${getValueAsString(exemplar.value)} ${exemplar.timestamp}`;
}
values.push(fullMetricLine);
}

return values.join('\n');
Expand Down
25 changes: 25 additions & 0 deletions test/__snapshots__/exemplarsTest.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Exemplars with OpenMetrics registry should make histogram with exemplars on multiple buckets 1`] = `
"# HELP counter_exemplar_test help
# TYPE counter_exemplar_test counter
counter_exemplar_test_total{method="get",code="200"} 2 # {traceId="trace_id_test",spanId="span_id_test"} 2 1678654679
# HELP histogram_exemplar_test test
# TYPE histogram_exemplar_test histogram
histogram_exemplar_test_bucket{le="0.005",method="get",code="200"} 0
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
histogram_exemplar_test_bucket{le="0.025",method="get",code="200"} 1
histogram_exemplar_test_bucket{le="0.05",method="get",code="200"} 1
histogram_exemplar_test_bucket{le="0.1",method="get",code="200"} 1
histogram_exemplar_test_bucket{le="0.25",method="get",code="200"} 1
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
histogram_exemplar_test_bucket{le="1",method="get",code="200"} 2
histogram_exemplar_test_bucket{le="2.5",method="get",code="200"} 2
histogram_exemplar_test_bucket{le="5",method="get",code="200"} 2
histogram_exemplar_test_bucket{le="10",method="get",code="200"} 2
histogram_exemplar_test_bucket{le="+Inf",method="get",code="200"} 3 # {traceId="trace_id_test_3",spanId="span_id_test_3"} 11 1678654679
histogram_exemplar_test_sum{method="get",code="200"} 11.407
histogram_exemplar_test_count{method="get",code="200"} 3
# EOF
"
`;
13 changes: 13 additions & 0 deletions test/__snapshots__/registerTest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ exports[`Register with OpenMetrics type should not output all initialized metric
"
`;

exports[`Register with OpenMetrics type should not output all initialized metrics at value 0 if labels and exemplars enabled 1`] = `
"# HELP counter help
# TYPE counter counter
# HELP gauge help
# TYPE gauge gauge
# HELP histogram help
# TYPE histogram histogram
# HELP summary help
# TYPE summary summary
# EOF
"
`;

exports[`Register with OpenMetrics type should output all initialized metrics at value 0 1`] = `
"# HELP counter help
# TYPE counter counter
Expand Down
4 changes: 4 additions & 0 deletions test/exemplarsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const globalRegistry = require('../index').register;
const Histogram = require('../index').Histogram;
const Counter = require('../index').Counter;

Date.now = jest.fn(() => 1678654679000);

describe('Exemplars', () => {
it('should throw when using with Prometheus registry', async () => {
globalRegistry.setContentType(Registry.PROMETHEUS_CONTENT_TYPE);
Expand Down Expand Up @@ -96,6 +98,8 @@ describe('Exemplars', () => {
getValuesByLabel('+Inf', vals)[0].exemplar.labelSet.traceId,
).toEqual('trace_id_test_3');
expect(getValuesByLabel('+Inf', vals)[0].exemplar.value).toEqual(11);

expect(await globalRegistry.metrics()).toMatchSnapshot();
});

it('should throw if exemplar is too long', async () => {
Expand Down
56 changes: 48 additions & 8 deletions test/registerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,24 +223,64 @@ describe('Register', () => {
}
});

it('should output all initialized metrics at value 0', async () => {
new Counter({ name: 'counter', help: 'help' });
new Gauge({ name: 'gauge', help: 'help' });
new Histogram({ name: 'histogram', help: 'help' });
new Summary({ name: 'summary', help: 'help' });
if (regType === Registry.OPENMETRICS_CONTENT_TYPE) {
it('should output all initialized metrics at value 0', async () => {
new Counter({ name: 'counter', help: 'help', enableExemplars: true });
new Gauge({ name: 'gauge', help: 'help' });
new Histogram({
name: 'histogram',
help: 'help',
enableExemplars: true,
});
new Summary({ name: 'summary', help: 'help' });

expect(await register.metrics()).toMatchSnapshot();
});
expect(await register.metrics()).toMatchSnapshot();
});
} else {
it('should output all initialized metrics at value 0', async () => {
new Counter({ name: 'counter', help: 'help' });
new Gauge({ name: 'gauge', help: 'help' });
new Histogram({ name: 'histogram', help: 'help' });
new Summary({ name: 'summary', help: 'help' });

expect(await register.metrics()).toMatchSnapshot();
});
}

it('should not output all initialized metrics at value 0 if labels', async () => {
new Counter({ name: 'counter', help: 'help', labelNames: ['label'] });
new Gauge({ name: 'gauge', help: 'help', labelNames: ['label'] });
new Histogram({ name: 'histogram', help: 'help', labelNames: ['label'] });
new Histogram({
name: 'histogram',
help: 'help',
labelNames: ['label'],
});
new Summary({ name: 'summary', help: 'help', labelNames: ['label'] });

expect(await register.metrics()).toMatchSnapshot();
});

if (regType === Registry.OPENMETRICS_CONTENT_TYPE) {
it('should not output all initialized metrics at value 0 if labels and exemplars enabled', async () => {
new Counter({
name: 'counter',
help: 'help',
labelNames: ['label'],
enableExemplars: true,
});
new Gauge({ name: 'gauge', help: 'help', labelNames: ['label'] });
new Histogram({
name: 'histogram',
help: 'help',
labelNames: ['label'],
enableExemplars: true,
});
new Summary({ name: 'summary', help: 'help', labelNames: ['label'] });

expect(await register.metrics()).toMatchSnapshot();
});
}

describe('should escape', () => {
let escapedResult;
beforeEach(async () => {
Expand Down