2021-01-22 18:21:40 +03:00
|
|
|
// Package meter is for instrumentation
|
|
|
|
package meter
|
|
|
|
|
|
|
|
import (
|
2021-01-27 00:54:19 +03:00
|
|
|
"io"
|
|
|
|
"sort"
|
2021-01-22 18:21:40 +03:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-01-22 19:18:28 +03:00
|
|
|
// DefaultMeter is the default meter
|
|
|
|
DefaultMeter Meter = NewMeter()
|
|
|
|
// DefaultAddress data will be made available on this host:port
|
|
|
|
DefaultAddress = ":9090"
|
|
|
|
// DefaultPath the meter endpoint where the Meter data will be made available
|
|
|
|
DefaultPath = "/metrics"
|
2021-02-14 16:16:01 +03:00
|
|
|
// DefaultMetricPrefix holds the string that prepends to all metrics
|
2021-01-22 19:18:28 +03:00
|
|
|
DefaultMetricPrefix = "micro_"
|
2021-02-14 16:16:01 +03:00
|
|
|
// DefaultLabelPrefix holds the string that prepends to all labels
|
2021-01-22 19:18:28 +03:00
|
|
|
DefaultLabelPrefix = "micro_"
|
2021-02-14 16:16:01 +03:00
|
|
|
// DefaultSummaryQuantiles is the default spread of stats for summary
|
|
|
|
DefaultSummaryQuantiles = []float64{0.5, 0.9, 0.97, 0.99, 1}
|
|
|
|
// DefaultSummaryWindow is the default window for summary
|
|
|
|
DefaultSummaryWindow = 5 * time.Minute
|
2021-01-22 18:21:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Meter is an interface for collecting and instrumenting metrics
|
|
|
|
type Meter interface {
|
2021-01-29 13:17:32 +03:00
|
|
|
Name() string
|
2021-02-14 16:16:01 +03:00
|
|
|
Init(opts ...Option) error
|
|
|
|
Counter(name string, opts ...Option) Counter
|
|
|
|
FloatCounter(name string, opts ...Option) FloatCounter
|
|
|
|
Gauge(name string, fn func() float64, opts ...Option) Gauge
|
|
|
|
Set(opts ...Option) Meter
|
|
|
|
Histogram(name string, opts ...Option) Histogram
|
|
|
|
Summary(name string, opts ...Option) Summary
|
|
|
|
SummaryExt(name string, window time.Duration, quantiles []float64, opts ...Option) Summary
|
|
|
|
Write(w io.Writer, opts ...Option) error
|
2021-01-22 18:21:40 +03:00
|
|
|
Options() Options
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Counter is a counter
|
|
|
|
type Counter interface {
|
|
|
|
Add(int)
|
|
|
|
Dec()
|
|
|
|
Get() uint64
|
|
|
|
Inc()
|
|
|
|
Set(uint64)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FloatCounter is a float64 counter
|
|
|
|
type FloatCounter interface {
|
|
|
|
Add(float64)
|
|
|
|
Get() float64
|
|
|
|
Set(float64)
|
|
|
|
Sub(float64)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gauge is a float64 gauge
|
|
|
|
type Gauge interface {
|
|
|
|
Get() float64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Histogram is a histogram for non-negative values with automatically created buckets
|
|
|
|
type Histogram interface {
|
|
|
|
Reset()
|
|
|
|
Update(float64)
|
|
|
|
UpdateDuration(time.Time)
|
|
|
|
// VisitNonZeroBuckets(f func(vmrange string, count uint64))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Summary is the summary
|
|
|
|
type Summary interface {
|
|
|
|
Update(float64)
|
|
|
|
UpdateDuration(time.Time)
|
|
|
|
}
|
2021-01-27 00:54:19 +03:00
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// Labels holds the metrics labels with k, v
|
2021-01-27 00:54:19 +03:00
|
|
|
type Labels struct {
|
|
|
|
keys []string
|
|
|
|
vals []string
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:57:42 +03:00
|
|
|
// Append adds labels to label set
|
|
|
|
func (ls Labels) Append(nls Labels) Labels {
|
|
|
|
for n := range nls.keys {
|
|
|
|
ls.keys = append(ls.keys, nls.keys[n])
|
|
|
|
ls.vals = append(ls.vals, nls.vals[n])
|
|
|
|
}
|
|
|
|
return ls
|
|
|
|
}
|
2021-02-14 16:16:01 +03:00
|
|
|
|
2021-02-18 15:57:42 +03:00
|
|
|
// Len returns number of labels
|
|
|
|
func (ls Labels) Len() int {
|
|
|
|
return len(ls.keys)
|
2021-02-14 16:16:01 +03:00
|
|
|
}
|
|
|
|
|
2021-02-18 15:57:42 +03:00
|
|
|
type labels Labels
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
func (ls labels) Len() int {
|
2021-01-27 00:54:19 +03:00
|
|
|
return len(ls.keys)
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:57:42 +03:00
|
|
|
func (ls labels) Sort() {
|
|
|
|
sort.Sort(ls)
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
func (ls labels) Swap(i, j int) {
|
2021-01-27 00:54:19 +03:00
|
|
|
ls.keys[i], ls.keys[j] = ls.keys[j], ls.keys[i]
|
|
|
|
ls.vals[i], ls.vals[j] = ls.vals[j], ls.vals[i]
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
func (ls labels) Less(i, j int) bool {
|
2021-02-18 15:57:42 +03:00
|
|
|
return ls.keys[i] < ls.keys[j]
|
2021-02-18 14:41:51 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// LabelIter holds the
|
2021-01-27 00:54:19 +03:00
|
|
|
type LabelIter struct {
|
|
|
|
labels Labels
|
|
|
|
cnt int
|
|
|
|
cur int
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// Iter returns labels iterator
|
2021-01-27 00:54:19 +03:00
|
|
|
func (ls Labels) Iter() *LabelIter {
|
2021-02-18 15:57:42 +03:00
|
|
|
labels(ls).Sort()
|
2021-01-27 00:54:19 +03:00
|
|
|
return &LabelIter{labels: ls, cnt: len(ls.keys)}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// Next advance itarator to new pos
|
2021-01-27 00:54:19 +03:00
|
|
|
func (iter *LabelIter) Next(k, v *string) bool {
|
|
|
|
if iter.cur+1 > iter.cnt {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
*k = iter.labels.keys[iter.cur]
|
|
|
|
*v = iter.labels.vals[iter.cur]
|
|
|
|
iter.cur++
|
|
|
|
return true
|
|
|
|
}
|