Rethink struct for newly added metrics, adapt and add tests
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
This commit is contained in:
parent
5d64fb7625
commit
17b306559d
|
@ -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) {
|
func WithGoCollectorRuntimeEnvVarsMetricsDisabled() func(options *internal.GoCollectorOptions) {
|
||||||
return func(o *internal.GoCollectorOptions) {
|
return func(o *internal.GoCollectorOptions) {
|
||||||
o.DisableRuntimeEnvVarsMetrics = true
|
o.DisableRuntimeEnvVarsMetrics = true
|
||||||
|
|
|
@ -54,10 +54,11 @@ func TestGoCollectorMarshalling(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithGoCollectorMemStatsMetricsDisabled(t *testing.T) {
|
func TestWithBaseMetricsOnly(t *testing.T) {
|
||||||
reg := prometheus.NewRegistry()
|
reg := prometheus.NewRegistry()
|
||||||
reg.MustRegister(NewGoCollector(
|
reg.MustRegister(NewGoCollector(
|
||||||
WithGoCollectorMemStatsMetricsDisabled(),
|
WithGoCollectorMemStatsMetricsDisabled(),
|
||||||
|
WithGoCollectorRuntimeEnvVarsMetricsDisabled(),
|
||||||
))
|
))
|
||||||
result, err := reg.Gather()
|
result, err := reg.Gather()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -116,6 +117,7 @@ func TestGoCollectorAllowList(t *testing.T) {
|
||||||
reg.MustRegister(NewGoCollector(
|
reg.MustRegister(NewGoCollector(
|
||||||
WithGoCollectorMemStatsMetricsDisabled(),
|
WithGoCollectorMemStatsMetricsDisabled(),
|
||||||
WithGoCollectorRuntimeMetrics(test.rules...),
|
WithGoCollectorRuntimeMetrics(test.rules...),
|
||||||
|
WithGoCollectorRuntimeEnvVarsMetricsDisabled(),
|
||||||
))
|
))
|
||||||
result, err := reg.Gather()
|
result, err := reg.Gather()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -170,6 +172,7 @@ func TestGoCollectorDenyList(t *testing.T) {
|
||||||
reg.MustRegister(NewGoCollector(
|
reg.MustRegister(NewGoCollector(
|
||||||
WithGoCollectorMemStatsMetricsDisabled(),
|
WithGoCollectorMemStatsMetricsDisabled(),
|
||||||
WithoutGoCollectorRuntimeMetrics(test.matchers...),
|
WithoutGoCollectorRuntimeMetrics(test.matchers...),
|
||||||
|
WithGoCollectorRuntimeEnvVarsMetricsDisabled(),
|
||||||
))
|
))
|
||||||
result, err := reg.Gather()
|
result, err := reg.Gather()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -213,6 +216,7 @@ func ExampleGoCollector_WithAdvancedGoMetrics() {
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
WithoutGoCollectorRuntimeMetrics(regexp.MustCompile("^/gc/.*")),
|
WithoutGoCollectorRuntimeMetrics(regexp.MustCompile("^/gc/.*")),
|
||||||
|
WithGoCollectorRuntimeEnvVarsMetricsDisabled(),
|
||||||
))
|
))
|
||||||
|
|
||||||
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
|
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
|
||||||
|
|
|
@ -289,8 +289,7 @@ func goRuntimeEnvVarsMetrics() runtimeEnvVarsMetrics {
|
||||||
"Value of GOGC (percentage).",
|
"Value of GOGC (percentage).",
|
||||||
nil, nil,
|
nil, nil,
|
||||||
),
|
),
|
||||||
eval: float64(readRunMetrSample("/gc/gogc:percent")[0].Value.Uint64()),
|
origMetricName: "/gc/gogc:percent",
|
||||||
valType: GaugeValue,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: NewDesc(
|
desc: NewDesc(
|
||||||
|
@ -298,8 +297,7 @@ func goRuntimeEnvVarsMetrics() runtimeEnvVarsMetrics {
|
||||||
"Value of GOMEMLIMIT (bytes).",
|
"Value of GOMEMLIMIT (bytes).",
|
||||||
nil, nil,
|
nil, nil,
|
||||||
),
|
),
|
||||||
eval: float64(readRunMetrSample("/gc/gomemlimit:bytes")[0].Value.Uint64()),
|
origMetricName: "/gc/gomemlimit:bytes",
|
||||||
valType: GaugeValue,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: NewDesc(
|
desc: NewDesc(
|
||||||
|
@ -307,21 +305,19 @@ func goRuntimeEnvVarsMetrics() runtimeEnvVarsMetrics {
|
||||||
"Value of GOMAXPROCS, i.e number of usable threads.",
|
"Value of GOMAXPROCS, i.e number of usable threads.",
|
||||||
nil, nil,
|
nil, nil,
|
||||||
),
|
),
|
||||||
eval: float64(runtime.NumCPU()),
|
origMetricName: "/sched/gomaxprocs:threads",
|
||||||
valType: GaugeValue,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type runtimeEnvVarsMetrics []struct { // how to call this struct?
|
type runtimeEnvVarsMetrics []struct { // I couldn't come up with a better name. Any suggestions?
|
||||||
desc *Desc
|
desc *Desc
|
||||||
eval float64
|
origMetricName string
|
||||||
valType ValueType
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func readRunMetrSample(metricName string) []runmetr.Sample {
|
func readRunMetrSampleBuf(metricName string) []runmetr.Sample {
|
||||||
sample := make([]runmetr.Sample, 1)
|
sampleBuf := make([]runmetr.Sample, 1)
|
||||||
sample[0].Name = metricName
|
sampleBuf[0].Name = metricName
|
||||||
runmetr.Read(sample)
|
runmetr.Read(sampleBuf)
|
||||||
return sample
|
return sampleBuf
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,7 +81,7 @@ var rmNamesForMemStatsMetrics = []string{
|
||||||
goMemoryClassesOtherBytes,
|
goMemoryClassesOtherBytes,
|
||||||
}
|
}
|
||||||
|
|
||||||
var rmNamesForEnvVarsMetrics = []string{ // how to call them???
|
var rmNamesForEnvVarsMetrics = []string{ // how to name this var???
|
||||||
goGCGogcPercent,
|
goGCGogcPercent,
|
||||||
goGCMemLimit,
|
goGCMemLimit,
|
||||||
goSchedMaxProcs,
|
goSchedMaxProcs,
|
||||||
|
@ -389,6 +389,12 @@ func (c *goCollector) Collect(ch chan<- Metric) {
|
||||||
ch <- MustNewConstMetric(i.desc, i.valType, i.eval(&ms))
|
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
|
// unwrapScalarRMValue unwraps a runtime/metrics value that is assumed
|
||||||
|
|
|
@ -74,6 +74,13 @@ func addExpectedRuntimeMetrics(metrics map[string]struct{}) map[string]struct{}
|
||||||
return metrics
|
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) {
|
func TestGoCollector_ExposedMetrics(t *testing.T) {
|
||||||
for _, tcase := range []struct {
|
for _, tcase := range []struct {
|
||||||
opts internal.GoCollectorOptions
|
opts internal.GoCollectorOptions
|
||||||
|
@ -82,17 +89,26 @@ func TestGoCollector_ExposedMetrics(t *testing.T) {
|
||||||
{
|
{
|
||||||
opts: internal.GoCollectorOptions{
|
opts: internal.GoCollectorOptions{
|
||||||
DisableMemStatsLikeMetrics: true,
|
DisableMemStatsLikeMetrics: true,
|
||||||
|
DisableRuntimeEnvVarsMetrics: true,
|
||||||
},
|
},
|
||||||
expectedFQNameSet: expectedBaseMetrics(),
|
expectedFQNameSet: expectedBaseMetrics(),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// Default, only MemStats.
|
// Default, only Memstats and RuntimeEnvVars.
|
||||||
|
expectedFQNameSet: addExpectedEnvVarsMetrics(addExpectedRuntimeMemStats(expectedBaseMetrics())),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// Only MemStats.
|
||||||
|
opts: internal.GoCollectorOptions{
|
||||||
|
DisableRuntimeEnvVarsMetrics: true,
|
||||||
|
},
|
||||||
expectedFQNameSet: addExpectedRuntimeMemStats(expectedBaseMetrics()),
|
expectedFQNameSet: addExpectedRuntimeMemStats(expectedBaseMetrics()),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// Get all runtime/metrics without MemStats.
|
// Get all runtime/metrics without MemStats nor RuntimeEnvVars.
|
||||||
opts: internal.GoCollectorOptions{
|
opts: internal.GoCollectorOptions{
|
||||||
DisableMemStatsLikeMetrics: true,
|
DisableMemStatsLikeMetrics: true,
|
||||||
|
DisableRuntimeEnvVarsMetrics: true,
|
||||||
RuntimeMetricRules: []internal.GoCollectorRule{
|
RuntimeMetricRules: []internal.GoCollectorRule{
|
||||||
{Matcher: regexp.MustCompile("/.*")},
|
{Matcher: regexp.MustCompile("/.*")},
|
||||||
},
|
},
|
||||||
|
@ -100,13 +116,20 @@ func TestGoCollector_ExposedMetrics(t *testing.T) {
|
||||||
expectedFQNameSet: addExpectedRuntimeMetrics(expectedBaseMetrics()),
|
expectedFQNameSet: addExpectedRuntimeMetrics(expectedBaseMetrics()),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// Get all runtime/metrics and MemStats.
|
// Get all runtime/metrics, MemStats and RuntimeEnvVars.
|
||||||
opts: internal.GoCollectorOptions{
|
opts: internal.GoCollectorOptions{
|
||||||
RuntimeMetricRules: []internal.GoCollectorRule{
|
RuntimeMetricRules: []internal.GoCollectorRule{
|
||||||
{Matcher: regexp.MustCompile("/.*")},
|
{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) {
|
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.DisableMemStatsLikeMetrics = opts.DisableMemStatsLikeMetrics
|
||||||
o.RuntimeMetricSumForHist = opts.RuntimeMetricSumForHist
|
o.RuntimeMetricSumForHist = opts.RuntimeMetricSumForHist
|
||||||
o.RuntimeMetricRules = opts.RuntimeMetricRules
|
o.RuntimeMetricRules = opts.RuntimeMetricRules
|
||||||
|
o.DisableRuntimeEnvVarsMetrics = opts.DisableRuntimeEnvVarsMetrics
|
||||||
}).(*goCollector)
|
}).(*goCollector)
|
||||||
|
|
||||||
// Collect all metrics.
|
// Collect all metrics.
|
||||||
|
|
Loading…
Reference in New Issue