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 <arianna.vespri@yahoo.it>

* Adapt withDefaultRuntimeMetrics function to work regardless of the Go version

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>

* Autogenerate go collector test for go1.23

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>

* Modify gen_go_collector_set.go to please linter and regenerate files

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>

* Simplify gen_go_collector_set.go logic by modifying func computeMetricsList

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>

* Slight simplification of withDefaultRuntimeMetrics func

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>

* Refactor withDefaultRuntimeMetrics with generated default runtime metrics subsets

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>

---------

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
This commit is contained in:
Arianna Vespri 2024-09-23 15:55:38 +02:00 committed by GitHub
parent 64158c5c00
commit ff60566f1b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 120 additions and 97 deletions

View File

@ -78,17 +78,48 @@ func main() {
v := goVersion(gv.Segments()[1]) v := goVersion(gv.Segments()[1])
log.Printf("generating metrics for Go version %q", v) log.Printf("generating metrics for Go version %q", v)
descriptions := computeMetricsList() descriptions := computeMetricsList(metrics.All())
groupedMetrics := groupMetrics(descriptions) 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. // Generate code.
var buf bytes.Buffer var buf bytes.Buffer
err = testFile.Execute(&buf, struct { err = testFile.Execute(&buf, struct {
GoVersion goVersion GoVersion goVersion
Groups []metricGroup Groups []metricGroup
DefaultRuntimeMetricsList []string
OnlyGCDefRuntimeMetricsList []string
OnlySchedDefRuntimeMetricsList []string
}{ }{
GoVersion: v, GoVersion: v,
Groups: groupedMetrics, Groups: groupedMetrics,
DefaultRuntimeMetricsList: defaultRuntimeMetricsList,
OnlyGCDefRuntimeMetricsList: onlyGCDefRuntimeMetricsList,
OnlySchedDefRuntimeMetricsList: onlySchedDefRuntimeMetricsList,
}) })
if err != nil { if err != nil {
log.Fatalf("executing template: %v", err) log.Fatalf("executing template: %v", err)
@ -107,9 +138,9 @@ func main() {
} }
} }
func computeMetricsList() []string { func computeMetricsList(descs []metrics.Description) []string {
var metricsList []string var metricsList []string
for _, d := range metrics.All() { for _, d := range descs {
if trans := rm2prom(d); trans != "" { if trans := rm2prom(d); trans != "" {
metricsList = append(metricsList, trans) metricsList = append(metricsList, trans)
} }
@ -186,4 +217,22 @@ func {{ .Name }}() []string {
}) })
} }
{{ end }} {{ 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 }}
}
)
`)) `))

View File

@ -16,8 +16,6 @@
package collectors package collectors
import "sort"
func withAllMetrics() []string { func withAllMetrics() []string {
return withBaseMetrics([]string{ return withBaseMetrics([]string{
"go_cgo_go_to_c_calls_calls_total", "go_cgo_go_to_c_calls_calls_total",
@ -119,17 +117,12 @@ func withDebugMetrics() []string {
return withBaseMetrics([]string{}) return withBaseMetrics([]string{})
} }
var defaultRuntimeMetrics = []string{ var (
defaultRuntimeMetrics = []string{
"go_sched_gomaxprocs_threads", "go_sched_gomaxprocs_threads",
} }
onlyGCDefRuntimeMetrics = []string{}
func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string { onlySchedDefRuntimeMetrics = []string{
// If withoutSched is true, exclude "go_sched_gomaxprocs_threads". "go_sched_gomaxprocs_threads",
if withoutSched {
return metricNames
}
metricNames = append(metricNames, defaultRuntimeMetrics...)
// sorting is required
sort.Strings(metricNames)
return metricNames
} }
)

View File

@ -16,8 +16,6 @@
package collectors package collectors
import "sort"
func withAllMetrics() []string { func withAllMetrics() []string {
return withBaseMetrics([]string{ return withBaseMetrics([]string{
"go_cgo_go_to_c_calls_calls_total", "go_cgo_go_to_c_calls_calls_total",
@ -172,27 +170,17 @@ func withDebugMetrics() []string {
}) })
} }
var defaultRuntimeMetrics = []string{ var (
defaultRuntimeMetrics = []string{
"go_gc_gogc_percent", "go_gc_gogc_percent",
"go_gc_gomemlimit_bytes", "go_gc_gomemlimit_bytes",
"go_sched_gomaxprocs_threads", "go_sched_gomaxprocs_threads",
} }
onlyGCDefRuntimeMetrics = []string{
func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string { "go_gc_gogc_percent",
if withoutGC && withoutSched { "go_gc_gomemlimit_bytes",
// 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 onlySchedDefRuntimeMetrics = []string{
sort.Strings(metricNames) "go_sched_gomaxprocs_threads",
return metricNames
} }
)

View File

@ -16,8 +16,6 @@
package collectors package collectors
import "sort"
func withAllMetrics() []string { func withAllMetrics() []string {
return withBaseMetrics([]string{ return withBaseMetrics([]string{
"go_cgo_go_to_c_calls_calls_total", "go_cgo_go_to_c_calls_calls_total",
@ -194,27 +192,17 @@ func withDebugMetrics() []string {
}) })
} }
var defaultRuntimeMetrics = []string{ var (
defaultRuntimeMetrics = []string{
"go_gc_gogc_percent", "go_gc_gogc_percent",
"go_gc_gomemlimit_bytes", "go_gc_gomemlimit_bytes",
"go_sched_gomaxprocs_threads", "go_sched_gomaxprocs_threads",
} }
onlyGCDefRuntimeMetrics = []string{
func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string { "go_gc_gogc_percent",
if withoutGC && withoutSched { "go_gc_gomemlimit_bytes",
// 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 onlySchedDefRuntimeMetrics = []string{
sort.Strings(metricNames) "go_sched_gomaxprocs_threads",
return metricNames
} }
)

View File

@ -16,8 +16,6 @@
package collectors package collectors
import "sort"
func withAllMetrics() []string { func withAllMetrics() []string {
return withBaseMetrics([]string{ return withBaseMetrics([]string{
"go_cgo_go_to_c_calls_calls_total", "go_cgo_go_to_c_calls_calls_total",
@ -206,27 +204,17 @@ func withDebugMetrics() []string {
}) })
} }
var defaultRuntimeMetrics = []string{ var (
defaultRuntimeMetrics = []string{
"go_gc_gogc_percent", "go_gc_gogc_percent",
"go_gc_gomemlimit_bytes", "go_gc_gomemlimit_bytes",
"go_sched_gomaxprocs_threads", "go_sched_gomaxprocs_threads",
} }
onlyGCDefRuntimeMetrics = []string{
func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string { "go_gc_gogc_percent",
if withoutGC && withoutSched { "go_gc_gomemlimit_bytes",
// 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 onlySchedDefRuntimeMetrics = []string{
sort.Strings(metricNames) "go_sched_gomaxprocs_threads",
return metricNames
} }
)

View File

@ -62,6 +62,23 @@ var memstatMetrics = []string{
"go_memstats_sys_bytes", "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) { func TestGoCollectorMarshalling(t *testing.T) {
reg := prometheus.NewPedanticRegistry() reg := prometheus.NewPedanticRegistry()
reg.MustRegister(NewGoCollector( reg.MustRegister(NewGoCollector(