Rethink struct for newly added metrics, adapt and add tests

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
This commit is contained in:
Arianna Vespri 2024-07-21 10:17:02 +02:00
parent 5d64fb7625
commit 17b306559d
5 changed files with 57 additions and 23 deletions

View File

@ -88,6 +88,10 @@ func WithGoCollectorMemStatsMetricsDisabled() func(options *internal.GoCollector
}
}
// WithGoCollectorRuntimeEnvVarsMetricsDisabled disables the following default metrics:
// go_gogc_percent
// go_gomemlimit
// go_gomaxprocs
func WithGoCollectorRuntimeEnvVarsMetricsDisabled() func(options *internal.GoCollectorOptions) {
return func(o *internal.GoCollectorOptions) {
o.DisableRuntimeEnvVarsMetrics = true

View File

@ -54,10 +54,11 @@ func TestGoCollectorMarshalling(t *testing.T) {
}
}
func TestWithGoCollectorMemStatsMetricsDisabled(t *testing.T) {
func TestWithBaseMetricsOnly(t *testing.T) {
reg := prometheus.NewRegistry()
reg.MustRegister(NewGoCollector(
WithGoCollectorMemStatsMetricsDisabled(),
WithGoCollectorRuntimeEnvVarsMetricsDisabled(),
))
result, err := reg.Gather()
if err != nil {
@ -116,6 +117,7 @@ func TestGoCollectorAllowList(t *testing.T) {
reg.MustRegister(NewGoCollector(
WithGoCollectorMemStatsMetricsDisabled(),
WithGoCollectorRuntimeMetrics(test.rules...),
WithGoCollectorRuntimeEnvVarsMetricsDisabled(),
))
result, err := reg.Gather()
if err != nil {
@ -170,6 +172,7 @@ func TestGoCollectorDenyList(t *testing.T) {
reg.MustRegister(NewGoCollector(
WithGoCollectorMemStatsMetricsDisabled(),
WithoutGoCollectorRuntimeMetrics(test.matchers...),
WithGoCollectorRuntimeEnvVarsMetricsDisabled(),
))
result, err := reg.Gather()
if err != nil {
@ -213,6 +216,7 @@ func ExampleGoCollector_WithAdvancedGoMetrics() {
},
),
WithoutGoCollectorRuntimeMetrics(regexp.MustCompile("^/gc/.*")),
WithGoCollectorRuntimeEnvVarsMetricsDisabled(),
))
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))

View File

@ -289,8 +289,7 @@ func goRuntimeEnvVarsMetrics() runtimeEnvVarsMetrics {
"Value of GOGC (percentage).",
nil, nil,
),
eval: float64(readRunMetrSample("/gc/gogc:percent")[0].Value.Uint64()),
valType: GaugeValue,
origMetricName: "/gc/gogc:percent",
},
{
desc: NewDesc(
@ -298,8 +297,7 @@ func goRuntimeEnvVarsMetrics() runtimeEnvVarsMetrics {
"Value of GOMEMLIMIT (bytes).",
nil, nil,
),
eval: float64(readRunMetrSample("/gc/gomemlimit:bytes")[0].Value.Uint64()),
valType: GaugeValue,
origMetricName: "/gc/gomemlimit:bytes",
},
{
desc: NewDesc(
@ -307,21 +305,19 @@ func goRuntimeEnvVarsMetrics() runtimeEnvVarsMetrics {
"Value of GOMAXPROCS, i.e number of usable threads.",
nil, nil,
),
eval: float64(runtime.NumCPU()),
valType: GaugeValue,
origMetricName: "/sched/gomaxprocs:threads",
},
}
}
type runtimeEnvVarsMetrics []struct { // how to call this struct?
type runtimeEnvVarsMetrics []struct { // I couldn't come up with a better name. Any suggestions?
desc *Desc
eval float64
valType ValueType
origMetricName string
}
func readRunMetrSample(metricName string) []runmetr.Sample {
sample := make([]runmetr.Sample, 1)
sample[0].Name = metricName
runmetr.Read(sample)
return sample
func readRunMetrSampleBuf(metricName string) []runmetr.Sample {
sampleBuf := make([]runmetr.Sample, 1)
sampleBuf[0].Name = metricName
runmetr.Read(sampleBuf)
return sampleBuf
}

View File

@ -81,7 +81,7 @@ var rmNamesForMemStatsMetrics = []string{
goMemoryClassesOtherBytes,
}
var rmNamesForEnvVarsMetrics = []string{ // how to call them???
var rmNamesForEnvVarsMetrics = []string{ // how to name this var???
goGCGogcPercent,
goGCMemLimit,
goSchedMaxProcs,
@ -389,6 +389,12 @@ func (c *goCollector) Collect(ch chan<- Metric) {
ch <- MustNewConstMetric(i.desc, i.valType, i.eval(&ms))
}
}
if c.rmEnvVarMetricsEnabled {
for _, v := range c.rmEnvVarMetrics {
ch <- MustNewConstMetric(v.desc, GaugeValue, float64(readRunMetrSampleBuf(v.origMetricName)[0].Value.Uint64()))
}
}
}
// unwrapScalarRMValue unwraps a runtime/metrics value that is assumed

View File

@ -74,6 +74,13 @@ func addExpectedRuntimeMetrics(metrics map[string]struct{}) map[string]struct{}
return metrics
}
func addExpectedEnvVarsMetrics(metrics map[string]struct{}) map[string]struct{} {
for _, m := range goRuntimeEnvVarsMetrics() {
metrics[m.desc.fqName] = struct{}{}
}
return metrics
}
func TestGoCollector_ExposedMetrics(t *testing.T) {
for _, tcase := range []struct {
opts internal.GoCollectorOptions
@ -82,17 +89,26 @@ func TestGoCollector_ExposedMetrics(t *testing.T) {
{
opts: internal.GoCollectorOptions{
DisableMemStatsLikeMetrics: true,
DisableRuntimeEnvVarsMetrics: true,
},
expectedFQNameSet: expectedBaseMetrics(),
},
{
// Default, only MemStats.
// Default, only Memstats and RuntimeEnvVars.
expectedFQNameSet: addExpectedEnvVarsMetrics(addExpectedRuntimeMemStats(expectedBaseMetrics())),
},
{
// Only MemStats.
opts: internal.GoCollectorOptions{
DisableRuntimeEnvVarsMetrics: true,
},
expectedFQNameSet: addExpectedRuntimeMemStats(expectedBaseMetrics()),
},
{
// Get all runtime/metrics without MemStats.
// Get all runtime/metrics without MemStats nor RuntimeEnvVars.
opts: internal.GoCollectorOptions{
DisableMemStatsLikeMetrics: true,
DisableRuntimeEnvVarsMetrics: true,
RuntimeMetricRules: []internal.GoCollectorRule{
{Matcher: regexp.MustCompile("/.*")},
},
@ -100,13 +116,20 @@ func TestGoCollector_ExposedMetrics(t *testing.T) {
expectedFQNameSet: addExpectedRuntimeMetrics(expectedBaseMetrics()),
},
{
// Get all runtime/metrics and MemStats.
// Get all runtime/metrics, MemStats and RuntimeEnvVars.
opts: internal.GoCollectorOptions{
RuntimeMetricRules: []internal.GoCollectorRule{
{Matcher: regexp.MustCompile("/.*")},
},
},
expectedFQNameSet: addExpectedRuntimeMemStats(addExpectedRuntimeMetrics(expectedBaseMetrics())),
expectedFQNameSet: addExpectedEnvVarsMetrics(addExpectedRuntimeMemStats(addExpectedRuntimeMetrics(expectedBaseMetrics()))),
},
{
// Only RuntimeEnvVars.
opts: internal.GoCollectorOptions{
DisableMemStatsLikeMetrics: true,
},
expectedFQNameSet: addExpectedEnvVarsMetrics(expectedBaseMetrics()),
},
} {
if ok := t.Run("", func(t *testing.T) {
@ -229,6 +252,7 @@ func collectGoMetrics(t *testing.T, opts internal.GoCollectorOptions) []Metric {
o.DisableMemStatsLikeMetrics = opts.DisableMemStatsLikeMetrics
o.RuntimeMetricSumForHist = opts.RuntimeMetricSumForHist
o.RuntimeMetricRules = opts.RuntimeMetricRules
o.DisableRuntimeEnvVarsMetrics = opts.DisableRuntimeEnvVarsMetrics
}).(*goCollector)
// Collect all metrics.