Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop counterCache only when already started #19103

Merged
merged 11 commits into from
Jun 17, 2020
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- 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*

Expand Down
42 changes: 30 additions & 12 deletions metricbeat/module/prometheus/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package collector
import (
"regexp"
"sync"
"time"

"github.com/pkg/errors"
dto "github.com/prometheus/client_model/go"
Expand Down Expand Up @@ -76,13 +77,15 @@ 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
once sync.Once
host string
eventGenStarted bool
period time.Duration
}

// MetricSetBuilder returns a builder function for a new Prometheus metricset using
Expand All @@ -104,10 +107,12 @@ 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,
period: config.Period,
}
// store host here to use it as a pointer when building `up` metric
ms.host = ms.Host()
Expand All @@ -126,7 +131,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)
m.once.Do(func() {
m.promEventsGen.Start()
m.eventGenStarted = true
})
jsoriano marked this conversation as resolved.
Show resolved Hide resolved

families, err := m.prometheus.GetFamilies()
eventList := map[string]common.MapStr{}
Expand Down Expand Up @@ -186,6 +194,16 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {

// Close stops the metricset
func (m *MetricSet) Close() error {
attempts := 0
for !m.eventGenStarted {
time.Sleep(m.period)
// waiting 2 periods so as the first Fetch to start the generator, otherwise return since
// Fetch is not called at all
if attempts > 2 {
return nil
}
attempts++
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need a backoff here. MB will make sure Fetch is not called after Close. This could just be:

if !m.eventGenStarted {
m.promEventsGen.Stop()
}

m.promEventsGen.Stop()
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions metricbeat/module/prometheus/collector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@

package collector

import (
"time"
)

type metricsetConfig struct {
MetricsFilters MetricFilters `config:"metrics_filters" yaml:"metrics_filters,omitempty"`
Period time.Duration `config:"period" yaml:"period"`
}

type MetricFilters struct {
Expand Down
1 change: 0 additions & 1 deletion x-pack/filebeat/input/cloudfoundry/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/elastic/beats/v7/filebeat/channel"
"github.com/elastic/beats/v7/filebeat/input"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/cfgwarn"
"github.com/elastic/beats/v7/libbeat/logp"
Expand Down
2 changes: 2 additions & 0 deletions x-pack/metricbeat/module/prometheus/collector/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -54,6 +55,7 @@ func (g *typedGenerator) Start() {
}

func (g *typedGenerator) Stop() {
logp.Debug("prometheus.collector.cache", "stopping counterCache")
g.counterCache.Stop()
}

Expand Down