Skip to content

Commit

Permalink
gocollector: Add regex option to allow collection of debug runtime me…
Browse files Browse the repository at this point in the history
…trics

Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com>
  • Loading branch information
Arthur Silva Sens committed Mar 30, 2024
1 parent 93cf5d4 commit f78903c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 16 deletions.
16 changes: 7 additions & 9 deletions prometheus/collectors/gen_go_collector_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -148,13 +148,11 @@ func groupMetrics(metricsList []string) []metricGroup {
matchedMetrics = append(matchedMetrics, baseMatrices...)
}
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
}
Expand All @@ -174,7 +172,7 @@ var testFile = template.Must(template.New("testFile").Funcs(map[string]interface
return (version + goVersion(1)).String()
},
"needsBaseMetrics": func(groupName string) bool {
return groupName == "withAllMetrics" || groupName == "withGCMetrics" || groupName == "withMemoryMetrics"
return groupName == "withAllMetrics" || groupName == "withGCMetrics" || groupName == "withMemoryMetrics" || groupName == "withDebugMetrics"
},
}).Parse(`// Copyright 2022 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
4 changes: 4 additions & 0 deletions prometheus/collectors/go_collector_go117_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ func withSchedulerMetrics() []string {
"go_threads",
}
}

func withDebugMetrics() []string {
return withBaseMetrics([]string{})
}
4 changes: 4 additions & 0 deletions prometheus/collectors/go_collector_go119_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ func withSchedulerMetrics() []string {
"go_threads",
}
}

func withDebugMetrics() []string {
return withBaseMetrics([]string{})
}
10 changes: 7 additions & 3 deletions prometheus/collectors/go_collector_go120_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ package collectors
func withAllMetrics() []string {
return withBaseMetrics([]string{
"go_cgo_go_to_c_calls_calls_total",
"go_gc_cycles_automatic_gc_cycles_total",
"go_gc_cycles_forced_gc_cycles_total",
"go_gc_cycles_total_gc_cycles_total",
"go_cpu_classes_gc_mark_assist_cpu_seconds_total",
"go_cpu_classes_gc_mark_dedicated_cpu_seconds_total",
"go_cpu_classes_gc_mark_idle_cpu_seconds_total",
Expand All @@ -33,6 +30,9 @@ func withAllMetrics() []string {
"go_cpu_classes_scavenge_total_cpu_seconds_total",
"go_cpu_classes_total_cpu_seconds_total",
"go_cpu_classes_user_cpu_seconds_total",
"go_gc_cycles_automatic_gc_cycles_total",
"go_gc_cycles_forced_gc_cycles_total",
"go_gc_cycles_total_gc_cycles_total",
"go_gc_heap_allocs_by_size_bytes",
"go_gc_heap_allocs_bytes_total",
"go_gc_heap_allocs_objects_total",
Expand Down Expand Up @@ -117,3 +117,7 @@ func withSchedulerMetrics() []string {
"go_threads",
}
}

func withDebugMetrics() []string {
return withBaseMetrics([]string{})
}
4 changes: 2 additions & 2 deletions prometheus/collectors/go_collector_go121_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func withSchedulerMetrics() []string {
}

func withDebugMetrics() []string {
return []string{
return withBaseMetrics([]string{
"go_godebug_non_default_behavior_execerrdot_events_total",
"go_godebug_non_default_behavior_gocachehash_events_total",
"go_godebug_non_default_behavior_gocachetest_events_total",
Expand All @@ -170,5 +170,5 @@ func withDebugMetrics() []string {
"go_godebug_non_default_behavior_x509sha1_events_total",
"go_godebug_non_default_behavior_x509usefallbackroots_events_total",
"go_godebug_non_default_behavior_zipinsecurepath_events_total",
}
})
}
4 changes: 2 additions & 2 deletions prometheus/collectors/go_collector_go122_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func withSchedulerMetrics() []string {
}

func withDebugMetrics() []string {
return []string{
return withBaseMetrics([]string{
"go_godebug_non_default_behavior_execerrdot_events_total",
"go_godebug_non_default_behavior_gocachehash_events_total",
"go_godebug_non_default_behavior_gocachetest_events_total",
Expand All @@ -192,5 +192,5 @@ func withDebugMetrics() []string {
"go_godebug_non_default_behavior_x509usefallbackroots_events_total",
"go_godebug_non_default_behavior_x509usepolicies_events_total",
"go_godebug_non_default_behavior_zipinsecurepath_events_total",
}
})
}
3 changes: 3 additions & 0 deletions prometheus/collectors/go_collector_latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions prometheus/collectors/go_collector_latest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f78903c

Please sign in to comment.