Skip to content

Commit

Permalink
Apply filters on prometheus 'up' metrics (#16568)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrsMark authored Feb 26, 2020
1 parent 65b31bd commit ae34415
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
23 changes: 19 additions & 4 deletions metricbeat/module/prometheus/collector/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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$"]
-------------------------------------------------------------------------------------
19 changes: 13 additions & 6 deletions metricbeat/module/prometheus/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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_*
Expand All @@ -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
}
}
Expand Down

0 comments on commit ae34415

Please sign in to comment.