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

add possibility to record histogram with weight #252

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 10 additions & 2 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func (h *histogram) cachedReport() {
}
}

func (h *histogram) RecordValue(value float64) {
func (h *histogram) recordValueWithWeight(value float64, weight int64) {
if h.htype != valueHistogramType {
return
}
Expand All @@ -376,7 +376,15 @@ func (h *histogram) RecordValue(value float64) {
idx := sort.Search(len(h.buckets), func(i int) bool {
return h.buckets[i].valueUpperBound >= value
})
h.samples[idx].counter.Inc(1)
h.samples[idx].counter.Inc(weight)
}

func (h *histogram) RecordValue(value float64) {
h.recordValueWithWeight(value, 1)
}

func (h *histogram) RecordValueWithWeight(value float64, weight int64) {
h.recordValueWithWeight(value, weight)
}

func (h *histogram) RecordDuration(value time.Duration) {
Expand Down
3 changes: 3 additions & 0 deletions stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,14 @@ func TestHistogramValueSamples(t *testing.T) {
for i := 0; i < 5; i++ {
h.RecordValue(offset + rand.Float64()*10)
}
offset = 60
h.RecordValueWithWeight(offset+rand.Float64()*10, 2)

h.report(h.name, h.tags, r)

assert.Equal(t, 3, r.valueSamples[10.0])
assert.Equal(t, 5, r.valueSamples[60.0])
assert.Equal(t, 2, r.valueSamples[70.0])
assert.Equal(t, buckets, r.buckets)
}

Expand Down
11 changes: 8 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ import (
// all emitted values have a given prefix or set of tags.
//
// IMPORTANT: When using Prometheus reporters, users must take care to
// not create metrics from both parent scopes and subscopes
// that have the same metric name but different tag keys,
// as metric allocation will panic.
//
// not create metrics from both parent scopes and subscopes
// that have the same metric name but different tag keys,
// as metric allocation will panic.
type Scope interface {
// Counter returns the Counter object corresponding to the name.
Counter(name string) Counter
Expand Down Expand Up @@ -91,6 +92,10 @@ type Histogram interface {
// Will use the configured value buckets for the histogram.
RecordValue(value float64)

// RecordValueWithWeight records a specific value directly with a weight.
// Will use the configured value buckets for the histogram.
RecordValueWithWeight(value float64, weight int64)

// RecordDuration records a specific duration directly.
// Will use the configured duration buckets for the histogram.
RecordDuration(value time.Duration)
Expand Down