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 Apr 8, 2024
1 parent c586fcc commit 3c75821
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
17 changes: 9 additions & 8 deletions prometheus/collectors/gen_go_collector_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,19 @@ 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)
}
}

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 @@ -161,6 +159,9 @@ var testFile = template.Must(template.New("testFile").Funcs(map[string]interface
"nextVersion": func(version goVersion) string {
return (version + goVersion(1)).String()
},
"needsBaseMetrics": func(groupName string) bool {
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");
// you may not use this file except in compliance with 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{})
}
4 changes: 4 additions & 0 deletions prometheus/collectors/go_collector_go120_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,7 @@ func withSchedulerMetrics() []string {
"go_sched_latencies_seconds",
})
}

func withDebugMetrics() []string {
return withBaseMetrics([]string{})
}
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 3c75821

Please sign in to comment.