Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
dbcf52e590 | |||
4fef299e78 |
2
go.mod
2
go.mod
@ -5,7 +5,7 @@ go 1.22
|
|||||||
toolchain go1.23.2
|
toolchain go1.23.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/VictoriaMetrics/metrics v1.35.1
|
go.unistack.org/metrics v0.0.1
|
||||||
go.unistack.org/micro/v3 v3.10.100
|
go.unistack.org/micro/v3 v3.10.100
|
||||||
)
|
)
|
||||||
|
|
||||||
|
4
go.sum
4
go.sum
@ -1,9 +1,9 @@
|
|||||||
github.com/VictoriaMetrics/metrics v1.35.1 h1:o84wtBKQbzLdDy14XeskkCZih6anG+veZ1SwJHFGwrU=
|
|
||||||
github.com/VictoriaMetrics/metrics v1.35.1/go.mod h1:r7hveu6xMdUACXvB8TYdAj8WEsKzWB0EkpJN+RDtOf8=
|
|
||||||
github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8=
|
github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8=
|
||||||
github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ=
|
github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ=
|
||||||
github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ=
|
github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ=
|
||||||
github.com/valyala/histogram v1.2.0/go.mod h1:Hb4kBwb4UxsaNbbbh+RRz8ZR6pdodR57tzWUS3BUzXY=
|
github.com/valyala/histogram v1.2.0/go.mod h1:Hb4kBwb4UxsaNbbbh+RRz8ZR6pdodR57tzWUS3BUzXY=
|
||||||
|
go.unistack.org/metrics v0.0.1 h1:sCnGO059ZccGC/D34iRH121eSk+7ci5+OY9cl5K7GKY=
|
||||||
|
go.unistack.org/metrics v0.0.1/go.mod h1:1FY4R7EKJa9Oz2D6wlGScNerpl6igRs9Cx/3et4Rgs4=
|
||||||
go.unistack.org/micro/v3 v3.10.100 h1:yWOaU0ImCGm5k5MUzlIobJUOr+KLfrR/BoDZvcHyKxM=
|
go.unistack.org/micro/v3 v3.10.100 h1:yWOaU0ImCGm5k5MUzlIobJUOr+KLfrR/BoDZvcHyKxM=
|
||||||
go.unistack.org/micro/v3 v3.10.100/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
|
go.unistack.org/micro/v3 v3.10.100/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
|
||||||
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
|
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
|
||||||
|
@ -4,105 +4,129 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/VictoriaMetrics/metrics"
|
"go.unistack.org/metrics"
|
||||||
"go.unistack.org/micro/v3/meter"
|
"go.unistack.org/micro/v3/meter"
|
||||||
)
|
)
|
||||||
|
|
||||||
type victoriametricsMeter struct {
|
type victoriametricsMeter struct {
|
||||||
set *metrics.Set
|
set *metrics.Set
|
||||||
opts meter.Options
|
opts meter.Options
|
||||||
|
prometheusCompat bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type prometheusCompatKey struct{}
|
||||||
|
|
||||||
|
func PrometheusCompat(b bool) meter.Option {
|
||||||
|
return meter.SetOption(prometheusCompatKey{}, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMeter(opts ...meter.Option) meter.Meter {
|
func NewMeter(opts ...meter.Option) meter.Meter {
|
||||||
return &victoriametricsMeter{set: metrics.NewSet(), opts: meter.NewOptions(opts...)}
|
m := &victoriametricsMeter{set: metrics.NewSet(), opts: meter.NewOptions(opts...)}
|
||||||
|
if v, ok := m.opts.Context.Value(prometheusCompatKey{}).(bool); ok {
|
||||||
|
m.prometheusCompat = v
|
||||||
|
}
|
||||||
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *victoriametricsMeter) Name() string {
|
func (m *victoriametricsMeter) Name() string {
|
||||||
return r.opts.Name
|
return m.opts.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *victoriametricsMeter) Clone(opts ...meter.Option) meter.Meter {
|
func (m *victoriametricsMeter) Clone(opts ...meter.Option) meter.Meter {
|
||||||
options := r.opts
|
options := m.opts
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
return &victoriametricsMeter{set: r.set, opts: options}
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *victoriametricsMeter) buildName(name string, labels ...string) string {
|
func (m *victoriametricsMeter) buildName(name string, labels ...string) string {
|
||||||
nl := len(r.opts.Labels) + len(labels)
|
nl := len(m.opts.Labels) + len(labels)
|
||||||
if nl == 0 {
|
if nl == 0 {
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
nlabels := make([]string, 0, nl)
|
nlabels := make([]string, 0, nl)
|
||||||
nlabels = append(nlabels, r.opts.Labels...)
|
nlabels = append(nlabels, m.opts.Labels...)
|
||||||
nlabels = append(nlabels, labels...)
|
nlabels = append(nlabels, labels...)
|
||||||
|
|
||||||
return meter.BuildName(name, nlabels...)
|
return meter.BuildName(name, nlabels...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *victoriametricsMeter) Counter(name string, labels ...string) meter.Counter {
|
func (m *victoriametricsMeter) Counter(name string, labels ...string) meter.Counter {
|
||||||
return r.set.GetOrCreateCounter(r.buildName(name, labels...))
|
return m.set.GetOrCreateCounter(m.buildName(name, labels...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *victoriametricsMeter) FloatCounter(name string, labels ...string) meter.FloatCounter {
|
func (m *victoriametricsMeter) FloatCounter(name string, labels ...string) meter.FloatCounter {
|
||||||
return r.set.GetOrCreateFloatCounter(r.buildName(name, labels...))
|
return m.set.GetOrCreateFloatCounter(m.buildName(name, labels...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *victoriametricsMeter) Gauge(name string, f func() float64, labels ...string) meter.Gauge {
|
func (m *victoriametricsMeter) Gauge(name string, f func() float64, labels ...string) meter.Gauge {
|
||||||
return r.set.GetOrCreateGauge(r.buildName(name, labels...), f)
|
return m.set.GetOrCreateGauge(m.buildName(name, labels...), f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *victoriametricsMeter) Histogram(name string, labels ...string) meter.Histogram {
|
func (m *victoriametricsMeter) Histogram(name string, labels ...string) meter.Histogram {
|
||||||
return r.set.GetOrCreateHistogram(r.buildName(name, labels...))
|
if m.prometheusCompat {
|
||||||
|
return m.set.GetOrCreateCompatibleHistogram(m.buildName(name, labels...))
|
||||||
|
}
|
||||||
|
return m.set.GetOrCreateHistogram(m.buildName(name, labels...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *victoriametricsMeter) Summary(name string, labels ...string) meter.Summary {
|
func (m *victoriametricsMeter) Summary(name string, labels ...string) meter.Summary {
|
||||||
return r.set.GetOrCreateSummary(r.buildName(name, labels...))
|
return m.set.GetOrCreateSummary(m.buildName(name, labels...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *victoriametricsMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary {
|
func (m *victoriametricsMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary {
|
||||||
return r.set.GetOrCreateSummaryExt(r.buildName(name, labels...), window, quantiles)
|
return m.set.GetOrCreateSummaryExt(m.buildName(name, labels...), window, quantiles)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *victoriametricsMeter) Set(opts ...meter.Option) meter.Meter {
|
func (m *victoriametricsMeter) Set(opts ...meter.Option) meter.Meter {
|
||||||
m := &victoriametricsMeter{opts: r.opts}
|
nm := &victoriametricsMeter{opts: m.opts}
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&nm.opts)
|
||||||
|
}
|
||||||
|
nm.set = metrics.NewSet()
|
||||||
|
if v, ok := nm.opts.Context.Value(prometheusCompatKey{}).(bool); ok {
|
||||||
|
nm.prometheusCompat = v
|
||||||
|
}
|
||||||
|
return nm
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *victoriametricsMeter) Init(opts ...meter.Option) error {
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&m.opts)
|
o(&m.opts)
|
||||||
}
|
}
|
||||||
m.set = metrics.NewSet()
|
if v, ok := m.opts.Context.Value(prometheusCompatKey{}).(bool); ok {
|
||||||
return m
|
m.prometheusCompat = v
|
||||||
}
|
|
||||||
|
|
||||||
func (r *victoriametricsMeter) Init(opts ...meter.Option) error {
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&r.opts)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *victoriametricsMeter) Write(w io.Writer, opts ...meter.Option) error {
|
func (m *victoriametricsMeter) Write(w io.Writer, opts ...meter.Option) error {
|
||||||
options := r.opts
|
options := m.opts
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
r.set.WritePrometheus(w)
|
m.set.WritePrometheus(w)
|
||||||
if options.WriteProcessMetrics {
|
if options.WriteProcessMetrics {
|
||||||
metrics.WriteProcessMetrics(w)
|
metrics.WriteProcessMetrics(w)
|
||||||
}
|
}
|
||||||
if options.WriteFDMetrics {
|
if options.WriteFDMetrics {
|
||||||
metrics.WriteFDMetrics(w)
|
metrics.WriteFDMetrics(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *victoriametricsMeter) Options() meter.Options {
|
func (m *victoriametricsMeter) Options() meter.Options {
|
||||||
return r.opts
|
return m.opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *victoriametricsMeter) String() string {
|
func (m *victoriametricsMeter) String() string {
|
||||||
return "victoriametrics"
|
return "victoriametrics"
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package victoriametrics
|
package victoriametrics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,3 +17,13 @@ func TestBuildName(t *testing.T) {
|
|||||||
cnt := m.Counter("counter", "key", "val")
|
cnt := m.Counter("counter", "key", "val")
|
||||||
cnt.Inc()
|
cnt.Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPrometheusCompat(t *testing.T) {
|
||||||
|
m := NewMeter(PrometheusCompat(true))
|
||||||
|
m.Histogram("foo", "key", "val").Update(15)
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
|
_ = m.Write(buf)
|
||||||
|
t.Logf("\n%s", buf.Bytes())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user