2019-04-08 16:29:16 +03:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-04-11 12:59:53 +03:00
|
|
|
"math"
|
2019-06-28 14:14:47 +03:00
|
|
|
"strings"
|
2019-04-08 16:29:16 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/valyala/histogram"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultSummaryWindow = 5 * time.Minute
|
|
|
|
|
|
|
|
var defaultSummaryQuantiles = []float64{0.5, 0.9, 0.97, 0.99, 1}
|
|
|
|
|
|
|
|
// Summary implements summary.
|
|
|
|
type Summary struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
|
|
|
|
curr *histogram.Fast
|
|
|
|
next *histogram.Fast
|
|
|
|
|
|
|
|
quantiles []float64
|
|
|
|
quantileValues []float64
|
2019-04-11 14:03:30 +03:00
|
|
|
|
2019-06-28 13:58:37 +03:00
|
|
|
sum float64
|
|
|
|
count uint64
|
|
|
|
|
2019-04-11 14:03:30 +03:00
|
|
|
window time.Duration
|
2019-04-08 16:29:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSummary creates and returns new summary with the given name.
|
|
|
|
//
|
2019-09-30 00:06:17 +03:00
|
|
|
// name must be valid Prometheus-compatible metric with possible labels.
|
2019-04-08 16:29:16 +03:00
|
|
|
// For instance,
|
|
|
|
//
|
2022-08-08 17:15:06 +03:00
|
|
|
// - foo
|
|
|
|
// - foo{bar="baz"}
|
|
|
|
// - foo{bar="baz",aaa="b"}
|
2019-04-08 16:29:16 +03:00
|
|
|
//
|
|
|
|
// The returned summary is safe to use from concurrent goroutines.
|
|
|
|
func NewSummary(name string) *Summary {
|
2019-06-01 23:18:41 +03:00
|
|
|
return defaultSet.NewSummary(name)
|
2019-04-08 16:29:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSummaryExt creates and returns new summary with the given name,
|
|
|
|
// window and quantiles.
|
|
|
|
//
|
2019-09-30 00:06:17 +03:00
|
|
|
// name must be valid Prometheus-compatible metric with possible labels.
|
2019-04-08 16:29:16 +03:00
|
|
|
// For instance,
|
|
|
|
//
|
2022-08-08 17:15:06 +03:00
|
|
|
// - foo
|
|
|
|
// - foo{bar="baz"}
|
|
|
|
// - foo{bar="baz",aaa="b"}
|
2019-04-08 16:29:16 +03:00
|
|
|
//
|
|
|
|
// The returned summary is safe to use from concurrent goroutines.
|
|
|
|
func NewSummaryExt(name string, window time.Duration, quantiles []float64) *Summary {
|
2019-06-01 23:18:41 +03:00
|
|
|
return defaultSet.NewSummaryExt(name, window, quantiles)
|
2019-04-11 14:03:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newSummary(window time.Duration, quantiles []float64) *Summary {
|
|
|
|
// Make a copy of quantiles in order to prevent from their modification by the caller.
|
|
|
|
quantiles = append([]float64{}, quantiles...)
|
2019-04-11 12:59:53 +03:00
|
|
|
validateQuantiles(quantiles)
|
2019-06-01 23:18:41 +03:00
|
|
|
sm := &Summary{
|
2019-04-08 16:29:16 +03:00
|
|
|
curr: histogram.NewFast(),
|
|
|
|
next: histogram.NewFast(),
|
|
|
|
quantiles: quantiles,
|
|
|
|
quantileValues: make([]float64, len(quantiles)),
|
2019-04-11 14:03:30 +03:00
|
|
|
window: window,
|
2019-04-08 16:29:16 +03:00
|
|
|
}
|
2019-06-01 23:18:41 +03:00
|
|
|
return sm
|
2019-04-08 16:29:16 +03:00
|
|
|
}
|
|
|
|
|
2019-04-11 12:59:53 +03:00
|
|
|
func validateQuantiles(quantiles []float64) {
|
|
|
|
for _, q := range quantiles {
|
|
|
|
if q < 0 || q > 1 {
|
|
|
|
panic(fmt.Errorf("BUG: quantile must be in the range [0..1]; got %v", q))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 16:29:16 +03:00
|
|
|
// Update updates the summary.
|
2019-06-01 23:18:41 +03:00
|
|
|
func (sm *Summary) Update(v float64) {
|
|
|
|
sm.mu.Lock()
|
|
|
|
sm.curr.Update(v)
|
|
|
|
sm.next.Update(v)
|
2019-06-28 13:58:37 +03:00
|
|
|
sm.sum += v
|
|
|
|
sm.count++
|
2019-06-01 23:18:41 +03:00
|
|
|
sm.mu.Unlock()
|
2019-04-08 16:29:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDuration updates request duration based on the given startTime.
|
2019-06-01 23:18:41 +03:00
|
|
|
func (sm *Summary) UpdateDuration(startTime time.Time) {
|
2019-04-08 16:29:16 +03:00
|
|
|
d := time.Since(startTime).Seconds()
|
2019-06-01 23:18:41 +03:00
|
|
|
sm.Update(d)
|
2019-04-08 16:29:16 +03:00
|
|
|
}
|
|
|
|
|
2019-06-01 23:18:41 +03:00
|
|
|
func (sm *Summary) marshalTo(prefix string, w io.Writer) {
|
2019-06-28 13:58:37 +03:00
|
|
|
// Marshal only *_sum and *_count values.
|
|
|
|
// Quantile values should be already updated by the caller via sm.updateQuantiles() call.
|
2019-06-01 23:18:41 +03:00
|
|
|
// sm.quantileValues will be marshaled later via quantileValue.marshalTo.
|
2019-06-28 13:58:37 +03:00
|
|
|
sm.mu.Lock()
|
|
|
|
sum := sm.sum
|
|
|
|
count := sm.count
|
|
|
|
sm.mu.Unlock()
|
|
|
|
|
|
|
|
if count > 0 {
|
2019-06-28 14:14:47 +03:00
|
|
|
name, filters := splitMetricName(prefix)
|
2019-08-13 13:24:46 +03:00
|
|
|
if float64(int64(sum)) == sum {
|
|
|
|
// Marshal integer sum without scientific notation
|
|
|
|
fmt.Fprintf(w, "%s_sum%s %d\n", name, filters, int64(sum))
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "%s_sum%s %g\n", name, filters, sum)
|
|
|
|
}
|
2019-06-28 14:14:47 +03:00
|
|
|
fmt.Fprintf(w, "%s_count%s %d\n", name, filters, count)
|
2019-06-28 13:58:37 +03:00
|
|
|
}
|
2019-04-08 16:29:16 +03:00
|
|
|
}
|
|
|
|
|
2023-12-19 01:36:54 +01:00
|
|
|
func (sm *Summary) metricType() string {
|
|
|
|
return "summary"
|
|
|
|
}
|
|
|
|
|
2019-06-28 14:14:47 +03:00
|
|
|
func splitMetricName(name string) (string, string) {
|
|
|
|
n := strings.IndexByte(name, '{')
|
|
|
|
if n < 0 {
|
|
|
|
return name, ""
|
|
|
|
}
|
|
|
|
return name[:n], name[n:]
|
|
|
|
}
|
|
|
|
|
2019-06-01 23:18:41 +03:00
|
|
|
func (sm *Summary) updateQuantiles() {
|
|
|
|
sm.mu.Lock()
|
|
|
|
sm.quantileValues = sm.curr.Quantiles(sm.quantileValues[:0], sm.quantiles)
|
|
|
|
sm.mu.Unlock()
|
2019-04-08 16:29:16 +03:00
|
|
|
}
|
|
|
|
|
2019-04-11 14:03:30 +03:00
|
|
|
// GetOrCreateSummary returns registered summary with the given name
|
|
|
|
// or creates new summary if the registry doesn't contain summary with
|
|
|
|
// the given name.
|
|
|
|
//
|
2019-09-30 00:06:17 +03:00
|
|
|
// name must be valid Prometheus-compatible metric with possible labels.
|
2019-04-11 14:03:30 +03:00
|
|
|
// For instance,
|
|
|
|
//
|
2022-08-08 17:15:06 +03:00
|
|
|
// - foo
|
|
|
|
// - foo{bar="baz"}
|
|
|
|
// - foo{bar="baz",aaa="b"}
|
2019-04-11 14:03:30 +03:00
|
|
|
//
|
|
|
|
// The returned summary is safe to use from concurrent goroutines.
|
|
|
|
//
|
|
|
|
// Performance tip: prefer NewSummary instead of GetOrCreateSummary.
|
|
|
|
func GetOrCreateSummary(name string) *Summary {
|
2019-06-01 23:18:41 +03:00
|
|
|
return defaultSet.GetOrCreateSummary(name)
|
2019-04-11 14:03:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrCreateSummaryExt returns registered summary with the given name,
|
|
|
|
// window and quantiles or creates new summary if the registry doesn't
|
|
|
|
// contain summary with the given name.
|
|
|
|
//
|
2019-09-30 00:06:17 +03:00
|
|
|
// name must be valid Prometheus-compatible metric with possible labels.
|
2019-04-11 14:03:30 +03:00
|
|
|
// For instance,
|
|
|
|
//
|
2022-08-08 17:15:06 +03:00
|
|
|
// - foo
|
|
|
|
// - foo{bar="baz"}
|
|
|
|
// - foo{bar="baz",aaa="b"}
|
2019-04-11 14:03:30 +03:00
|
|
|
//
|
|
|
|
// The returned summary is safe to use from concurrent goroutines.
|
|
|
|
//
|
|
|
|
// Performance tip: prefer NewSummaryExt instead of GetOrCreateSummaryExt.
|
|
|
|
func GetOrCreateSummaryExt(name string, window time.Duration, quantiles []float64) *Summary {
|
2019-06-01 23:18:41 +03:00
|
|
|
return defaultSet.GetOrCreateSummaryExt(name, window, quantiles)
|
2019-04-11 14:03:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func isEqualQuantiles(a, b []float64) bool {
|
|
|
|
// Do not use relfect.DeepEqual, since it is slower than the direct comparison.
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range a {
|
|
|
|
if a[i] != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-04-08 16:29:16 +03:00
|
|
|
type quantileValue struct {
|
2019-06-01 23:18:41 +03:00
|
|
|
sm *Summary
|
2019-04-08 16:29:16 +03:00
|
|
|
idx int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qv *quantileValue) marshalTo(prefix string, w io.Writer) {
|
2019-06-01 23:18:41 +03:00
|
|
|
qv.sm.mu.Lock()
|
|
|
|
v := qv.sm.quantileValues[qv.idx]
|
|
|
|
qv.sm.mu.Unlock()
|
2019-04-11 12:59:53 +03:00
|
|
|
if !math.IsNaN(v) {
|
|
|
|
fmt.Fprintf(w, "%s %g\n", prefix, v)
|
|
|
|
}
|
2019-04-08 16:29:16 +03:00
|
|
|
}
|
|
|
|
|
2023-12-19 01:36:54 +01:00
|
|
|
func (qv *quantileValue) metricType() string {
|
|
|
|
return "unsupported"
|
|
|
|
}
|
|
|
|
|
2019-04-08 16:29:16 +03:00
|
|
|
func addTag(name, tag string) string {
|
|
|
|
if len(name) == 0 || name[len(name)-1] != '}' {
|
|
|
|
return fmt.Sprintf("%s{%s}", name, tag)
|
|
|
|
}
|
2019-04-11 12:59:53 +03:00
|
|
|
return fmt.Sprintf("%s,%s}", name[:len(name)-1], tag)
|
2019-04-08 16:29:16 +03:00
|
|
|
}
|
|
|
|
|
2020-08-07 10:59:44 +01:00
|
|
|
func registerSummaryLocked(sm *Summary) {
|
2019-06-01 23:18:41 +03:00
|
|
|
window := sm.window
|
2019-04-08 16:29:16 +03:00
|
|
|
summariesLock.Lock()
|
2019-06-01 23:18:41 +03:00
|
|
|
summaries[window] = append(summaries[window], sm)
|
2019-04-08 16:29:16 +03:00
|
|
|
if len(summaries[window]) == 1 {
|
|
|
|
go summariesSwapCron(window)
|
|
|
|
}
|
|
|
|
summariesLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-02-26 20:37:47 +02:00
|
|
|
func unregisterSummary(sm *Summary) {
|
|
|
|
window := sm.window
|
|
|
|
summariesLock.Lock()
|
|
|
|
sms := summaries[window]
|
|
|
|
found := false
|
|
|
|
for i, xsm := range sms {
|
|
|
|
if xsm == sm {
|
|
|
|
sms = append(sms[:i], sms[i+1:]...)
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Errorf("BUG: cannot find registered summary %p", sm))
|
|
|
|
}
|
|
|
|
summaries[window] = sms
|
|
|
|
summariesLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-04-08 16:29:16 +03:00
|
|
|
func summariesSwapCron(window time.Duration) {
|
|
|
|
for {
|
|
|
|
time.Sleep(window / 2)
|
|
|
|
summariesLock.Lock()
|
2019-06-01 23:18:41 +03:00
|
|
|
for _, sm := range summaries[window] {
|
|
|
|
sm.mu.Lock()
|
|
|
|
tmp := sm.curr
|
|
|
|
sm.curr = sm.next
|
|
|
|
sm.next = tmp
|
|
|
|
sm.next.Reset()
|
|
|
|
sm.mu.Unlock()
|
2019-04-08 16:29:16 +03:00
|
|
|
}
|
|
|
|
summariesLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
summaries = map[time.Duration][]*Summary{}
|
|
|
|
summariesLock sync.Mutex
|
|
|
|
)
|