Skip to content

Commit

Permalink
add useCounterAsExemplarValue support
Browse files Browse the repository at this point in the history
  • Loading branch information
psimk committed Mar 26, 2024
1 parent 4e514e7 commit 5fcf716
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lib/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 };
}

/**
Expand All @@ -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) {
Expand Down
50 changes: 48 additions & 2 deletions test/exemplarsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ describe('Exemplars', () => {
labelNames: ['method', 'code'],
enableExemplars: true,
});
const vals = (await histogramInstance.get()).values;

histogramInstance.observe({
value: 0.3,
Expand All @@ -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,
Expand All @@ -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) {
Expand Down

0 comments on commit 5fcf716

Please sign in to comment.