2019-12-04 01:49:07 +03:00
|
|
|
package victoriametrics
|
|
|
|
|
|
|
|
import (
|
2021-01-27 00:56:50 +03:00
|
|
|
"io"
|
2019-12-04 01:49:07 +03:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
"github.com/unistack-org/micro/v3/meter"
|
2019-12-04 01:49:07 +03:00
|
|
|
)
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
type victoriametricsMeter struct {
|
|
|
|
set *metrics.Set
|
|
|
|
opts meter.Options
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
func NewMeter(opts ...meter.Option) meter.Meter {
|
|
|
|
return &victoriametricsMeter{set: metrics.NewSet(), opts: meter.NewOptions(opts...)}
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
2019-12-04 01:49:07 +03:00
|
|
|
|
2021-02-12 12:59:41 +03:00
|
|
|
func (r *victoriametricsMeter) Name() string {
|
|
|
|
return r.opts.Name
|
|
|
|
}
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
func (r *victoriametricsMeter) buildName(name string, opts ...meter.Option) string {
|
|
|
|
var b strings.Builder
|
2020-06-20 17:09:22 +03:00
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
options := r.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
2019-12-04 01:49:07 +03:00
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
if len(options.MetricPrefix) > 0 {
|
|
|
|
_, _ = b.WriteString(options.MetricPrefix)
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|
2021-01-27 00:56:50 +03:00
|
|
|
labelPrefix := false
|
|
|
|
if len(options.LabelPrefix) > 0 {
|
|
|
|
labelPrefix = true
|
|
|
|
}
|
|
|
|
_, _ = b.WriteString(name)
|
|
|
|
if options.Labels.Len() > 0 {
|
|
|
|
_, _ = b.WriteRune('{')
|
|
|
|
iter := options.Labels.Iter()
|
|
|
|
var k, v string
|
|
|
|
for idx := 0; iter.Next(&k, &v); idx++ {
|
|
|
|
if idx > 0 {
|
|
|
|
_, _ = b.WriteRune(',')
|
|
|
|
}
|
|
|
|
if labelPrefix {
|
|
|
|
_, _ = b.WriteString(options.LabelPrefix)
|
|
|
|
}
|
|
|
|
_, _ = b.WriteString(k)
|
|
|
|
_, _ = b.WriteString(`="`)
|
|
|
|
_, _ = b.WriteString(v)
|
|
|
|
_, _ = b.WriteString(`"`)
|
|
|
|
}
|
|
|
|
_, _ = b.WriteRune('}')
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
return b.String()
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
func (r *victoriametricsMeter) Counter(name string, opts ...meter.Option) meter.Counter {
|
|
|
|
return r.set.GetOrCreateCounter(r.buildName(name, opts...))
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
func (r *victoriametricsMeter) FloatCounter(name string, opts ...meter.Option) meter.FloatCounter {
|
|
|
|
return r.set.GetOrCreateFloatCounter(r.buildName(name, opts...))
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
func (r *victoriametricsMeter) Gauge(name string, f func() float64, opts ...meter.Option) meter.Gauge {
|
|
|
|
return r.set.GetOrCreateGauge(r.buildName(name, opts...), f)
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
func (r *victoriametricsMeter) Histogram(name string, opts ...meter.Option) meter.Histogram {
|
|
|
|
return r.set.GetOrCreateHistogram(r.buildName(name, opts...))
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
func (r *victoriametricsMeter) Summary(name string, opts ...meter.Option) meter.Summary {
|
|
|
|
return r.set.GetOrCreateSummary(r.buildName(name, opts...))
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
func (r *victoriametricsMeter) SummaryExt(name string, window time.Duration, quantiles []float64, opts ...meter.Option) meter.Summary {
|
|
|
|
return r.set.GetOrCreateSummaryExt(r.buildName(name, opts...), window, quantiles)
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
func (r *victoriametricsMeter) Set(opts ...meter.Option) meter.Meter {
|
|
|
|
m := &victoriametricsMeter{opts: meter.NewOptions(opts...), set: metrics.NewSet()}
|
|
|
|
return m
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
func (r *victoriametricsMeter) Init(opts ...meter.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&r.opts)
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|
2020-06-20 17:09:22 +03:00
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
return nil
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-02-18 16:01:13 +03:00
|
|
|
func (r *victoriametricsMeter) Write(w io.Writer, opts ...meter.Option) error {
|
|
|
|
options := r.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
r.set.WritePrometheus(w)
|
2021-02-18 16:01:13 +03:00
|
|
|
if options.WriteProcessMetrics {
|
2021-01-27 00:56:50 +03:00
|
|
|
metrics.WriteProcessMetrics(w)
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|
2021-02-18 16:01:13 +03:00
|
|
|
if options.WriteFDMetrics {
|
|
|
|
metrics.WriteFDMetrics(w)
|
|
|
|
}
|
2021-01-27 00:56:50 +03:00
|
|
|
return nil
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
func (r *victoriametricsMeter) Options() meter.Options {
|
|
|
|
return r.opts
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
func (r *victoriametricsMeter) String() string {
|
|
|
|
return "victoriametrics"
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|