From 5a88bc973f909d1175bc4ff3121b4e21272287e1 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 20 Dec 2023 14:23:38 +0200 Subject: [PATCH] all: use Gauge instead of Counter for `*_config_last_reload_successful` metrics This allows exposing the correct TYPE metadata for these labels when the app runs with -metrics.exposeMetadata command-line flag. See https://github.com/VictoriaMetrics/metrics/pull/61#issuecomment-1860085508 for more details. This is follow-up for 326a77c6974454f1585ac662b762138727fa41f4 --- app/vmagent/remotewrite/remotewrite.go | 2 +- app/vmalert/main.go | 2 +- app/vmalert/main_test.go | 4 +-- app/vmauth/auth_config.go | 2 +- app/vminsert/common/streamaggr.go | 2 +- app/vminsert/relabel/relabel.go | 2 +- go.mod | 2 +- go.sum | 4 +-- lib/promscrape/scraper.go | 2 +- .../VictoriaMetrics/metrics/gauge.go | 31 +++++++++++++++---- .../github.com/VictoriaMetrics/metrics/set.go | 6 ---- vendor/modules.txt | 2 +- 12 files changed, 37 insertions(+), 24 deletions(-) diff --git a/app/vmagent/remotewrite/remotewrite.go b/app/vmagent/remotewrite/remotewrite.go index 04d38a4aa..678193cc5 100644 --- a/app/vmagent/remotewrite/remotewrite.go +++ b/app/vmagent/remotewrite/remotewrite.go @@ -276,7 +276,7 @@ func reloadRelabelConfigs() { var ( relabelConfigReloads = metrics.NewCounter(`vmagent_relabel_config_reloads_total`) relabelConfigReloadErrors = metrics.NewCounter(`vmagent_relabel_config_reloads_errors_total`) - relabelConfigSuccess = metrics.NewCounter(`vmagent_relabel_config_last_reload_successful`) + relabelConfigSuccess = metrics.NewGauge(`vmagent_relabel_config_last_reload_successful`, nil) relabelConfigTimestamp = metrics.NewCounter(`vmagent_relabel_config_last_reload_success_timestamp_seconds`) ) diff --git a/app/vmalert/main.go b/app/vmalert/main.go index 1c7a78408..a87897204 100644 --- a/app/vmalert/main.go +++ b/app/vmalert/main.go @@ -194,7 +194,7 @@ func main() { var ( configReloads = metrics.NewCounter(`vmalert_config_last_reload_total`) configReloadErrors = metrics.NewCounter(`vmalert_config_last_reload_errors_total`) - configSuccess = metrics.NewCounter(`vmalert_config_last_reload_successful`) + configSuccess = metrics.NewGauge(`vmalert_config_last_reload_successful`, nil) configTimestamp = metrics.NewCounter(`vmalert_config_last_reload_success_timestamp_seconds`) ) diff --git a/app/vmalert/main_test.go b/app/vmalert/main_test.go index 12531c66e..d6a289285 100644 --- a/app/vmalert/main_test.go +++ b/app/vmalert/main_test.go @@ -141,7 +141,7 @@ groups: t.Fatalf("expected to have config error %s; got nil instead", cErr) } if cfgSuc != 0 { - t.Fatalf("expected to have metric configSuccess to be set to 0; got %d instead", cfgSuc) + t.Fatalf("expected to have metric configSuccess to be set to 0; got %v instead", cfgSuc) } return } @@ -150,7 +150,7 @@ groups: t.Fatalf("unexpected config error: %s", cErr) } if cfgSuc != 1 { - t.Fatalf("expected to have metric configSuccess to be set to 1; got %d instead", cfgSuc) + t.Fatalf("expected to have metric configSuccess to be set to 1; got %v instead", cfgSuc) } } diff --git a/app/vmauth/auth_config.go b/app/vmauth/auth_config.go index fa05a0aaf..59e004997 100644 --- a/app/vmauth/auth_config.go +++ b/app/vmauth/auth_config.go @@ -386,7 +386,7 @@ func (r *Regex) MarshalYAML() (interface{}, error) { var ( configReloads = metrics.NewCounter(`vmauth_config_last_reload_total`) configReloadErrors = metrics.NewCounter(`vmauth_config_last_reload_errors_total`) - configSuccess = metrics.NewCounter(`vmauth_config_last_reload_successful`) + configSuccess = metrics.NewGauge(`vmauth_config_last_reload_successful`, nil) configTimestamp = metrics.NewCounter(`vmauth_config_last_reload_success_timestamp_seconds`) ) diff --git a/app/vminsert/common/streamaggr.go b/app/vminsert/common/streamaggr.go index 03624377a..6bee3dc0e 100644 --- a/app/vminsert/common/streamaggr.go +++ b/app/vminsert/common/streamaggr.go @@ -38,7 +38,7 @@ var ( saCfgReloads = metrics.NewCounter(`vminsert_streamagg_config_reloads_total`) saCfgReloadErr = metrics.NewCounter(`vminsert_streamagg_config_reloads_errors_total`) - saCfgSuccess = metrics.NewCounter(`vminsert_streamagg_config_last_reload_successful`) + saCfgSuccess = metrics.NewGauge(`vminsert_streamagg_config_last_reload_successful`, nil) saCfgTimestamp = metrics.NewCounter(`vminsert_streamagg_config_last_reload_success_timestamp_seconds`) sasGlobal atomic.Pointer[streamaggr.Aggregators] diff --git a/app/vminsert/relabel/relabel.go b/app/vminsert/relabel/relabel.go index 3409fb4c0..3508f154b 100644 --- a/app/vminsert/relabel/relabel.go +++ b/app/vminsert/relabel/relabel.go @@ -65,7 +65,7 @@ func Init() { var ( configReloads = metrics.NewCounter(`vm_relabel_config_reloads_total`) configReloadErrors = metrics.NewCounter(`vm_relabel_config_reloads_errors_total`) - configSuccess = metrics.NewCounter(`vm_relabel_config_last_reload_successful`) + configSuccess = metrics.NewGauge(`vm_relabel_config_last_reload_successful`, nil) configTimestamp = metrics.NewCounter(`vm_relabel_config_last_reload_success_timestamp_seconds`) ) diff --git a/go.mod b/go.mod index 323fe3d4d..ccd3e7655 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( // Do not use the original github.com/valyala/fasthttp because of issues // like https://github.com/valyala/fasthttp/commit/996610f021ff45fdc98c2ce7884d5fa4e7f9199b github.com/VictoriaMetrics/fasthttp v1.2.0 - github.com/VictoriaMetrics/metrics v1.28.2 + github.com/VictoriaMetrics/metrics v1.29.0 github.com/VictoriaMetrics/metricsql v0.70.0 github.com/aws/aws-sdk-go-v2 v1.24.0 github.com/aws/aws-sdk-go-v2/config v1.26.1 diff --git a/go.sum b/go.sum index 5535b14f5..03a063261 100644 --- a/go.sum +++ b/go.sum @@ -63,8 +63,8 @@ github.com/VictoriaMetrics/fastcache v1.12.2/go.mod h1:AmC+Nzz1+3G2eCPapF6UcsnkT github.com/VictoriaMetrics/fasthttp v1.2.0 h1:nd9Wng4DlNtaI27WlYh5mGXCJOmee/2c2blTJwfyU9I= github.com/VictoriaMetrics/fasthttp v1.2.0/go.mod h1:zv5YSmasAoSyv8sBVexfArzFDIGGTN4TfCKAtAw7IfE= github.com/VictoriaMetrics/metrics v1.24.0/go.mod h1:eFT25kvsTidQFHb6U0oa0rTrDRdz4xTYjpL8+UPohys= -github.com/VictoriaMetrics/metrics v1.28.2 h1:yWIq53N8G6hJI6vQ8I2NsiD4p+UsmQa/msLo3yqDfPQ= -github.com/VictoriaMetrics/metrics v1.28.2/go.mod h1:r7hveu6xMdUACXvB8TYdAj8WEsKzWB0EkpJN+RDtOf8= +github.com/VictoriaMetrics/metrics v1.29.0 h1:3qC+jcvymGJaQKt6wsXIlJieVFQwD/par9J1Bxul+Mc= +github.com/VictoriaMetrics/metrics v1.29.0/go.mod h1:r7hveu6xMdUACXvB8TYdAj8WEsKzWB0EkpJN+RDtOf8= github.com/VictoriaMetrics/metricsql v0.70.0 h1:G0k/m1yAF6pmk0dM3VT9/XI5PZ8dL7EbcLhREf4bgeI= github.com/VictoriaMetrics/metricsql v0.70.0/go.mod h1:k4UaP/+CjuZslIjd+kCigNG9TQmUqh5v0TP/nMEy90I= github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow= diff --git a/lib/promscrape/scraper.go b/lib/promscrape/scraper.go index 2ad7b2698..d5ec1cf8b 100644 --- a/lib/promscrape/scraper.go +++ b/lib/promscrape/scraper.go @@ -202,7 +202,7 @@ var ( configMetricsSet = metrics.NewSet() configReloads = configMetricsSet.NewCounter(`vm_promscrape_config_reloads_total`) configReloadErrors = configMetricsSet.NewCounter(`vm_promscrape_config_reloads_errors_total`) - configSuccess = configMetricsSet.NewCounter(`vm_promscrape_config_last_reload_successful`) + configSuccess = configMetricsSet.NewGauge(`vm_promscrape_config_last_reload_successful`, nil) configTimestamp = configMetricsSet.NewCounter(`vm_promscrape_config_last_reload_success_timestamp_seconds`) ) diff --git a/vendor/github.com/VictoriaMetrics/metrics/gauge.go b/vendor/github.com/VictoriaMetrics/metrics/gauge.go index d40b73098..9f676f40e 100644 --- a/vendor/github.com/VictoriaMetrics/metrics/gauge.go +++ b/vendor/github.com/VictoriaMetrics/metrics/gauge.go @@ -3,10 +3,11 @@ package metrics import ( "fmt" "io" + "math" + "sync/atomic" ) -// NewGauge registers and returns gauge with the given name, which calls f -// to obtain gauge value. +// NewGauge registers and returns gauge with the given name, which calls f to obtain gauge value. // // name must be valid Prometheus-compatible metric with possible labels. // For instance, @@ -16,6 +17,7 @@ import ( // - foo{bar="baz",aaa="b"} // // f must be safe for concurrent calls. +// if f is nil, then it is expected that the gauge value is changed via Gauge.Set() call. // // The returned gauge is safe to use from concurrent goroutines. // @@ -25,19 +27,36 @@ func NewGauge(name string, f func() float64) *Gauge { } // Gauge is a float64 gauge. -// -// See also Counter, which could be used as a gauge with Set and Dec calls. type Gauge struct { + // f is a callback, which is called for returning the gauge value. f func() float64 + + // valueBits contains uint64 representation of float64 passed to Gauge.Set. + valueBits uint64 } // Get returns the current value for g. func (g *Gauge) Get() float64 { - return g.f() + if f := g.f; f != nil { + return f() + } + n := atomic.LoadUint64(&g.valueBits) + return math.Float64frombits(n) +} + +// Set sets g value to v. +// +// The g must be created with nil callback in order to be able to call this function. +func (g *Gauge) Set(v float64) { + if g.f != nil { + panic(fmt.Errorf("cannot call Set on gauge created with non-nil callback")) + } + n := math.Float64bits(v) + atomic.StoreUint64(&g.valueBits, n) } func (g *Gauge) marshalTo(prefix string, w io.Writer) { - v := g.f() + v := g.Get() if float64(int64(v)) == v { // Marshal integer values without scientific notation fmt.Fprintf(w, "%s %d\n", prefix, int64(v)) diff --git a/vendor/github.com/VictoriaMetrics/metrics/set.go b/vendor/github.com/VictoriaMetrics/metrics/set.go index 9949b7c1c..50a095b53 100644 --- a/vendor/github.com/VictoriaMetrics/metrics/set.go +++ b/vendor/github.com/VictoriaMetrics/metrics/set.go @@ -251,9 +251,6 @@ func (s *Set) GetOrCreateFloatCounter(name string) *FloatCounter { // // The returned gauge is safe to use from concurrent goroutines. func (s *Set) NewGauge(name string, f func() float64) *Gauge { - if f == nil { - panic(fmt.Errorf("BUG: f cannot be nil")) - } g := &Gauge{ f: f, } @@ -280,9 +277,6 @@ func (s *Set) GetOrCreateGauge(name string, f func() float64) *Gauge { s.mu.Unlock() if nm == nil { // Slow path - create and register missing gauge. - if f == nil { - panic(fmt.Errorf("BUG: f cannot be nil")) - } if err := validateMetric(name); err != nil { panic(fmt.Errorf("BUG: invalid metric name %q: %s", name, err)) } diff --git a/vendor/modules.txt b/vendor/modules.txt index 66e89e036..52ac9993b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -97,7 +97,7 @@ github.com/VictoriaMetrics/fastcache github.com/VictoriaMetrics/fasthttp github.com/VictoriaMetrics/fasthttp/fasthttputil github.com/VictoriaMetrics/fasthttp/stackless -# github.com/VictoriaMetrics/metrics v1.28.2 +# github.com/VictoriaMetrics/metrics v1.29.0 ## explicit; go 1.17 github.com/VictoriaMetrics/metrics # github.com/VictoriaMetrics/metricsql v0.70.0