From ae34415c2057047e4a25bed5fcf85241de208f69 Mon Sep 17 00:00:00 2001 From: Chris Mark Date: Wed, 26 Feb 2020 11:51:47 +0200 Subject: [PATCH] Apply filters on prometheus 'up' metrics (#16568) --- .../prometheus/collector/_meta/docs.asciidoc | 23 +++++++++++++++---- .../module/prometheus/collector/collector.go | 19 ++++++++++----- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/metricbeat/module/prometheus/collector/_meta/docs.asciidoc b/metricbeat/module/prometheus/collector/_meta/docs.asciidoc index 0c93d9aac690..82d58cecab28 100644 --- a/metricbeat/module/prometheus/collector/_meta/docs.asciidoc +++ b/metricbeat/module/prometheus/collector/_meta/docs.asciidoc @@ -51,12 +51,27 @@ In order to filter out/in metrics one can make use of `metrics_filters.include` ------------------------------------------------------------------------------------- - module: prometheus period: 10s - hosts: ["localhost:9092"] + hosts: ["localhost:9090"] metrics_path: /metrics metrics_filters: include: ["node_filesystem_*"] - exclude: ["node_filesystem_device_*", "node_filesystem_readonly"] + exclude: ["node_filesystem_device_*"] ------------------------------------------------------------------------------------- -The configuration above will include only metrics that match `node_filesystem_*` pattern and do not match `node_filesystem_device_*` -and are not `node_filesystem_readonly` metric. +The configuration above will include only metrics that match `node_filesystem_*` pattern and do not match `node_filesystem_device_*`. + + +To keep only specific metrics, anchor the start and the end of the regexp of each metric: + +- the caret ^ matches the beginning of a text or line, +- the dollar sign $ matches the end of a text. + +[source,yaml] +------------------------------------------------------------------------------------- +- module: prometheus + period: 10s + hosts: ["localhost:9090"] + metrics_path: /metrics + metrics_filters: + include: ["^node_network_net_dev_group$", "^node_network_up$"] +------------------------------------------------------------------------------------- diff --git a/metricbeat/module/prometheus/collector/collector.go b/metricbeat/module/prometheus/collector/collector.go index 41a67076e525..c23b08a1fa04 100644 --- a/metricbeat/module/prometheus/collector/collector.go +++ b/metricbeat/module/prometheus/collector/collector.go @@ -148,6 +148,10 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { } func (m *MetricSet) addUpEvent(eventList map[string]common.MapStr, up int) { + metricName := "up" + if m.skipFamilyName(metricName) { + return + } upPromEvent := PromEvent{ labels: common.MapStr{ "instance": m.Host(), @@ -164,6 +168,13 @@ func (m *MetricSet) addUpEvent(eventList map[string]common.MapStr, up int) { } func (m *MetricSet) skipFamily(family *dto.MetricFamily) bool { + if family == nil { + return false + } + return m.skipFamilyName(*family.Name) +} + +func (m *MetricSet) skipFamilyName(family string) bool { // example: // include_metrics: // - node_* @@ -173,19 +184,15 @@ func (m *MetricSet) skipFamily(family *dto.MetricFamily) bool { // This would mean that we want to keep only the metrics that start with node_ prefix but // are not related to disk so we exclude node_disk_* metrics from them. - if family == nil { - return true - } - // if include_metrics are defined, check if this metric should be included if len(m.includeMetrics) > 0 { - if !matchMetricFamily(*family.Name, m.includeMetrics) { + if !matchMetricFamily(family, m.includeMetrics) { return true } } // now exclude the metric if it matches any of the given patterns if len(m.excludeMetrics) > 0 { - if matchMetricFamily(*family.Name, m.excludeMetrics) { + if matchMetricFamily(family, m.excludeMetrics) { return true } }