From c1ffe8b3918053c59ae265f4498f24f58e165cbc Mon Sep 17 00:00:00 2001 From: Chris Mark Date: Wed, 17 Jun 2020 12:44:27 +0300 Subject: [PATCH] Stop counterCache only when already started (#19103) (cherry picked from commit 4a45bb443cd60a72df049e12d685a5ecb69b7e8e) --- CHANGELOG.next.asciidoc | 1 + .../module/prometheus/collector/collector.go | 33 +++++++++++-------- .../module/prometheus/collector/data.go | 2 ++ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 9c2943d5f43..3d5ebbf49e7 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -285,6 +285,7 @@ field. You can revert this change by configuring tags for the module and omittin - Fix compute and pubsub dashboard for googlecloud module. {issue}18962[18962] {pull}18980[18980] - Fix crash on vsphere module when Host information is not available. {issue}18996[18996] {pull}19078[19078] - Fix incorrect usage of hints builder when exposed port is a substring of the hint {pull}19052[19052] +- Stop counterCache only when already started {pull}19103[19103] *Packetbeat* diff --git a/metricbeat/module/prometheus/collector/collector.go b/metricbeat/module/prometheus/collector/collector.go index 690672ff829..6941f30bd8a 100644 --- a/metricbeat/module/prometheus/collector/collector.go +++ b/metricbeat/module/prometheus/collector/collector.go @@ -19,7 +19,6 @@ package collector import ( "regexp" - "sync" "github.com/pkg/errors" dto "github.com/prometheus/client_model/go" @@ -76,13 +75,13 @@ type PromEventsGeneratorFactory func(ms mb.BaseMetricSet) (PromEventsGenerator, // MetricSet for fetching prometheus data type MetricSet struct { mb.BaseMetricSet - prometheus p.Prometheus - includeMetrics []*regexp.Regexp - excludeMetrics []*regexp.Regexp - namespace string - promEventsGen PromEventsGenerator - once sync.Once - host string + prometheus p.Prometheus + includeMetrics []*regexp.Regexp + excludeMetrics []*regexp.Regexp + namespace string + promEventsGen PromEventsGenerator + host string + eventGenStarted bool } // MetricSetBuilder returns a builder function for a new Prometheus metricset using @@ -104,10 +103,11 @@ func MetricSetBuilder(namespace string, genFactory PromEventsGeneratorFactory) f } ms := &MetricSet{ - BaseMetricSet: base, - prometheus: prometheus, - namespace: namespace, - promEventsGen: promEventsGen, + BaseMetricSet: base, + prometheus: prometheus, + namespace: namespace, + promEventsGen: promEventsGen, + eventGenStarted: false, } // store host here to use it as a pointer when building `up` metric ms.host = ms.Host() @@ -126,7 +126,10 @@ func MetricSetBuilder(namespace string, genFactory PromEventsGeneratorFactory) f // Fetch fetches data and reports it func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { - m.once.Do(m.promEventsGen.Start) + if !m.eventGenStarted { + m.promEventsGen.Start() + m.eventGenStarted = true + } families, err := m.prometheus.GetFamilies() eventList := map[string]common.MapStr{} @@ -186,7 +189,9 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { // Close stops the metricset func (m *MetricSet) Close() error { - m.promEventsGen.Stop() + if m.eventGenStarted { + m.promEventsGen.Stop() + } return nil } diff --git a/x-pack/metricbeat/module/prometheus/collector/data.go b/x-pack/metricbeat/module/prometheus/collector/data.go index 8f747ce16ff..23ef386291b 100644 --- a/x-pack/metricbeat/module/prometheus/collector/data.go +++ b/x-pack/metricbeat/module/prometheus/collector/data.go @@ -10,6 +10,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" + "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/prometheus/collector" @@ -54,6 +55,7 @@ func (g *typedGenerator) Start() { } func (g *typedGenerator) Stop() { + logp.Debug("prometheus.collector.cache", "stopping counterCache") g.counterCache.Stop() }