From 13b607acf45b242ee125c30e1e4756180afcafbb Mon Sep 17 00:00:00 2001 From: Arianna Vespri Date: Thu, 19 Sep 2024 15:10:40 +0200 Subject: [PATCH 1/7] Enable autogeneration for default runtime metrics list in collectors tests according to Go version Signed-off-by: Arianna Vespri --- prometheus/collectors/gen_go_collector_set.go | 38 +++++++++++++++++-- .../collectors/go_collector_go117_test.go | 6 --- .../collectors/go_collector_go119_test.go | 17 --------- .../collectors/go_collector_go120_test.go | 19 ++-------- .../collectors/go_collector_go121_test.go | 31 +++------------ .../collectors/go_collector_go122_test.go | 31 +++------------ .../collectors/go_collector_latest_test.go | 19 ++++++++++ 7 files changed, 69 insertions(+), 92 deletions(-) diff --git a/prometheus/collectors/gen_go_collector_set.go b/prometheus/collectors/gen_go_collector_set.go index 6d1ff7e71..e0576686b 100644 --- a/prometheus/collectors/gen_go_collector_set.go +++ b/prometheus/collectors/gen_go_collector_set.go @@ -81,14 +81,35 @@ func main() { descriptions := computeMetricsList() groupedMetrics := groupMetrics(descriptions) + allDesc := metrics.All() + + // Find default metrics. + var defaultRuntimeDesc []metrics.Description + for _, d := range allDesc { + if !internal.GoCollectorDefaultRuntimeMetrics.MatchString(d.Name) { + continue + } + defaultRuntimeDesc = append(defaultRuntimeDesc, d) + } + + var defaultRuntimeMetricsList []string + + for _, d := range defaultRuntimeDesc { + if trans := rm2prom(d); trans != "" { + defaultRuntimeMetricsList = append(defaultRuntimeMetricsList, trans) + } + } + // Generate code. var buf bytes.Buffer err = testFile.Execute(&buf, struct { - GoVersion goVersion - Groups []metricGroup + GoVersion goVersion + Groups []metricGroup + DefaultRuntimeMetricsList []string }{ - GoVersion: v, - Groups: groupedMetrics, + GoVersion: v, + Groups: groupedMetrics, + DefaultRuntimeMetricsList: defaultRuntimeMetricsList, }) if err != nil { log.Fatalf("executing template: %v", err) @@ -186,4 +207,13 @@ func {{ .Name }}() []string { }) } {{ end }} + +var ( + defaultRuntimeMetrics = []string{ + {{- range $metric := .DefaultRuntimeMetricsList }} + {{ $metric | printf "%q"}}, + {{- end }} + } +) + `)) diff --git a/prometheus/collectors/go_collector_go117_test.go b/prometheus/collectors/go_collector_go117_test.go index f378d4097..760ad046b 100644 --- a/prometheus/collectors/go_collector_go117_test.go +++ b/prometheus/collectors/go_collector_go117_test.go @@ -102,9 +102,3 @@ func withSchedulerMetrics() []string { func withDebugMetrics() []string { return withBaseMetrics([]string{}) } - -var defaultRuntimeMetrics = []string{} - -func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string { - return metricNames -} diff --git a/prometheus/collectors/go_collector_go119_test.go b/prometheus/collectors/go_collector_go119_test.go index 4febad2ce..1ef9a4cf0 100644 --- a/prometheus/collectors/go_collector_go119_test.go +++ b/prometheus/collectors/go_collector_go119_test.go @@ -16,8 +16,6 @@ package collectors -import "sort" - func withAllMetrics() []string { return withBaseMetrics([]string{ "go_cgo_go_to_c_calls_calls_total", @@ -111,18 +109,3 @@ func withSchedulerMetrics() []string { 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 - } - metricNames = append(metricNames, defaultRuntimeMetrics...) - // sorting is required - sort.Strings(metricNames) - return metricNames -} diff --git a/prometheus/collectors/go_collector_go120_test.go b/prometheus/collectors/go_collector_go120_test.go index 968a016ab..5516e95c4 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,8 @@ 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 -} +) diff --git a/prometheus/collectors/go_collector_go121_test.go b/prometheus/collectors/go_collector_go121_test.go index f9cc318cc..15bceb581 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,10 @@ 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 -} +) diff --git a/prometheus/collectors/go_collector_go122_test.go b/prometheus/collectors/go_collector_go122_test.go index 2413c2750..3fd363e41 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,10 @@ 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 -} +) diff --git a/prometheus/collectors/go_collector_latest_test.go b/prometheus/collectors/go_collector_latest_test.go index 2f5440abb..8f9ad69a8 100644 --- a/prometheus/collectors/go_collector_latest_test.go +++ b/prometheus/collectors/go_collector_latest_test.go @@ -62,6 +62,25 @@ var memstatMetrics = []string{ "go_memstats_sys_bytes", } +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...) + } + // sorting is required + sort.Strings(metricNames) + return metricNames +} + func TestGoCollectorMarshalling(t *testing.T) { reg := prometheus.NewPedanticRegistry() reg.MustRegister(NewGoCollector( From 154e9da4cafb001290980983bfc94a0ce1cf8231 Mon Sep 17 00:00:00 2001 From: Arianna Vespri Date: Thu, 19 Sep 2024 16:52:06 +0200 Subject: [PATCH 2/7] Adapt withDefaultRuntimeMetrics function to work regardless of the Go version Signed-off-by: Arianna Vespri --- prometheus/collectors/go_collector_latest_test.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/prometheus/collectors/go_collector_latest_test.go b/prometheus/collectors/go_collector_latest_test.go index 8f9ad69a8..ce2266c95 100644 --- a/prometheus/collectors/go_collector_latest_test.go +++ b/prometheus/collectors/go_collector_latest_test.go @@ -66,14 +66,17 @@ func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched boo 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. + } else if withoutGC && !withoutSched && defaultRuntimeMetrics != nil { + // If only withoutGC is true and the Go version is at least 1.20, 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". + } else if withoutSched && !withoutGC && len(defaultRuntimeMetrics) > 1 { + // If only withoutSched is true and the Go version is higher than 1.20, exclude "go_sched_gomaxprocs_threads". metricNames = append(metricNames, []string{"go_gc_gogc_percent", "go_gc_gomemlimit_bytes"}...) + } else if withoutSched && len(defaultRuntimeMetrics) == 1 { + // If only withoutSched is true and the Go version is 1.20, return the metricNames as is. + return metricNames } else { - // If neither flag is true, use the default metrics. + // In any other case, use the default metrics. metricNames = append(metricNames, defaultRuntimeMetrics...) } // sorting is required From 3cdd6830a7f45fd2e0116f7af046f2f2a01ef619 Mon Sep 17 00:00:00 2001 From: Arianna Vespri Date: Thu, 19 Sep 2024 17:22:58 +0200 Subject: [PATCH 3/7] Autogenerate go collector test for go1.23 Signed-off-by: Arianna Vespri --- .../collectors/go_collector_go123_test.go | 31 ++++--------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/prometheus/collectors/go_collector_go123_test.go b/prometheus/collectors/go_collector_go123_test.go index 43395dde4..2ced37783 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,10 @@ 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 -} +) From dcb8936934a404e1e5c1954a65069796870e8b8a Mon Sep 17 00:00:00 2001 From: Arianna Vespri Date: Fri, 20 Sep 2024 10:00:51 +0200 Subject: [PATCH 4/7] Modify gen_go_collector_set.go to please linter and regenerate files Signed-off-by: Arianna Vespri --- prometheus/collectors/gen_go_collector_set.go | 5 +---- prometheus/collectors/go_collector_go120_test.go | 8 +++----- prometheus/collectors/go_collector_go121_test.go | 12 +++++------- prometheus/collectors/go_collector_go122_test.go | 12 +++++------- prometheus/collectors/go_collector_go123_test.go | 12 +++++------- 5 files changed, 19 insertions(+), 30 deletions(-) diff --git a/prometheus/collectors/gen_go_collector_set.go b/prometheus/collectors/gen_go_collector_set.go index e0576686b..9045a5c48 100644 --- a/prometheus/collectors/gen_go_collector_set.go +++ b/prometheus/collectors/gen_go_collector_set.go @@ -208,12 +208,9 @@ func {{ .Name }}() []string { } {{ end }} -var ( - defaultRuntimeMetrics = []string{ +var defaultRuntimeMetrics = []string{ {{- range $metric := .DefaultRuntimeMetricsList }} {{ $metric | printf "%q"}}, {{- end }} } -) - `)) diff --git a/prometheus/collectors/go_collector_go120_test.go b/prometheus/collectors/go_collector_go120_test.go index 5516e95c4..47ee192a7 100644 --- a/prometheus/collectors/go_collector_go120_test.go +++ b/prometheus/collectors/go_collector_go120_test.go @@ -117,8 +117,6 @@ func withDebugMetrics() []string { return withBaseMetrics([]string{}) } -var ( - defaultRuntimeMetrics = []string{ - "go_sched_gomaxprocs_threads", - } -) +var defaultRuntimeMetrics = []string{ + "go_sched_gomaxprocs_threads", +} diff --git a/prometheus/collectors/go_collector_go121_test.go b/prometheus/collectors/go_collector_go121_test.go index 15bceb581..c342b9c77 100644 --- a/prometheus/collectors/go_collector_go121_test.go +++ b/prometheus/collectors/go_collector_go121_test.go @@ -170,10 +170,8 @@ func withDebugMetrics() []string { }) } -var ( - defaultRuntimeMetrics = []string{ - "go_gc_gogc_percent", - "go_gc_gomemlimit_bytes", - "go_sched_gomaxprocs_threads", - } -) +var defaultRuntimeMetrics = []string{ + "go_gc_gogc_percent", + "go_gc_gomemlimit_bytes", + "go_sched_gomaxprocs_threads", +} diff --git a/prometheus/collectors/go_collector_go122_test.go b/prometheus/collectors/go_collector_go122_test.go index 3fd363e41..ce06f0f53 100644 --- a/prometheus/collectors/go_collector_go122_test.go +++ b/prometheus/collectors/go_collector_go122_test.go @@ -192,10 +192,8 @@ func withDebugMetrics() []string { }) } -var ( - defaultRuntimeMetrics = []string{ - "go_gc_gogc_percent", - "go_gc_gomemlimit_bytes", - "go_sched_gomaxprocs_threads", - } -) +var defaultRuntimeMetrics = []string{ + "go_gc_gogc_percent", + "go_gc_gomemlimit_bytes", + "go_sched_gomaxprocs_threads", +} diff --git a/prometheus/collectors/go_collector_go123_test.go b/prometheus/collectors/go_collector_go123_test.go index 2ced37783..3527166ef 100644 --- a/prometheus/collectors/go_collector_go123_test.go +++ b/prometheus/collectors/go_collector_go123_test.go @@ -204,10 +204,8 @@ func withDebugMetrics() []string { }) } -var ( - defaultRuntimeMetrics = []string{ - "go_gc_gogc_percent", - "go_gc_gomemlimit_bytes", - "go_sched_gomaxprocs_threads", - } -) +var defaultRuntimeMetrics = []string{ + "go_gc_gogc_percent", + "go_gc_gomemlimit_bytes", + "go_sched_gomaxprocs_threads", +} From fe0487bdd2e859e4585f0fb731e6e892f2056b99 Mon Sep 17 00:00:00 2001 From: Arianna Vespri Date: Fri, 20 Sep 2024 10:26:15 +0200 Subject: [PATCH 5/7] Simplify gen_go_collector_set.go logic by modifying func computeMetricsList Signed-off-by: Arianna Vespri --- prometheus/collectors/gen_go_collector_set.go | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/prometheus/collectors/gen_go_collector_set.go b/prometheus/collectors/gen_go_collector_set.go index 9045a5c48..71beeb4fc 100644 --- a/prometheus/collectors/gen_go_collector_set.go +++ b/prometheus/collectors/gen_go_collector_set.go @@ -78,27 +78,19 @@ 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) - allDesc := metrics.All() - // Find default metrics. var defaultRuntimeDesc []metrics.Description - for _, d := range allDesc { + for _, d := range metrics.All() { if !internal.GoCollectorDefaultRuntimeMetrics.MatchString(d.Name) { continue } defaultRuntimeDesc = append(defaultRuntimeDesc, d) } - var defaultRuntimeMetricsList []string - - for _, d := range defaultRuntimeDesc { - if trans := rm2prom(d); trans != "" { - defaultRuntimeMetricsList = append(defaultRuntimeMetricsList, trans) - } - } + defaultRuntimeMetricsList := computeMetricsList(defaultRuntimeDesc) // Generate code. var buf bytes.Buffer @@ -128,9 +120,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) } From 2158a2f4e3af26c512399d826659cc78416fbd6a Mon Sep 17 00:00:00 2001 From: Arianna Vespri Date: Sat, 21 Sep 2024 08:26:35 +0200 Subject: [PATCH 6/7] Slight simplification of withDefaultRuntimeMetrics func Signed-off-by: Arianna Vespri --- prometheus/collectors/go_collector_latest_test.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/prometheus/collectors/go_collector_latest_test.go b/prometheus/collectors/go_collector_latest_test.go index ce2266c95..54b9c2357 100644 --- a/prometheus/collectors/go_collector_latest_test.go +++ b/prometheus/collectors/go_collector_latest_test.go @@ -63,18 +63,15 @@ var memstatMetrics = []string{ } func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string { - if withoutGC && withoutSched { - // If both flags are true, return the metricNames as is. + if withoutSched && len(defaultRuntimeMetrics) == 1 { + // If only withoutSched is true and the Go version is 1.20, return the metricNames as is. return metricNames - } else if withoutGC && !withoutSched && defaultRuntimeMetrics != nil { - // If only withoutGC is true and the Go version is at least 1.20, include "go_sched_gomaxprocs_threads" only. + } 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 && len(defaultRuntimeMetrics) > 1 { // If only withoutSched is true and the Go version is higher than 1.20, exclude "go_sched_gomaxprocs_threads". metricNames = append(metricNames, []string{"go_gc_gogc_percent", "go_gc_gomemlimit_bytes"}...) - } else if withoutSched && len(defaultRuntimeMetrics) == 1 { - // If only withoutSched is true and the Go version is 1.20, return the metricNames as is. - return metricNames } else { // In any other case, use the default metrics. metricNames = append(metricNames, defaultRuntimeMetrics...) From 70b1a8bbb449252022b62cc2c1177045509bdddf Mon Sep 17 00:00:00 2001 From: Arianna Vespri Date: Sat, 21 Sep 2024 09:57:33 +0200 Subject: [PATCH 7/7] Refactor withDefaultRuntimeMetrics with generated default runtime metrics subsets Signed-off-by: Arianna Vespri --- prometheus/collectors/gen_go_collector_set.go | 44 ++++++++++++++++--- .../collectors/go_collector_go120_test.go | 12 +++-- .../collectors/go_collector_go121_test.go | 19 +++++--- .../collectors/go_collector_go122_test.go | 19 +++++--- .../collectors/go_collector_go123_test.go | 19 +++++--- .../collectors/go_collector_latest_test.go | 18 ++++---- 6 files changed, 96 insertions(+), 35 deletions(-) diff --git a/prometheus/collectors/gen_go_collector_set.go b/prometheus/collectors/gen_go_collector_set.go index 71beeb4fc..7a5044fa7 100644 --- a/prometheus/collectors/gen_go_collector_set.go +++ b/prometheus/collectors/gen_go_collector_set.go @@ -92,16 +92,34 @@ func main() { 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 - DefaultRuntimeMetricsList []string + GoVersion goVersion + Groups []metricGroup + DefaultRuntimeMetricsList []string + OnlyGCDefRuntimeMetricsList []string + OnlySchedDefRuntimeMetricsList []string }{ - GoVersion: v, - Groups: groupedMetrics, - DefaultRuntimeMetricsList: defaultRuntimeMetricsList, + GoVersion: v, + Groups: groupedMetrics, + DefaultRuntimeMetricsList: defaultRuntimeMetricsList, + OnlyGCDefRuntimeMetricsList: onlyGCDefRuntimeMetricsList, + OnlySchedDefRuntimeMetricsList: onlySchedDefRuntimeMetricsList, }) if err != nil { log.Fatalf("executing template: %v", err) @@ -200,9 +218,21 @@ func {{ .Name }}() []string { } {{ end }} -var defaultRuntimeMetrics = []string{ +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 47ee192a7..e67fea10e 100644 --- a/prometheus/collectors/go_collector_go120_test.go +++ b/prometheus/collectors/go_collector_go120_test.go @@ -117,6 +117,12 @@ func withDebugMetrics() []string { return withBaseMetrics([]string{}) } -var defaultRuntimeMetrics = []string{ - "go_sched_gomaxprocs_threads", -} +var ( + defaultRuntimeMetrics = []string{ + "go_sched_gomaxprocs_threads", + } + 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 c342b9c77..f8a5879af 100644 --- a/prometheus/collectors/go_collector_go121_test.go +++ b/prometheus/collectors/go_collector_go121_test.go @@ -170,8 +170,17 @@ func withDebugMetrics() []string { }) } -var defaultRuntimeMetrics = []string{ - "go_gc_gogc_percent", - "go_gc_gomemlimit_bytes", - "go_sched_gomaxprocs_threads", -} +var ( + defaultRuntimeMetrics = []string{ + "go_gc_gogc_percent", + "go_gc_gomemlimit_bytes", + "go_sched_gomaxprocs_threads", + } + 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 ce06f0f53..2ed46ae19 100644 --- a/prometheus/collectors/go_collector_go122_test.go +++ b/prometheus/collectors/go_collector_go122_test.go @@ -192,8 +192,17 @@ func withDebugMetrics() []string { }) } -var defaultRuntimeMetrics = []string{ - "go_gc_gogc_percent", - "go_gc_gomemlimit_bytes", - "go_sched_gomaxprocs_threads", -} +var ( + defaultRuntimeMetrics = []string{ + "go_gc_gogc_percent", + "go_gc_gomemlimit_bytes", + "go_sched_gomaxprocs_threads", + } + 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 3527166ef..1c26dc25b 100644 --- a/prometheus/collectors/go_collector_go123_test.go +++ b/prometheus/collectors/go_collector_go123_test.go @@ -204,8 +204,17 @@ func withDebugMetrics() []string { }) } -var defaultRuntimeMetrics = []string{ - "go_gc_gogc_percent", - "go_gc_gomemlimit_bytes", - "go_sched_gomaxprocs_threads", -} +var ( + defaultRuntimeMetrics = []string{ + "go_gc_gogc_percent", + "go_gc_gomemlimit_bytes", + "go_sched_gomaxprocs_threads", + } + 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 54b9c2357..974db6c13 100644 --- a/prometheus/collectors/go_collector_latest_test.go +++ b/prometheus/collectors/go_collector_latest_test.go @@ -63,16 +63,14 @@ var memstatMetrics = []string{ } func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string { - if withoutSched && len(defaultRuntimeMetrics) == 1 { - // If only withoutSched is true and the Go version is 1.20, 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 && len(defaultRuntimeMetrics) > 1 { - // If only withoutSched is true and the Go version is higher than 1.20, exclude "go_sched_gomaxprocs_threads". - metricNames = append(metricNames, []string{"go_gc_gogc_percent", "go_gc_gomemlimit_bytes"}...) - } else { + 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...) }