-
Notifications
You must be signed in to change notification settings - Fork 7k
[core][metric] improve histogram metrics midpoint calculation #57948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[core][metric] improve histogram metrics midpoint calculation #57948
Conversation
Signed-off-by: justwph <[email protected]>
There was a problem hiding this 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
|
@can-anyscale PTAL |
can-anyscale
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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]>
…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]>
…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]>

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.