Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Copy Markdown
Contributor

@mtojek mtojek Feb 26, 2020

Choose a reason for hiding this comment

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

Ok, I see what you did here.

I'm not sure if it isn't safer to keep the family == nil check here, but split the whole method body into two methods: skipFamily and skipFamilyName. WDYT? It might prevent next developers from making mistakes (nil structure).

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