diff --git a/go.mod b/go.mod index 29f3084..db06616 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22 toolchain go1.23.2 require ( - github.com/VictoriaMetrics/metrics v1.35.1 + go.unistack.org/metrics v0.0.1 go.unistack.org/micro/v3 v3.10.100 ) diff --git a/go.sum b/go.sum index 167b108..c1162d9 100644 --- a/go.sum +++ b/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/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ= github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ= 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/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g= golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= diff --git a/victoriametrics.go b/victoriametrics.go index 634f9ab..a121cb6 100644 --- a/victoriametrics.go +++ b/victoriametrics.go @@ -4,17 +4,28 @@ import ( "io" "time" - "github.com/VictoriaMetrics/metrics" + "go.unistack.org/metrics" "go.unistack.org/micro/v3/meter" ) type victoriametricsMeter struct { - set *metrics.Set - opts meter.Options + set *metrics.Set + 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 { - 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 && v { + m.prometheusCompat = true + } + return m } func (r *victoriametricsMeter) Name() string { @@ -55,6 +66,9 @@ func (r *victoriametricsMeter) Gauge(name string, f func() float64, labels ...st } func (r *victoriametricsMeter) Histogram(name string, labels ...string) meter.Histogram { + if r.prometheusCompat { + return r.set.GetOrCreateCompatibleHistogram(r.buildName(name, labels...)) + } return r.set.GetOrCreateHistogram(r.buildName(name, labels...)) } @@ -96,6 +110,7 @@ func (r *victoriametricsMeter) Write(w io.Writer, opts ...meter.Option) error { if options.WriteFDMetrics { metrics.WriteFDMetrics(w) } + return nil } diff --git a/victoriametrics_test.go b/victoriametrics_test.go index 51bbcee..81f9e59 100644 --- a/victoriametrics_test.go +++ b/victoriametrics_test.go @@ -1,6 +1,7 @@ package victoriametrics import ( + "bytes" "testing" ) @@ -16,3 +17,13 @@ func TestBuildName(t *testing.T) { cnt := m.Counter("counter", "key", "val") 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()) +}