From 1495848807c25b9de3f8ec3e2de1322bbf764796 Mon Sep 17 00:00:00 2001 From: Bartlomiej Plotka Date: Tue, 12 Apr 2022 13:02:05 +0100 Subject: [PATCH] gocollector: Added options to Go Collector for changing the Fixes https://github.com/prometheus/client_golang/issues/983 Signed-off-by: Bartlomiej Plotka --- prometheus/collectors/go_collector.go | 12 ++++++++++-- ...go_collector_go117.go => go_collector_latest.go} | 13 ++++++++++++- ...or_go117_test.go => go_collector_latest_test.go} | 0 3 files changed, 22 insertions(+), 3 deletions(-) rename prometheus/{go_collector_go117.go => go_collector_latest.go} (96%) rename prometheus/{go_collector_go117_test.go => go_collector_latest_test.go} (100%) diff --git a/prometheus/collectors/go_collector.go b/prometheus/collectors/go_collector.go index edaa4e50b..ce89da811 100644 --- a/prometheus/collectors/go_collector.go +++ b/prometheus/collectors/go_collector.go @@ -15,6 +15,14 @@ package collectors import "github.com/prometheus/client_golang/prometheus" +type goCollectorOption func(o *prometheus.GoCollectorOptions) + +func WithTBD() goCollectorOption { + return func(o *prometheus.GoCollectorOptions) { + o.TBD = true + } +} + // NewGoCollector returns a collector that exports metrics about the current Go // process. This includes memory stats. To collect those, runtime.ReadMemStats // is called. This requires to “stop the world”, which usually only happens for @@ -41,9 +49,9 @@ import "github.com/prometheus/client_golang/prometheus" // // NOTE: The problem is solved in Go 1.15, see // https://github.com/golang/go/issues/19812 for the related Go issue. -func NewGoCollector() prometheus.Collector { +func NewGoCollector(opts ...prometheus.GoCollectorOption) prometheus.Collector { //nolint:staticcheck // Ignore SA1019 until v2. - return prometheus.NewGoCollector() + return prometheus.NewGoCollector(opts...) } // NewBuildInfoCollector returns a collector collecting a single metric diff --git a/prometheus/go_collector_go117.go b/prometheus/go_collector_latest.go similarity index 96% rename from prometheus/go_collector_go117.go rename to prometheus/go_collector_latest.go index d43bdcdda..1847ac110 100644 --- a/prometheus/go_collector_go117.go +++ b/prometheus/go_collector_latest.go @@ -51,11 +51,22 @@ type goCollector struct { msMetrics memStatsMetrics } +type GoCollectorOption func(o *GoCollectorOptions) + +// GoCollectorOptions should not used be directly by anyone, except `collectors` package. +// Use it via collectors package instead. See issue why it exists in this package +// https://github.com/prometheus/client_golang/issues/1030. +// +// Deprecated: Use collectors.With +type GoCollectorOptions struct { + TBD bool +} + // NewGoCollector is the obsolete version of collectors.NewGoCollector. // See there for documentation. // // Deprecated: Use collectors.NewGoCollector instead. -func NewGoCollector() Collector { +func NewGoCollector(opts ...GoCollectorOption) Collector { descriptions := metrics.All() // Collect all histogram samples so that we can get their buckets. diff --git a/prometheus/go_collector_go117_test.go b/prometheus/go_collector_latest_test.go similarity index 100% rename from prometheus/go_collector_go117_test.go rename to prometheus/go_collector_latest_test.go