diff --git a/Makefile b/Makefile index 95382bb1775..4dd44746845 100644 --- a/Makefile +++ b/Makefile @@ -78,9 +78,19 @@ 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 +# TODO(bwplotka): Add Printf, NewGaugeFunc and MustRegister once execption are accepted. +FAILLINT_PATHS ?="errors=github.com/pkg/errors,\ +fmt.{Errorf}=github.com/pkg/errors.{Errorf},\ +fmt.{Print,Println,Sprint},\ +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.{DefaultRegisterer,DefaultGatherer,DefBuckets,NewUntypedFunc,UntypedFunc},\ +github.com/prometheus/client_golang/prometheus.{NewCounter,NewCounterVec,NewCounterVec,NewGauge,NewGaugeVec,\ +NewHistorgram,NewHistogramVec,NewSummary,NewSummaryVec}=github.com/prometheus/client_golang/prometheus/promauto.{NewCounter,\ +NewCounterVec,NewCounterVec,NewGauge,NewGaugeVec,NewGaugeFunc,NewHistorgram,NewHistogramVec,NewSummary,NewSummaryVec}" # fetch_go_bin_version downloads (go gets) the binary from specific version and installs it in $(GOBIN)/- # arguments: @@ -139,7 +149,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 +304,7 @@ 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) ./... + @$(FAILLINT) -paths $(FAILLINT_PATHS) ./... @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/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_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/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/compact/compact.go b/pkg/compact/compact.go index b268724e108..dc9e6404cad 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 } 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..e9fca0e1f51 100644 --- a/pkg/extprom/tx_gauge.go +++ b/pkg/extprom/tx_gauge.go @@ -24,7 +24,7 @@ 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 { f := func() *prometheus.GaugeVec { g := prometheus.NewGaugeVec(opts, labelNames) for _, vals := range initLabelValues { @@ -32,10 +32,14 @@ func NewTxGaugeVec(opts prometheus.GaugeOpts, labelNames []string, initLabelValu } 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..1f27be46257 100644 --- a/pkg/extprom/tx_gauge_test.go +++ b/pkg/extprom/tx_gauge_test.go @@ -14,7 +14,7 @@ import ( ) 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"}) 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/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/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..5b66aea3fe1 100644 --- a/pkg/replicate/replicater.go +++ b/pkg/replicate/replicater.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/model" "github.com/prometheus/prometheus/pkg/labels" "github.com/thanos-io/thanos/pkg/compact" @@ -139,19 +140,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, diff --git a/pkg/replicate/scheme.go b/pkg/replicate/scheme.go index 30be01f0f2a..d8779e5064e 100644 --- a/pkg/replicate/scheme.go +++ b/pkg/replicate/scheme.go @@ -17,6 +17,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 +125,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 } 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/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 }