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 a Prometheus-compatible Histogram function, and convert the vmrange label to the le label #45

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
52 changes: 50 additions & 2 deletions histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"math"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -55,6 +56,8 @@ type Histogram struct {
upper uint64

sum float64

compatible bool
}

// Reset resets the given histogram.
Expand Down Expand Up @@ -149,7 +152,10 @@ func (h *Histogram) VisitNonZeroBuckets(f func(vmrange string, count uint64)) {
//
// The returned histogram is safe to use from concurrent goroutines.
func NewHistogram(name string) *Histogram {
return defaultSet.NewHistogram(name)
return defaultSet.NewHistogram(name, false)
}
func NewCompatibleHistogram(name string) *Histogram {
return defaultSet.NewHistogram(name, true)
}

// GetOrCreateHistogram returns registered histogram with the given name
Expand All @@ -167,7 +173,10 @@ func NewHistogram(name string) *Histogram {
//
// Performance tip: prefer NewHistogram instead of GetOrCreateHistogram.
func GetOrCreateHistogram(name string) *Histogram {
return defaultSet.GetOrCreateHistogram(name)
return defaultSet.GetOrCreateHistogram(name, false)
}
func GetOrCreateCompatibleHistogram(name string) *Histogram {
return defaultSet.GetOrCreateHistogram(name, true)
}

// UpdateDuration updates request duration based on the given startTime.
Expand Down Expand Up @@ -201,6 +210,10 @@ var (
)

func (h *Histogram) marshalTo(prefix string, w io.Writer) {
if h.compatible {
h.marshalToPrometheus(prefix, w)
return
}
countTotal := uint64(0)
h.VisitNonZeroBuckets(func(vmrange string, count uint64) {
tag := fmt.Sprintf("vmrange=%q", vmrange)
Expand All @@ -221,6 +234,41 @@ func (h *Histogram) marshalTo(prefix string, w io.Writer) {
}
fmt.Fprintf(w, "%s_count%s %d\n", name, labels, countTotal)
}
func (h *Histogram) marshalToPrometheus(prefix string, w io.Writer) {
countTotal := uint64(0)
inf := false
h.VisitNonZeroBuckets(func(vmrange string, count uint64) {
v := strings.Split(vmrange, "...")
if len(v) != 2 {
return
}
if v[1] == "+Inf" {
inf = true
}
tag := fmt.Sprintf("le=%q", v[1])
metricName := addTag(prefix, tag)
name, labels := splitMetricName(metricName)
countTotal += count
fmt.Fprintf(w, "%s_bucket%s %d\n", name, labels, countTotal)
})
if countTotal == 0 {
return
}
if !inf {
tag := fmt.Sprintf("le=%q", "+Inf")
metricName := addTag(prefix, tag)
name, labels := splitMetricName(metricName)
fmt.Fprintf(w, "%s_bucket%s %d\n", name, labels, countTotal)
}
name, labels := splitMetricName(prefix)
sum := h.getSum()
if float64(int64(sum)) == sum {
fmt.Fprintf(w, "%s_sum%s %d\n", name, labels, int64(sum))
} else {
fmt.Fprintf(w, "%s_sum%s %g\n", name, labels, sum)
}
fmt.Fprintf(w, "%s_count%s %d\n", name, labels, countTotal)
}

func (h *Histogram) getSum() float64 {
h.mu.Lock()
Expand Down
4 changes: 2 additions & 2 deletions push.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func InitPushExt(pushURL string, interval time.Duration, extraLabels string, wri
pushesTotal := pushMetrics.GetOrCreateCounter(fmt.Sprintf(`metrics_push_total{url=%q}`, pushURLRedacted))
pushErrorsTotal := pushMetrics.GetOrCreateCounter(fmt.Sprintf(`metrics_push_errors_total{url=%q}`, pushURLRedacted))
bytesPushedTotal := pushMetrics.GetOrCreateCounter(fmt.Sprintf(`metrics_push_bytes_pushed_total{url=%q}`, pushURLRedacted))
pushDuration := pushMetrics.GetOrCreateHistogram(fmt.Sprintf(`metrics_push_duration_seconds{url=%q}`, pushURLRedacted))
pushBlockSize := pushMetrics.GetOrCreateHistogram(fmt.Sprintf(`metrics_push_block_size_bytes{url=%q}`, pushURLRedacted))
pushDuration := pushMetrics.GetOrCreateHistogram(fmt.Sprintf(`metrics_push_duration_seconds{url=%q}`, pushURLRedacted), false)
pushBlockSize := pushMetrics.GetOrCreateHistogram(fmt.Sprintf(`metrics_push_block_size_bytes{url=%q}`, pushURLRedacted), false)
pushMetrics.GetOrCreateFloatCounter(fmt.Sprintf(`metrics_push_interval_seconds{url=%q}`, pushURLRedacted)).Set(interval.Seconds())
go func() {
ticker := time.NewTicker(interval)
Expand Down
8 changes: 4 additions & 4 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func (s *Set) WritePrometheus(w io.Writer) {
// - foo{bar="baz",aaa="b"}
//
// The returned histogram is safe to use from concurrent goroutines.
func (s *Set) NewHistogram(name string) *Histogram {
h := &Histogram{}
func (s *Set) NewHistogram(name string, compatible bool) *Histogram {
greyireland marked this conversation as resolved.
Show resolved Hide resolved
h := &Histogram{compatible: compatible}
s.registerMetric(name, h)
return h
}
Expand All @@ -84,7 +84,7 @@ func (s *Set) NewHistogram(name string) *Histogram {
// The returned histogram is safe to use from concurrent goroutines.
//
// Performance tip: prefer NewHistogram instead of GetOrCreateHistogram.
func (s *Set) GetOrCreateHistogram(name string) *Histogram {
func (s *Set) GetOrCreateHistogram(name string, compatible bool) *Histogram {
s.mu.Lock()
nm := s.m[name]
s.mu.Unlock()
Expand All @@ -95,7 +95,7 @@ func (s *Set) GetOrCreateHistogram(name string) *Histogram {
}
nmNew := &namedMetric{
name: name,
metric: &Histogram{},
metric: &Histogram{compatible: compatible},
}
s.mu.Lock()
nm = s.m[name]
Expand Down
4 changes: 2 additions & 2 deletions set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestNewSet(t *testing.T) {
if sm == nil {
t.Fatalf("NewSummary returned nil")
}
h := s.NewHistogram(fmt.Sprintf("histogram_%d", j))
h := s.NewHistogram(fmt.Sprintf("histogram_%d", j), false)
if h == nil {
t.Fatalf("NewHistogram returned nil")
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestSetUnregisterAllMetrics(t *testing.T) {
for i := 0; i < 10; i++ {
_ = s.NewCounter(fmt.Sprintf("counter_%d", i))
_ = s.NewSummary(fmt.Sprintf("summary_%d", i))
_ = s.NewHistogram(fmt.Sprintf("histogram_%d", i))
_ = s.NewHistogram(fmt.Sprintf("histogram_%d", i), false)
_ = s.NewGauge(fmt.Sprintf("gauge_%d", i), func() float64 { return 0 })
expectedMetricsCount += 4
}
Expand Down