Skip to content

Add exemplars for native histograms #1686

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

shivanthzen
Copy link
Contributor

@shivanthzen shivanthzen commented Nov 17, 2024

Support exemplars in native histograms:
#1617

Remove unused variable

fix tests

Signed-off-by: Shivanth <[email protected]>
@shivanthzen shivanthzen force-pushed the nativehistogram_exemplars branch from 7569bb1 to 6a6a456 Compare January 4, 2025 20:34
@shivanthzen shivanthzen marked this pull request as ready for review January 4, 2025 20:36
@shivanthzen shivanthzen marked this pull request as draft January 6, 2025 12:38
@shivanthzen shivanthzen marked this pull request as ready for review January 14, 2025 14:45
@shivanthzen
Copy link
Contributor Author

shivanthzen commented Jan 14, 2025

@bwplotka
Copy link
Member

Do you mind checking @beorn7 @ArthurSens ? I don't have context on this logic, not sure if this PR is what #1654 (comment) wanted.

@beorn7
Copy link
Member

beorn7 commented Jan 21, 2025

This has been on my review list for a while. It's just hard to catch up through all my backlog… 😮‍💨

Copy link
Member

@beorn7 beorn7 left a comment

Choose a reason for hiding this comment

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

I think this implements the intended idea. Thank you very much. However, I think we should also be able to handle a histogram with both native and classic buckets (even though we have no MustNewConst... constructor for it, but people could create it on their own using dto primitives). And we need to test that. See comments.

10, 12.1, map[int]int64{1: 7, 2: 1, 3: 2}, map[int]int64{}, 0, 2, 0.2, time.Date(
2009, 11, 17, 20, 34, 58, 651387237, time.UTC))
m := &withExemplarsMetric{Metric: h, exemplars: []*dto.Exemplar{
{Value: proto.Float64(2000.0), Timestamp: timestamppb.New(time.Date(2009, 11, 17, 20, 34, 58, 3243244, time.UTC))},
Copy link
Member

Choose a reason for hiding this comment

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

We should also test the following cases:

  • More than one exemplar.
  • An exemplar without timestamp (to verify that a timestamp is added automatically then, because exemplars without timestamps are not allowed for native histograms).
  • A histogram with both classic and native buckets. (I believe for that you might have to create an ad-hoc implementation of Metric that spits out a custom-made dto.Metric.)

shivanthzen and others added 2 commits February 4, 2025 13:03
Co-authored-by: Björn Rabenstein <[email protected]>
Signed-off-by: Shivanth MP <[email protected]>
Co-authored-by: Björn Rabenstein <[email protected]>
Signed-off-by: Shivanth MP <[email protected]>
@shivanthzen
Copy link
Contributor Author

Update: This PR is still being worked on. Not stale.

@beorn7
Copy link
Member

beorn7 commented Feb 6, 2025

@shivanthzen let us know when it is ready for the next round of review.

@shivanthzen shivanthzen force-pushed the nativehistogram_exemplars branch from 951bff7 to 7f9f76d Compare April 14, 2025 21:17
Signed-off-by: Shivanth <[email protected]>
@shivanthzen
Copy link
Contributor Author

@beorn7 Added a test to cover the case where exemplars are added to both buckets and exemplars.

Copy link
Member

@beorn7 beorn7 left a comment

Choose a reason for hiding this comment

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

Thank you. I have just a naming nit and also just discovered that we need one small actual code change after all.

Comment on lines 296 to 298
// This is a dummy Histogram which has both buckets and nativehistogram
// to test metrics with exemplars
func NewDummyConstNativeHistogram(
Copy link
Member

Choose a reason for hiding this comment

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

Let's not export this, so that nobody will use this for real. Also, how about a more descriptive name like "newNativeHistogramWithClassicBuckets"? And finally, end a sentence with a period. :)

Suggested change
// This is a dummy Histogram which has both buckets and nativehistogram
// to test metrics with exemplars
func NewDummyConstNativeHistogram(
// newNativeHistogramWithClassicBuckets returns a Metric representing
// a native histogram that also has classic buckets. This is for testing purposes.
func newNativeHistogramWithClassicBuckets(

@@ -187,6 +187,15 @@ func (m *withExemplarsMetric) Write(pb *dto.Metric) error {
pb.Counter.Exemplar = m.exemplars[len(m.exemplars)-1]
case pb.Histogram != nil:
for _, e := range m.exemplars {
if pb.Histogram.Schema != nil {
if *pb.Histogram.Schema > math.MinInt32 && e.GetTimestamp() != nil {
Copy link
Member

Choose a reason for hiding this comment

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

While we use math.MinInt32 as a schema within the histogram struct to mark the absence of sparse buckets, we do not use the same marker in the protobuf. In the protobuf, we know it represents a native histogram if the zero threshold or the zero count or the number of negative spans or the number of positive spans are not zero. (If need be, we add a no-op span.)

So what we have to check is something like the following:

h := pb.Histogram
if (*h.ZeroThreshold != 0 || *h.ZeroCount != 0 || len(h.PositiveSpan) != 0 || len(h.NegativeSpan) != 0) && e.GetTimestamp() != nil {

@shivanthzen shivanthzen requested a review from beorn7 April 17, 2025 16:28
@shivanthzen
Copy link
Contributor Author

@beorn7

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.

3 participants