From bb10a7278f52ffe73ac3a06fb317d2df1ca305a0 Mon Sep 17 00:00:00 2001 From: Andrei Dobre Date: Fri, 3 Dec 2021 12:48:11 +0200 Subject: [PATCH] update tests --- test/exemplarsTest.js | 63 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/test/exemplarsTest.js b/test/exemplarsTest.js index 560c724b..ba37924c 100644 --- a/test/exemplarsTest.js +++ b/test/exemplarsTest.js @@ -21,26 +21,71 @@ 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.4, + labels: { method: 'get', code: '200' }, + exemplarLabels: { traceId: 'trace_id_test_2', spanId: 'span_id_test_2' }, + }); histogramInstance.observe({ - value: 0.5, + value: 11, labels: { method: 'get', code: '200' }, + exemplarLabels: { traceId: 'trace_id_test_3', spanId: 'span_id_test_3' }, }); - // console.log(await globalRegistry.metrics()); - expect(true).toEqual(true); + + 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); + + console.log(vals); }); + function getValuesByLabel(label, values, key) { + return values.reduce((acc, val) => { + if (val.labels && val.labels[key || 'le'] === label) { + acc.push(val); + } + return acc; + }, []); + } });