diff --git a/lib/counter.js b/lib/counter.js index 39a7bd1b..dd7dc7b2 100644 --- a/lib/counter.js +++ b/lib/counter.js @@ -22,6 +22,8 @@ class Counter extends Metric { this.defaultLabels = {}; this.defaultValue = 1; this.defaultExemplarLabelSet = {}; + this.useCounterValueAsExemplarValue = + config.useCounterValueAsExemplarValue || false; if (config.enableExemplars) { this.enableExemplars = true; this.inc = this.incWithExemplar; @@ -58,7 +60,7 @@ class Counter extends Metric { setValue(this.hashMap, value, labels, hash); - return { labelHash: hash }; + return { labelHash: hash, value: this.hashMap[hash].value }; } /** @@ -81,7 +83,11 @@ class Counter extends Metric { exemplarLabels = this.defaultExemplarLabelSet, } = {}) { const res = this.incWithoutExemplar(labels, value); - this.updateExemplar(exemplarLabels, value, res.labelHash); + const exemplarValue = this.useCounterValueAsExemplarValue + ? res.value + : value; + + this.updateExemplar(exemplarLabels, exemplarValue, res.labelHash); } updateExemplar(exemplarLabels, value, hash) { diff --git a/test/exemplarsTest.js b/test/exemplarsTest.js index ff72ffcb..cbaf327f 100644 --- a/test/exemplarsTest.js +++ b/test/exemplarsTest.js @@ -213,6 +213,7 @@ describe('Exemplars', () => { labelNames: ['method', 'code'], enableExemplars: true, }); + const vals = (await histogramInstance.get()).values; histogramInstance.observe({ value: 0.3, @@ -227,8 +228,6 @@ describe('Exemplars', () => { labels: { method: 'get', code: '200' }, }); - const vals = (await histogramInstance.get()).values; - expect(getValuesByLabel(0.5, vals)[0].value).toEqual(2); expect( getValuesByLabel(0.5, vals)[0].exemplar.labelSet.traceId, @@ -240,6 +239,53 @@ describe('Exemplars', () => { }); }); + describe('when useCounterValueAsExemplarValue is enabled', () => { + it('uses the counter value for the exemplar', async () => { + const counterInstance = new Counter({ + name: 'counter_exemplar_value_as_counter_test', + help: 'help', + labelNames: ['method', 'code'], + enableExemplars: true, + useCounterValueAsExemplarValue: true, + }); + + counterInstance.inc({ + labelValues: { method: 'get', code: '200' }, + exemplarLabels: { + traceId: 'trace_id_test_1', + spanId: 'span_id_test_1', + }, + }); + + counterInstance.inc({ + labelValues: { method: 'get', code: '200' }, + exemplarLabels: { + traceId: 'trace_id_test_2', + spanId: 'span_id_test_2', + }, + }); + + counterInstance.inc({ + labelValues: { method: 'get', code: '200' }, + exemplarLabels: { + traceId: 'trace_id_test_3', + spanId: 'span_id_test_3', + }, + }); + + const vals = await counterInstance.get(); + expect(vals.values[0].value).toEqual(3); + expect(vals.values[0].exemplar.value).toEqual(3); + expect(vals.values[0].exemplar.labelSet.traceId).toEqual( + 'trace_id_test_3', + ); + + expect(vals.values[0].exemplar.labelSet.spanId).toEqual( + 'span_id_test_3', + ); + }); + }); + function getValueByLabel(label, values, key) { return values.reduce((acc, val) => { if (val.labels && val.labels[key || 'le'] === label) {