Skip to content

Include counter metric name in negative value errors#2315

Open
HarshDevelops wants to merge 1 commit into
prometheus:mainfrom
HarshDevelops:fix/1090-counter-negative-name
Open

Include counter metric name in negative value errors#2315
HarshDevelops wants to merge 1 commit into
prometheus:mainfrom
HarshDevelops:fix/1090-counter-negative-name

Conversation

@HarshDevelops

Copy link
Copy Markdown

Fixes #1090

What was wrong

When a counter scrape produced a negative value, the exception was only the bare number and the phrase counters cannot have a negative value. In large applications that is hard to map back to a meter.

What changed

CounterDataPointSnapshot can carry an optional metric name used only in the validation message. Counter and CounterWithCallback pass the name when collecting. Labels are appended when present. Callers such as Micrometer can set the name through the builder.

Testing

  • CounterSnapshotTest.testNegativeValueIncludesMetricNameInMessage
  • CounterSnapshotTest (9 tests, 0 failures) on JDK 25
  • Signed-off-by (DCO)
  • Honest note: local package of prometheus-metrics-core succeeded; full multi-module CI is expected on GitHub

When a CounterSnapshot data point has a negative value, the exception
message can now include the metric name (and labels) so large scrapes
are easier to diagnose. Counter and CounterWithCallback pass the name
at collect time; callers such as Micrometer can set it via the builder.

Fixes prometheus#1090

Signed-off-by: Harsh Srivastava <harsh10822@gmail.com>

@zeitlinger zeitlinger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. Please make these changes before approval:

  • Do not add new public constructors to CounterDataPointSnapshot. This is stable API surface; keep the existing constructors and use the builder for the optional metric name instead. Update the internal collection paths to configure the builder rather than adding public constructor overloads.
  • Add coverage through the actual metric collection paths, especially CounterWithCallback, to verify that a negative callback value includes the metric name while existing constructor behavior remains unchanged.

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.

Add counter name to exception message when negative value is detected

2 participants