Skip to content

histogram: expose FindBucket in Histogram API #3

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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions prometheus/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ type Histogram interface {
Observe(float64)
}

type HistogramInternal interface {
Histogram

ObserveInternal(float64, int)
FindBucket(v float64) int
}

// bucketLabel is used for the label that defines the upper bound of a
// bucket of a histogram ("le" -> "less or equal").
const bucketLabel = "le"
Expand Down Expand Up @@ -520,7 +527,7 @@ type HistogramVecOpts struct {
// The returned implementation also implements ExemplarObserver. It is safe to
// perform the corresponding type assertion. Exemplars are tracked separately
// for each bucket.
func NewHistogram(opts HistogramOpts) Histogram {
func NewHistogram(opts HistogramOpts) HistogramInternal {
return newHistogram(
NewDesc(
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
Expand All @@ -532,7 +539,7 @@ func NewHistogram(opts HistogramOpts) Histogram {
)
}

func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) Histogram {
func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) HistogramInternal {
if len(desc.variableLabels.names) != len(labelValues) {
panic(makeInconsistentCardinalityError(desc.fqName, desc.variableLabels.names, labelValues))
}
Expand Down Expand Up @@ -764,15 +771,15 @@ func (h *histogram) Desc() *Desc {
}

func (h *histogram) Observe(v float64) {
h.observe(v, h.findBucket(v))
h.ObserveInternal(v, h.FindBucket(v))
}

// ObserveWithExemplar should not be called in a high-frequency setting
// for a native histogram with configured exemplars. For this case,
// the implementation isn't lock-free and might suffer from lock contention.
func (h *histogram) ObserveWithExemplar(v float64, e Labels) {
i := h.findBucket(v)
h.observe(v, i)
i := h.FindBucket(v)
h.ObserveInternal(v, i)
h.updateExemplar(v, i, e)
}

Expand Down Expand Up @@ -861,9 +868,9 @@ func (h *histogram) Write(out *dto.Metric) error {
return nil
}

// findBucket returns the index of the bucket for the provided value, or
// FindBucket returns the index of the bucket for the provided value, or
// len(h.upperBounds) for the +Inf bucket.
func (h *histogram) findBucket(v float64) int {
func (h *histogram) FindBucket(v float64) int {
n := len(h.upperBounds)
if n == 0 {
return 0
Expand Down Expand Up @@ -896,8 +903,8 @@ func (h *histogram) findBucket(v float64) int {
return sort.SearchFloat64s(h.upperBounds, v)
}

// observe is the implementation for Observe without the findBucket part.
func (h *histogram) observe(v float64, bucket int) {
// ObserveInternal is the implementation for Observe without the FindBucket part.
func (h *histogram) ObserveInternal(v float64, bucket int) {
// Do not add to sparse buckets for NaN observations.
doSparse := h.nativeHistogramSchema > math.MinInt32 && !math.IsNaN(v)
// We increment h.countAndHotIdx so that the counter in the lower
Expand Down
Loading