2019-11-23 01:16:17 +03:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math"
|
2023-02-07 09:27:40 +03:00
|
|
|
"strings"
|
2019-11-23 01:16:17 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-11-25 14:03:26 +03:00
|
|
|
const (
|
2021-02-15 15:15:01 +03:00
|
|
|
e10Min = -9
|
|
|
|
e10Max = 18
|
|
|
|
bucketsPerDecimal = 18
|
|
|
|
decimalBucketsCount = e10Max - e10Min
|
|
|
|
bucketsCount = decimalBucketsCount * bucketsPerDecimal
|
2019-11-25 14:03:26 +03:00
|
|
|
)
|
|
|
|
|
2021-02-15 15:15:01 +03:00
|
|
|
var bucketMultiplier = math.Pow(10, 1.0/bucketsPerDecimal)
|
|
|
|
|
2019-11-25 14:03:26 +03:00
|
|
|
// Histogram is a histogram for non-negative values with automatically created buckets.
|
2019-11-23 01:16:17 +03:00
|
|
|
//
|
2019-11-27 15:01:15 +03:00
|
|
|
// See https://medium.com/@valyala/improving-histogram-usability-for-prometheus-and-grafana-bc7e5df0e350
|
|
|
|
//
|
2019-11-23 01:16:17 +03:00
|
|
|
// Each bucket contains a counter for values in the given range.
|
2019-11-25 14:03:26 +03:00
|
|
|
// Each non-empty bucket is exposed via the following metric:
|
2019-11-23 01:16:17 +03:00
|
|
|
//
|
2022-08-08 17:15:06 +03:00
|
|
|
// <metric_name>_bucket{<optional_tags>,vmrange="<start>...<end>"} <counter>
|
2019-11-23 01:16:17 +03:00
|
|
|
//
|
|
|
|
// Where:
|
|
|
|
//
|
2022-08-08 17:15:06 +03:00
|
|
|
// - <metric_name> is the metric name passed to NewHistogram
|
|
|
|
// - <optional_tags> is optional tags for the <metric_name>, which are passed to NewHistogram
|
|
|
|
// - <start> and <end> - start and end values for the given bucket
|
|
|
|
// - <counter> - the number of hits to the given bucket during Update* calls
|
2019-11-23 01:16:17 +03:00
|
|
|
//
|
2019-11-23 14:04:24 +03:00
|
|
|
// Histogram buckets can be converted to Prometheus-like buckets with `le` labels
|
2019-12-12 20:24:01 +03:00
|
|
|
// with `prometheus_buckets(<metric_name>_bucket)` function from PromQL extensions in VictoriaMetrics.
|
2020-02-11 00:05:09 +03:00
|
|
|
// (see https://github.com/VictoriaMetrics/VictoriaMetrics/wiki/MetricsQL ):
|
2019-11-23 01:16:17 +03:00
|
|
|
//
|
2022-08-08 17:15:06 +03:00
|
|
|
// prometheus_buckets(request_duration_bucket)
|
2019-11-23 01:16:17 +03:00
|
|
|
//
|
2019-11-23 14:04:24 +03:00
|
|
|
// Time series produced by the Histogram have better compression ratio comparing to
|
|
|
|
// Prometheus histogram buckets with `le` labels, since they don't include counters
|
|
|
|
// for all the previous buckets.
|
2019-11-25 14:03:26 +03:00
|
|
|
//
|
|
|
|
// Zero histogram is usable.
|
2019-11-23 01:16:17 +03:00
|
|
|
type Histogram struct {
|
2019-11-25 14:03:26 +03:00
|
|
|
// Mu gurantees synchronous update for all the counters and sum.
|
|
|
|
mu sync.Mutex
|
|
|
|
|
2021-02-15 15:15:01 +03:00
|
|
|
decimalBuckets [decimalBucketsCount]*[bucketsPerDecimal]uint64
|
2019-11-25 14:03:26 +03:00
|
|
|
|
|
|
|
lower uint64
|
|
|
|
upper uint64
|
|
|
|
|
|
|
|
sum float64
|
2023-02-07 09:27:40 +03:00
|
|
|
|
|
|
|
compatible bool
|
2019-11-25 14:03:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets the given histogram.
|
|
|
|
func (h *Histogram) Reset() {
|
|
|
|
h.mu.Lock()
|
2021-02-15 15:15:01 +03:00
|
|
|
for _, db := range h.decimalBuckets[:] {
|
|
|
|
if db == nil {
|
2019-11-25 14:03:26 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-02-15 15:15:01 +03:00
|
|
|
for i := range db[:] {
|
|
|
|
db[i] = 0
|
2019-11-25 14:03:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
h.lower = 0
|
|
|
|
h.upper = 0
|
2021-03-16 13:13:43 +03:00
|
|
|
h.sum = 0
|
2021-03-16 13:14:36 +03:00
|
|
|
h.mu.Unlock()
|
2019-11-25 14:03:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates h with v.
|
|
|
|
//
|
|
|
|
// Negative values and NaNs are ignored.
|
|
|
|
func (h *Histogram) Update(v float64) {
|
|
|
|
if math.IsNaN(v) || v < 0 {
|
|
|
|
// Skip NaNs and negative values.
|
|
|
|
return
|
|
|
|
}
|
2021-02-15 15:15:01 +03:00
|
|
|
bucketIdx := (math.Log10(v) - e10Min) * bucketsPerDecimal
|
2019-11-25 14:03:26 +03:00
|
|
|
h.mu.Lock()
|
|
|
|
h.sum += v
|
|
|
|
if bucketIdx < 0 {
|
2021-02-15 15:15:01 +03:00
|
|
|
h.lower++
|
|
|
|
} else if bucketIdx >= bucketsCount {
|
|
|
|
h.upper++
|
|
|
|
} else {
|
2021-02-28 18:39:11 +03:00
|
|
|
idx := uint(bucketIdx)
|
2021-03-01 17:46:59 +03:00
|
|
|
if bucketIdx == float64(idx) && idx > 0 {
|
2021-02-28 18:39:11 +03:00
|
|
|
// Edge case for 10^n values, which must go to the lower bucket
|
|
|
|
// according to Prometheus logic for `le`-based histograms.
|
|
|
|
idx--
|
|
|
|
}
|
|
|
|
decimalBucketIdx := idx / bucketsPerDecimal
|
|
|
|
offset := idx % bucketsPerDecimal
|
2021-02-15 15:15:01 +03:00
|
|
|
db := h.decimalBuckets[decimalBucketIdx]
|
|
|
|
if db == nil {
|
|
|
|
var b [bucketsPerDecimal]uint64
|
|
|
|
db = &b
|
|
|
|
h.decimalBuckets[decimalBucketIdx] = db
|
2019-11-25 14:03:26 +03:00
|
|
|
}
|
2021-02-15 15:15:01 +03:00
|
|
|
db[offset]++
|
2019-11-25 14:03:26 +03:00
|
|
|
}
|
2021-02-15 15:15:01 +03:00
|
|
|
h.mu.Unlock()
|
2019-11-25 14:03:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// VisitNonZeroBuckets calls f for all buckets with non-zero counters.
|
|
|
|
//
|
|
|
|
// vmrange contains "<start>...<end>" string with bucket bounds. The lower bound
|
|
|
|
// isn't included in the bucket, while the upper bound is included.
|
|
|
|
// This is required to be compatible with Prometheus-style histogram buckets
|
|
|
|
// with `le` (less or equal) labels.
|
|
|
|
func (h *Histogram) VisitNonZeroBuckets(f func(vmrange string, count uint64)) {
|
|
|
|
h.mu.Lock()
|
|
|
|
if h.lower > 0 {
|
2021-02-15 15:15:01 +03:00
|
|
|
f(lowerBucketRange, h.lower)
|
2019-11-25 14:03:26 +03:00
|
|
|
}
|
2021-02-15 15:15:01 +03:00
|
|
|
for decimalBucketIdx, db := range h.decimalBuckets[:] {
|
|
|
|
if db == nil {
|
2019-11-25 14:03:26 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-02-15 15:15:01 +03:00
|
|
|
for offset, count := range db[:] {
|
2019-11-25 14:03:26 +03:00
|
|
|
if count > 0 {
|
2021-02-15 15:15:01 +03:00
|
|
|
bucketIdx := decimalBucketIdx*bucketsPerDecimal + offset
|
|
|
|
vmrange := getVMRange(bucketIdx)
|
2019-11-25 14:03:26 +03:00
|
|
|
f(vmrange, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if h.upper > 0 {
|
2021-02-15 15:15:01 +03:00
|
|
|
f(upperBucketRange, h.upper)
|
2019-11-25 14:03:26 +03:00
|
|
|
}
|
2021-02-15 15:15:01 +03:00
|
|
|
h.mu.Unlock()
|
2019-11-23 01:16:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHistogram creates and returns new histogram with the given name.
|
|
|
|
//
|
|
|
|
// name must be valid Prometheus-compatible metric with possible labels.
|
|
|
|
// For instance,
|
|
|
|
//
|
2022-08-08 17:15:06 +03:00
|
|
|
// - foo
|
|
|
|
// - foo{bar="baz"}
|
|
|
|
// - foo{bar="baz",aaa="b"}
|
2019-11-23 01:16:17 +03:00
|
|
|
//
|
|
|
|
// The returned histogram is safe to use from concurrent goroutines.
|
|
|
|
func NewHistogram(name string) *Histogram {
|
2023-02-20 07:54:52 +03:00
|
|
|
return defaultSet.NewHistogram(name)
|
2019-11-23 01:16:17 +03:00
|
|
|
}
|
2023-02-07 10:38:08 +03:00
|
|
|
func NewCompatibleHistogram(name string) *Histogram {
|
2023-02-20 07:54:52 +03:00
|
|
|
return defaultSet.NewCompatibleHistogram(name)
|
2023-02-07 10:38:08 +03:00
|
|
|
}
|
2019-11-23 01:16:17 +03:00
|
|
|
|
|
|
|
// GetOrCreateHistogram returns registered histogram with the given name
|
|
|
|
// or creates new histogram if the registry doesn't contain histogram with
|
|
|
|
// the given name.
|
|
|
|
//
|
|
|
|
// name must be valid Prometheus-compatible metric with possible labels.
|
|
|
|
// For instance,
|
|
|
|
//
|
2022-08-08 17:15:06 +03:00
|
|
|
// - foo
|
|
|
|
// - foo{bar="baz"}
|
|
|
|
// - foo{bar="baz",aaa="b"}
|
2019-11-23 01:16:17 +03:00
|
|
|
//
|
|
|
|
// The returned histogram is safe to use from concurrent goroutines.
|
|
|
|
//
|
|
|
|
// Performance tip: prefer NewHistogram instead of GetOrCreateHistogram.
|
|
|
|
func GetOrCreateHistogram(name string) *Histogram {
|
2023-02-20 07:54:52 +03:00
|
|
|
return defaultSet.GetOrCreateHistogram(name)
|
2019-11-23 01:16:17 +03:00
|
|
|
}
|
2023-02-07 10:38:08 +03:00
|
|
|
func GetOrCreateCompatibleHistogram(name string) *Histogram {
|
2023-02-20 07:54:52 +03:00
|
|
|
return defaultSet.GetOrCreateCompatibleHistogram(name)
|
2023-02-07 10:38:08 +03:00
|
|
|
}
|
2019-11-23 01:16:17 +03:00
|
|
|
|
|
|
|
// UpdateDuration updates request duration based on the given startTime.
|
|
|
|
func (h *Histogram) UpdateDuration(startTime time.Time) {
|
|
|
|
d := time.Since(startTime).Seconds()
|
|
|
|
h.Update(d)
|
|
|
|
}
|
|
|
|
|
2021-02-15 15:15:01 +03:00
|
|
|
func getVMRange(bucketIdx int) string {
|
2019-11-25 14:03:26 +03:00
|
|
|
bucketRangesOnce.Do(initBucketRanges)
|
2021-02-15 15:15:01 +03:00
|
|
|
return bucketRanges[bucketIdx]
|
2019-11-24 00:58:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func initBucketRanges() {
|
2021-02-15 15:15:01 +03:00
|
|
|
v := math.Pow10(e10Min)
|
|
|
|
start := fmt.Sprintf("%.3e", v)
|
|
|
|
for i := 0; i < bucketsCount; i++ {
|
|
|
|
v *= bucketMultiplier
|
|
|
|
end := fmt.Sprintf("%.3e", v)
|
|
|
|
bucketRanges[i] = start + "..." + end
|
|
|
|
start = end
|
2019-11-24 00:58:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2021-02-15 15:15:01 +03:00
|
|
|
lowerBucketRange = fmt.Sprintf("0...%.3e", math.Pow10(e10Min))
|
|
|
|
upperBucketRange = fmt.Sprintf("%.3e...+Inf", math.Pow10(e10Max))
|
|
|
|
|
|
|
|
bucketRanges [bucketsCount]string
|
2019-11-24 00:58:18 +03:00
|
|
|
bucketRangesOnce sync.Once
|
|
|
|
)
|
|
|
|
|
2019-11-23 01:16:17 +03:00
|
|
|
func (h *Histogram) marshalTo(prefix string, w io.Writer) {
|
2023-02-07 09:27:40 +03:00
|
|
|
if h.compatible {
|
|
|
|
h.marshalToPrometheus(prefix, w)
|
|
|
|
return
|
|
|
|
}
|
2019-11-25 14:03:26 +03:00
|
|
|
countTotal := uint64(0)
|
|
|
|
h.VisitNonZeroBuckets(func(vmrange string, count uint64) {
|
|
|
|
tag := fmt.Sprintf("vmrange=%q", vmrange)
|
|
|
|
metricName := addTag(prefix, tag)
|
2021-02-15 15:15:01 +03:00
|
|
|
name, labels := splitMetricName(metricName)
|
|
|
|
fmt.Fprintf(w, "%s_bucket%s %d\n", name, labels, count)
|
2019-11-25 14:03:26 +03:00
|
|
|
countTotal += count
|
|
|
|
})
|
|
|
|
if countTotal == 0 {
|
2019-11-23 01:16:17 +03:00
|
|
|
return
|
|
|
|
}
|
2021-02-15 15:15:01 +03:00
|
|
|
name, labels := splitMetricName(prefix)
|
2020-03-13 13:36:02 +03:00
|
|
|
sum := h.getSum()
|
|
|
|
if float64(int64(sum)) == sum {
|
2021-02-15 15:15:01 +03:00
|
|
|
fmt.Fprintf(w, "%s_sum%s %d\n", name, labels, int64(sum))
|
2019-11-23 01:16:17 +03:00
|
|
|
} else {
|
2021-02-15 15:15:01 +03:00
|
|
|
fmt.Fprintf(w, "%s_sum%s %g\n", name, labels, sum)
|
2019-11-23 01:16:17 +03:00
|
|
|
}
|
2021-02-15 15:15:01 +03:00
|
|
|
fmt.Fprintf(w, "%s_count%s %d\n", name, labels, countTotal)
|
2019-11-23 01:16:17 +03:00
|
|
|
}
|
2023-02-07 09:27:40 +03:00
|
|
|
func (h *Histogram) marshalToPrometheus(prefix string, w io.Writer) {
|
|
|
|
countTotal := uint64(0)
|
2023-02-07 10:00:56 +03:00
|
|
|
inf := false
|
2023-02-07 09:27:40 +03:00
|
|
|
h.VisitNonZeroBuckets(func(vmrange string, count uint64) {
|
|
|
|
v := strings.Split(vmrange, "...")
|
|
|
|
if len(v) != 2 {
|
|
|
|
return
|
|
|
|
}
|
2023-02-07 10:00:56 +03:00
|
|
|
if v[1] == "+Inf" {
|
|
|
|
inf = true
|
|
|
|
}
|
2023-02-07 09:27:40 +03:00
|
|
|
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
|
|
|
|
}
|
2023-02-07 10:00:56 +03:00
|
|
|
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)
|
|
|
|
}
|
2023-02-07 09:27:40 +03:00
|
|
|
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)
|
|
|
|
}
|
2019-11-23 01:16:17 +03:00
|
|
|
|
2020-03-13 13:36:02 +03:00
|
|
|
func (h *Histogram) getSum() float64 {
|
|
|
|
h.mu.Lock()
|
|
|
|
sum := h.sum
|
|
|
|
h.mu.Unlock()
|
|
|
|
return sum
|
|
|
|
}
|