From 0f500036fcf00e1e2dd7f2980c640c5d2613363e Mon Sep 17 00:00:00 2001 From: Arthur Silva Sens Date: Wed, 29 Nov 2023 16:10:53 -0300 Subject: [PATCH] gocollector: Add regex option to allow collection of debug runtime metrics Signed-off-by: Arthur Silva Sens --- prometheus/collectors/gen_go_collector_set.go | 14 ++++++-------- prometheus/collectors/go_collector_go117_test.go | 4 ++++ prometheus/collectors/go_collector_go119_test.go | 4 ++++ prometheus/collectors/go_collector_go120_test.go | 4 ++++ prometheus/collectors/go_collector_latest.go | 3 +++ prometheus/collectors/go_collector_latest_test.go | 5 +++++ 6 files changed, 26 insertions(+), 8 deletions(-) diff --git a/prometheus/collectors/gen_go_collector_set.go b/prometheus/collectors/gen_go_collector_set.go index 48b3ae5..6d1ff7e 100644 --- a/prometheus/collectors/gen_go_collector_set.go +++ b/prometheus/collectors/gen_go_collector_set.go @@ -128,7 +128,7 @@ func rm2prom(d metrics.Description) string { func groupMetrics(metricsList []string) []metricGroup { var groupedMetrics []metricGroup for _, group := range metricGroups { - var matchedMetrics []string + matchedMetrics := make([]string, 0) for _, metric := range metricsList { if group.Regex == nil || group.Regex.MatchString(metric) { matchedMetrics = append(matchedMetrics, metric) @@ -136,13 +136,11 @@ func groupMetrics(metricsList []string) []metricGroup { } sort.Strings(matchedMetrics) - if len(matchedMetrics) > 0 { - groupedMetrics = append(groupedMetrics, metricGroup{ - Name: group.Name, - Regex: group.Regex, - Metrics: matchedMetrics, - }) - } + groupedMetrics = append(groupedMetrics, metricGroup{ + Name: group.Name, + Regex: group.Regex, + Metrics: matchedMetrics, + }) } return groupedMetrics } diff --git a/prometheus/collectors/go_collector_go117_test.go b/prometheus/collectors/go_collector_go117_test.go index 370758d..760ad04 100644 --- a/prometheus/collectors/go_collector_go117_test.go +++ b/prometheus/collectors/go_collector_go117_test.go @@ -98,3 +98,7 @@ func withSchedulerMetrics() []string { "go_threads", } } + +func withDebugMetrics() []string { + return withBaseMetrics([]string{}) +} diff --git a/prometheus/collectors/go_collector_go119_test.go b/prometheus/collectors/go_collector_go119_test.go index 4577a21..1ef9a4c 100644 --- a/prometheus/collectors/go_collector_go119_test.go +++ b/prometheus/collectors/go_collector_go119_test.go @@ -105,3 +105,7 @@ func withSchedulerMetrics() []string { "go_threads", } } + +func withDebugMetrics() []string { + return withBaseMetrics([]string{}) +} diff --git a/prometheus/collectors/go_collector_go120_test.go b/prometheus/collectors/go_collector_go120_test.go index d87ba5d..723cbf8 100644 --- a/prometheus/collectors/go_collector_go120_test.go +++ b/prometheus/collectors/go_collector_go120_test.go @@ -112,3 +112,7 @@ func withSchedulerMetrics() []string { "go_sched_latencies_seconds", }) } + +func withDebugMetrics() []string { + return withBaseMetrics([]string{}) +} diff --git a/prometheus/collectors/go_collector_latest.go b/prometheus/collectors/go_collector_latest.go index bcfa4fa..771a72a 100644 --- a/prometheus/collectors/go_collector_latest.go +++ b/prometheus/collectors/go_collector_latest.go @@ -37,6 +37,9 @@ var ( // MetricsScheduler allows only scheduler metrics to be collected from Go runtime. // e.g. go_sched_goroutines_goroutines MetricsScheduler = GoRuntimeMetricsRule{regexp.MustCompile(`^/sched/.*`)} + // MetricsDebug allows only debug metrics to be collected from Go runtime. + // e.g. go_godebug_non_default_behavior_gocachetest_events_total + MetricsDebug = GoRuntimeMetricsRule{regexp.MustCompile(`^/godebug/.*`)} ) // WithGoCollectorMemStatsMetricsDisabled disables metrics that is gathered in runtime.MemStats structure such as: diff --git a/prometheus/collectors/go_collector_latest_test.go b/prometheus/collectors/go_collector_latest_test.go index ebde891..76673f3 100644 --- a/prometheus/collectors/go_collector_latest_test.go +++ b/prometheus/collectors/go_collector_latest_test.go @@ -105,6 +105,11 @@ func TestGoCollectorAllowList(t *testing.T) { rules: []GoRuntimeMetricsRule{MetricsScheduler}, expected: withSchedulerMetrics(), }, + { + name: "allow debug", + rules: []GoRuntimeMetricsRule{MetricsDebug}, + expected: withDebugMetrics(), + }, } { t.Run(test.name, func(t *testing.T) { reg := prometheus.NewRegistry()