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"
|
|
|
|
|
2024-11-09 19:49:45 +03:00
|
|
|
"go.unistack.org/metrics"
|
2021-10-09 01:14:16 +03:00
|
|
|
"go.unistack.org/micro/v3/meter"
|
2019-12-04 01:49:07 +03:00
|
|
|
)
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
type victoriametricsMeter struct {
|
2024-11-09 19:49:45 +03:00
|
|
|
set *metrics.Set
|
|
|
|
opts meter.Options
|
|
|
|
prometheusCompat bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type prometheusCompatKey struct{}
|
|
|
|
|
|
|
|
func PrometheusCompat(b bool) meter.Option {
|
|
|
|
return meter.SetOption(prometheusCompatKey{}, b)
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
func NewMeter(opts ...meter.Option) meter.Meter {
|
2024-11-09 19:49:45 +03:00
|
|
|
m := &victoriametricsMeter{set: metrics.NewSet(), opts: meter.NewOptions(opts...)}
|
2024-11-10 17:49:01 +03:00
|
|
|
if v, ok := m.opts.Context.Value(prometheusCompatKey{}).(bool); ok {
|
|
|
|
m.prometheusCompat = v
|
2024-11-09 19:49:45 +03:00
|
|
|
}
|
|
|
|
return m
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
2019-12-04 01:49:07 +03:00
|
|
|
|
2024-11-10 17:49:01 +03:00
|
|
|
func (m *victoriametricsMeter) Name() string {
|
|
|
|
return m.opts.Name
|
2021-02-12 12:59:41 +03:00
|
|
|
}
|
|
|
|
|
2024-11-10 17:49:01 +03:00
|
|
|
func (m *victoriametricsMeter) Clone(opts ...meter.Option) meter.Meter {
|
|
|
|
options := m.opts
|
2021-10-09 23:56:12 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2024-11-10 17:49:01 +03:00
|
|
|
nm := &victoriametricsMeter{set: m.set, opts: options, prometheusCompat: m.prometheusCompat}
|
|
|
|
if v, ok := m.opts.Context.Value(prometheusCompatKey{}).(bool); ok {
|
|
|
|
m.prometheusCompat = v
|
|
|
|
}
|
|
|
|
return nm
|
2021-10-09 23:56:12 +03:00
|
|
|
}
|
|
|
|
|
2024-11-10 17:49:01 +03:00
|
|
|
func (m *victoriametricsMeter) buildName(name string, labels ...string) string {
|
|
|
|
nl := len(m.opts.Labels) + len(labels)
|
2021-10-10 00:35:16 +03:00
|
|
|
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)
|
2024-11-10 17:49:01 +03:00
|
|
|
nlabels = append(nlabels, m.opts.Labels...)
|
2021-10-10 00:35:16 +03:00
|
|
|
nlabels = append(nlabels, labels...)
|
|
|
|
|
2021-07-21 16:37:51 +03:00
|
|
|
return meter.BuildName(name, nlabels...)
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2024-11-10 17:49:01 +03:00
|
|
|
func (m *victoriametricsMeter) Counter(name string, labels ...string) meter.Counter {
|
|
|
|
return m.set.GetOrCreateCounter(m.buildName(name, labels...))
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2024-11-10 17:49:01 +03:00
|
|
|
func (m *victoriametricsMeter) FloatCounter(name string, labels ...string) meter.FloatCounter {
|
|
|
|
return m.set.GetOrCreateFloatCounter(m.buildName(name, labels...))
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2024-11-10 17:49:01 +03:00
|
|
|
func (m *victoriametricsMeter) Gauge(name string, f func() float64, labels ...string) meter.Gauge {
|
|
|
|
return m.set.GetOrCreateGauge(m.buildName(name, labels...), f)
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2024-11-10 17:49:01 +03:00
|
|
|
func (m *victoriametricsMeter) Histogram(name string, labels ...string) meter.Histogram {
|
|
|
|
if m.prometheusCompat {
|
|
|
|
return m.set.GetOrCreateCompatibleHistogram(m.buildName(name, labels...))
|
2024-11-09 19:49:45 +03:00
|
|
|
}
|
2024-11-10 17:49:01 +03:00
|
|
|
return m.set.GetOrCreateHistogram(m.buildName(name, labels...))
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2024-11-10 17:49:01 +03:00
|
|
|
func (m *victoriametricsMeter) Summary(name string, labels ...string) meter.Summary {
|
|
|
|
return m.set.GetOrCreateSummary(m.buildName(name, labels...))
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2024-11-10 17:49:01 +03:00
|
|
|
func (m *victoriametricsMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary {
|
|
|
|
return m.set.GetOrCreateSummaryExt(m.buildName(name, labels...), window, quantiles)
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2024-11-10 17:49:01 +03:00
|
|
|
func (m *victoriametricsMeter) Set(opts ...meter.Option) meter.Meter {
|
|
|
|
nm := &victoriametricsMeter{opts: m.opts}
|
2021-04-28 01:13:08 +03:00
|
|
|
for _, o := range opts {
|
2024-11-10 17:49:01 +03:00
|
|
|
o(&nm.opts)
|
2021-04-28 01:13:08 +03:00
|
|
|
}
|
2024-11-10 17:49:01 +03:00
|
|
|
nm.set = metrics.NewSet()
|
|
|
|
if v, ok := nm.opts.Context.Value(prometheusCompatKey{}).(bool); ok {
|
|
|
|
nm.prometheusCompat = v
|
|
|
|
}
|
|
|
|
return nm
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2024-11-10 17:49:01 +03:00
|
|
|
func (m *victoriametricsMeter) Init(opts ...meter.Option) error {
|
2021-01-27 00:56:50 +03:00
|
|
|
for _, o := range opts {
|
2024-11-10 17:49:01 +03:00
|
|
|
o(&m.opts)
|
|
|
|
}
|
|
|
|
if v, ok := m.opts.Context.Value(prometheusCompatKey{}).(bool); ok {
|
|
|
|
m.prometheusCompat = v
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|
2021-01-27 00:56:50 +03:00
|
|
|
return nil
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2024-11-10 17:49:01 +03:00
|
|
|
func (m *victoriametricsMeter) Write(w io.Writer, opts ...meter.Option) error {
|
|
|
|
options := m.opts
|
2021-02-18 16:01:13 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2024-11-10 17:49:01 +03:00
|
|
|
m.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)
|
|
|
|
}
|
2024-11-09 19:49:45 +03:00
|
|
|
|
2021-01-27 00:56:50 +03:00
|
|
|
return nil
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2024-11-10 17:49:01 +03:00
|
|
|
func (m *victoriametricsMeter) Options() meter.Options {
|
|
|
|
return m.opts
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
2024-11-10 17:49:01 +03:00
|
|
|
func (m *victoriametricsMeter) String() string {
|
2021-01-27 00:56:50 +03:00
|
|
|
return "victoriametrics"
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|