Skip to content
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

[1617] Add ConstnativeHistogram #1654

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

Conversation

shivanthzen
Copy link

@shivanthzen shivanthzen commented Oct 16, 2024

Fixes: #1617
Add exported ConstNativeHistogram - this can be used to build native histograms if one already has the relevant data without calling the observe function. A viable use case is while converting exponential histograms to nativehistograms.

@shivanthzen
Copy link
Author

@ArthurSens Maybe you can take a look at this PR.

Copy link
Member

@ArthurSens ArthurSens 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 working on this! I've added a few comments but I'm not a native histogram expert, it would be nice if @beorn7 or @krajorama could also take a look here.

question: Have you tried using your commit on OTel's side to make sure you can translate their format into ours?

prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram_test.go Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
@beorn7
Copy link
Member

beorn7 commented Oct 17, 2024

Will have a look ASAP (which won't be before next week, sorry).

@shivanthzen shivanthzen force-pushed the constNativeHistogram branch 2 times, most recently from 96eeddd to ad21924 Compare October 18, 2024 10:04
Copy link
Member

@krajorama krajorama left a comment

Choose a reason for hiding this comment

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

Hi, I understand the use case of wanting to expose OTEL exponential histograms from the Prometheus exporter in the collector. How would you use it actually? Constantly register and unregister the metric to update the exposed values? Or just use it's Write() method to get to the data?

In any case, the naming is going to be weird as you're not actually using it as a const :)

If I understand correctly, currently it will only work for counter histograms, not gauge histograms due to limitations in client_golang.

I'm a little afraid of abuse of this interface, but don't see true stoppers for now, will consult @beorn7 .

prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Show resolved Hide resolved
zeroBucket uint64
}

func NewConstNativeHistogram(desc *Desc, count uint64, sum float64, postiveBuckets, negativeBuckets map[int]int64, zeroBucket uint64,
Copy link
Member

Choose a reason for hiding this comment

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

Needs doc string to explain the arguments and the fact that this is exposed as a counter type histogram, treated as a counter on scrape.

Copy link
Member

Choose a reason for hiding this comment

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

In fact, I do actually think that exposing a gauge histogram will be one of the common use cases for using this function. So let's maybe add a bool argument here so that the user can switch between counter and gauge histogram.

Copy link
Member

Choose a reason for hiding this comment

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

Style nit: Prometheus code usually does a line break after the opening parenthesis in case of multiline argument lists.

Copy link
Author

Choose a reason for hiding this comment

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

Tried this. Wouldn't this blow up the argument list? Is it better to create a seperate a constNaticveGuageHistogram ?

Copy link
Author

@shivanthzen shivanthzen Nov 1, 2024

Choose a reason for hiding this comment

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

I'm still unclear about including a gauge histogram . As per my understanding , we will need to add postiveBuckets, negativeBuckets of type map[int]float64 as an argument (or use a generic parameter). Adding a boolean parameter will not suffice.

Copy link
Member

Choose a reason for hiding this comment

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

It sounds like you are thinking about a float histogram. That's something we might want to add later (ideally together with the option of creating classic float histograms). However, the use case for float histograms in instrumentation is very rare.

I am in fact talking about a gauge histogram. It merely requires to set a different MetricType in the MetricFamily. However, I've just realized that we currently have no way of communicating the difference of counter histogram (i.e. the "normal" histogram) and gauge histogram during metrics collection. For that, we needed Desc to have a metric type field (which was the plan for quite some time).

So I'm pretty sure we cannot solve this problem in this PR, because it needed too much re-architecting.

But if we assume that the difference between counter histogram and gauge histogram will be in the Desc, we actually don't need a bool parameter here, but just have to later allow the option of creating "typed" Descs.

In conclusion, let's not do gauge histograms right now.

Copy link
Author

Choose a reason for hiding this comment

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

That's true. I was indeed confused about a float histogram and gauge histogram.

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.

Thanks @shivanthzen for tackling this, and thanks for the review so far @krajorama and @ArthurSens . If I haven't commented on a previous review comment, I agree with it. :)

zeroBucket uint64
}

func NewConstNativeHistogram(desc *Desc, count uint64, sum float64, postiveBuckets, negativeBuckets map[int]int64, zeroBucket uint64,
Copy link
Member

Choose a reason for hiding this comment

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

In fact, I do actually think that exposing a gauge histogram will be one of the common use cases for using this function. So let's maybe add a bool argument here so that the user can switch between counter and gauge histogram.

zeroBucket uint64
}

func NewConstNativeHistogram(desc *Desc, count uint64, sum float64, postiveBuckets, negativeBuckets map[int]int64, zeroBucket uint64,
Copy link
Member

Choose a reason for hiding this comment

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

Style nit: Prometheus code usually does a line break after the opening parenthesis in case of multiline argument lists.

prometheus/histogram_test.go Outdated Show resolved Hide resolved
prometheus/histogram_test.go Show resolved Hide resolved
prometheus/histogram.go Show resolved Hide resolved
@shivanthzen
Copy link
Author

Thanks for the comments. I'll resolve them this weekend

Copy link
Member

@ArthurSens ArthurSens left a comment

Choose a reason for hiding this comment

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

I had just a quick look today :)

prometheus/histogram.go Outdated Show resolved Hide resolved
Comment on lines 1882 to 1925
if err := validateCount(count, negativeBuckets, positiveBuckets, zeroBucket); err != nil {
return nil, err
}
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to validate Sum as well?

Copy link
Member

Choose a reason for hiding this comment

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

Not really. Sum could by anything. I mean, there could be consistency checks like the sum cannot be negative if there are only positive observations, or the sum has to be zero without any observations, and many more. But I don't know if that would be very helpful. We should only check for conditions that would make Prometheus reject the histogram upon ingestion.

return nil
}

func makeBucketsAny(buckets map[int]int64) ([]*dto.BucketSpan, []int64) {
Copy link
Member

Choose a reason for hiding this comment

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

hey, could you clarify what Any means here?

Copy link
Member

Choose a reason for hiding this comment

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

Probably "positive or negative".
I would just call it makeBuckets.

Copy link
Member

Choose a reason for hiding this comment

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

+1 for makeBuckets

Copy link
Author

Choose a reason for hiding this comment

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

Hmm, makebuckets alreadyexists in the namespace. I could do makeBucketfrommap

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.

Looks good in general.

Despite the frequent nit pickings about naming etc., I haven't yet checked the technical correctness very deeply yet (although it looks fine as far as I can see right now).

prometheus/histogram.go Outdated Show resolved Hide resolved
return nil
}

func NewConstNativeHistogram(
Copy link
Member

Choose a reason for hiding this comment

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

Needs doc comment.

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 have a version MustNewConstNativeHistogram (which wraps NewConstNativeHistogram, panicking where NewConstNativeHistogram would have returned an error).

Copy link
Author

Choose a reason for hiding this comment

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

Added the docstring

prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
Comment on lines 1882 to 1925
if err := validateCount(count, negativeBuckets, positiveBuckets, zeroBucket); err != nil {
return nil, err
}
Copy link
Member

Choose a reason for hiding this comment

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

Not really. Sum could by anything. I mean, there could be consistency checks like the sum cannot be negative if there are only positive observations, or the sum has to be zero without any observations, and many more. But I don't know if that would be very helpful. We should only check for conditions that would make Prometheus reject the histogram upon ingestion.

return nil
}

func makeBucketsAny(buckets map[int]int64) ([]*dto.BucketSpan, []int64) {
Copy link
Member

Choose a reason for hiding this comment

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

Probably "positive or negative".
I would just call it makeBuckets.

prometheus/histogram.go Outdated Show resolved Hide resolved
Copy link
Member

@ArthurSens ArthurSens left a comment

Choose a reason for hiding this comment

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

LGTM, but let's wait for an approval from Beorn or Krajo as well

Thanks for the work!

@ArthurSens
Copy link
Member

Oh, looks like some commits weren't signed and there's one test failing 🤔

The test seems to be flaky, it's also failing in other PRs

@shivanthzen
Copy link
Author

I can sign the commits once the PR is approved, It seems signing will need a force push

@beorn7
Copy link
Member

beorn7 commented Nov 2, 2024

I can sign the commits once the PR is approved, It seems signing will need a force push

Yes, it changes the commits and therefore needs a force-push. However, it is essential to sign before approval. That's kind of the point.

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.

Thanks for addressing the feedback.

I have no realized something about exemplars we had not discussed before.

I have also realized we cannot do gauge histograms right now. Needs more discussion and planning, which is out of scope of this PR.

prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram_test.go Outdated Show resolved Hide resolved
@shivanthzen
Copy link
Author

Force pushed with signed commits.

@ArthurSens
Copy link
Member

@shivanthzen, looks like you accidentally added unrelated commits to your PR, could you keep only the ones that you've authored here?

shivanthzen and others added 10 commits November 6, 2024 08:21
Signed-off-by: Shivanth <[email protected]>
Co-authored-by: Arthur Silva Sens <[email protected]>
Signed-off-by: Shivanth MP <[email protected]>
Signed-off-by: Shivanth <[email protected]>
Signed-off-by: Shivanth <[email protected]>
Signed-off-by: Shivanth <[email protected]>
Signed-off-by: Shivanth <[email protected]>
Signed-off-by: Shivanth <[email protected]>
Signed-off-by: Shivanth <[email protected]>
Signed-off-by: Shivanth <[email protected]>
Signed-off-by: Shivanth <[email protected]>
@shivanthzen
Copy link
Author

shivanthzen commented Nov 6, 2024

@ArthurSens Removed other commits from the branch

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.

Consider supporting constant native histograms
4 participants