From ec77707f033c4d647b1e0a92e87d20b0290d00fc Mon Sep 17 00:00:00 2001 From: Bartlomiej Plotka Date: Fri, 6 Mar 2020 15:00:00 +0000 Subject: [PATCH] Blocked classic prometheus constructors, moved all to promauto; Removed unnecessary printfs. Fixes: https://github.com/thanos-io/thanos/issues/2102 Also blocked them on CI side, thanks to https://github.com/fatih/faillint/pull/8 Signed-off-by: Bartlomiej Plotka --- Makefile | 13 ++++- cmd/thanos/bucket.go | 4 +- cmd/thanos/compact.go | 14 ++--- cmd/thanos/downsample.go | 8 +-- cmd/thanos/main.go | 1 + cmd/thanos/main_test.go | 5 +- cmd/thanos/query.go | 4 +- cmd/thanos/sidecar.go | 6 +- go.mod | 2 +- go.sum | 4 +- pkg/alert/alert.go | 25 ++++---- pkg/block/fetcher.go | 22 +++---- pkg/block/fetcher_test.go | 13 +++-- pkg/cacheutil/memcached_client.go | 11 ++-- .../memcached_server_selector_test.go | 5 +- pkg/compact/clean_test.go | 3 +- pkg/compact/compact.go | 35 ++++------- pkg/discovery/dns/provider.go | 13 ++--- pkg/extgrpc/client.go | 1 - pkg/extprom/http/instrument_server.go | 10 ++-- pkg/extprom/tx_gauge.go | 12 +++- pkg/extprom/tx_gauge_test.go | 6 +- pkg/gate/gate.go | 9 +-- pkg/objstore/objstore.go | 24 ++++---- pkg/prober/http_test.go | 7 ++- pkg/prober/intrumentation.go | 8 +-- pkg/query/api/v1.go | 10 ++-- .../test-storeset-pre-v0.8.0/storeset.go | 6 +- pkg/receive/config.go | 33 ++++------- pkg/receive/handler.go | 4 +- pkg/replicate/replicater.go | 11 ++-- pkg/replicate/scheme.go | 58 ++++++++----------- pkg/rule/api/v1.go | 4 +- pkg/server/grpc/grpc.go | 4 +- pkg/shipper/shipper.go | 30 ++++------ pkg/store/bucket.go | 57 ++++++------------ pkg/store/bucket_test.go | 3 +- pkg/store/cache/inmemory.go | 44 +++++++------- pkg/store/cache/memcached.go | 9 +-- pkg/store/proxy.go | 8 +-- pkg/ui/ui.go | 8 +-- 41 files changed, 230 insertions(+), 324 deletions(-) diff --git a/Makefile b/Makefile index 95382bb1775..f83617a6f84 100644 --- a/Makefile +++ b/Makefile @@ -78,9 +78,9 @@ ALERTMANAGER ?= $(GOBIN)/alertmanager-$(ALERTMANAGER_VERSION) MINIO_SERVER_VERSION ?= RELEASE.2018-10-06T00-15-16Z MINIO_SERVER ?=$(GOBIN)/minio-$(MINIO_SERVER_VERSION) +# TODO(bwplotka): Update to newest as soon as https://github.com/fatih/faillint/pull/8 merges. FAILLINT_VERSION ?= v1.0.1 FAILLINT ?=$(GOBIN)/faillint-$(FAILLINT_VERSION) -MODULES_TO_AVOID ?=errors,github.com/prometheus/tsdb,github.com/prometheus/prometheus/pkg/testutils # fetch_go_bin_version downloads (go gets) the binary from specific version and installs it in $(GOBIN)/- # arguments: @@ -139,7 +139,6 @@ assets: $(GOBINDATA) @$(GOBINDATA) $(bindata_flags) -pkg ui -o pkg/ui/bindata.go -ignore '(.*\.map|bootstrap\.js|bootstrap-theme\.css|bootstrap\.css)' pkg/ui/templates/... pkg/ui/static/... @go fmt ./pkg/ui - .PHONY: build build: ## Builds Thanos binary using `promu`. build: check-git deps $(PROMU) @@ -295,7 +294,15 @@ lint: ## Runs various static analysis against our code. lint: check-git deps $(GOLANGCILINT) $(MISSPELL) $(FAILLINT) $(call require_clean_work_tree,"detected not clean master before running lint") @echo ">> verifying modules being imported" - @$(FAILLINT) -paths $(MODULES_TO_AVOID) ./... + @# TODO(bwplotka): Add, Printf, DefaultRegisterer, NewGaugeFunc and MustRegister once exception are accepted. Add fmt.{Errorf}=github.com/pkg/errors.{Errorf} once https://github.com/fatih/faillint/issues/10 is addressed. + @$(FAILLINT) -paths "errors=github.com/pkg/errors,\ +github.com/prometheus/tsdb=github.com/prometheus/prometheus/tsdb,\ +github.com/prometheus/prometheus/pkg/testutils=github.com/thanos-io/thanos/pkg/testutil,\ +github.com/prometheus/client_golang/prometheus.{DefaultGatherer,DefBuckets,NewUntypedFunc,UntypedFunc},\ +github.com/prometheus/client_golang/prometheus.{NewCounter,NewCounterVec,NewCounterVec,NewGauge,NewGaugeVec,NewGaugeFunc,\ +NewHistorgram,NewHistogramVec,NewSummary,NewSummaryVec}=github.com/prometheus/client_golang/prometheus/promauto.{NewCounter,\ +NewCounterVec,NewCounterVec,NewGauge,NewGaugeVec,NewGaugeFunc,NewHistorgram,NewHistogramVec,NewSummary,NewSummaryVec}" ./... + @$(FAILLINT) -paths "fmt.{Print,Println,Sprint}" -ignore-tests ./... @echo ">> examining all of the Go files" @go vet -stdmethods=false ./pkg/... ./cmd/... && go vet doc.go @echo ">> linting all of the Go files GOGC=${GOGC}" diff --git a/cmd/thanos/bucket.go b/cmd/thanos/bucket.go index e922057e969..d4ed513cce1 100644 --- a/cmd/thanos/bucket.go +++ b/cmd/thanos/bucket.go @@ -269,7 +269,7 @@ func registerBucketInspect(m map[string]setupFunc, root *kingpin.CmdClause, name // Parse selector. selectorLabels, err := parseFlagLabels(*selector) if err != nil { - return fmt.Errorf("error parsing selector flag: %v", err) + return errors.Errorf("error parsing selector flag: %v", err) } confContentYaml, err := objStoreConfig.Content() @@ -523,7 +523,7 @@ func printTable(blockMetas []*metadata.Meta, selectorLabels labels.Labels, sortB for _, col := range sortBy { index := getIndex(header, col) if index == -1 { - return fmt.Errorf("column %s not found", col) + return errors.Errorf("column %s not found", col) } sortByColNum = append(sortByColNum, index) } diff --git a/cmd/thanos/compact.go b/cmd/thanos/compact.go index b0ea2f346cd..c14bbac3a9e 100644 --- a/cmd/thanos/compact.go +++ b/cmd/thanos/compact.go @@ -19,6 +19,7 @@ import ( "github.com/opentracing/opentracing-go" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/prometheus/tsdb" "github.com/thanos-io/thanos/pkg/block" "github.com/thanos-io/thanos/pkg/block/indexheader" @@ -170,25 +171,23 @@ func runCompact( concurrency int, selectorRelabelConf *extflag.PathOrContent, ) error { - halted := prometheus.NewGauge(prometheus.GaugeOpts{ + halted := promauto.With(reg).NewGauge(prometheus.GaugeOpts{ Name: "thanos_compactor_halted", Help: "Set to 1 if the compactor halted due to an unexpected error.", }) halted.Set(0) - retried := prometheus.NewCounter(prometheus.CounterOpts{ + retried := promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_compactor_retries_total", Help: "Total number of retries after retriable compactor error.", }) - iterations := prometheus.NewCounter(prometheus.CounterOpts{ + iterations := promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_compactor_iterations_total", Help: "Total number of iterations that were executed successfully.", }) - partialUploadDeleteAttempts := prometheus.NewCounter(prometheus.CounterOpts{ + partialUploadDeleteAttempts := promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_compactor_aborted_partial_uploads_deletion_attempts_total", Help: "Total number of started deletions of blocks that are assumed aborted and only partially uploaded.", }) - reg.MustRegister(halted, retried, iterations, partialUploadDeleteAttempts) - downsampleMetrics := newDownsampleMetrics(reg) httpProbe := prober.NewHTTP() @@ -390,11 +389,10 @@ func runCompact( // genMissingIndexCacheFiles scans over all blocks, generates missing index cache files and uploads them to object storage. func genMissingIndexCacheFiles(ctx context.Context, logger log.Logger, reg *prometheus.Registry, bkt objstore.Bucket, fetcher block.MetadataFetcher, dir string) error { - genIndex := prometheus.NewCounter(prometheus.CounterOpts{ + genIndex := promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: metricIndexGenerateName, Help: metricIndexGenerateHelp, }) - reg.MustRegister(genIndex) if err := os.RemoveAll(dir); err != nil { return errors.Wrap(err, "clean index cache directory") diff --git a/cmd/thanos/downsample.go b/cmd/thanos/downsample.go index d38ea26273c..d513d2f0a3b 100644 --- a/cmd/thanos/downsample.go +++ b/cmd/thanos/downsample.go @@ -16,6 +16,7 @@ import ( opentracing "github.com/opentracing/opentracing-go" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/prometheus/tsdb" "github.com/prometheus/prometheus/tsdb/chunkenc" "github.com/thanos-io/thanos/pkg/block" @@ -57,18 +58,15 @@ type DownsampleMetrics struct { func newDownsampleMetrics(reg *prometheus.Registry) *DownsampleMetrics { m := new(DownsampleMetrics) - m.downsamples = prometheus.NewCounterVec(prometheus.CounterOpts{ + m.downsamples = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_compact_downsample_total", Help: "Total number of downsampling attempts.", }, []string{"group"}) - m.downsampleFailures = prometheus.NewCounterVec(prometheus.CounterOpts{ + m.downsampleFailures = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_compact_downsample_failures_total", Help: "Total number of failed downsampling attempts.", }, []string{"group"}) - reg.MustRegister(m.downsamples) - reg.MustRegister(m.downsampleFailures) - return m } diff --git a/cmd/thanos/main.go b/cmd/thanos/main.go index f77424d8ced..b407d252bfb 100644 --- a/cmd/thanos/main.go +++ b/cmd/thanos/main.go @@ -119,6 +119,7 @@ func main() { prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}), ) + // Some packages still use default Register. Replace to have those metrics. prometheus.DefaultRegisterer = metrics // Memberlist uses go-metrics. sink, err := gprom.NewPrometheusSink() diff --git a/cmd/thanos/main_test.go b/cmd/thanos/main_test.go index 1917a79f208..d44844d7685 100644 --- a/cmd/thanos/main_test.go +++ b/cmd/thanos/main_test.go @@ -15,6 +15,7 @@ import ( "github.com/go-kit/kit/log" "github.com/oklog/ulid" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" promtest "github.com/prometheus/client_golang/prometheus/testutil" "github.com/prometheus/prometheus/pkg/labels" "github.com/thanos-io/thanos/pkg/block" @@ -71,12 +72,10 @@ func TestCleanupIndexCacheFolder(t *testing.T) { reg := prometheus.NewRegistry() expReg := prometheus.NewRegistry() - genIndexExp := prometheus.NewCounter(prometheus.CounterOpts{ + genIndexExp := promauto.With(expReg).NewCounter(prometheus.CounterOpts{ Name: metricIndexGenerateName, Help: metricIndexGenerateHelp, }) - expReg.MustRegister(genIndexExp) - metaFetcher, err := block.NewMetaFetcher(nil, 32, bkt, "", nil) testutil.Ok(t, err) diff --git a/cmd/thanos/query.go b/cmd/thanos/query.go index ddbb315d2a4..0d687b02e11 100644 --- a/cmd/thanos/query.go +++ b/cmd/thanos/query.go @@ -18,6 +18,7 @@ import ( opentracing "github.com/opentracing/opentracing-go" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/common/route" "github.com/prometheus/prometheus/discovery/file" "github.com/prometheus/prometheus/discovery/targetgroup" @@ -204,11 +205,10 @@ func runQuery( comp component.Component, ) error { // TODO(bplotka in PR #513 review): Move arguments into struct. - duplicatedStores := prometheus.NewCounter(prometheus.CounterOpts{ + duplicatedStores := promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_query_duplicated_store_addresses_total", Help: "The number of times a duplicated store addresses is detected from the different configs in query", }) - reg.MustRegister(duplicatedStores) dialOpts, err := extgrpc.StoreClientGRPCOpts(logger, reg, tracer, secure, cert, key, caCert, serverName) if err != nil { diff --git a/cmd/thanos/sidecar.go b/cmd/thanos/sidecar.go index 3780ad1812d..3024b1a7160 100644 --- a/cmd/thanos/sidecar.go +++ b/cmd/thanos/sidecar.go @@ -17,6 +17,7 @@ import ( "github.com/opentracing/opentracing-go" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/pkg/labels" "github.com/thanos-io/thanos/pkg/block/metadata" @@ -183,15 +184,14 @@ func runSidecar( // Setup all the concurrent groups. { - promUp := prometheus.NewGauge(prometheus.GaugeOpts{ + promUp := promauto.With(reg).NewGauge(prometheus.GaugeOpts{ Name: "thanos_sidecar_prometheus_up", Help: "Boolean indicator whether the sidecar can reach its Prometheus peer.", }) - lastHeartbeat := prometheus.NewGauge(prometheus.GaugeOpts{ + lastHeartbeat := promauto.With(reg).NewGauge(prometheus.GaugeOpts{ Name: "thanos_sidecar_last_heartbeat_success_time_seconds", Help: "Second timestamp of the last successful heartbeat.", }) - reg.MustRegister(promUp, lastHeartbeat) ctx, cancel := context.WithCancel(context.Background()) g.Add(func() error { diff --git a/go.mod b/go.mod index 87e285eaf06..d1530f6a045 100644 --- a/go.mod +++ b/go.mod @@ -38,7 +38,7 @@ require ( github.com/opentracing/opentracing-go v1.1.1-0.20200124165624-2876d2018785 github.com/pkg/errors v0.9.1 github.com/prometheus/alertmanager v0.20.0 - github.com/prometheus/client_golang v1.4.2-0.20200214154132-b25ce2693a6d + github.com/prometheus/client_golang v1.5.0 github.com/prometheus/client_model v0.2.0 github.com/prometheus/common v0.9.1 github.com/prometheus/prometheus v1.8.2-0.20200213233353-b90be6f32a33 diff --git a/go.sum b/go.sum index 9877fbf2436..b5031118a94 100644 --- a/go.sum +++ b/go.sum @@ -592,8 +592,8 @@ github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQ github.com/prometheus/client_golang v1.2.0/go.mod h1:XMU6Z2MjaRKVu/dC1qupJI9SiNkDYzz3xecMgSW/F+U= github.com/prometheus/client_golang v1.2.1 h1:JnMpQc6ppsNgw9QPAGF6Dod479itz7lvlsMzzNayLOI= github.com/prometheus/client_golang v1.2.1/go.mod h1:XMU6Z2MjaRKVu/dC1qupJI9SiNkDYzz3xecMgSW/F+U= -github.com/prometheus/client_golang v1.4.2-0.20200214154132-b25ce2693a6d h1:6GpNaEnOxPO8IxMm5zmXdIpCGayuQmp7udttdxB2BbM= -github.com/prometheus/client_golang v1.4.2-0.20200214154132-b25ce2693a6d/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU= +github.com/prometheus/client_golang v1.5.0 h1:Ctq0iGpCmr3jeP77kbF2UxgvRwzWWz+4Bh9/vJTyg1A= +github.com/prometheus/client_golang v1.5.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU= github.com/prometheus/client_model v0.0.0-20170216185247-6f3806018612/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= diff --git a/pkg/alert/alert.go b/pkg/alert/alert.go index 4ad30b7c03b..0bb500e77a0 100644 --- a/pkg/alert/alert.go +++ b/pkg/alert/alert.go @@ -23,6 +23,7 @@ import ( "github.com/pkg/errors" "github.com/prometheus/alertmanager/api/v2/models" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/prometheus/pkg/labels" "github.com/thanos-io/thanos/pkg/runutil" @@ -133,34 +134,31 @@ func NewQueue(logger log.Logger, reg prometheus.Registerer, capacity, maxBatchSi toAddLset: toAdd, toExcludeLabels: toExclude, - dropped: prometheus.NewCounter(prometheus.CounterOpts{ + dropped: promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_alert_queue_alerts_dropped_total", Help: "Total number of alerts that were dropped from the queue.", }), - pushed: prometheus.NewCounter(prometheus.CounterOpts{ + pushed: promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_alert_queue_alerts_pushed_total", Help: "Total number of alerts pushed to the queue.", }), - popped: prometheus.NewCounter(prometheus.CounterOpts{ + popped: promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_alert_queue_alerts_popped_total", Help: "Total number of alerts popped from the queue.", }), } - capMetric := prometheus.NewGaugeFunc(prometheus.GaugeOpts{ + _ = promauto.With(reg).NewGaugeFunc(prometheus.GaugeOpts{ Name: "thanos_alert_queue_capacity", Help: "Capacity of the alert queue.", }, func() float64 { return float64(q.Cap()) }) - lenMetric := prometheus.NewGaugeFunc(prometheus.GaugeOpts{ + _ = promauto.With(reg).NewGaugeFunc(prometheus.GaugeOpts{ Name: "thanos_alert_queue_length", Help: "Length of the alert queue.", }, func() float64 { return float64(q.Len()) }) - if reg != nil { - reg.MustRegister(q.pushed, q.popped, q.dropped, lenMetric, capMetric) - } return q } @@ -292,29 +290,26 @@ func NewSender( alertmanagers: alertmanagers, versions: versions, - sent: prometheus.NewCounterVec(prometheus.CounterOpts{ + sent: promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_alert_sender_alerts_sent_total", Help: "Total number of alerts sent by alertmanager.", }, []string{"alertmanager"}), - errs: prometheus.NewCounterVec(prometheus.CounterOpts{ + errs: promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_alert_sender_errors_total", Help: "Total number of errors while sending alerts to alertmanager.", }, []string{"alertmanager"}), - dropped: prometheus.NewCounter(prometheus.CounterOpts{ + dropped: promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_alert_sender_alerts_dropped_total", Help: "Total number of alerts dropped in case of all sends to alertmanagers failed.", }), - latency: prometheus.NewHistogramVec(prometheus.HistogramOpts{ + latency: promauto.With(reg).NewHistogramVec(prometheus.HistogramOpts{ Name: "thanos_alert_sender_latency_seconds", Help: "Latency for sending alert notifications (not including dropped notifications).", }, []string{"alertmanager"}), } - if reg != nil { - reg.MustRegister(s.sent, s.errs, s.dropped, s.latency) - } return s } diff --git a/pkg/block/fetcher.go b/pkg/block/fetcher.go index bf288a7ad61..ae4f3b454b1 100644 --- a/pkg/block/fetcher.go +++ b/pkg/block/fetcher.go @@ -19,6 +19,7 @@ import ( "github.com/oklog/ulid" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/relabel" "github.com/prometheus/prometheus/tsdb" @@ -54,26 +55,26 @@ const ( duplicateMeta = "duplicate" ) -func newSyncMetrics(r prometheus.Registerer) *syncMetrics { +func newSyncMetrics(reg prometheus.Registerer) *syncMetrics { var m syncMetrics - m.syncs = prometheus.NewCounter(prometheus.CounterOpts{ + m.syncs = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Subsystem: syncMetricSubSys, Name: "syncs_total", Help: "Total blocks metadata synchronization attempts", }) - m.syncFailures = prometheus.NewCounter(prometheus.CounterOpts{ + m.syncFailures = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Subsystem: syncMetricSubSys, Name: "sync_failures_total", Help: "Total blocks metadata synchronization failures", }) - m.syncDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ + m.syncDuration = promauto.With(reg).NewHistogram(prometheus.HistogramOpts{ Subsystem: syncMetricSubSys, Name: "sync_duration_seconds", Help: "Duration of the blocks metadata synchronization in seconds", Buckets: []float64{0.01, 1, 10, 100, 1000}, }) - m.synced = extprom.NewTxGaugeVec(prometheus.GaugeOpts{ + m.synced = extprom.NewTxGaugeVec(reg, prometheus.GaugeOpts{ Subsystem: syncMetricSubSys, Name: "synced", Help: "Number of block metadata synced", @@ -88,14 +89,6 @@ func newSyncMetrics(r prometheus.Registerer) *syncMetrics { []string{timeExcludedMeta}, []string{duplicateMeta}, ) - if r != nil { - r.MustRegister( - m.syncs, - m.syncFailures, - m.syncDuration, - m.synced, - ) - } return &m } @@ -544,13 +537,12 @@ func NewConsistencyDelayMetaFilter(logger log.Logger, consistencyDelay time.Dura if logger == nil { logger = log.NewNopLogger() } - consistencyDelayMetric := prometheus.NewGaugeFunc(prometheus.GaugeOpts{ + _ = promauto.With(reg).NewGaugeFunc(prometheus.GaugeOpts{ Name: "consistency_delay_seconds", Help: "Configured consistency delay in seconds.", }, func() float64 { return consistencyDelay.Seconds() }) - reg.MustRegister(consistencyDelayMetric) return &ConsistencyDelayMetaFilter{ logger: logger, diff --git a/pkg/block/fetcher_test.go b/pkg/block/fetcher_test.go index eb93a5246b5..d2c4c9b251c 100644 --- a/pkg/block/fetcher_test.go +++ b/pkg/block/fetcher_test.go @@ -22,6 +22,7 @@ import ( "github.com/oklog/ulid" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" promtest "github.com/prometheus/client_golang/prometheus/testutil" "github.com/prometheus/prometheus/pkg/relabel" "github.com/prometheus/prometheus/tsdb" @@ -332,7 +333,7 @@ func TestLabelShardedMetaFilter_Filter_Basic(t *testing.T) { ULID(6): input[ULID(6)], } - synced := prometheus.NewGaugeVec(prometheus.GaugeOpts{}, []string{"state"}) + synced := promauto.With(nil).NewGaugeVec(prometheus.GaugeOpts{}, []string{"state"}) f.Filter(input, synced, false) testutil.Equals(t, 3.0, promtest.ToFloat64(synced.WithLabelValues(labelExcludedMeta))) @@ -427,7 +428,7 @@ func TestLabelShardedMetaFilter_Filter_Hashmod(t *testing.T) { } deleted := len(input) - len(expected) - synced := prometheus.NewGaugeVec(prometheus.GaugeOpts{}, []string{"state"}) + synced := promauto.With(nil).NewGaugeVec(prometheus.GaugeOpts{}, []string{"state"}) f.Filter(input, synced, false) testutil.Equals(t, expected, input) @@ -488,7 +489,7 @@ func TestTimePartitionMetaFilter_Filter(t *testing.T) { ULID(4): input[ULID(4)], } - synced := prometheus.NewGaugeVec(prometheus.GaugeOpts{}, []string{"state"}) + synced := promauto.With(nil).NewGaugeVec(prometheus.GaugeOpts{}, []string{"state"}) f.Filter(input, synced, false) testutil.Equals(t, 2.0, promtest.ToFloat64(synced.WithLabelValues(timeExcludedMeta))) @@ -819,7 +820,7 @@ func TestDeduplicateFilter_Filter(t *testing.T) { } { f := NewDeduplicateFilter() if ok := t.Run(tcase.name, func(t *testing.T) { - synced := prometheus.NewGaugeVec(prometheus.GaugeOpts{}, []string{"state"}) + synced := promauto.With(nil).NewGaugeVec(prometheus.GaugeOpts{}, []string{"state"}) metas := make(map[ulid.ULID]*metadata.Meta) inputLen := len(tcase.input) for id, metaInfo := range tcase.input { @@ -932,7 +933,7 @@ func TestConsistencyDelayMetaFilter_Filter_0(t *testing.T) { } t.Run("consistency 0 (turned off)", func(t *testing.T) { - synced := prometheus.NewGaugeVec(prometheus.GaugeOpts{}, []string{"state"}) + synced := promauto.With(nil).NewGaugeVec(prometheus.GaugeOpts{}, []string{"state"}) expected := map[ulid.ULID]*metadata.Meta{} // Copy all. for _, id := range u.created { @@ -950,7 +951,7 @@ func TestConsistencyDelayMetaFilter_Filter_0(t *testing.T) { }) t.Run("consistency 30m.", func(t *testing.T) { - synced := prometheus.NewGaugeVec(prometheus.GaugeOpts{}, []string{"state"}) + synced := promauto.With(nil).NewGaugeVec(prometheus.GaugeOpts{}, []string{"state"}) expected := map[ulid.ULID]*metadata.Meta{} // Only certain sources and those with 30m or more age go through. for i, id := range u.created { diff --git a/pkg/cacheutil/memcached_client.go b/pkg/cacheutil/memcached_client.go index 80c2d1f9fdb..67d93466f7c 100644 --- a/pkg/cacheutil/memcached_client.go +++ b/pkg/cacheutil/memcached_client.go @@ -13,6 +13,7 @@ import ( "github.com/go-kit/kit/log/level" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/thanos-io/thanos/pkg/discovery/dns" "github.com/thanos-io/thanos/pkg/extprom" "github.com/thanos-io/thanos/pkg/gate" @@ -202,29 +203,25 @@ func newMemcachedClient( ), } - c.operations = prometheus.NewCounterVec(prometheus.CounterOpts{ + c.operations = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_memcached_operations_total", Help: "Total number of operations against memcached.", ConstLabels: prometheus.Labels{"name": name}, }, []string{"operation"}) - c.failures = prometheus.NewCounterVec(prometheus.CounterOpts{ + c.failures = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_memcached_operation_failures_total", Help: "Total number of operations against memcached that failed.", ConstLabels: prometheus.Labels{"name": name}, }, []string{"operation"}) - c.duration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + c.duration = promauto.With(reg).NewHistogramVec(prometheus.HistogramOpts{ Name: "thanos_memcached_operation_duration_seconds", Help: "Duration of operations against memcached.", ConstLabels: prometheus.Labels{"name": name}, Buckets: []float64{0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1}, }, []string{"operation"}) - if reg != nil { - reg.MustRegister(c.operations, c.failures, c.duration) - } - // As soon as the client is created it must ensure that memcached server // addresses are resolved, so we're going to trigger an initial addresses // resolution here. diff --git a/pkg/cacheutil/memcached_server_selector_test.go b/pkg/cacheutil/memcached_server_selector_test.go index a8da4346ca8..ab5848bb077 100644 --- a/pkg/cacheutil/memcached_server_selector_test.go +++ b/pkg/cacheutil/memcached_server_selector_test.go @@ -12,6 +12,7 @@ import ( "github.com/bradfitz/gomemcache/memcache" "github.com/facette/natsort" "github.com/fortytw2/leaktest" + "github.com/pkg/errors" "github.com/thanos-io/thanos/pkg/testutil" ) @@ -144,7 +145,7 @@ func TestMemcachedJumpHashSelector_PickServer_ShouldEvenlyDistributeKeysToServer for addr, count := range distribution { if count < minKeysPerServer { - testutil.Ok(t, fmt.Errorf("expected %s to have received at least %d keys instead it received %d", addr, minKeysPerServer, count)) + testutil.Ok(t, errors.Errorf("expected %s to have received at least %d keys instead it received %d", addr, minKeysPerServer, count)) } } } @@ -199,7 +200,7 @@ func TestMemcachedJumpHashSelector_PickServer_ShouldUseConsistentHashing(t *test maxExpectedMovedPerc := (1.0 / float64(len(servers))) + 0.02 maxExpectedMoved := int(float64(numKeys) * maxExpectedMovedPerc) if moved > maxExpectedMoved { - testutil.Ok(t, fmt.Errorf("expected resharding moved no more then %d keys while %d have been moved", maxExpectedMoved, moved)) + testutil.Ok(t, errors.Errorf("expected resharding moved no more then %d keys while %d have been moved", maxExpectedMoved, moved)) } } diff --git a/pkg/compact/clean_test.go b/pkg/compact/clean_test.go index 85654f8c8a3..90b136be47e 100644 --- a/pkg/compact/clean_test.go +++ b/pkg/compact/clean_test.go @@ -14,6 +14,7 @@ import ( "github.com/go-kit/kit/log" "github.com/oklog/ulid" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" promtest "github.com/prometheus/client_golang/prometheus/testutil" "github.com/thanos-io/thanos/pkg/block" "github.com/thanos-io/thanos/pkg/block/metadata" @@ -57,7 +58,7 @@ func TestBestEffortCleanAbortedPartialUploads(t *testing.T) { testutil.Ok(t, bkt.Upload(ctx, path.Join(shouldIgnoreID2.String(), "chunks", "000001"), &fakeChunk)) - deleteAttempts := prometheus.NewCounter(prometheus.CounterOpts{}) + deleteAttempts := promauto.With(nil).NewCounter(prometheus.CounterOpts{}) BestEffortCleanAbortedPartialUploads(ctx, logger, metaFetcher, bkt, deleteAttempts) testutil.Equals(t, 1.0, promtest.ToFloat64(deleteAttempts)) diff --git a/pkg/compact/compact.go b/pkg/compact/compact.go index b268724e108..df9f8492abe 100644 --- a/pkg/compact/compact.go +++ b/pkg/compact/compact.go @@ -18,6 +18,7 @@ import ( "github.com/oklog/ulid" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb" terrors "github.com/prometheus/prometheus/tsdb/errors" @@ -67,58 +68,44 @@ type syncerMetrics struct { func newSyncerMetrics(reg prometheus.Registerer) *syncerMetrics { var m syncerMetrics - m.garbageCollectedBlocks = prometheus.NewCounter(prometheus.CounterOpts{ + m.garbageCollectedBlocks = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_compact_garbage_collected_blocks_total", Help: "Total number of deleted blocks by compactor.", }) - m.garbageCollections = prometheus.NewCounter(prometheus.CounterOpts{ + m.garbageCollections = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_compact_garbage_collection_total", Help: "Total number of garbage collection operations.", }) - m.garbageCollectionFailures = prometheus.NewCounter(prometheus.CounterOpts{ + m.garbageCollectionFailures = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_compact_garbage_collection_failures_total", Help: "Total number of failed garbage collection operations.", }) - m.garbageCollectionDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ + m.garbageCollectionDuration = promauto.With(reg).NewHistogram(prometheus.HistogramOpts{ Name: "thanos_compact_garbage_collection_duration_seconds", Help: "Time it took to perform garbage collection iteration.", Buckets: []float64{0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120, 240, 360, 720}, }) - m.compactions = prometheus.NewCounterVec(prometheus.CounterOpts{ + m.compactions = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_compact_group_compactions_total", Help: "Total number of group compaction attempts that resulted in a new block.", }, []string{"group"}) - m.compactionRunsStarted = prometheus.NewCounterVec(prometheus.CounterOpts{ + m.compactionRunsStarted = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_compact_group_compaction_runs_started_total", Help: "Total number of group compaction attempts.", }, []string{"group"}) - m.compactionRunsCompleted = prometheus.NewCounterVec(prometheus.CounterOpts{ + m.compactionRunsCompleted = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_compact_group_compaction_runs_completed_total", Help: "Total number of group completed compaction runs. This also includes compactor group runs that resulted with no compaction.", }, []string{"group"}) - m.compactionFailures = prometheus.NewCounterVec(prometheus.CounterOpts{ + m.compactionFailures = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_compact_group_compactions_failures_total", Help: "Total number of failed group compactions.", }, []string{"group"}) - m.verticalCompactions = prometheus.NewCounterVec(prometheus.CounterOpts{ + m.verticalCompactions = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_compact_group_vertical_compactions_total", Help: "Total number of group compaction attempts that resulted in a new block based on overlapping blocks.", }, []string{"group"}) - - if reg != nil { - reg.MustRegister( - m.garbageCollectedBlocks, - m.garbageCollections, - m.garbageCollectionFailures, - m.garbageCollectionDuration, - m.compactions, - m.compactionRunsStarted, - m.compactionRunsCompleted, - m.compactionFailures, - m.verticalCompactions, - ) - } return &m } @@ -157,7 +144,7 @@ func UntilNextDownsampling(m *metadata.Meta) (time.Duration, error) { case downsample.ResLevel0: return time.Duration(downsample.DownsampleRange0*time.Millisecond) - timeRange, nil default: - panic(fmt.Errorf("invalid resolution %v", m.Thanos.Downsample.Resolution)) + panic(errors.Errorf("invalid resolution %v", m.Thanos.Downsample.Resolution)) } } diff --git a/pkg/discovery/dns/provider.go b/pkg/discovery/dns/provider.go index 90cf72bfb98..5df9e97320a 100644 --- a/pkg/discovery/dns/provider.go +++ b/pkg/discovery/dns/provider.go @@ -12,6 +12,7 @@ import ( "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/thanos-io/thanos/pkg/discovery/dns/miekgdns" "github.com/thanos-io/thanos/pkg/extprom" ) @@ -57,26 +58,20 @@ func NewProvider(logger log.Logger, reg prometheus.Registerer, resolverType Reso resolver: NewResolver(resolverType.ToResolver(logger)), resolved: make(map[string][]string), logger: logger, - resolverAddrs: extprom.NewTxGaugeVec(prometheus.GaugeOpts{ + resolverAddrs: extprom.NewTxGaugeVec(reg, prometheus.GaugeOpts{ Name: "dns_provider_results", Help: "The number of resolved endpoints for each configured address", }, []string{"addr"}), - resolverLookupsCount: prometheus.NewCounter(prometheus.CounterOpts{ + resolverLookupsCount: promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "dns_lookups_total", Help: "The number of DNS lookups resolutions attempts", }), - resolverFailuresCount: prometheus.NewCounter(prometheus.CounterOpts{ + resolverFailuresCount: promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "dns_failures_total", Help: "The number of DNS lookup failures", }), } - if reg != nil { - reg.MustRegister(p.resolverAddrs) - reg.MustRegister(p.resolverLookupsCount) - reg.MustRegister(p.resolverFailuresCount) - } - return p } diff --git a/pkg/extgrpc/client.go b/pkg/extgrpc/client.go index eb6ac475953..5262b76fed2 100644 --- a/pkg/extgrpc/client.go +++ b/pkg/extgrpc/client.go @@ -43,7 +43,6 @@ func StoreClientGRPCOpts(logger log.Logger, reg *prometheus.Registry, tracer ope ), ), } - if reg != nil { reg.MustRegister(grpcMets) } diff --git a/pkg/extprom/http/instrument_server.go b/pkg/extprom/http/instrument_server.go index 907d3da6e54..c09bb6f8b3d 100644 --- a/pkg/extprom/http/instrument_server.go +++ b/pkg/extprom/http/instrument_server.go @@ -7,6 +7,7 @@ import ( "net/http" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" ) @@ -40,7 +41,7 @@ type defaultInstrumentationMiddleware struct { // NewInstrumentationMiddleware provides default InstrumentationMiddleware. func NewInstrumentationMiddleware(reg prometheus.Registerer) InstrumentationMiddleware { ins := defaultInstrumentationMiddleware{ - requestDuration: prometheus.NewHistogramVec( + requestDuration: promauto.With(reg).NewHistogramVec( prometheus.HistogramOpts{ Name: "http_request_duration_seconds", Help: "Tracks the latencies for HTTP requests.", @@ -49,7 +50,7 @@ func NewInstrumentationMiddleware(reg prometheus.Registerer) InstrumentationMidd []string{"code", "handler", "method"}, ), - requestSize: prometheus.NewSummaryVec( + requestSize: promauto.With(reg).NewSummaryVec( prometheus.SummaryOpts{ Name: "http_request_size_bytes", Help: "Tracks the size of HTTP requests.", @@ -57,14 +58,14 @@ func NewInstrumentationMiddleware(reg prometheus.Registerer) InstrumentationMidd []string{"code", "handler", "method"}, ), - requestsTotal: prometheus.NewCounterVec( + requestsTotal: promauto.With(reg).NewCounterVec( prometheus.CounterOpts{ Name: "http_requests_total", Help: "Tracks the number of HTTP requests.", }, []string{"code", "handler", "method"}, ), - responseSize: prometheus.NewSummaryVec( + responseSize: promauto.With(reg).NewSummaryVec( prometheus.SummaryOpts{ Name: "http_response_size_bytes", Help: "Tracks the size of HTTP responses.", @@ -72,7 +73,6 @@ func NewInstrumentationMiddleware(reg prometheus.Registerer) InstrumentationMidd []string{"code", "handler", "method"}, ), } - reg.MustRegister(ins.requestDuration, ins.requestSize, ins.requestsTotal, ins.responseSize) return &ins } diff --git a/pkg/extprom/tx_gauge.go b/pkg/extprom/tx_gauge.go index d85bf4f9215..a619b122e97 100644 --- a/pkg/extprom/tx_gauge.go +++ b/pkg/extprom/tx_gauge.go @@ -7,6 +7,7 @@ import ( "sync" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) type TxGaugeVec struct { @@ -24,18 +25,23 @@ type TxGaugeVec struct { // // Additionally it allows to init LabelValues on each transaction. // NOTE: This is quite naive implementation creating new prometheus.GaugeVec on each `ResetTx`, use wisely. -func NewTxGaugeVec(opts prometheus.GaugeOpts, labelNames []string, initLabelValues ...[]string) *TxGaugeVec { +func NewTxGaugeVec(reg prometheus.Registerer, opts prometheus.GaugeOpts, labelNames []string, initLabelValues ...[]string) *TxGaugeVec { + // Nil as we will register it on our own later. f := func() *prometheus.GaugeVec { - g := prometheus.NewGaugeVec(opts, labelNames) + g := promauto.With(nil).NewGaugeVec(opts, labelNames) for _, vals := range initLabelValues { g.WithLabelValues(vals...) } return g } - return &TxGaugeVec{ + tx := &TxGaugeVec{ current: f(), newMetricVal: f, } + if reg != nil { + reg.MustRegister(tx) + } + return tx } // ResetTx starts new transaction. Not goroutine-safe. diff --git a/pkg/extprom/tx_gauge_test.go b/pkg/extprom/tx_gauge_test.go index 4be8b02de42..92af9dc43c5 100644 --- a/pkg/extprom/tx_gauge_test.go +++ b/pkg/extprom/tx_gauge_test.go @@ -4,17 +4,17 @@ package extprom import ( - "fmt" "strings" "testing" + "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" dto "github.com/prometheus/client_model/go" "github.com/thanos-io/thanos/pkg/testutil" ) func TestTxGaugeVec(t *testing.T) { - g := NewTxGaugeVec(prometheus.GaugeOpts{ + g := NewTxGaugeVec(nil, prometheus.GaugeOpts{ Name: "metric", }, []string{"a", "b"}, []string{"a1", "b1"}, []string{"a2", "b2"}) @@ -167,7 +167,7 @@ func toFloat64(t *testing.T, c prometheus.Collector) map[string]float64 { if pb.Untyped != nil { exp[lbToString(pb.GetLabel())] = pb.Untyped.GetValue() } - panic(fmt.Errorf("collected a non-gauge/counter/untyped metric: %s", pb)) + panic(errors.Errorf("collected a non-gauge/counter/untyped metric: %s", pb)) } return exp diff --git a/pkg/gate/gate.go b/pkg/gate/gate.go index 43bc3a47b4c..549b0f3300d 100644 --- a/pkg/gate/gate.go +++ b/pkg/gate/gate.go @@ -8,6 +8,7 @@ import ( "time" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/prometheus/pkg/gate" ) @@ -27,21 +28,17 @@ type Gate struct { func NewGate(maxConcurrent int, reg prometheus.Registerer) *Gate { g := &Gate{ g: gate.New(maxConcurrent), - inflightQueries: prometheus.NewGauge(prometheus.GaugeOpts{ + inflightQueries: promauto.With(reg).NewGauge(prometheus.GaugeOpts{ Name: "gate_queries_in_flight", Help: "Number of queries that are currently in flight.", }), - gateTiming: prometheus.NewHistogram(prometheus.HistogramOpts{ + gateTiming: promauto.With(reg).NewHistogram(prometheus.HistogramOpts{ Name: "gate_duration_seconds", Help: "How many seconds it took for queries to wait at the gate.", Buckets: []float64{0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120, 240, 360, 720}, }), } - if reg != nil { - reg.MustRegister(g.inflightQueries, g.gateTiming) - } - return g } diff --git a/pkg/objstore/objstore.go b/pkg/objstore/objstore.go index ef8fc4ca4a2..c5defc43c13 100644 --- a/pkg/objstore/objstore.go +++ b/pkg/objstore/objstore.go @@ -18,6 +18,7 @@ import ( "github.com/go-kit/kit/log/level" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/thanos-io/thanos/pkg/runutil" ) @@ -200,42 +201,39 @@ const ( // BucketWithMetrics takes a bucket and registers metrics with the given registry for // operations run against the bucket. -func BucketWithMetrics(name string, b Bucket, r prometheus.Registerer) Bucket { +func BucketWithMetrics(name string, b Bucket, reg prometheus.Registerer) Bucket { bkt := &metricBucket{ bkt: b, - ops: prometheus.NewCounterVec(prometheus.CounterOpts{ + ops: promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_objstore_bucket_operations_total", Help: "Total number of operations against a bucket.", ConstLabels: prometheus.Labels{"bucket": name}, }, []string{"operation"}), - opsFailures: prometheus.NewCounterVec(prometheus.CounterOpts{ + opsFailures: promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_objstore_bucket_operation_failures_total", Help: "Total number of operations against a bucket that failed.", ConstLabels: prometheus.Labels{"bucket": name}, }, []string{"operation"}), - opsDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{ + opsDuration: promauto.With(reg).NewHistogramVec(prometheus.HistogramOpts{ Name: "thanos_objstore_bucket_operation_duration_seconds", Help: "Duration of operations against the bucket", ConstLabels: prometheus.Labels{"bucket": name}, Buckets: []float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120}, }, []string{"operation"}), - lastSuccessfulUploadTime: prometheus.NewGaugeVec(prometheus.GaugeOpts{ + lastSuccessfulUploadTime: promauto.With(reg).NewGaugeVec(prometheus.GaugeOpts{ Name: "thanos_objstore_bucket_last_successful_upload_time", Help: "Second timestamp of the last successful upload to the bucket.", }, []string{"bucket"}), } - if r != nil { - r.MustRegister(bkt.ops, bkt.opsFailures, bkt.opsDuration, bkt.lastSuccessfulUploadTime) - for _, op := range []string{iterOp, sizeOp, getOp, getRangeOp, existsOp, uploadOp, deleteOp} { - bkt.ops.WithLabelValues(op) - bkt.opsFailures.WithLabelValues(op) - bkt.opsDuration.WithLabelValues(op) - } - bkt.lastSuccessfulUploadTime.WithLabelValues(b.Name()) + for _, op := range []string{iterOp, sizeOp, getOp, getRangeOp, existsOp, uploadOp, deleteOp} { + bkt.ops.WithLabelValues(op) + bkt.opsFailures.WithLabelValues(op) + bkt.opsDuration.WithLabelValues(op) } + bkt.lastSuccessfulUploadTime.WithLabelValues(b.Name()) return bkt } diff --git a/pkg/prober/http_test.go b/pkg/prober/http_test.go index e31368a0f33..bf09f526f29 100644 --- a/pkg/prober/http_test.go +++ b/pkg/prober/http_test.go @@ -13,6 +13,7 @@ import ( "github.com/go-kit/kit/log" "github.com/oklog/run" + "github.com/pkg/errors" "github.com/thanos-io/thanos/pkg/testutil" ) @@ -29,7 +30,7 @@ func TestHTTPProberReadinessInitialState(t *testing.T) { } func TestHTTPProberHealthyStatusSetting(t *testing.T) { - testError := fmt.Errorf("test error") + testError := errors.Errorf("test error") p := NewHTTP() p.Healthy() @@ -42,7 +43,7 @@ func TestHTTPProberHealthyStatusSetting(t *testing.T) { } func TestHTTPProberReadyStatusSetting(t *testing.T) { - testError := fmt.Errorf("test error") + testError := errors.Errorf("test error") p := NewHTTP() p.Ready() @@ -71,7 +72,7 @@ func TestHTTPProberMuxRegistering(t *testing.T) { var g run.Group g.Add(func() error { - return fmt.Errorf("serve probes %w", http.Serve(l, mux)) + return errors.Errorf("serve probes %w", http.Serve(l, mux)) }, func(err error) { t.Fatalf("server failed: %v", err) }) diff --git a/pkg/prober/intrumentation.go b/pkg/prober/intrumentation.go index e9869750071..6c1d7b65407 100644 --- a/pkg/prober/intrumentation.go +++ b/pkg/prober/intrumentation.go @@ -7,6 +7,7 @@ import ( "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/thanos-io/thanos/pkg/component" ) @@ -30,7 +31,7 @@ func NewInstrumentation(component component.Component, logger log.Logger, reg pr p := InstrumentationProbe{ component: component, logger: logger, - status: prometheus.NewGaugeVec(prometheus.GaugeOpts{ + status: promauto.With(reg).NewGaugeVec(prometheus.GaugeOpts{ Name: "status", Help: "Represents status (0 indicates failure, 1 indicates success) of the component.", ConstLabels: map[string]string{"component": component.String()}, @@ -38,11 +39,6 @@ func NewInstrumentation(component component.Component, logger log.Logger, reg pr []string{"check"}, ), } - - if reg != nil { - reg.MustRegister(p.status) - } - return &p } diff --git a/pkg/query/api/v1.go b/pkg/query/api/v1.go index cc6a20a0a7c..089f227e0f0 100644 --- a/pkg/query/api/v1.go +++ b/pkg/query/api/v1.go @@ -421,7 +421,7 @@ func (api *API) labelValues(r *http.Request) (interface{}, []error, *ApiError) { name := route.Param(ctx, "name") if !model.LabelNameRE.MatchString(name) { - return nil, nil, &ApiError{errorBadData, fmt.Errorf("invalid label name: %q", name)} + return nil, nil, &ApiError{errorBadData, errors.Errorf("invalid label name: %q", name)} } enablePartialResponse, apiErr := api.parsePartialResponseParam(r) @@ -456,7 +456,7 @@ func (api *API) series(r *http.Request) (interface{}, []error, *ApiError) { } if len(r.Form["match[]"]) == 0 { - return nil, nil, &ApiError{errorBadData, fmt.Errorf("no match[] parameter provided")} + return nil, nil, &ApiError{errorBadData, errors.Errorf("no match[] parameter provided")} } var start time.Time @@ -588,21 +588,21 @@ func parseTime(s string) (time.Time, error) { if t, err := time.Parse(time.RFC3339Nano, s); err == nil { return t, nil } - return time.Time{}, fmt.Errorf("cannot parse %q to a valid timestamp", s) + return time.Time{}, errors.Errorf("cannot parse %q to a valid timestamp", s) } func parseDuration(s string) (time.Duration, error) { if d, err := strconv.ParseFloat(s, 64); err == nil { ts := d * float64(time.Second) if ts > float64(math.MaxInt64) || ts < float64(math.MinInt64) { - return 0, fmt.Errorf("cannot parse %q to a valid duration. It overflows int64", s) + return 0, errors.Errorf("cannot parse %q to a valid duration. It overflows int64", s) } return time.Duration(ts), nil } if d, err := model.ParseDuration(s); err == nil { return time.Duration(d), nil } - return 0, fmt.Errorf("cannot parse %q to a valid duration", s) + return 0, errors.Errorf("cannot parse %q to a valid duration", s) } func (api *API) labelNames(r *http.Request) (interface{}, []error, *ApiError) { diff --git a/pkg/query/internal/test-storeset-pre-v0.8.0/storeset.go b/pkg/query/internal/test-storeset-pre-v0.8.0/storeset.go index 326c50b80bc..fd5a64716d5 100644 --- a/pkg/query/internal/test-storeset-pre-v0.8.0/storeset.go +++ b/pkg/query/internal/test-storeset-pre-v0.8.0/storeset.go @@ -19,6 +19,7 @@ import ( "github.com/go-kit/kit/log/level" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/prometheus/pkg/labels" "github.com/thanos-io/thanos/pkg/component" "github.com/thanos-io/thanos/pkg/runutil" @@ -131,7 +132,7 @@ func NewStoreSet( dialOpts []grpc.DialOption, unhealthyStoreTimeout time.Duration, ) *StoreSet { - storeNodeConnections := prometheus.NewGauge(prometheus.GaugeOpts{ + storeNodeConnections := promauto.With(reg).NewGauge(prometheus.GaugeOpts{ Name: "thanos_store_nodes_grpc_connections", Help: "Number indicating current number of gRPC connection to store nodes. This indicates also to how many stores query node have access to.", }) @@ -139,9 +140,6 @@ func NewStoreSet( if logger == nil { logger = log.NewNopLogger() } - if reg != nil { - reg.MustRegister(storeNodeConnections) - } if storeSpecs == nil { storeSpecs = func() []StoreSpec { return nil } } diff --git a/pkg/receive/config.go b/pkg/receive/config.go index 782e9a9b115..ed706e0f1f8 100644 --- a/pkg/receive/config.go +++ b/pkg/receive/config.go @@ -16,6 +16,7 @@ import ( "github.com/go-kit/kit/log/level" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/common/model" "gopkg.in/fsnotify.v1" ) @@ -58,7 +59,7 @@ type ConfigWatcher struct { } // NewConfigWatcher creates a new ConfigWatcher. -func NewConfigWatcher(logger log.Logger, r prometheus.Registerer, path string, interval model.Duration) (*ConfigWatcher, error) { +func NewConfigWatcher(logger log.Logger, reg prometheus.Registerer, path string, interval model.Duration) (*ConfigWatcher, error) { if logger == nil { logger = log.NewNopLogger() } @@ -77,63 +78,49 @@ func NewConfigWatcher(logger log.Logger, r prometheus.Registerer, path string, i interval: time.Duration(interval), logger: logger, watcher: watcher, - hashGauge: prometheus.NewGauge( + hashGauge: promauto.With(reg).NewGauge( prometheus.GaugeOpts{ Name: "thanos_receive_config_hash", Help: "Hash of the currently loaded hashring configuration file.", }), - successGauge: prometheus.NewGauge( + successGauge: promauto.With(reg).NewGauge( prometheus.GaugeOpts{ Name: "thanos_receive_config_last_reload_successful", Help: "Whether the last hashring configuration file reload attempt was successful.", }), - lastSuccessTimeGauge: prometheus.NewGauge( + lastSuccessTimeGauge: promauto.With(reg).NewGauge( prometheus.GaugeOpts{ Name: "thanos_receive_config_last_reload_success_timestamp_seconds", Help: "Timestamp of the last successful hashring configuration file reload.", }), - changesCounter: prometheus.NewCounter( + changesCounter: promauto.With(reg).NewCounter( prometheus.CounterOpts{ Name: "thanos_receive_hashrings_file_changes_total", Help: "The number of times the hashrings configuration file has changed.", }), - errorCounter: prometheus.NewCounter( + errorCounter: promauto.With(reg).NewCounter( prometheus.CounterOpts{ Name: "thanos_receive_hashrings_file_errors_total", Help: "The number of errors watching the hashrings configuration file.", }), - refreshCounter: prometheus.NewCounter( + refreshCounter: promauto.With(reg).NewCounter( prometheus.CounterOpts{ Name: "thanos_receive_hashrings_file_refreshes_total", Help: "The number of refreshes of the hashrings configuration file.", }), - hashringNodesGauge: prometheus.NewGaugeVec( + hashringNodesGauge: promauto.With(reg).NewGaugeVec( prometheus.GaugeOpts{ Name: "thanos_receive_hashring_nodes", Help: "The number of nodes per hashring.", }, []string{"name"}), - hashringTenantsGauge: prometheus.NewGaugeVec( + hashringTenantsGauge: promauto.With(reg).NewGaugeVec( prometheus.GaugeOpts{ Name: "thanos_receive_hashring_tenants", Help: "The number of tenants per hashring.", }, []string{"name"}), } - - if r != nil { - r.MustRegister( - c.hashGauge, - c.successGauge, - c.lastSuccessTimeGauge, - c.changesCounter, - c.errorCounter, - c.refreshCounter, - c.hashringNodesGauge, - c.hashringTenantsGauge, - ) - } - return c, nil } diff --git a/pkg/receive/handler.go b/pkg/receive/handler.go index 5b5f7897d72..741e99bfc94 100644 --- a/pkg/receive/handler.go +++ b/pkg/receive/handler.go @@ -22,6 +22,7 @@ import ( opentracing "github.com/opentracing/opentracing-go" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/common/route" "github.com/prometheus/prometheus/prompb" "github.com/prometheus/prometheus/storage" @@ -90,7 +91,7 @@ func NewHandler(logger log.Logger, o *Options) *Handler { router: route.New(), options: o, peers: newPeerGroup(o.DialOpts...), - forwardRequestsTotal: prometheus.NewCounterVec( + forwardRequestsTotal: promauto.With(o.Registry).NewCounterVec( prometheus.CounterOpts{ Name: "thanos_receive_forward_requests_total", Help: "The number of forward requests.", @@ -101,7 +102,6 @@ func NewHandler(logger log.Logger, o *Options) *Handler { ins := extpromhttp.NewNopInstrumentationMiddleware() if o.Registry != nil { ins = extpromhttp.NewInstrumentationMiddleware(o.Registry) - o.Registry.MustRegister(h.forwardRequestsTotal) } readyf := h.testReady diff --git a/pkg/replicate/replicater.go b/pkg/replicate/replicater.go index 2a505d4f943..c4428e86966 100644 --- a/pkg/replicate/replicater.go +++ b/pkg/replicate/replicater.go @@ -5,7 +5,6 @@ package replicate import ( "context" - "fmt" "math/rand" "strconv" "strings" @@ -18,6 +17,7 @@ import ( opentracing "github.com/opentracing/opentracing-go" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/pkg/labels" "github.com/thanos-io/thanos/pkg/compact" @@ -139,19 +139,16 @@ func RunReplicate( return err } - replicationRunCounter := prometheus.NewCounterVec(prometheus.CounterOpts{ + replicationRunCounter := promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_replicate_replication_runs_total", Help: "The number of replication runs split by success and error.", }, []string{"result"}) - replicationRunDuration := prometheus.NewHistogramVec(prometheus.HistogramOpts{ + replicationRunDuration := promauto.With(reg).NewHistogramVec(prometheus.HistogramOpts{ Name: "thanos_replicate_replication_run_duration_seconds", Help: "The Duration of replication runs split by success and error.", }, []string{"result"}) - reg.MustRegister(replicationRunCounter) - reg.MustRegister(replicationRunDuration) - blockFilter := NewBlockFilter( logger, labelSelector, @@ -174,7 +171,7 @@ func RunReplicate( level.Info(logger).Log("msg", "running replication attempt") if err := newReplicationScheme(logger, metrics, blockFilter, fromBkt, toBkt, reg).execute(ctx); err != nil { - return fmt.Errorf("replication execute: %w", err) + return errors.Errorf("replication execute: %w", err) } return nil diff --git a/pkg/replicate/scheme.go b/pkg/replicate/scheme.go index 30be01f0f2a..b237bab5e13 100644 --- a/pkg/replicate/scheme.go +++ b/pkg/replicate/scheme.go @@ -6,7 +6,6 @@ package replicate import ( "bytes" "context" - "fmt" "io" "io/ioutil" "path" @@ -17,6 +16,7 @@ import ( "github.com/oklog/ulid" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/prometheus/pkg/labels" thanosblock "github.com/thanos-io/thanos/pkg/block" "github.com/thanos-io/thanos/pkg/block/metadata" @@ -124,41 +124,31 @@ type replicationMetrics struct { func newReplicationMetrics(reg prometheus.Registerer) *replicationMetrics { m := &replicationMetrics{ - originIterations: prometheus.NewCounter(prometheus.CounterOpts{ + originIterations: promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_replicate_origin_iterations_total", Help: "Total number of objects iterated over in the origin bucket.", }), - originMetaLoads: prometheus.NewCounter(prometheus.CounterOpts{ + originMetaLoads: promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_replicate_origin_meta_loads_total", Help: "Total number of meta.json reads in the origin bucket.", }), - originPartialMeta: prometheus.NewCounter(prometheus.CounterOpts{ + originPartialMeta: promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_replicate_origin_partial_meta_reads_total", Help: "Total number of partial meta reads encountered.", }), - blocksAlreadyReplicated: prometheus.NewCounter(prometheus.CounterOpts{ + blocksAlreadyReplicated: promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_replicate_blocks_already_replicated_total", Help: "Total number of blocks skipped due to already being replicated.", }), - blocksReplicated: prometheus.NewCounter(prometheus.CounterOpts{ + blocksReplicated: promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_replicate_blocks_replicated_total", Help: "Total number of blocks replicated.", }), - objectsReplicated: prometheus.NewCounter(prometheus.CounterOpts{ + objectsReplicated: promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_replicate_objects_replicated_total", Help: "Total number of objects replicated.", }), } - - if reg != nil { - reg.MustRegister(m.originIterations) - reg.MustRegister(m.originMetaLoads) - reg.MustRegister(m.originPartialMeta) - reg.MustRegister(m.blocksAlreadyReplicated) - reg.MustRegister(m.blocksReplicated) - reg.MustRegister(m.objectsReplicated) - } - return m } @@ -202,7 +192,7 @@ func (rs *replicationScheme) execute(ctx context.Context) error { return nil } if err != nil { - return fmt.Errorf("load meta for block %v from origin bucket: %w", id.String(), err) + return errors.Errorf("load meta for block %v from origin bucket: %w", id.String(), err) } if len(meta.Thanos.Labels) == 0 { @@ -217,7 +207,7 @@ func (rs *replicationScheme) execute(ctx context.Context) error { return nil }); err != nil { - return fmt.Errorf("iterate over origin bucket: %w", err) + return errors.Errorf("iterate over origin bucket: %w", err) } candidateBlocks := []*metadata.Meta{} @@ -237,7 +227,7 @@ func (rs *replicationScheme) execute(ctx context.Context) error { for _, b := range candidateBlocks { if err := rs.ensureBlockIsReplicated(ctx, b.BlockMeta.ULID); err != nil { - return fmt.Errorf("ensure block %v is replicated: %w", b.BlockMeta.ULID.String(), err) + return errors.Errorf("ensure block %v is replicated: %w", b.BlockMeta.ULID.String(), err) } } @@ -256,7 +246,7 @@ func (rs *replicationScheme) ensureBlockIsReplicated(ctx context.Context, id uli originMetaFile, err := rs.fromBkt.Get(ctx, metaFile) if err != nil { - return fmt.Errorf("get meta file from origin bucket: %w", err) + return errors.Errorf("get meta file from origin bucket: %w", err) } defer runutil.CloseWithLogOnErr(rs.logger, originMetaFile, "close original meta file") @@ -268,18 +258,18 @@ func (rs *replicationScheme) ensureBlockIsReplicated(ctx context.Context, id uli } if err != nil && !rs.toBkt.IsObjNotFoundErr(err) && err != io.EOF { - return fmt.Errorf("get meta file from target bucket: %w", err) + return errors.Errorf("get meta file from target bucket: %w", err) } originMetaFileContent, err := ioutil.ReadAll(originMetaFile) if err != nil { - return fmt.Errorf("read origin meta file: %w", err) + return errors.Errorf("read origin meta file: %w", err) } if targetMetaFile != nil && !rs.toBkt.IsObjNotFoundErr(err) { targetMetaFileContent, err := ioutil.ReadAll(targetMetaFile) if err != nil { - return fmt.Errorf("read target meta file: %w", err) + return errors.Errorf("read target meta file: %w", err) } if bytes.Equal(originMetaFileContent, targetMetaFileContent) { @@ -296,7 +286,7 @@ func (rs *replicationScheme) ensureBlockIsReplicated(ctx context.Context, id uli if err := rs.fromBkt.Iter(ctx, chunksDir, func(objectName string) error { err := rs.ensureObjectReplicated(ctx, objectName) if err != nil { - return fmt.Errorf("replicate object %v: %w", objectName, err) + return errors.Errorf("replicate object %v: %w", objectName, err) } return nil @@ -305,13 +295,13 @@ func (rs *replicationScheme) ensureBlockIsReplicated(ctx context.Context, id uli } if err := rs.ensureObjectReplicated(ctx, indexFile); err != nil { - return fmt.Errorf("replicate index file: %w", err) + return errors.Errorf("replicate index file: %w", err) } level.Debug(rs.logger).Log("msg", "replicating meta file", "object", metaFile) if err := rs.toBkt.Upload(ctx, metaFile, bytes.NewReader(originMetaFileContent)); err != nil { - return fmt.Errorf("upload meta file: %w", err) + return errors.Errorf("upload meta file: %w", err) } rs.metrics.blocksReplicated.Inc() @@ -326,7 +316,7 @@ func (rs *replicationScheme) ensureObjectReplicated(ctx context.Context, objectN exists, err := rs.toBkt.Exists(ctx, objectName) if err != nil { - return fmt.Errorf("check if %v exists in target bucket: %w", objectName, err) + return errors.Errorf("check if %v exists in target bucket: %w", objectName, err) } // skip if already exists. @@ -339,13 +329,13 @@ func (rs *replicationScheme) ensureObjectReplicated(ctx context.Context, objectN r, err := rs.fromBkt.Get(ctx, objectName) if err != nil { - return fmt.Errorf("get %v from origin bucket: %w", objectName, err) + return errors.Errorf("get %v from origin bucket: %w", objectName, err) } defer r.Close() if err = rs.toBkt.Upload(ctx, objectName, r); err != nil { - return fmt.Errorf("upload %v to target bucket: %w", objectName, err) + return errors.Errorf("upload %v to target bucket: %w", objectName, err) } level.Info(rs.logger).Log("msg", "object replicated", "object", objectName) @@ -362,22 +352,22 @@ func (rs *replicationScheme) ensureObjectReplicated(ctx context.Context, objectN func loadMeta(ctx context.Context, rs *replicationScheme, id ulid.ULID) (*metadata.Meta, bool, error) { fetcher, err := thanosblock.NewMetaFetcher(rs.logger, 32, rs.fromBkt, "", rs.reg) if err != nil { - return nil, false, fmt.Errorf("create meta fetcher with buecket %v: %w", rs.fromBkt, err) + return nil, false, errors.Errorf("create meta fetcher with buecket %v: %w", rs.fromBkt, err) } metas, _, err := fetcher.Fetch(ctx) if err != nil { switch errors.Cause(err) { default: - return nil, false, fmt.Errorf("fetch meta: %w", err) + return nil, false, errors.Errorf("fetch meta: %w", err) case thanosblock.ErrorSyncMetaNotFound: - return nil, true, fmt.Errorf("fetch meta: %w", err) + return nil, true, errors.Errorf("fetch meta: %w", err) } } m, ok := metas[id] if !ok { - return nil, true, fmt.Errorf("fetch meta: %w", err) + return nil, true, errors.Errorf("fetch meta: %w", err) } return m, false, nil diff --git a/pkg/rule/api/v1.go b/pkg/rule/api/v1.go index 682a79d5e77..628d678f708 100644 --- a/pkg/rule/api/v1.go +++ b/pkg/rule/api/v1.go @@ -4,12 +4,12 @@ package v1 import ( - "fmt" "net/http" "strconv" "time" "github.com/NYTimes/gziphandler" + "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" "github.com/go-kit/kit/log" @@ -111,7 +111,7 @@ func (api *API) rules(r *http.Request) (interface{}, []error, *qapi.ApiError) { Type: "recording", } default: - err := fmt.Errorf("rule %q: unsupported type %T", r.Name(), rule) + err := errors.Errorf("rule %q: unsupported type %T", r.Name(), rule) return nil, nil, &qapi.ApiError{Typ: qapi.ErrorInternal, Err: err} } diff --git a/pkg/server/grpc/grpc.go b/pkg/server/grpc/grpc.go index 34e25a90f24..2e230e82c81 100644 --- a/pkg/server/grpc/grpc.go +++ b/pkg/server/grpc/grpc.go @@ -17,6 +17,7 @@ import ( "github.com/opentracing/opentracing-go" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/thanos-io/thanos/pkg/component" "github.com/thanos-io/thanos/pkg/prober" "github.com/thanos-io/thanos/pkg/store/storepb" @@ -50,11 +51,10 @@ func New(logger log.Logger, reg prometheus.Registerer, tracer opentracing.Tracer met.EnableHandlingTimeHistogram( grpc_prometheus.WithHistogramBuckets([]float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120}), ) - panicsTotal := prometheus.NewCounter(prometheus.CounterOpts{ + panicsTotal := promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_grpc_req_panics_recovered_total", Help: "Total number of gRPC requests recovered from internal panic.", }) - reg.MustRegister(met, panicsTotal) grpcPanicRecoveryHandler := func(p interface{}) (err error) { panicsTotal.Inc() diff --git a/pkg/shipper/shipper.go b/pkg/shipper/shipper.go index 39e7d61d868..3055bec8177 100644 --- a/pkg/shipper/shipper.go +++ b/pkg/shipper/shipper.go @@ -20,6 +20,7 @@ import ( "github.com/oklog/ulid" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb" "github.com/prometheus/prometheus/tsdb/fileutil" @@ -37,40 +38,33 @@ type metrics struct { uploadedCompacted prometheus.Gauge } -func newMetrics(r prometheus.Registerer, uploadCompacted bool) *metrics { +func newMetrics(reg prometheus.Registerer, uploadCompacted bool) *metrics { var m metrics - m.dirSyncs = prometheus.NewCounter(prometheus.CounterOpts{ + m.dirSyncs = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_shipper_dir_syncs_total", Help: "Total number of dir syncs", }) - m.dirSyncFailures = prometheus.NewCounter(prometheus.CounterOpts{ + m.dirSyncFailures = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_shipper_dir_sync_failures_total", Help: "Total number of failed dir syncs", }) - m.uploads = prometheus.NewCounter(prometheus.CounterOpts{ + m.uploads = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_shipper_uploads_total", Help: "Total number of uploaded blocks", }) - m.uploadFailures = prometheus.NewCounter(prometheus.CounterOpts{ + m.uploadFailures = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_shipper_upload_failures_total", Help: "Total number of block upload failures", }) - m.uploadedCompacted = prometheus.NewGauge(prometheus.GaugeOpts{ + uploadCompactedGaugeOpts := prometheus.GaugeOpts{ Name: "thanos_shipper_upload_compacted_done", Help: "If 1 it means shipper uploaded all compacted blocks from the filesystem.", - }) - - if r != nil { - r.MustRegister( - m.dirSyncs, - m.dirSyncFailures, - m.uploads, - m.uploadFailures, - ) - if uploadCompacted { - r.MustRegister(m.uploadedCompacted) - } + } + if uploadCompacted { + m.uploadedCompacted = promauto.With(reg).NewGauge(uploadCompactedGaugeOpts) + } else { + m.uploadedCompacted = promauto.With(nil).NewGauge(uploadCompactedGaugeOpts) } return &m } diff --git a/pkg/store/bucket.go b/pkg/store/bucket.go index 0a91f0032da..7c17a2fa957 100644 --- a/pkg/store/bucket.go +++ b/pkg/store/bucket.go @@ -24,6 +24,7 @@ import ( "github.com/oklog/ulid" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/chunkenc" "github.com/prometheus/prometheus/tsdb/chunks" @@ -101,65 +102,65 @@ type bucketStoreMetrics struct { func newBucketStoreMetrics(reg prometheus.Registerer) *bucketStoreMetrics { var m bucketStoreMetrics - m.blockLoads = prometheus.NewCounter(prometheus.CounterOpts{ + m.blockLoads = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_bucket_store_block_loads_total", Help: "Total number of remote block loading attempts.", }) - m.blockLoadFailures = prometheus.NewCounter(prometheus.CounterOpts{ + m.blockLoadFailures = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_bucket_store_block_load_failures_total", Help: "Total number of failed remote block loading attempts.", }) - m.blockDrops = prometheus.NewCounter(prometheus.CounterOpts{ + m.blockDrops = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_bucket_store_block_drops_total", Help: "Total number of local blocks that were dropped.", }) - m.blockDropFailures = prometheus.NewCounter(prometheus.CounterOpts{ + m.blockDropFailures = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_bucket_store_block_drop_failures_total", Help: "Total number of local blocks that failed to be dropped.", }) - m.blocksLoaded = prometheus.NewGauge(prometheus.GaugeOpts{ + m.blocksLoaded = promauto.With(reg).NewGauge(prometheus.GaugeOpts{ Name: "thanos_bucket_store_blocks_loaded", Help: "Number of currently loaded blocks.", }) - m.seriesDataTouched = prometheus.NewSummaryVec(prometheus.SummaryOpts{ + m.seriesDataTouched = promauto.With(reg).NewSummaryVec(prometheus.SummaryOpts{ Name: "thanos_bucket_store_series_data_touched", Help: "How many items of a data type in a block were touched for a single series request.", }, []string{"data_type"}) - m.seriesDataFetched = prometheus.NewSummaryVec(prometheus.SummaryOpts{ + m.seriesDataFetched = promauto.With(reg).NewSummaryVec(prometheus.SummaryOpts{ Name: "thanos_bucket_store_series_data_fetched", Help: "How many items of a data type in a block were fetched for a single series request.", }, []string{"data_type"}) - m.seriesDataSizeTouched = prometheus.NewSummaryVec(prometheus.SummaryOpts{ + m.seriesDataSizeTouched = promauto.With(reg).NewSummaryVec(prometheus.SummaryOpts{ Name: "thanos_bucket_store_series_data_size_touched_bytes", Help: "Size of all items of a data type in a block were touched for a single series request.", }, []string{"data_type"}) - m.seriesDataSizeFetched = prometheus.NewSummaryVec(prometheus.SummaryOpts{ + m.seriesDataSizeFetched = promauto.With(reg).NewSummaryVec(prometheus.SummaryOpts{ Name: "thanos_bucket_store_series_data_size_fetched_bytes", Help: "Size of all items of a data type in a block were fetched for a single series request.", }, []string{"data_type"}) - m.seriesBlocksQueried = prometheus.NewSummary(prometheus.SummaryOpts{ + m.seriesBlocksQueried = promauto.With(reg).NewSummary(prometheus.SummaryOpts{ Name: "thanos_bucket_store_series_blocks_queried", Help: "Number of blocks in a bucket store that were touched to satisfy a query.", }) - m.seriesGetAllDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ + m.seriesGetAllDuration = promauto.With(reg).NewHistogram(prometheus.HistogramOpts{ Name: "thanos_bucket_store_series_get_all_duration_seconds", Help: "Time it takes until all per-block prepares and preloads for a query are finished.", Buckets: []float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120}, }) - m.seriesMergeDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ + m.seriesMergeDuration = promauto.With(reg).NewHistogram(prometheus.HistogramOpts{ Name: "thanos_bucket_store_series_merge_duration_seconds", Help: "Time it takes to merge sub-results from all queried blocks into a single result.", Buckets: []float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120}, }) - m.resultSeriesCount = prometheus.NewSummary(prometheus.SummaryOpts{ + m.resultSeriesCount = promauto.With(reg).NewSummary(prometheus.SummaryOpts{ Name: "thanos_bucket_store_series_result_series", Help: "Number of series observed in the final result of a query.", }) - m.chunkSizeBytes = prometheus.NewHistogram(prometheus.HistogramOpts{ + m.chunkSizeBytes = promauto.With(reg).NewHistogram(prometheus.HistogramOpts{ Name: "thanos_bucket_store_sent_chunk_size_bytes", Help: "Size in bytes of the chunks for the single series, which is adequate to the gRPC message size sent to querier.", Buckets: []float64{ @@ -167,40 +168,18 @@ func newBucketStoreMetrics(reg prometheus.Registerer) *bucketStoreMetrics { }, }) - m.queriesDropped = prometheus.NewCounter(prometheus.CounterOpts{ + m.queriesDropped = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_bucket_store_queries_dropped_total", Help: "Number of queries that were dropped due to the sample limit.", }) - m.queriesLimit = prometheus.NewGauge(prometheus.GaugeOpts{ + m.queriesLimit = promauto.With(reg).NewGauge(prometheus.GaugeOpts{ Name: "thanos_bucket_store_queries_concurrent_max", Help: "Number of maximum concurrent queries.", }) - m.seriesRefetches = prometheus.NewCounter(prometheus.CounterOpts{ + m.seriesRefetches = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_bucket_store_series_refetches_total", Help: fmt.Sprintf("Total number of cases where %v bytes was not enough was to fetch series from index, resulting in refetch.", maxSeriesSize), }) - - if reg != nil { - reg.MustRegister( - m.blockLoads, - m.blockLoadFailures, - m.blockDrops, - m.blockDropFailures, - m.blocksLoaded, - m.seriesDataTouched, - m.seriesDataFetched, - m.seriesDataSizeTouched, - m.seriesDataSizeFetched, - m.seriesBlocksQueried, - m.seriesGetAllDuration, - m.seriesMergeDuration, - m.resultSeriesCount, - m.chunkSizeBytes, - m.queriesDropped, - m.queriesLimit, - m.seriesRefetches, - ) - } return &m } diff --git a/pkg/store/bucket_test.go b/pkg/store/bucket_test.go index 94550c7b5bc..a95372ec8f0 100644 --- a/pkg/store/bucket_test.go +++ b/pkg/store/bucket_test.go @@ -28,6 +28,7 @@ import ( "github.com/leanovate/gopter/prop" "github.com/oklog/ulid" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" promtest "github.com/prometheus/client_golang/prometheus/testutil" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/relabel" @@ -1298,7 +1299,7 @@ func benchSeries(t testutil.TB, number int, dimension Dimension, cases ...int) { partitioner: gapBasedPartitioner{maxGapSize: partitionerMaxGapSize}, chunkObjs: []string{filepath.Join(id.String(), "chunks", "000001")}, chunkPool: chunkPool, - seriesRefetches: prometheus.NewCounter(prometheus.CounterOpts{}), + seriesRefetches: promauto.With(nil).NewCounter(prometheus.CounterOpts{}), } blocks = append(blocks, b) } diff --git a/pkg/store/cache/inmemory.go b/pkg/store/cache/inmemory.go index 739d73b5bbd..abc589f1db7 100644 --- a/pkg/store/cache/inmemory.go +++ b/pkg/store/cache/inmemory.go @@ -16,6 +16,7 @@ import ( "github.com/oklog/ulid" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/prometheus/pkg/labels" "gopkg.in/yaml.v2" ) @@ -89,77 +90,74 @@ func NewInMemoryIndexCacheWithConfig(logger log.Logger, reg prometheus.Registere maxItemSizeBytes: uint64(config.MaxItemSize), } - c.evicted = prometheus.NewCounterVec(prometheus.CounterOpts{ + c.evicted = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_store_index_cache_items_evicted_total", Help: "Total number of items that were evicted from the index cache.", }, []string{"item_type"}) c.evicted.WithLabelValues(cacheTypePostings) c.evicted.WithLabelValues(cacheTypeSeries) - c.added = prometheus.NewCounterVec(prometheus.CounterOpts{ + c.added = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_store_index_cache_items_added_total", Help: "Total number of items that were added to the index cache.", }, []string{"item_type"}) c.added.WithLabelValues(cacheTypePostings) c.added.WithLabelValues(cacheTypeSeries) - c.requests = prometheus.NewCounterVec(prometheus.CounterOpts{ + c.requests = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_store_index_cache_requests_total", Help: "Total number of requests to the cache.", }, []string{"item_type"}) c.requests.WithLabelValues(cacheTypePostings) c.requests.WithLabelValues(cacheTypeSeries) - c.overflow = prometheus.NewCounterVec(prometheus.CounterOpts{ + c.overflow = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_store_index_cache_items_overflowed_total", Help: "Total number of items that could not be added to the cache due to being too big.", }, []string{"item_type"}) c.overflow.WithLabelValues(cacheTypePostings) c.overflow.WithLabelValues(cacheTypeSeries) - c.hits = prometheus.NewCounterVec(prometheus.CounterOpts{ + c.hits = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_store_index_cache_hits_total", Help: "Total number of requests to the cache that were a hit.", }, []string{"item_type"}) c.hits.WithLabelValues(cacheTypePostings) c.hits.WithLabelValues(cacheTypeSeries) - c.current = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + c.current = promauto.With(reg).NewGaugeVec(prometheus.GaugeOpts{ Name: "thanos_store_index_cache_items", Help: "Current number of items in the index cache.", }, []string{"item_type"}) c.current.WithLabelValues(cacheTypePostings) c.current.WithLabelValues(cacheTypeSeries) - c.currentSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + c.currentSize = promauto.With(reg).NewGaugeVec(prometheus.GaugeOpts{ Name: "thanos_store_index_cache_items_size_bytes", Help: "Current byte size of items in the index cache.", }, []string{"item_type"}) c.currentSize.WithLabelValues(cacheTypePostings) c.currentSize.WithLabelValues(cacheTypeSeries) - c.totalCurrentSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + c.totalCurrentSize = promauto.With(reg).NewGaugeVec(prometheus.GaugeOpts{ Name: "thanos_store_index_cache_total_size_bytes", Help: "Current byte size of items (both value and key) in the index cache.", }, []string{"item_type"}) c.totalCurrentSize.WithLabelValues(cacheTypePostings) c.totalCurrentSize.WithLabelValues(cacheTypeSeries) - if reg != nil { - reg.MustRegister(prometheus.NewGaugeFunc(prometheus.GaugeOpts{ - Name: "thanos_store_index_cache_max_size_bytes", - Help: "Maximum number of bytes to be held in the index cache.", - }, func() float64 { - return float64(c.maxSizeBytes) - })) - reg.MustRegister(prometheus.NewGaugeFunc(prometheus.GaugeOpts{ - Name: "thanos_store_index_cache_max_item_size_bytes", - Help: "Maximum number of bytes for single entry to be held in the index cache.", - }, func() float64 { - return float64(c.maxItemSizeBytes) - })) - reg.MustRegister(c.requests, c.hits, c.added, c.evicted, c.current, c.currentSize, c.totalCurrentSize, c.overflow) - } + _ = promauto.With(reg).NewGaugeFunc(prometheus.GaugeOpts{ + Name: "thanos_store_index_cache_max_size_bytes", + Help: "Maximum number of bytes to be held in the index cache.", + }, func() float64 { + return float64(c.maxSizeBytes) + }) + _ = promauto.With(reg).NewGaugeFunc(prometheus.GaugeOpts{ + Name: "thanos_store_index_cache_max_item_size_bytes", + Help: "Maximum number of bytes for single entry to be held in the index cache.", + }, func() float64 { + return float64(c.maxItemSizeBytes) + }) // Initialize LRU cache with a high size limit since we will manage evictions ourselves // based on stored size using `RemoveOldest` method. diff --git a/pkg/store/cache/memcached.go b/pkg/store/cache/memcached.go index db7ec61c635..74ce30299e1 100644 --- a/pkg/store/cache/memcached.go +++ b/pkg/store/cache/memcached.go @@ -11,6 +11,7 @@ import ( "github.com/go-kit/kit/log/level" "github.com/oklog/ulid" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/prometheus/pkg/labels" "github.com/thanos-io/thanos/pkg/cacheutil" ) @@ -36,24 +37,20 @@ func NewMemcachedIndexCache(logger log.Logger, memcached cacheutil.MemcachedClie memcached: memcached, } - c.requests = prometheus.NewCounterVec(prometheus.CounterOpts{ + c.requests = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_store_index_cache_requests_total", Help: "Total number of items requests to the cache.", }, []string{"item_type"}) c.requests.WithLabelValues(cacheTypePostings) c.requests.WithLabelValues(cacheTypeSeries) - c.hits = prometheus.NewCounterVec(prometheus.CounterOpts{ + c.hits = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ Name: "thanos_store_index_cache_hits_total", Help: "Total number of items requests to the cache that were a hit.", }, []string{"item_type"}) c.hits.WithLabelValues(cacheTypePostings) c.hits.WithLabelValues(cacheTypeSeries) - if reg != nil { - reg.MustRegister(c.requests, c.hits) - } - level.Info(logger).Log("msg", "created memcached index cache") return c, nil diff --git a/pkg/store/proxy.go b/pkg/store/proxy.go index 4539ec6e093..5aeaca32588 100644 --- a/pkg/store/proxy.go +++ b/pkg/store/proxy.go @@ -19,6 +19,7 @@ import ( "github.com/opentracing/opentracing-go" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/prometheus/pkg/labels" "github.com/thanos-io/thanos/pkg/component" "github.com/thanos-io/thanos/pkg/store/storepb" @@ -63,16 +64,11 @@ type proxyStoreMetrics struct { func newProxyStoreMetrics(reg prometheus.Registerer) *proxyStoreMetrics { var m proxyStoreMetrics - m.emptyStreamResponses = prometheus.NewCounter(prometheus.CounterOpts{ + m.emptyStreamResponses = promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "thanos_proxy_store_empty_stream_responses_total", Help: "Total number of empty responses received.", }) - if reg != nil { - reg.MustRegister( - m.emptyStreamResponses, - ) - } return &m } diff --git a/pkg/ui/ui.go b/pkg/ui/ui.go index 1114b8774e9..269b22cd9c1 100644 --- a/pkg/ui/ui.go +++ b/pkg/ui/ui.go @@ -5,7 +5,6 @@ package ui import ( "bytes" - "fmt" "html/template" "io" "net/http" @@ -16,6 +15,7 @@ import ( "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" + "github.com/pkg/errors" "github.com/prometheus/common/route" "github.com/prometheus/common/version" ) @@ -58,15 +58,15 @@ func (bu *BaseUI) serveStaticAsset(w http.ResponseWriter, req *http.Request) { func (bu *BaseUI) getTemplate(name string) (string, error) { baseTmpl, err := Asset("pkg/ui/templates/_base.html") if err != nil { - return "", fmt.Errorf("error reading base template: %s", err) + return "", errors.Errorf("error reading base template: %s", err) } menuTmpl, err := Asset(filepath.Join("pkg/ui/templates", bu.menuTmpl)) if err != nil { - return "", fmt.Errorf("error reading menu template %s: %s", bu.menuTmpl, err) + return "", errors.Errorf("error reading menu template %s: %s", bu.menuTmpl, err) } pageTmpl, err := Asset(filepath.Join("pkg/ui/templates", name)) if err != nil { - return "", fmt.Errorf("error reading page template %s: %s", name, err) + return "", errors.Errorf("error reading page template %s: %s", name, err) } return string(baseTmpl) + string(menuTmpl) + string(pageTmpl), nil }