Skip to content

Conversation

@taisho6339
Copy link

@taisho6339 taisho6339 commented Nov 23, 2025

Fixes #3037

Changes

  • Add clear documentation for monotonic counters, mentioning that negative values are dropped
  • Add validations to reject negative values in sdk for Counter and ObservableCounter

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

@codecov
Copy link

codecov bot commented Nov 23, 2025

Codecov Report

❌ Patch coverage is 88.97638% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.8%. Comparing base (53c9f47) to head (b48c4a2).

Files with missing lines Patch % Lines
opentelemetry-sdk/src/metrics/internal/sum.rs 88.8% 8 Missing ⚠️
...emetry-sdk/src/metrics/internal/precomputed_sum.rs 89.0% 6 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff           @@
##            main   #3260    +/-   ##
======================================
  Coverage   80.8%   80.8%            
======================================
  Files        129     129            
  Lines      23203   23330   +127     
======================================
+ Hits       18750   18863   +113     
- Misses      4453    4467    +14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@taisho6339 taisho6339 marked this pull request as ready for review November 23, 2025 02:10
@taisho6339 taisho6339 requested a review from a team as a code owner November 23, 2025 02:10
fn call(&self, measurement: T, attrs: &[KeyValue]) {
// Validate monotonic counter increment is non-negative
if self.monotonic && measurement < T::default() {
otel_warn!(
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure if the idea of producing one log per invalid metric measurement is a good idea. For now, let's keep it to debug.

A better idea could be to have an Sdk internal metric, that'd track all measurement_drops... We can discuss this as a future improvement.

/// Counters are used to measure values that only increase over time, such as the number
/// of requests received, bytes sent, or errors encountered. Only non-negative values
/// should be recorded. Negative values violate the monotonic property and will be
/// dropped by the SDK with a warning.
Copy link
Member

Choose a reason for hiding this comment

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

while its true that the spec says API should not validate this, and instead it is upto the implementations (i.e SDK) to do this validation, the SDK spec does not explicitly state what should it do when the value is negative. I'd love to get a spec clarification before implementing this.

I did take this route long ago (open-telemetry/opentelemetry-dotnet#2519) but never got the time to clarify if this is the right behavior to do in the sdk.

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.

[Bug]: f64_counter can be decremented

2 participants