From 48e7b73a0a244f1156ef6c606e661278caa1c6d9 Mon Sep 17 00:00:00 2001 From: Andrew Gizas Date: Wed, 10 Apr 2024 11:56:21 +0300 Subject: [PATCH] Setting Prometheus Remote_write Period default value to 1m (#38553) * Correcting period for Prometheus remote_write * Update CHANGELOG.next.asciidoc (cherry picked from commit e798bb158c3ce8ae2fccdf0024da32136ed5217b) --- CHANGELOG.next.asciidoc | 2 +- .../prometheus/remote_write/_meta/docs.asciidoc | 7 +++++++ .../module/prometheus/remote_write/config.go | 17 +++++++++++++++-- .../module/prometheus/remote_write/data.go | 3 ++- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 7114d9659cee..070fb0d89805 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -21,7 +21,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff] *Heartbeat* *Metricbeat* - +- Setting period for counter cache for Prometheus remote_write at least to 60sec {pull}38553[38553] *Osquerybeat* diff --git a/metricbeat/module/prometheus/remote_write/_meta/docs.asciidoc b/metricbeat/module/prometheus/remote_write/_meta/docs.asciidoc index 2e1cc2ca4f0d..d99f9837160c 100644 --- a/metricbeat/module/prometheus/remote_write/_meta/docs.asciidoc +++ b/metricbeat/module/prometheus/remote_write/_meta/docs.asciidoc @@ -86,6 +86,9 @@ metricbeat.modules: metricsets: ["remote_write"] host: "localhost" port: "9201" + use_types: true + rate_counters: true + period: 60s ------------------------------------------------------------------------------------- `use_types` parameter (default: false) enables a different layout for metrics storage, leveraging Elasticsearch @@ -95,6 +98,10 @@ types, including https://www.elastic.co/guide/en/elasticsearch/reference/current the counter increment since the last collection. This metric should make some aggregations easier and with better performance. This parameter can only be enabled in combination with `use_types`. +`period` parameter (default: 60s) configures the timeout of internal cache, which stores counter values in order to calculate rates between consecutive fetches. The parameter will be validated and all values lower than 60sec will be reset to the default value. + +Note that by default prometheus pushes data with the interval of 60s (in remote write). In case that prometheus push rate is changed, the `period` parameter needs to be configured accordingly. + When `use_types` and `rate_counters` are enabled, metrics are stored like this: [source,json] diff --git a/x-pack/metricbeat/module/prometheus/remote_write/config.go b/x-pack/metricbeat/module/prometheus/remote_write/config.go index 8c5fe12a6592..9e86facb1d08 100644 --- a/x-pack/metricbeat/module/prometheus/remote_write/config.go +++ b/x-pack/metricbeat/module/prometheus/remote_write/config.go @@ -4,12 +4,16 @@ package remote_write -import "errors" +import ( + "errors" + "time" +) type config struct { UseTypes bool `config:"use_types"` RateCounters bool `config:"rate_counters"` TypesPatterns TypesPatterns `config:"types_patterns" yaml:"types_patterns,omitempty"` + Period time.Duration `config:"period" validate:"positive"` } type TypesPatterns struct { @@ -21,12 +25,21 @@ var defaultConfig = config{ TypesPatterns: TypesPatterns{ CounterPatterns: nil, HistogramPatterns: nil}, + Period: time.Second * 60, } func (c *config) Validate() error { if c.RateCounters && !c.UseTypes { return errors.New("'rate_counters' can only be enabled when `use_types` is also enabled") } - + duration, err := time.ParseDuration(c.Period.String()) + { + if err != nil { + return err + } else if duration < 60*time.Second { + // by default prometheus push data with the interval 60s, in order to calculate counter rate we are setting Period to 60secs accordingly + c.Period = time.Second * 60 + } + } return nil } diff --git a/x-pack/metricbeat/module/prometheus/remote_write/data.go b/x-pack/metricbeat/module/prometheus/remote_write/data.go index a1bdbd58c204..5161d785fb02 100644 --- a/x-pack/metricbeat/module/prometheus/remote_write/data.go +++ b/x-pack/metricbeat/module/prometheus/remote_write/data.go @@ -44,9 +44,10 @@ func remoteWriteEventsGeneratorFactory(base mb.BaseMetricSet) (remote_write.Remo } if config.UseTypes { + logp.Debug("prometheus.remote_write.cache", "Period for counter cache for remote_write: %v", config.Period.String()) // use a counter cache with a timeout of 5x the period, as a safe value // to make sure that all counters are available between fetches - counters := collector.NewCounterCache(base.Module().Config().Period * 5) + counters := collector.NewCounterCache(config.Period * 5) g := remoteWriteTypedGenerator{ counterCache: counters,