Skip to content

Conversation

@wph95
Copy link
Contributor

@wph95 wph95 commented Oct 21, 2025

There’s a potential risk in the current midpoint calculation. It can be wrong when values are negative.

Line 167: lower_bound + buckets[0] / 2.0
Line 171: (buckets[i] + buckets[i - 1]) / 2.0

I improved the formula and added a test to make sure it works.

@wph95 wph95 requested a review from a team as a code owner October 21, 2025 08:17
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly fixes a bug in the histogram midpoint calculation when bucket boundaries are negative. The change is simple and effective, and a new test case has been added to cover this scenario. My review includes a couple of suggestions to improve code structure and test organization for better long-term maintainability.

lower_bound = 0.0 if buckets[0] > 0 else buckets[0] * 2.0
self._histogram_bucket_midpoints[name].append(
lower_bound + buckets[0] / 2.0
(lower_bound + buckets[0]) / 2.0
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

While this change correctly fixes the midpoint calculation, the logic for calculating all bucket midpoints (lines 161-177) could be extracted into a separate private helper method, e.g., _calculate_histogram_bucket_midpoints(buckets). This would improve the readability and maintainability of register_histogram_metric by separating the registration logic from the midpoint calculation. It would also allow the midpoint calculation to be unit tested more directly.

Comment on lines +122 to +130
mock_meter.create_histogram.return_value = NoOpHistogram(name="neg_histogram")
recorder.register_histogram_metric(
name="neg_histogram",
description="Histogram with negative first boundary",
buckets=[-5.0, 0.0, 10.0],
)

mids = recorder.get_histogram_bucket_midpoints("neg_histogram")
assert mids == pytest.approx([-7.5, -2.5, 5.0, 20.0])
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better test isolation and clarity, it would be beneficial to move this test case for negative bucket boundaries into its own separate test function, for example test_register_histogram_metric_negative_boundary. This makes it easier to identify which specific scenario fails if the test breaks in the future.

@ray-gardener ray-gardener bot added core Issues that should be addressed in Ray Core observability Issues related to the Ray Dashboard, Logging, Metrics, Tracing, and/or Profiling community-contribution Contributed by the community labels Oct 21, 2025
@edoakes
Copy link
Collaborator

edoakes commented Oct 21, 2025

@can-anyscale PTAL

@can-anyscale can-anyscale added the go add ONLY when ready to merge, run all tests label Oct 21, 2025
Copy link
Contributor

@can-anyscale can-anyscale left a comment

Choose a reason for hiding this comment

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

Thanks for catching! This makes sense.

@can-anyscale can-anyscale enabled auto-merge (squash) October 21, 2025 16:23
@can-anyscale can-anyscale merged commit 7945631 into ray-project:master Oct 21, 2025
8 checks passed
elliot-barn pushed a commit that referenced this pull request Oct 23, 2025
There’s a potential risk in the current midpoint calculation. It can be
wrong when values are negative.

Line 167: lower_bound + buckets[0] / 2.0
Line 171: (buckets[i] + buckets[i - 1]) / 2.0

I improved the formula and added a test to make sure it works.

Signed-off-by: justwph <[email protected]>
Signed-off-by: elliot-barn <[email protected]>
landscapepainter pushed a commit to landscapepainter/ray that referenced this pull request Nov 17, 2025
…oject#57948)

There’s a potential risk in the current midpoint calculation. It can be
wrong when values are negative.

Line 167: lower_bound + buckets[0] / 2.0
Line 171: (buckets[i] + buckets[i - 1]) / 2.0

I improved the formula and added a test to make sure it works.

Signed-off-by: justwph <[email protected]>
Aydin-ab pushed a commit to Aydin-ab/ray-aydin that referenced this pull request Nov 19, 2025
…oject#57948)

There’s a potential risk in the current midpoint calculation. It can be
wrong when values are negative.

Line 167: lower_bound + buckets[0] / 2.0
Line 171: (buckets[i] + buckets[i - 1]) / 2.0

I improved the formula and added a test to make sure it works.

Signed-off-by: justwph <[email protected]>
Signed-off-by: Aydin Abiar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests observability Issues related to the Ray Dashboard, Logging, Metrics, Tracing, and/or Profiling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants