Skip to content

Commit 3ad2722

Browse files
vesaribwplotka
andauthored
Add default Go runtime metrics for /gc/gogc:percent, /gc/gomemlimit:bytes, /sched/gomaxprocs:threads (#1559)
* Add go_gomaxprocs, go_gogc_percent and go_gomemlimit to the default Go runtime metrics Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Move newly added metrics out of base metrics and into goCollector Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Rethink struct for newly added metrics, adapt and add tests Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Simplify new metrics reading Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Correct loop, add debugging lines Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Make goRuntimeEnvVarsMetrics function Go version dependent Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Fix go mod Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Remove debuggin line Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Move default runtime metrics into the runtime metrics flow, change tests accordingly Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Go version expected default runtime metrics map for tests Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * 1.21 update. Signed-off-by: bwplotka <bwplotka@gmail.com> * Addressed comments on Arianna's PR. Signed-off-by: bwplotka <bwplotka@gmail.com> * Use default GoCollector func in test Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> --------- Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> Signed-off-by: bwplotka <bwplotka@gmail.com> Co-authored-by: bwplotka <bwplotka@gmail.com>
1 parent 0715727 commit 3ad2722

13 files changed

+389
-227
lines changed

prometheus/collectors/go_collector_go117_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,9 @@ func withSchedulerMetrics() []string {
102102
func withDebugMetrics() []string {
103103
return withBaseMetrics([]string{})
104104
}
105+
106+
var defaultRuntimeMetrics = []string{}
107+
108+
func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string {
109+
return metricNames
110+
}

prometheus/collectors/go_collector_go119_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package collectors
1818

19+
import "sort"
20+
1921
func withAllMetrics() []string {
2022
return withBaseMetrics([]string{
2123
"go_cgo_go_to_c_calls_calls_total",
@@ -109,3 +111,18 @@ func withSchedulerMetrics() []string {
109111
func withDebugMetrics() []string {
110112
return withBaseMetrics([]string{})
111113
}
114+
115+
var defaultRuntimeMetrics = []string{
116+
"go_sched_gomaxprocs_threads",
117+
}
118+
119+
func withDefaultRuntimeMetrics(metricNames []string, withoutGC, withoutSched bool) []string {
120+
// If withoutSched is true, exclude "go_sched_gomaxprocs_threads".
121+
if withoutSched {
122+
return metricNames
123+
}
124+
metricNames = append(metricNames, defaultRuntimeMetrics...)
125+
// sorting is required
126+
sort.Strings(metricNames)
127+
return metricNames
128+
}

prometheus/collectors/go_collector_go120_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package collectors
1818

19+
import "sort"
20+
1921
func withAllMetrics() []string {
2022
return withBaseMetrics([]string{
2123
"go_cgo_go_to_c_calls_calls_total",
@@ -116,3 +118,18 @@ func withSchedulerMetrics() []string {
116118
func withDebugMetrics() []string {
117119
return withBaseMetrics([]string{})
118120
}
121+
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
130+
}
131+
metricNames = append(metricNames, defaultRuntimeMetrics...)
132+
// sorting is required
133+
sort.Strings(metricNames)
134+
return metricNames
135+
}

prometheus/collectors/go_collector_go121_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package collectors
1818

19+
import "sort"
20+
1921
func withAllMetrics() []string {
2022
return withBaseMetrics([]string{
2123
"go_cgo_go_to_c_calls_calls_total",
@@ -169,3 +171,28 @@ func withDebugMetrics() []string {
169171
"go_godebug_non_default_behavior_zipinsecurepath_events_total",
170172
})
171173
}
174+
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...)
194+
}
195+
// sorting is required
196+
sort.Strings(metricNames)
197+
return metricNames
198+
}

prometheus/collectors/go_collector_go122_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package collectors
1818

19+
import "sort"
20+
1921
func withAllMetrics() []string {
2022
return withBaseMetrics([]string{
2123
"go_cgo_go_to_c_calls_calls_total",
@@ -191,3 +193,28 @@ func withDebugMetrics() []string {
191193
"go_godebug_non_default_behavior_zipinsecurepath_events_total",
192194
})
193195
}
196+
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...)
216+
}
217+
// sorting is required
218+
sort.Strings(metricNames)
219+
return metricNames
220+
}

prometheus/collectors/go_collector_latest_test.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ func TestWithGoCollectorDefault(t *testing.T) {
9393
got = append(got, r.GetName())
9494
}
9595

96-
if diff := cmp.Diff(got, withBaseMetrics(memstatMetrics)); diff != "" {
96+
expected := append(withBaseMetrics(memstatMetrics), defaultRuntimeMetrics...)
97+
sort.Strings(expected)
98+
if diff := cmp.Diff(got, expected); diff != "" {
9799
t.Errorf("[IMPORTANT, those are default metrics, can't change in 1.x] missmatch (-want +got):\n%s", diff)
98100
}
99101
}
@@ -113,7 +115,7 @@ func TestWithGoCollectorMemStatsMetricsDisabled(t *testing.T) {
113115
got = append(got, r.GetName())
114116
}
115117

116-
if diff := cmp.Diff(got, baseMetrics); diff != "" {
118+
if diff := cmp.Diff(got, withBaseMetrics(defaultRuntimeMetrics)); diff != "" {
117119
t.Errorf("missmatch (-want +got):\n%s", diff)
118120
}
119121
}
@@ -127,7 +129,7 @@ func TestGoCollectorAllowList(t *testing.T) {
127129
{
128130
name: "Without any rules",
129131
rules: nil,
130-
expected: baseMetrics,
132+
expected: withBaseMetrics(defaultRuntimeMetrics),
131133
},
132134
{
133135
name: "allow all",
@@ -137,22 +139,22 @@ func TestGoCollectorAllowList(t *testing.T) {
137139
{
138140
name: "allow GC",
139141
rules: []GoRuntimeMetricsRule{MetricsGC},
140-
expected: withGCMetrics(),
142+
expected: withDefaultRuntimeMetrics(withGCMetrics(), true, false),
141143
},
142144
{
143145
name: "allow Memory",
144146
rules: []GoRuntimeMetricsRule{MetricsMemory},
145-
expected: withMemoryMetrics(),
147+
expected: withDefaultRuntimeMetrics(withMemoryMetrics(), false, false),
146148
},
147149
{
148150
name: "allow Scheduler",
149151
rules: []GoRuntimeMetricsRule{MetricsScheduler},
150-
expected: withSchedulerMetrics(),
152+
expected: withDefaultRuntimeMetrics(withSchedulerMetrics(), false, true),
151153
},
152154
{
153155
name: "allow debug",
154156
rules: []GoRuntimeMetricsRule{MetricsDebug},
155-
expected: withDebugMetrics(),
157+
expected: withDefaultRuntimeMetrics(withDebugMetrics(), false, false),
156158
},
157159
} {
158160
t.Run(test.name, func(t *testing.T) {
@@ -193,7 +195,7 @@ func TestGoCollectorDenyList(t *testing.T) {
193195
{
194196
name: "Without any matchers",
195197
matchers: nil,
196-
expected: baseMetrics,
198+
expected: withBaseMetrics(defaultRuntimeMetrics),
197199
},
198200
{
199201
name: "deny all",
@@ -206,6 +208,14 @@ func TestGoCollectorDenyList(t *testing.T) {
206208
regexp.MustCompile("^/gc/.*"),
207209
regexp.MustCompile("^/sched/latencies:.*"),
208210
},
211+
expected: withDefaultRuntimeMetrics(baseMetrics, true, false),
212+
},
213+
{
214+
name: "deny gc and scheduler",
215+
matchers: []*regexp.Regexp{
216+
regexp.MustCompile("^/gc/.*"),
217+
regexp.MustCompile("^/sched/.*"),
218+
},
209219
expected: baseMetrics,
210220
},
211221
} {
@@ -235,7 +245,7 @@ func TestGoCollectorDenyList(t *testing.T) {
235245
func ExampleGoCollector() {
236246
reg := prometheus.NewRegistry()
237247

238-
// Register the GoCollector with the default options. Only the base metrics and memstats are enabled.
248+
// Register the GoCollector with the default options. Only the base metrics, default runtime metrics and memstats are enabled.
239249
reg.MustRegister(NewGoCollector())
240250

241251
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))

prometheus/gen_go_collector_metrics_set.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,29 @@ func main() {
6363
v := goVersion(gv.Segments()[1])
6464
log.Printf("generating metrics for Go version %q", v)
6565

66+
allDesc := metrics.All()
67+
68+
// Find default metrics.
69+
var defaultDesc []metrics.Description
70+
for _, d := range allDesc {
71+
if !internal.GoCollectorDefaultRuntimeMetrics.MatchString(d.Name) {
72+
continue
73+
}
74+
defaultDesc = append(defaultDesc, d)
75+
}
76+
6677
// Generate code.
6778
var buf bytes.Buffer
6879
err = testFile.Execute(&buf, struct {
69-
Descriptions []metrics.Description
70-
GoVersion goVersion
71-
Cardinality int
80+
AllDescriptions []metrics.Description
81+
DefaultDescriptions []metrics.Description
82+
GoVersion goVersion
83+
Cardinality int
7284
}{
73-
Descriptions: metrics.All(),
74-
GoVersion: v,
75-
Cardinality: rmCardinality(),
85+
AllDescriptions: allDesc,
86+
DefaultDescriptions: defaultDesc,
87+
GoVersion: v,
88+
Cardinality: rmCardinality(),
7689
})
7790
if err != nil {
7891
log.Fatalf("executing template: %v", err)
@@ -167,14 +180,25 @@ var testFile = template.Must(template.New("testFile").Funcs(map[string]interface
167180
168181
package prometheus
169182
170-
var expectedRuntimeMetrics = map[string]string{
171-
{{- range .Descriptions -}}
183+
var (
184+
expectedRuntimeMetrics = map[string]string{
185+
{{- range .AllDescriptions -}}
172186
{{- $trans := rm2prom . -}}
173187
{{- if ne $trans "" }}
174188
{{.Name | printf "%q"}}: {{$trans | printf "%q"}},
175189
{{- end -}}
176190
{{end}}
177-
}
191+
}
192+
193+
expMetrics = map[string]string{
194+
{{- range .DefaultDescriptions -}}
195+
{{- $trans := rm2prom . -}}
196+
{{- if ne $trans "" }}
197+
{{.Name | printf "%q"}}: {{$trans | printf "%q"}},
198+
{{- end -}}
199+
{{end}}
200+
}
201+
)
178202
179203
const expectedRuntimeMetricsCardinality = {{.Cardinality}}
180204
`))

prometheus/go_collector_latest.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package prometheus
1818

1919
import (
20+
"fmt"
2021
"math"
2122
"runtime"
2223
"runtime/metrics"
@@ -153,7 +154,8 @@ func defaultGoCollectorOptions() internal.GoCollectorOptions {
153154
"/gc/heap/frees-by-size:bytes": goGCHeapFreesBytes,
154155
},
155156
RuntimeMetricRules: []internal.GoCollectorRule{
156-
//{Matcher: regexp.MustCompile("")},
157+
// Recommended metrics we want by default from runtime/metrics.
158+
{Matcher: internal.GoCollectorDefaultRuntimeMetrics},
157159
},
158160
}
159161
}
@@ -376,13 +378,13 @@ func unwrapScalarRMValue(v metrics.Value) float64 {
376378
//
377379
// This should never happen because we always populate our metric
378380
// set from the runtime/metrics package.
379-
panic("unexpected unsupported metric")
381+
panic("unexpected bad kind metric")
380382
default:
381383
// Unsupported metric kind.
382384
//
383385
// This should never happen because we check for this during initialization
384386
// and flag and filter metrics whose kinds we don't understand.
385-
panic("unexpected unsupported metric kind")
387+
panic(fmt.Sprintf("unexpected unsupported metric: %v", v.Kind()))
386388
}
387389
}
388390

prometheus/go_collector_latest_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ func addExpectedRuntimeMetrics(metrics map[string]struct{}) map[string]struct{}
7474
return metrics
7575
}
7676

77+
func addExpectedDefaultRuntimeMetrics(metrics map[string]struct{}) map[string]struct{} {
78+
for _, e := range expMetrics {
79+
metrics[e] = struct{}{}
80+
}
81+
return metrics
82+
}
83+
7784
func TestGoCollector_ExposedMetrics(t *testing.T) {
7885
for _, tcase := range []struct {
7986
opts internal.GoCollectorOptions
@@ -86,8 +93,9 @@ func TestGoCollector_ExposedMetrics(t *testing.T) {
8693
expectedFQNameSet: expectedBaseMetrics(),
8794
},
8895
{
89-
// Default, only MemStats.
90-
expectedFQNameSet: addExpectedRuntimeMemStats(expectedBaseMetrics()),
96+
// Default, only MemStats and default Runtime metrics.
97+
opts: defaultGoCollectorOptions(),
98+
expectedFQNameSet: addExpectedDefaultRuntimeMetrics(addExpectedRuntimeMemStats(expectedBaseMetrics())),
9199
},
92100
{
93101
// Get all runtime/metrics without MemStats.

0 commit comments

Comments
 (0)