Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
voltbit committed Dec 3, 2021
1 parent 846af5a commit 6ae459f
Showing 1 changed file with 52 additions and 9 deletions.
61 changes: 52 additions & 9 deletions test/exemplarsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,69 @@ describe.each([
const counterInstance = new Counter({
name: 'counter_exemplar_test',
help: 'help',
labelNames: ['method', 'endpoint'],
labelNames: ['method', 'code'],
enableExemplars: true,
});
counterInstance.inc({ value: 2 });
// console.log(counterInstance);
// console.log(globalRegistry.getSingleMetric('counter_exemplar_test'));
expect(true).toEqual(true);
counterInstance.inc({
value: 2,
labels: { method: 'get', code: '200' },
exemplarLabels: { traceId: 'trace_id_test', spanId: 'span_id_test' },
});
const vals = await counterInstance.get();
expect(vals.values[0].value).toEqual(2);
expect(vals.values[0].exemplar.value).toEqual(2);
expect(vals.values[0].exemplar.labelSet.traceId).toEqual('trace_id_test');
});
it('should make histogram with exemplar on all buckets', async () => {
it('should make histogram with exemplars on multiple buckets', async () => {
const histogramInstance = new Histogram({
name: 'histogram_exemplar_test',
help: 'test',
labelNames: ['method', 'code'],
enableExemplars: true,
});

histogramInstance.observe({
value: 0.007,
labels: { method: 'get', code: '200' },
exemplarLabels: { traceId: 'trace_id_test_1', spanId: 'span_id_test_1' },
});
histogramInstance.observe({
value: 0.5,
value: 0.4,
labels: { method: 'get', code: '200' },
exemplarLabels: { traceId: 'trace_id_test_2', spanId: 'span_id_test_2' },
});
// console.log(await globalRegistry.metrics());
expect(true).toEqual(true);
histogramInstance.observe({
value: 11,
labels: { method: 'get', code: '200' },
exemplarLabels: { traceId: 'trace_id_test_3', spanId: 'span_id_test_3' },
});

const vals = (await histogramInstance.get()).values;

expect(getValuesByLabel(0.005, vals)[0].value).toEqual(0);
expect(getValuesByLabel(0.005, vals)[0].exemplar).toEqual(null);

expect(getValuesByLabel(0.5, vals)[0].value).toEqual(2);
expect(getValuesByLabel(0.5, vals)[0].exemplar.labelSet.traceId).toEqual(
'trace_id_test_2',
);
expect(getValuesByLabel(0.5, vals)[0].exemplar.value).toEqual(0.4);

expect(getValuesByLabel(10, vals)[0].value).toEqual(2);
expect(getValuesByLabel(10, vals)[0].exemplar).toEqual(null);

expect(getValuesByLabel('+Inf', vals)[0].value).toEqual(3);
expect(getValuesByLabel('+Inf', vals)[0].exemplar.labelSet.traceId).toEqual(
'trace_id_test_3',
);
expect(getValuesByLabel('+Inf', vals)[0].exemplar.value).toEqual(11);
});
function getValuesByLabel(label, values, key) {
return values.reduce((acc, val) => {
if (val.labels && val.labels[key || 'le'] === label) {
acc.push(val);
}
return acc;
}, []);
}
});

0 comments on commit 6ae459f

Please sign in to comment.