Skip to content

Commit 20ebd6c

Browse files
committed
Move default runtime metrics into the runtime metrics flow, change tests accordingly
Signed-off-by: Arianna Vespri <[email protected]>
1 parent a04c891 commit 20ebd6c

13 files changed

+148
-180
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.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,6 @@ func WithGoCollectorMemStatsMetricsDisabled() func(options *internal.GoCollector
8888
}
8989
}
9090

91-
// WithGoCollectorRuntimeEnvVarsMetricsDisabled disables the following default metrics:
92-
// go_gogc_percent
93-
// go_gomemlimit
94-
// go_gomaxprocs
95-
func WithGoCollectorRuntimeEnvVarsMetricsDisabled() func(options *internal.GoCollectorOptions) {
96-
return func(o *internal.GoCollectorOptions) {
97-
o.DisableRuntimeEnvVarsMetrics = true
98-
}
99-
}
100-
10191
// GoRuntimeMetricsRule allow enabling and configuring particular group of runtime/metrics.
10292
// TODO(bwplotka): Consider adding ability to adjust buckets.
10393
type GoRuntimeMetricsRule struct {

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/go_collector.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,3 @@ type memStatsMetrics []struct {
280280
eval func(*runtime.MemStats) float64
281281
valType ValueType
282282
}
283-
284-
type runtimeEnvVarsMetrics []struct { // I couldn't come up with a better name. Any suggestions?
285-
desc *Desc
286-
origMetricName string
287-
}

prometheus/go_collector_go120.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

prometheus/go_collector_go121.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)