Skip to content

fix(counter): use counter total as exemplar value instead of increment#775

Closed
c436zhan wants to merge 1 commit into
prometheus:mainfrom
c436zhan:fix/counter-exemplar-total-value
Closed

fix(counter): use counter total as exemplar value instead of increment#775
c436zhan wants to merge 1 commit into
prometheus:mainfrom
c436zhan:fix/counter-exemplar-total-value

Conversation

@c436zhan

Copy link
Copy Markdown

Summary

When a Counter with exemplars enabled is incremented via inc({ exemplarLabels }), the exemplar's value was set to the increment amount (default 1) instead of the counter's current total. This caused every exemplar to read 1 in common usage, making exemplar markers in tools like Grafana sit at the bottom of the graph regardless of the counter's actual value.

Root cause

In Counter.incWithExemplar, after calling incWithoutExemplar(labels, value), the code passed the raw increment value to updateExemplar. The exemplar should instead record the counter's total after the increment, which is what other Prometheus clients do.

Changes

  • lib/counter.js: pass this.store.entry(labels).value (the counter total) to updateExemplar instead of value (the increment amount).
  • test/exemplarsTest.js: add regression test verifying that after inc() then inc(3), the second exemplar records 4 (the total), not 3.
  • CHANGELOG.md: document the breaking behavior change under the Unreleased section.

Testing

  • npm test — 543 passing (28 test suites)
  • npx jest test/exemplarsTest.js — 9 passing, 1 snapshot passing
  • Verified with a standalone reproduction script that exemplar values now match the counter total on each increment.

Compatibility

This changes the value written to counter exemplars from the increment amount to the counter total. Anyone relying on the previous (buggy) behavior of exemplars always being 1 will see a change. The fix aligns with the behavior described in the linked issue and with other Prometheus client libraries.

Fixes #621

Counter exemplars previously recorded the increment amount (default 1)
instead of the counter's current total. This meant that after multiple
increments, exemplar markers in tools like Grafana would all sit at the
bottom of the graph regardless of the actual counter value.

Change incWithExemplar to pass the store entry's current value to
updateExemplar, matching the behavior of other Prometheus clients.

Fixes prometheus#621

Signed-off-by: Chen Zhang <c436zhan@gmail.com>
@c436zhan c436zhan force-pushed the fix/counter-exemplar-total-value branch from 7f6a6e7 to a5d948c Compare July 13, 2026 02:04
@jdmarshall

Copy link
Copy Markdown
Contributor

Two different users, filing PRs for the same bug, with the exact same unit test in both versions. My bandwidth for dealing with AI generated PRs has reached its end for today.

@jdmarshall jdmarshall closed this Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exemplar values in counter metrics are always 1

2 participants