Skip to content

[CODE HEALTH] Fix clang-tidy warnings in base2 exponential histogram aggregation#3997

Merged
marcalff merged 2 commits intoopen-telemetry:mainfrom
thc1006:codehealth/base2-exp-histogram-narrowing
Apr 16, 2026
Merged

[CODE HEALTH] Fix clang-tidy warnings in base2 exponential histogram aggregation#3997
marcalff merged 2 commits intoopen-telemetry:mainfrom
thc1006:codehealth/base2-exp-histogram-narrowing

Conversation

@thc1006
Copy link
Copy Markdown
Contributor

@thc1006 thc1006 commented Apr 14, 2026

Fixes #3979
Contributes to #2053

Changes

  • In GetScaleReduction, widen the loop condition to int64_t so that
    end_index - start_index + 1 cannot overflow int32_t at
    max_scale_ = 20 with extreme-span histograms, and so
    bugprone-misplaced-widening-cast stops firing.
  • In Downscale, wrap the point_data_.scale_ -= by assignment with
    static_cast<int32_t>(point_data_.scale_ - by) so the
    int32_t - uint32_t arithmetic result is explicitly narrowed back to
    int32_t.
  • In Merge and Diff, apply the same static_cast<int32_t>(...) wrap to
    the five remaining scale_ -= scale_reduction sites.
  • Decrement the clang-tidy.yaml warning_limit by 7 for both
    all-options-abiv1-preview and all-options-abiv2-preview matrix
    entries to ratchet the limit down to the new measured count.

The arithmetic is behaviour-preserving: in the six scale_ -= uint32_t sites,
the existing code already relies on two's-complement wrap-around through the
unsigned intermediate, and static_cast<int32_t> reproduces the same bit
pattern. In GetScaleReduction, the int64_t widening gives the same
numeric result on values that fit in int32_t but also handles the
theoretical edge case at max_scale_ = 20 where the bucket range can span
near INT32_MAX.

Existing Base2ExponentialHistogramAggregation* cases in
sdk/test/metrics/aggregation_test.cc cover all modified paths; in
particular MergeCountInvariant uses max_buckets_ = 5 with five-bucket
delta cycles to deliberately exceed the limit and trigger the scale-reduction
branch in Merge (which calls GetScaleReduction and then the three
scale_ -= scale_reduction assignments). The Diff path is covered by the
scale-mismatched calls in the other Base2 tests.

Fixes the following warnings:


opentelemetry-cpp/sdk/src/metrics/aggregation/base2_exponential_histogram_aggregation.cc (7 warnings)

Line Check Message
35 bugprone-misplaced-widening-cast either cast from 'int' to 'size_t' (aka 'unsigned long') is ineffective, or there is loss of precision before the conversion
215 cppcoreguidelines-narrowing-conversions narrowing conversion from 'uint32_t' (aka 'unsigned int') to signed type 'int32_t' (aka 'int') is implementation-defined
327 cppcoreguidelines-narrowing-conversions narrowing conversion from 'uint32_t' (aka 'unsigned int') to signed type 'int32_t' (aka 'int') is implementation-defined
328 cppcoreguidelines-narrowing-conversions narrowing conversion from 'uint32_t' (aka 'unsigned int') to signed type 'int32_t' (aka 'int') is implementation-defined
329 cppcoreguidelines-narrowing-conversions narrowing conversion from 'uint32_t' (aka 'unsigned int') to signed type 'int32_t' (aka 'int') is implementation-defined
412 cppcoreguidelines-narrowing-conversions narrowing conversion from 'uint32_t' (aka 'unsigned int') to signed type 'int32_t' (aka 'int') is implementation-defined
413 cppcoreguidelines-narrowing-conversions narrowing conversion from 'uint32_t' (aka 'unsigned int') to signed type 'int32_t' (aka 'int') is implementation-defined

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

@thc1006 thc1006 requested a review from a team as a code owner April 14, 2026 13:23
Copilot AI review requested due to automatic review settings April 14, 2026 13:23
@thc1006 thc1006 force-pushed the codehealth/base2-exp-histogram-narrowing branch from aad04da to 54475cb Compare April 14, 2026 13:23
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses clang-tidy warnings in the SDK’s base2 exponential histogram aggregation implementation and tightens CI’s clang-tidy warning thresholds accordingly.

Changes:

  • Adjusts GetScaleReduction arithmetic to avoid int32_t overflow in the bucket-span computation.
  • Makes scale decrements explicit where scale_ is adjusted by uint32_t reductions (Downscale/Merge/Diff).
  • Lowers clang-tidy GitHub Actions warning_limit values to reflect the reduced warning count and adds an Unreleased changelog entry.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
sdk/src/metrics/aggregation/base2_exponential_histogram_aggregation.cc Arithmetic/casting changes intended to eliminate clang-tidy warnings in scale-reduction and scale-update paths.
CHANGELOG.md Adds an Unreleased entry for this code health change.
.github/workflows/clang-tidy.yaml Reduces clang-tidy warning limits in the CI matrix to “ratchet” the baseline downward.

Comment thread sdk/src/metrics/aggregation/base2_exponential_histogram_aggregation.cc Outdated
Comment thread sdk/src/metrics/aggregation/base2_exponential_histogram_aggregation.cc Outdated
Comment thread sdk/src/metrics/aggregation/base2_exponential_histogram_aggregation.cc Outdated
Comment thread CHANGELOG.md
@thc1006 thc1006 force-pushed the codehealth/base2-exp-histogram-narrowing branch from 54475cb to ccdaf85 Compare April 14, 2026 13:42
@thc1006 thc1006 requested a review from Copilot April 14, 2026 13:50
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses clang-tidy findings in the SDK’s base2 exponential histogram aggregation by making integer arithmetic conversions explicit and preventing edge-case overflow in scale-reduction calculations, and then ratchets the CI warning limits accordingly.

Changes:

  • Prevent potential int32_t overflow in GetScaleReduction by widening the span computation/comparison to int64_t.
  • Make scale_ -= <uint32_t> updates explicit by casting the RHS to int32_t in Downscale, Merge, and Diff to satisfy narrowing-conversion checks.
  • Lower clang-tidy CI warning_limit thresholds to reflect the reduced warning count, and add a changelog entry.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
sdk/src/metrics/aggregation/base2_exponential_histogram_aggregation.cc Adjusts arithmetic types/casts to eliminate clang-tidy warnings and avoid theoretical overflow in bucket-span calculations.
CHANGELOG.md Adds an Unreleased entry documenting the code health fix.
.github/workflows/clang-tidy.yaml Lowers warning limits for two matrix presets to match the new measured warning counts.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.33%. Comparing base (a42027a) to head (a86cf23).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3997      +/-   ##
==========================================
+ Coverage   90.18%   90.33%   +0.16%     
==========================================
  Files         230      230              
  Lines        7299     7299              
==========================================
+ Hits         6582     6593      +11     
+ Misses        717      706      -11     
Files with missing lines Coverage Δ
...egation/base2_exponential_histogram_aggregation.cc 95.87% <100.00%> (+4.55%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…aggregation

Clear the seven clang-tidy warnings reported under open-telemetry#3979 in
sdk/src/metrics/aggregation/base2_exponential_histogram_aggregation.cc:

* GetScaleReduction (L35): widen the loop condition to int64_t so that
  end_index - start_index + 1 cannot overflow int32_t at max_scale_=20
  with extreme-span histograms, and so bugprone-misplaced-widening-cast
  stops firing.
* Downscale (L215): wrap point_data_.scale_ -= by with
  static_cast<int32_t>(point_data_.scale_ - by).
* Merge (L327-329) and Diff (L412-413): same static_cast<int32_t> wrap
  on the five remaining int32_t -= uint32_t sites that land back into
  the scale_ int32_t field.

Behaviour is preserved: all nine existing Base2Exponential* cases in
sdk/test/metrics/aggregation_test.cc pass before and after the change.
The MergeCountInvariant test in particular exercises the scale-reduction
branch this PR touches.

Decrement the clang-tidy warning_limit by 7 for both
all-options-abiv1-preview and all-options-abiv2-preview matrix entries
to ratchet the limit down to the new measured count.

Resolves open-telemetry#3979. Part of open-telemetry#2053.

Signed-off-by: thc1006 <[email protected]>
@thc1006 thc1006 force-pushed the codehealth/base2-exp-histogram-narrowing branch from ccdaf85 to d06a138 Compare April 14, 2026 14:20
@thc1006 thc1006 requested a review from Copilot April 14, 2026 14:23
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses clang-tidy warnings in the SDK’s base2 exponential histogram aggregation implementation by making integer widening/narrowing explicit and overflow-safe, and then ratchets the clang-tidy CI warning limits to reflect the reduced warning count.

Changes:

  • Adjust GetScaleReduction’s span comparison to use int64_t arithmetic to avoid int32_t overflow and satisfy bugprone-misplaced-widening-cast.
  • Make scale_ updates explicitly subtract in the signed domain (static_cast<int32_t>(...)) across Downscale, Merge, and Diff to resolve narrowing-conversion warnings.
  • Add a focused unit test to exercise the Diff() downscaling path; update clang-tidy workflow warning limits and changelog.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
sdk/src/metrics/aggregation/base2_exponential_histogram_aggregation.cc Fixes clang-tidy warnings by widening span arithmetic and ensuring signed-domain subtraction for scale_.
sdk/test/metrics/aggregation_test.cc Adds a test that forces Diff() to take the scale-reduction/downscale branch.
CHANGELOG.md Records the code-health change in the Unreleased section.
.github/workflows/clang-tidy.yaml Lowers clang-tidy warning_limit values to reflect the new measured warning count.

Copy link
Copy Markdown
Member

@dbarker dbarker left a comment

Choose a reason for hiding this comment

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

Looks reasonable. Thanks for the warning fixes and test case!

Copy link
Copy Markdown
Member

@lalitb lalitb left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks.

@marcalff marcalff merged commit 0962d71 into open-telemetry:main Apr 16, 2026
68 checks passed
@thc1006 thc1006 deleted the codehealth/base2-exp-histogram-narrowing branch April 16, 2026 20:18
saphal1998 pushed a commit to saphal1998/opentelemetry-cpp that referenced this pull request Apr 16, 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.

[CODE HEALTH] clang-tidy warnings in base2_exponential_histogram_aggregation

5 participants