Skip to content

Commit ab6d736

Browse files
vesariamberpixels
authored andcommitted
Refactor default runtime metrics tests for Go collector so that default runtime metric set autogenerates (prometheus#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]> Signed-off-by: Eugene <[email protected]>
1 parent ee98f4e commit ab6d736

6 files changed

+120
-97
lines changed

prometheus/collectors/gen_go_collector_set.go

+56-7
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,48 @@ func main() {
7878
v := goVersion(gv.Segments()[1])
7979
log.Printf("generating metrics for Go version %q", v)
8080

81-
descriptions := computeMetricsList()
81+
descriptions := computeMetricsList(metrics.All())
8282
groupedMetrics := groupMetrics(descriptions)
8383

84+
// Find default metrics.
85+
var defaultRuntimeDesc []metrics.Description
86+
for _, d := range metrics.All() {
87+
if !internal.GoCollectorDefaultRuntimeMetrics.MatchString(d.Name) {
88+
continue
89+
}
90+
defaultRuntimeDesc = append(defaultRuntimeDesc, d)
91+
}
92+
93+
defaultRuntimeMetricsList := computeMetricsList(defaultRuntimeDesc)
94+
95+
onlyGCDefRuntimeMetricsList := []string{}
96+
onlySchedDefRuntimeMetricsList := []string{}
97+
98+
for _, m := range defaultRuntimeMetricsList {
99+
if strings.HasPrefix(m, "go_gc") {
100+
onlyGCDefRuntimeMetricsList = append(onlyGCDefRuntimeMetricsList, m)
101+
}
102+
if strings.HasPrefix(m, "go_sched") {
103+
onlySchedDefRuntimeMetricsList = append(onlySchedDefRuntimeMetricsList, m)
104+
} else {
105+
continue
106+
}
107+
}
108+
84109
// Generate code.
85110
var buf bytes.Buffer
86111
err = testFile.Execute(&buf, struct {
87-
GoVersion goVersion
88-
Groups []metricGroup
112+
GoVersion goVersion
113+
Groups []metricGroup
114+
DefaultRuntimeMetricsList []string
115+
OnlyGCDefRuntimeMetricsList []string
116+
OnlySchedDefRuntimeMetricsList []string
89117
}{
90-
GoVersion: v,
91-
Groups: groupedMetrics,
118+
GoVersion: v,
119+
Groups: groupedMetrics,
120+
DefaultRuntimeMetricsList: defaultRuntimeMetricsList,
121+
OnlyGCDefRuntimeMetricsList: onlyGCDefRuntimeMetricsList,
122+
OnlySchedDefRuntimeMetricsList: onlySchedDefRuntimeMetricsList,
92123
})
93124
if err != nil {
94125
log.Fatalf("executing template: %v", err)
@@ -107,9 +138,9 @@ func main() {
107138
}
108139
}
109140

110-
func computeMetricsList() []string {
141+
func computeMetricsList(descs []metrics.Description) []string {
111142
var metricsList []string
112-
for _, d := range metrics.All() {
143+
for _, d := range descs {
113144
if trans := rm2prom(d); trans != "" {
114145
metricsList = append(metricsList, trans)
115146
}
@@ -186,4 +217,22 @@ func {{ .Name }}() []string {
186217
})
187218
}
188219
{{ end }}
220+
221+
var (
222+
defaultRuntimeMetrics = []string{
223+
{{- range $metric := .DefaultRuntimeMetricsList }}
224+
{{ $metric | printf "%q"}},
225+
{{- end }}
226+
}
227+
onlyGCDefRuntimeMetrics = []string{
228+
{{- range $metric := .OnlyGCDefRuntimeMetricsList }}
229+
{{ $metric | printf "%q"}},
230+
{{- end }}
231+
}
232+
onlySchedDefRuntimeMetrics = []string{
233+
{{- range $metric := .OnlySchedDefRuntimeMetricsList }}
234+
{{ $metric | printf "%q"}},
235+
{{- end }}
236+
}
237+
)
189238
`))

prometheus/collectors/go_collector_go120_test.go

+8-15
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package collectors
1818

19-
import "sort"
20-
2119
func withAllMetrics() []string {
2220
return withBaseMetrics([]string{
2321
"go_cgo_go_to_c_calls_calls_total",
@@ -119,17 +117,12 @@ func withDebugMetrics() []string {
119117
return withBaseMetrics([]string{})
120118
}
121119

122-
var defaultRuntimeMetrics = []string{
123-
"go_sched_gomaxprocs_threads",
124-
}
125-
126-
func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string {
127-
// If withoutSched is true, exclude "go_sched_gomaxprocs_threads".
128-
if withoutSched {
129-
return metricNames
120+
var (
121+
defaultRuntimeMetrics = []string{
122+
"go_sched_gomaxprocs_threads",
130123
}
131-
metricNames = append(metricNames, defaultRuntimeMetrics...)
132-
// sorting is required
133-
sort.Strings(metricNames)
134-
return metricNames
135-
}
124+
onlyGCDefRuntimeMetrics = []string{}
125+
onlySchedDefRuntimeMetrics = []string{
126+
"go_sched_gomaxprocs_threads",
127+
}
128+
)

prometheus/collectors/go_collector_go121_test.go

+13-25
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package collectors
1818

19-
import "sort"
20-
2119
func withAllMetrics() []string {
2220
return withBaseMetrics([]string{
2321
"go_cgo_go_to_c_calls_calls_total",
@@ -172,27 +170,17 @@ func withDebugMetrics() []string {
172170
})
173171
}
174172

175-
var defaultRuntimeMetrics = []string{
176-
"go_gc_gogc_percent",
177-
"go_gc_gomemlimit_bytes",
178-
"go_sched_gomaxprocs_threads",
179-
}
180-
181-
func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string {
182-
if withoutGC && withoutSched {
183-
// If both flags are true, return the metricNames as is.
184-
return metricNames
185-
} else if withoutGC && !withoutSched {
186-
// If only withoutGC is true, include "go_sched_gomaxprocs_threads" only.
187-
metricNames = append(metricNames, []string{"go_sched_gomaxprocs_threads"}...)
188-
} else if withoutSched && !withoutGC {
189-
// If only withoutSched is true, exclude "go_sched_gomaxprocs_threads".
190-
metricNames = append(metricNames, []string{"go_gc_gogc_percent", "go_gc_gomemlimit_bytes"}...)
191-
} else {
192-
// If neither flag is true, use the default metrics.
193-
metricNames = append(metricNames, defaultRuntimeMetrics...)
173+
var (
174+
defaultRuntimeMetrics = []string{
175+
"go_gc_gogc_percent",
176+
"go_gc_gomemlimit_bytes",
177+
"go_sched_gomaxprocs_threads",
194178
}
195-
// sorting is required
196-
sort.Strings(metricNames)
197-
return metricNames
198-
}
179+
onlyGCDefRuntimeMetrics = []string{
180+
"go_gc_gogc_percent",
181+
"go_gc_gomemlimit_bytes",
182+
}
183+
onlySchedDefRuntimeMetrics = []string{
184+
"go_sched_gomaxprocs_threads",
185+
}
186+
)

prometheus/collectors/go_collector_go122_test.go

+13-25
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package collectors
1818

19-
import "sort"
20-
2119
func withAllMetrics() []string {
2220
return withBaseMetrics([]string{
2321
"go_cgo_go_to_c_calls_calls_total",
@@ -194,27 +192,17 @@ func withDebugMetrics() []string {
194192
})
195193
}
196194

197-
var defaultRuntimeMetrics = []string{
198-
"go_gc_gogc_percent",
199-
"go_gc_gomemlimit_bytes",
200-
"go_sched_gomaxprocs_threads",
201-
}
202-
203-
func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string {
204-
if withoutGC && withoutSched {
205-
// If both flags are true, return the metricNames as is.
206-
return metricNames
207-
} else if withoutGC && !withoutSched {
208-
// If only withoutGC is true, include "go_sched_gomaxprocs_threads" only.
209-
metricNames = append(metricNames, []string{"go_sched_gomaxprocs_threads"}...)
210-
} else if withoutSched && !withoutGC {
211-
// If only withoutSched is true, exclude "go_sched_gomaxprocs_threads".
212-
metricNames = append(metricNames, []string{"go_gc_gogc_percent", "go_gc_gomemlimit_bytes"}...)
213-
} else {
214-
// If neither flag is true, use the default metrics.
215-
metricNames = append(metricNames, defaultRuntimeMetrics...)
195+
var (
196+
defaultRuntimeMetrics = []string{
197+
"go_gc_gogc_percent",
198+
"go_gc_gomemlimit_bytes",
199+
"go_sched_gomaxprocs_threads",
216200
}
217-
// sorting is required
218-
sort.Strings(metricNames)
219-
return metricNames
220-
}
201+
onlyGCDefRuntimeMetrics = []string{
202+
"go_gc_gogc_percent",
203+
"go_gc_gomemlimit_bytes",
204+
}
205+
onlySchedDefRuntimeMetrics = []string{
206+
"go_sched_gomaxprocs_threads",
207+
}
208+
)

prometheus/collectors/go_collector_go123_test.go

+13-25
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package collectors
1818

19-
import "sort"
20-
2119
func withAllMetrics() []string {
2220
return withBaseMetrics([]string{
2321
"go_cgo_go_to_c_calls_calls_total",
@@ -206,27 +204,17 @@ func withDebugMetrics() []string {
206204
})
207205
}
208206

209-
var defaultRuntimeMetrics = []string{
210-
"go_gc_gogc_percent",
211-
"go_gc_gomemlimit_bytes",
212-
"go_sched_gomaxprocs_threads",
213-
}
214-
215-
func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string {
216-
if withoutGC && withoutSched {
217-
// If both flags are true, return the metricNames as is.
218-
return metricNames
219-
} else if withoutGC && !withoutSched {
220-
// If only withoutGC is true, include "go_sched_gomaxprocs_threads" only.
221-
metricNames = append(metricNames, []string{"go_sched_gomaxprocs_threads"}...)
222-
} else if withoutSched && !withoutGC {
223-
// If only withoutSched is true, exclude "go_sched_gomaxprocs_threads".
224-
metricNames = append(metricNames, []string{"go_gc_gogc_percent", "go_gc_gomemlimit_bytes"}...)
225-
} else {
226-
// If neither flag is true, use the default metrics.
227-
metricNames = append(metricNames, defaultRuntimeMetrics...)
207+
var (
208+
defaultRuntimeMetrics = []string{
209+
"go_gc_gogc_percent",
210+
"go_gc_gomemlimit_bytes",
211+
"go_sched_gomaxprocs_threads",
228212
}
229-
// sorting is required
230-
sort.Strings(metricNames)
231-
return metricNames
232-
}
213+
onlyGCDefRuntimeMetrics = []string{
214+
"go_gc_gogc_percent",
215+
"go_gc_gomemlimit_bytes",
216+
}
217+
onlySchedDefRuntimeMetrics = []string{
218+
"go_sched_gomaxprocs_threads",
219+
}
220+
)

prometheus/collectors/go_collector_latest_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ var memstatMetrics = []string{
6262
"go_memstats_sys_bytes",
6363
}
6464

65+
func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string {
66+
switch {
67+
case withoutGC && !withoutSched:
68+
// If only withoutGC is true, exclude "go_gc_*" metrics.
69+
metricNames = append(metricNames, onlySchedDefRuntimeMetrics...)
70+
case withoutSched && !withoutGC:
71+
// If only withoutSched is true, exclude "go_sched_*" metrics.
72+
metricNames = append(metricNames, onlyGCDefRuntimeMetrics...)
73+
default:
74+
// In any other case, use the default metrics.
75+
metricNames = append(metricNames, defaultRuntimeMetrics...)
76+
}
77+
// sorting is required
78+
sort.Strings(metricNames)
79+
return metricNames
80+
}
81+
6582
func TestGoCollectorMarshalling(t *testing.T) {
6683
reg := prometheus.NewPedanticRegistry()
6784
reg.MustRegister(NewGoCollector(

0 commit comments

Comments
 (0)