Skip to content
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ This release marks our first release under the Prometheus umbrella.
- Drop support for Node.js versions 16, 18, 20, 21 and 23
- Metric internal storage ('hashMap') changed to a separate object, LabelMap. If you have
subclassed the built-in metric types you may need to adjust your code.
- Counter exemplar values now record the counter's total value instead of the increment
amount, matching the behavior of other Prometheus clients (Fixes [#621](https://github.com/prometheus/client_js/issues/621))

### Changed

Expand Down
3 changes: 2 additions & 1 deletion lib/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ class Counter extends Metric {
exemplarLabels = this.defaultExemplarLabelSet,
} = {}) {
this.incWithoutExemplar(labels, value);
this.updateExemplar(labels, exemplarLabels, value);
const entry = this.store.entry(labels);
this.updateExemplar(labels, exemplarLabels, entry.value);
}

updateExemplar(labels, exemplarLabels, value) {
Expand Down
25 changes: 25 additions & 0 deletions test/exemplarsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,31 @@ describe('Exemplars', () => {
);
});

it('should use the counter total as the exemplar value, not the increment', async () => {
const localRegistry = new Registry();
localRegistry.setContentType(regType);
const counterInstance = new Counter({
name: 'counter_exemplar_total_test',
help: 'help',
labelNames: ['method', 'code'],
enableExemplars: true,
registers: [localRegistry],
});
counterInstance.inc({
labels: { method: 'get', code: '200' },
exemplarLabels: { traceId: 'trace_id_1' },
});
counterInstance.inc({
value: 3,
labels: { method: 'get', code: '200' },
exemplarLabels: { traceId: 'trace_id_2' },
});
const vals = await counterInstance.get();
expect(vals.values[0].value).toEqual(4);
expect(vals.values[0].exemplar.value).toEqual(4);
expect(vals.values[0].exemplar.labelSet.traceId).toEqual('trace_id_2');
});

it('should make histogram with exemplars on multiple buckets', async () => {
const histogramInstance = new Histogram({
name: 'histogram_exemplar_test',
Expand Down