From ff60566f1bb3fb4eaeea0c10fd68c4303360af0d Mon Sep 17 00:00:00 2001 From: Arianna Vespri <36129782+vesari@users.noreply.github.com> Date: Mon, 23 Sep 2024 15:55:38 +0200 Subject: [PATCH] Refactor default runtime metrics tests for Go collector so that default runtime metric set autogenerates (#1631) * Enable autogeneration for default runtime metrics list in collectors tests according to Go version Signed-off-by: Arianna Vespri * Adapt withDefaultRuntimeMetrics function to work regardless of the Go version Signed-off-by: Arianna Vespri * Autogenerate go collector test for go1.23 Signed-off-by: Arianna Vespri * Modify gen_go_collector_set.go to please linter and regenerate files Signed-off-by: Arianna Vespri * Simplify gen_go_collector_set.go logic by modifying func computeMetricsList Signed-off-by: Arianna Vespri * Slight simplification of withDefaultRuntimeMetrics func Signed-off-by: Arianna Vespri * Refactor withDefaultRuntimeMetrics with generated default runtime metrics subsets Signed-off-by: Arianna Vespri --------- Signed-off-by: Arianna Vespri --- prometheus/collectors/gen_go_collector_set.go | 63 ++++++++++++++++--- .../collectors/go_collector_go120_test.go | 23 +++---- .../collectors/go_collector_go121_test.go | 38 ++++------- .../collectors/go_collector_go122_test.go | 38 ++++------- .../collectors/go_collector_go123_test.go | 38 ++++------- .../collectors/go_collector_latest_test.go | 17 +++++ 6 files changed, 120 insertions(+), 97 deletions(-) diff --git a/prometheus/collectors/gen_go_collector_set.go b/prometheus/collectors/gen_go_collector_set.go index 6d1ff7e..7a5044f 100644 --- a/prometheus/collectors/gen_go_collector_set.go +++ b/prometheus/collectors/gen_go_collector_set.go @@ -78,17 +78,48 @@ func main() { v := goVersion(gv.Segments()[1]) log.Printf("generating metrics for Go version %q", v) - descriptions := computeMetricsList() + descriptions := computeMetricsList(metrics.All()) groupedMetrics := groupMetrics(descriptions) + // Find default metrics. + var defaultRuntimeDesc []metrics.Description + for _, d := range metrics.All() { + if !internal.GoCollectorDefaultRuntimeMetrics.MatchString(d.Name) { + continue + } + defaultRuntimeDesc = append(defaultRuntimeDesc, d) + } + + defaultRuntimeMetricsList := computeMetricsList(defaultRuntimeDesc) + + onlyGCDefRuntimeMetricsList := []string{} + onlySchedDefRuntimeMetricsList := []string{} + + for _, m := range defaultRuntimeMetricsList { + if strings.HasPrefix(m, "go_gc") { + onlyGCDefRuntimeMetricsList = append(onlyGCDefRuntimeMetricsList, m) + } + if strings.HasPrefix(m, "go_sched") { + onlySchedDefRuntimeMetricsList = append(onlySchedDefRuntimeMetricsList, m) + } else { + continue + } + } + // Generate code. var buf bytes.Buffer err = testFile.Execute(&buf, struct { - GoVersion goVersion - Groups []metricGroup + GoVersion goVersion + Groups []metricGroup + DefaultRuntimeMetricsList []string + OnlyGCDefRuntimeMetricsList []string + OnlySchedDefRuntimeMetricsList []string }{ - GoVersion: v, - Groups: groupedMetrics, + GoVersion: v, + Groups: groupedMetrics, + DefaultRuntimeMetricsList: defaultRuntimeMetricsList, + OnlyGCDefRuntimeMetricsList: onlyGCDefRuntimeMetricsList, + OnlySchedDefRuntimeMetricsList: onlySchedDefRuntimeMetricsList, }) if err != nil { log.Fatalf("executing template: %v", err) @@ -107,9 +138,9 @@ func main() { } } -func computeMetricsList() []string { +func computeMetricsList(descs []metrics.Description) []string { var metricsList []string - for _, d := range metrics.All() { + for _, d := range descs { if trans := rm2prom(d); trans != "" { metricsList = append(metricsList, trans) } @@ -186,4 +217,22 @@ func {{ .Name }}() []string { }) } {{ end }} + +var ( + defaultRuntimeMetrics = []string{ + {{- range $metric := .DefaultRuntimeMetricsList }} + {{ $metric | printf "%q"}}, + {{- end }} + } + onlyGCDefRuntimeMetrics = []string{ + {{- range $metric := .OnlyGCDefRuntimeMetricsList }} + {{ $metric | printf "%q"}}, + {{- end }} + } + onlySchedDefRuntimeMetrics = []string{ + {{- range $metric := .OnlySchedDefRuntimeMetricsList }} + {{ $metric | printf "%q"}}, + {{- end }} + } +) `)) diff --git a/prometheus/collectors/go_collector_go120_test.go b/prometheus/collectors/go_collector_go120_test.go index 968a016..e67fea1 100644 --- a/prometheus/collectors/go_collector_go120_test.go +++ b/prometheus/collectors/go_collector_go120_test.go @@ -16,8 +16,6 @@ package collectors -import "sort" - func withAllMetrics() []string { return withBaseMetrics([]string{ "go_cgo_go_to_c_calls_calls_total", @@ -119,17 +117,12 @@ func withDebugMetrics() []string { return withBaseMetrics([]string{}) } -var defaultRuntimeMetrics = []string{ - "go_sched_gomaxprocs_threads", -} - -func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string { - // If withoutSched is true, exclude "go_sched_gomaxprocs_threads". - if withoutSched { - return metricNames +var ( + defaultRuntimeMetrics = []string{ + "go_sched_gomaxprocs_threads", } - metricNames = append(metricNames, defaultRuntimeMetrics...) - // sorting is required - sort.Strings(metricNames) - return metricNames -} + onlyGCDefRuntimeMetrics = []string{} + onlySchedDefRuntimeMetrics = []string{ + "go_sched_gomaxprocs_threads", + } +) diff --git a/prometheus/collectors/go_collector_go121_test.go b/prometheus/collectors/go_collector_go121_test.go index f9cc318..f8a5879 100644 --- a/prometheus/collectors/go_collector_go121_test.go +++ b/prometheus/collectors/go_collector_go121_test.go @@ -16,8 +16,6 @@ package collectors -import "sort" - func withAllMetrics() []string { return withBaseMetrics([]string{ "go_cgo_go_to_c_calls_calls_total", @@ -172,27 +170,17 @@ func withDebugMetrics() []string { }) } -var defaultRuntimeMetrics = []string{ - "go_gc_gogc_percent", - "go_gc_gomemlimit_bytes", - "go_sched_gomaxprocs_threads", -} - -func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string { - if withoutGC && withoutSched { - // If both flags are true, return the metricNames as is. - return metricNames - } else if withoutGC && !withoutSched { - // If only withoutGC is true, include "go_sched_gomaxprocs_threads" only. - metricNames = append(metricNames, []string{"go_sched_gomaxprocs_threads"}...) - } else if withoutSched && !withoutGC { - // If only withoutSched is true, exclude "go_sched_gomaxprocs_threads". - metricNames = append(metricNames, []string{"go_gc_gogc_percent", "go_gc_gomemlimit_bytes"}...) - } else { - // If neither flag is true, use the default metrics. - metricNames = append(metricNames, defaultRuntimeMetrics...) +var ( + defaultRuntimeMetrics = []string{ + "go_gc_gogc_percent", + "go_gc_gomemlimit_bytes", + "go_sched_gomaxprocs_threads", } - // sorting is required - sort.Strings(metricNames) - return metricNames -} + onlyGCDefRuntimeMetrics = []string{ + "go_gc_gogc_percent", + "go_gc_gomemlimit_bytes", + } + onlySchedDefRuntimeMetrics = []string{ + "go_sched_gomaxprocs_threads", + } +) diff --git a/prometheus/collectors/go_collector_go122_test.go b/prometheus/collectors/go_collector_go122_test.go index 2413c27..2ed46ae 100644 --- a/prometheus/collectors/go_collector_go122_test.go +++ b/prometheus/collectors/go_collector_go122_test.go @@ -16,8 +16,6 @@ package collectors -import "sort" - func withAllMetrics() []string { return withBaseMetrics([]string{ "go_cgo_go_to_c_calls_calls_total", @@ -194,27 +192,17 @@ func withDebugMetrics() []string { }) } -var defaultRuntimeMetrics = []string{ - "go_gc_gogc_percent", - "go_gc_gomemlimit_bytes", - "go_sched_gomaxprocs_threads", -} - -func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string { - if withoutGC && withoutSched { - // If both flags are true, return the metricNames as is. - return metricNames - } else if withoutGC && !withoutSched { - // If only withoutGC is true, include "go_sched_gomaxprocs_threads" only. - metricNames = append(metricNames, []string{"go_sched_gomaxprocs_threads"}...) - } else if withoutSched && !withoutGC { - // If only withoutSched is true, exclude "go_sched_gomaxprocs_threads". - metricNames = append(metricNames, []string{"go_gc_gogc_percent", "go_gc_gomemlimit_bytes"}...) - } else { - // If neither flag is true, use the default metrics. - metricNames = append(metricNames, defaultRuntimeMetrics...) +var ( + defaultRuntimeMetrics = []string{ + "go_gc_gogc_percent", + "go_gc_gomemlimit_bytes", + "go_sched_gomaxprocs_threads", } - // sorting is required - sort.Strings(metricNames) - return metricNames -} + onlyGCDefRuntimeMetrics = []string{ + "go_gc_gogc_percent", + "go_gc_gomemlimit_bytes", + } + onlySchedDefRuntimeMetrics = []string{ + "go_sched_gomaxprocs_threads", + } +) diff --git a/prometheus/collectors/go_collector_go123_test.go b/prometheus/collectors/go_collector_go123_test.go index 43395dd..1c26dc2 100644 --- a/prometheus/collectors/go_collector_go123_test.go +++ b/prometheus/collectors/go_collector_go123_test.go @@ -16,8 +16,6 @@ package collectors -import "sort" - func withAllMetrics() []string { return withBaseMetrics([]string{ "go_cgo_go_to_c_calls_calls_total", @@ -206,27 +204,17 @@ func withDebugMetrics() []string { }) } -var defaultRuntimeMetrics = []string{ - "go_gc_gogc_percent", - "go_gc_gomemlimit_bytes", - "go_sched_gomaxprocs_threads", -} - -func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string { - if withoutGC && withoutSched { - // If both flags are true, return the metricNames as is. - return metricNames - } else if withoutGC && !withoutSched { - // If only withoutGC is true, include "go_sched_gomaxprocs_threads" only. - metricNames = append(metricNames, []string{"go_sched_gomaxprocs_threads"}...) - } else if withoutSched && !withoutGC { - // If only withoutSched is true, exclude "go_sched_gomaxprocs_threads". - metricNames = append(metricNames, []string{"go_gc_gogc_percent", "go_gc_gomemlimit_bytes"}...) - } else { - // If neither flag is true, use the default metrics. - metricNames = append(metricNames, defaultRuntimeMetrics...) +var ( + defaultRuntimeMetrics = []string{ + "go_gc_gogc_percent", + "go_gc_gomemlimit_bytes", + "go_sched_gomaxprocs_threads", } - // sorting is required - sort.Strings(metricNames) - return metricNames -} + onlyGCDefRuntimeMetrics = []string{ + "go_gc_gogc_percent", + "go_gc_gomemlimit_bytes", + } + onlySchedDefRuntimeMetrics = []string{ + "go_sched_gomaxprocs_threads", + } +) diff --git a/prometheus/collectors/go_collector_latest_test.go b/prometheus/collectors/go_collector_latest_test.go index 2f5440a..974db6c 100644 --- a/prometheus/collectors/go_collector_latest_test.go +++ b/prometheus/collectors/go_collector_latest_test.go @@ -62,6 +62,23 @@ var memstatMetrics = []string{ "go_memstats_sys_bytes", } +func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string { + switch { + case withoutGC && !withoutSched: + // If only withoutGC is true, exclude "go_gc_*" metrics. + metricNames = append(metricNames, onlySchedDefRuntimeMetrics...) + case withoutSched && !withoutGC: + // If only withoutSched is true, exclude "go_sched_*" metrics. + metricNames = append(metricNames, onlyGCDefRuntimeMetrics...) + default: + // In any other case, use the default metrics. + metricNames = append(metricNames, defaultRuntimeMetrics...) + } + // sorting is required + sort.Strings(metricNames) + return metricNames +} + func TestGoCollectorMarshalling(t *testing.T) { reg := prometheus.NewPedanticRegistry() reg.MustRegister(NewGoCollector(