Skip to content

Commit

Permalink
Refactor default runtime metrics tests for Go collector so that defau…
Browse files Browse the repository at this point in the history
…lt runtime metric set autogenerates (#1631)

* Enable autogeneration for default runtime metrics list in collectors tests according to Go version

Signed-off-by: Arianna Vespri <[email protected]>

* Adapt withDefaultRuntimeMetrics function to work regardless of the Go version

Signed-off-by: Arianna Vespri <[email protected]>

* Autogenerate go collector test for go1.23

Signed-off-by: Arianna Vespri <[email protected]>

* Modify gen_go_collector_set.go to please linter and regenerate files

Signed-off-by: Arianna Vespri <[email protected]>

* Simplify gen_go_collector_set.go logic by modifying func computeMetricsList

Signed-off-by: Arianna Vespri <[email protected]>

* Slight simplification of withDefaultRuntimeMetrics func

Signed-off-by: Arianna Vespri <[email protected]>

* Refactor withDefaultRuntimeMetrics with generated default runtime metrics subsets

Signed-off-by: Arianna Vespri <[email protected]>

---------

Signed-off-by: Arianna Vespri <[email protected]>
  • Loading branch information
vesari authored Sep 23, 2024
1 parent 64158c5 commit ff60566
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 97 deletions.
63 changes: 56 additions & 7 deletions prometheus/collectors/gen_go_collector_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 }}
}
)
`))
23 changes: 8 additions & 15 deletions prometheus/collectors/go_collector_go120_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package collectors

import "sort"

func withAllMetrics() []string {
return withBaseMetrics([]string{
"go_cgo_go_to_c_calls_calls_total",
Expand Down Expand Up @@ -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",
}
)
38 changes: 13 additions & 25 deletions prometheus/collectors/go_collector_go121_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package collectors

import "sort"

func withAllMetrics() []string {
return withBaseMetrics([]string{
"go_cgo_go_to_c_calls_calls_total",
Expand Down Expand Up @@ -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",
}
)
38 changes: 13 additions & 25 deletions prometheus/collectors/go_collector_go122_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package collectors

import "sort"

func withAllMetrics() []string {
return withBaseMetrics([]string{
"go_cgo_go_to_c_calls_calls_total",
Expand Down Expand Up @@ -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",
}
)
38 changes: 13 additions & 25 deletions prometheus/collectors/go_collector_go123_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package collectors

import "sort"

func withAllMetrics() []string {
return withBaseMetrics([]string{
"go_cgo_go_to_c_calls_calls_total",
Expand Down Expand Up @@ -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",
}
)
17 changes: 17 additions & 0 deletions prometheus/collectors/go_collector_latest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit ff60566

Please sign in to comment.