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
|
|
|
"time"
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
2023-05-07 19:26:50 +03:00
|
|
|
"go.unistack.org/micro/v4/meter"
|
2023-08-16 15:32:09 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
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
|
|
|
}
|
|
|
|
|
2023-08-16 15:32:09 +03:00
|
|
|
func NewMeter(opts ...options.Option) meter.Meter {
|
2021-01-27 00:56:50 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-08-16 15:32:09 +03:00
|
|
|
func (r *victoriametricsMeter) Clone(opts ...options.Option) meter.Meter {
|
2021-10-09 23:56:12 +03:00
|
|
|
options := r.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return &victoriametricsMeter{set: r.set, opts: options}
|
|
|
|
}
|
|
|
|
|
2021-04-28 01:13:08 +03:00
|
|
|
func (r *victoriametricsMeter) buildName(name string, labels ...string) string {
|
|
|
|
if len(r.opts.MetricPrefix) > 0 {
|
2021-07-21 16:37:51 +03:00
|
|
|
name = r.opts.MetricPrefix + name
|
2021-01-27 00:56:50 +03:00
|
|
|
}
|
2021-07-23 08:59:50 +03:00
|
|
|
|
2021-10-10 00:35:16 +03:00
|
|
|
nl := len(r.opts.Labels) + len(labels)
|
|
|
|
if nl == 0 {
|
2021-07-23 08:59:50 +03:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2021-10-10 00:35:16 +03:00
|
|
|
nlabels := make([]string, 0, nl)
|
|
|
|
nlabels = append(nlabels, r.opts.Labels...)
|
|
|
|
nlabels = append(nlabels, labels...)
|
|
|
|
|
2021-07-21 16:37:51 +03:00
|
|
|
if len(r.opts.LabelPrefix) == 0 {
|
2021-10-10 00:35:16 +03:00
|
|
|
return meter.BuildName(name, nlabels...)
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-10-31 14:46:40 +03:00
|
|
|
for idx := 0; idx < nl; idx++ {
|
2021-07-21 16:37:51 +03:00
|
|
|
nlabels[idx] = r.opts.LabelPrefix + nlabels[idx]
|
2021-10-31 14:46:40 +03:00
|
|
|
idx++
|
2021-07-21 16:37:51 +03:00
|
|
|
}
|
|
|
|
return meter.BuildName(name, nlabels...)
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-04-28 01:13:08 +03:00
|
|
|
func (r *victoriametricsMeter) Counter(name string, labels ...string) meter.Counter {
|
|
|
|
return r.set.GetOrCreateCounter(r.buildName(name, labels...))
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-04-28 01:13:08 +03:00
|
|
|
func (r *victoriametricsMeter) FloatCounter(name string, labels ...string) meter.FloatCounter {
|
|
|
|
return r.set.GetOrCreateFloatCounter(r.buildName(name, labels...))
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-04-28 01:13:08 +03:00
|
|
|
func (r *victoriametricsMeter) Gauge(name string, f func() float64, labels ...string) meter.Gauge {
|
|
|
|
return r.set.GetOrCreateGauge(r.buildName(name, labels...), f)
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-04-28 01:13:08 +03:00
|
|
|
func (r *victoriametricsMeter) Histogram(name string, labels ...string) meter.Histogram {
|
|
|
|
return r.set.GetOrCreateHistogram(r.buildName(name, labels...))
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-04-28 01:13:08 +03:00
|
|
|
func (r *victoriametricsMeter) Summary(name string, labels ...string) meter.Summary {
|
|
|
|
return r.set.GetOrCreateSummary(r.buildName(name, labels...))
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-04-28 01:13:08 +03:00
|
|
|
func (r *victoriametricsMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary {
|
|
|
|
return r.set.GetOrCreateSummaryExt(r.buildName(name, labels...), window, quantiles)
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2023-08-16 15:32:09 +03:00
|
|
|
func (r *victoriametricsMeter) Set(opts ...options.Option) meter.Meter {
|
2021-04-28 01:13:08 +03:00
|
|
|
m := &victoriametricsMeter{opts: r.opts}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&m.opts)
|
|
|
|
}
|
|
|
|
m.set = metrics.NewSet()
|
2021-01-27 00:56:50 +03:00
|
|
|
return m
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2023-08-16 15:32:09 +03:00
|
|
|
func (r *victoriametricsMeter) Init(opts ...options.Option) error {
|
2021-01-27 00:56:50 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-08-16 15:32:09 +03:00
|
|
|
func (r *victoriametricsMeter) Write(w io.Writer, opts ...options.Option) error {
|
2021-02-18 16:01:13 +03:00
|
|
|
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
|
|
|
}
|