From 1dc55cc0c9209754f7b0c2e6930267b0488429cb Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 15 Apr 2024 08:00:38 +0300 Subject: [PATCH] register std metrics once on init Signed-off-by: Vasiliy Tolstov --- prometheus.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/prometheus.go b/prometheus.go index 36fd917..a89fb0f 100644 --- a/prometheus.go +++ b/prometheus.go @@ -288,6 +288,14 @@ func (m *prometheusMeter) Init(opts ...meter.Option) error { for _, o := range opts { o(&m.opts) } + + if m.opts.WriteProcessMetrics || m.opts.WriteFDMetrics { + pc := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}) + _ = m.set.Register(pc) + gc := collectors.NewGoCollector(collectors.WithGoCollectorRuntimeMetrics(collectors.GoRuntimeMetricsRule{Matcher: regexp.MustCompile("/.*")})) + _ = m.set.Register(gc) + } + return nil } @@ -297,13 +305,6 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error { o(&options) } - if options.WriteProcessMetrics || options.WriteFDMetrics { - pc := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}) - _ = m.set.Register(pc) - gc := collectors.NewGoCollector(collectors.WithGoCollectorRuntimeMetrics(collectors.GoRuntimeMetricsRule{Matcher: regexp.MustCompile("/.*")})) - _ = m.set.Register(gc) - } - g, ok := m.set.(prometheus.Gatherer) if !ok { return fmt.Errorf("set type %T not prometheus.Gatherer", m.set)