Move newly added metrics out of base metrics and into goCollector

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
This commit is contained in:
Arianna Vespri 2024-07-19 15:14:03 +02:00
parent cd8445b70b
commit 5d64fb7625
6 changed files with 94 additions and 49 deletions

View File

@ -88,6 +88,12 @@ func WithGoCollectorMemStatsMetricsDisabled() func(options *internal.GoCollector
}
}
func WithGoCollectorRuntimeEnvVarsMetricsDisabled() func(options *internal.GoCollectorOptions) {
return func(o *internal.GoCollectorOptions) {
o.DisableRuntimeEnvVarsMetrics = true
}
}
// GoRuntimeMetricsRule allow enabling and configuring particular group of runtime/metrics.
// TODO(bwplotka): Consider adding ability to adjust buckets.
type GoRuntimeMetricsRule struct {

View File

@ -31,9 +31,6 @@ import (
var baseMetrics = []string{
"go_gc_duration_seconds",
"go_gogc_percent",
"go_gomaxprocs",
"go_gomemlimit",
"go_goroutines",
"go_info",
"go_memstats_last_gc_time_seconds",

View File

@ -212,9 +212,6 @@ type baseGoCollector struct {
gcDesc *Desc
gcLastTimeDesc *Desc
goInfoDesc *Desc
goMaxProcs *Desc
goGogcPercent *Desc
goMemLimit *Desc
}
func newBaseGoCollector() baseGoCollector {
@ -239,18 +236,6 @@ func newBaseGoCollector() baseGoCollector {
"go_info",
"Information about the Go environment.",
nil, Labels{"version": runtime.Version()}),
goMaxProcs: NewDesc(
"go_gomaxprocs",
"Value of GOMAXPROCS, i.e number of usable threads.",
nil, nil),
goGogcPercent: NewDesc(
"go_gogc_percent",
"Value of GOGC (percentage).",
nil, nil),
goMemLimit: NewDesc(
"go_gomemlimit",
"Value of GOMEMLIMIT (bytes).",
nil, nil),
}
}
@ -261,9 +246,6 @@ func (c *baseGoCollector) Describe(ch chan<- *Desc) {
ch <- c.gcDesc
ch <- c.gcLastTimeDesc
ch <- c.goInfoDesc
ch <- c.goMaxProcs
ch <- c.goGogcPercent
ch <- c.goMemLimit
}
// Collect returns the current state of all metrics of the collector.
@ -285,18 +267,6 @@ func (c *baseGoCollector) Collect(ch chan<- Metric) {
ch <- MustNewConstSummary(c.gcDesc, uint64(stats.NumGC), stats.PauseTotal.Seconds(), quantiles)
ch <- MustNewConstMetric(c.gcLastTimeDesc, GaugeValue, float64(stats.LastGC.UnixNano())/1e9)
ch <- MustNewConstMetric(c.goInfoDesc, GaugeValue, 1)
ch <- MustNewConstMetric(c.goMaxProcs, GaugeValue, float64(runtime.NumCPU()))
gogcSample := make([]runmetr.Sample, 1)
gogcSample[0].Name = "/gc/gogc:percent"
runmetr.Read(gogcSample)
ch <- MustNewConstMetric(c.goGogcPercent, GaugeValue, float64(gogcSample[0].Value.Uint64()))
memLimitSample := make([]runmetr.Sample, 1)
memLimitSample[0].Name = "/gc/gomemlimit:bytes"
runmetr.Read(memLimitSample)
ch <- MustNewConstMetric(c.goMemLimit, GaugeValue, float64(memLimitSample[0].Value.Uint64()))
}
func memstatNamespace(s string) string {
@ -310,3 +280,48 @@ type memStatsMetrics []struct {
eval func(*runtime.MemStats) float64
valType ValueType
}
func goRuntimeEnvVarsMetrics() runtimeEnvVarsMetrics {
return runtimeEnvVarsMetrics{
{
desc: NewDesc(
"go_gogc_percent",
"Value of GOGC (percentage).",
nil, nil,
),
eval: float64(readRunMetrSample("/gc/gogc:percent")[0].Value.Uint64()),
valType: GaugeValue,
},
{
desc: NewDesc(
"go_gomemlimit",
"Value of GOMEMLIMIT (bytes).",
nil, nil,
),
eval: float64(readRunMetrSample("/gc/gomemlimit:bytes")[0].Value.Uint64()),
valType: GaugeValue,
},
{
desc: NewDesc(
"go_gomaxprocs",
"Value of GOMAXPROCS, i.e number of usable threads.",
nil, nil,
),
eval: float64(runtime.NumCPU()),
valType: GaugeValue,
},
}
}
type runtimeEnvVarsMetrics []struct { // how to call this struct?
desc *Desc
eval float64
valType ValueType
}
func readRunMetrSample(metricName string) []runmetr.Sample {
sample := make([]runmetr.Sample, 1)
sample[0].Name = metricName
runmetr.Read(sample)
return sample
}

View File

@ -31,6 +31,7 @@ import (
const (
// constants for strings referenced more than once.
goGCGogcPercent = "/gc/gogc:percent"
goGCHeapTinyAllocsObjects = "/gc/heap/tiny/allocs:objects"
goGCHeapAllocsObjects = "/gc/heap/allocs:objects"
goGCHeapFreesObjects = "/gc/heap/frees:objects"
@ -38,6 +39,7 @@ const (
goGCHeapAllocsBytes = "/gc/heap/allocs:bytes"
goGCHeapObjects = "/gc/heap/objects:objects"
goGCHeapGoalBytes = "/gc/heap/goal:bytes"
goGCMemLimit = "/gc/gomemlimit:bytes"
goMemoryClassesTotalBytes = "/memory/classes/total:bytes"
goMemoryClassesHeapObjectsBytes = "/memory/classes/heap/objects:bytes"
goMemoryClassesHeapUnusedBytes = "/memory/classes/heap/unused:bytes"
@ -52,6 +54,7 @@ const (
goMemoryClassesProfilingBucketsBytes = "/memory/classes/profiling/buckets:bytes"
goMemoryClassesMetadataOtherBytes = "/memory/classes/metadata/other:bytes"
goMemoryClassesOtherBytes = "/memory/classes/other:bytes"
goSchedMaxProcs = "/sched/gomaxprocs:threads"
)
// rmNamesForMemStatsMetrics represents runtime/metrics names required to populate goRuntimeMemStats from like logic.
@ -78,6 +81,12 @@ var rmNamesForMemStatsMetrics = []string{
goMemoryClassesOtherBytes,
}
var rmNamesForEnvVarsMetrics = []string{ // how to call them???
goGCGogcPercent,
goGCMemLimit,
goSchedMaxProcs,
}
func bestEffortLookupRM(lookup []string) []metrics.Description {
ret := make([]metrics.Description, 0, len(lookup))
for _, rm := range metrics.All() {
@ -116,6 +125,9 @@ type goCollector struct {
// as well.
msMetrics memStatsMetrics
msMetricsEnabled bool
rmEnvVarMetrics runtimeEnvVarsMetrics // how to call them???
rmEnvVarMetricsEnabled bool
}
type rmMetricDesc struct {
@ -193,7 +205,7 @@ func NewGoCollector(opts ...func(o *internal.GoCollectorOptions)) Collector {
metricSet := make([]collectorMetric, 0, len(exposedDescriptions))
// SampleBuf is used for reading from runtime/metrics.
// We are assuming the largest case to have stable pointers for sampleMap purposes.
sampleBuf := make([]metrics.Sample, 0, len(exposedDescriptions)+len(opt.RuntimeMetricSumForHist)+len(rmNamesForMemStatsMetrics))
sampleBuf := make([]metrics.Sample, 0, len(exposedDescriptions)+len(opt.RuntimeMetricSumForHist)+len(rmNamesForMemStatsMetrics)+len(rmNamesForEnvVarsMetrics))
sampleMap := make(map[string]*metrics.Sample, len(exposedDescriptions))
for _, d := range exposedDescriptions {
namespace, subsystem, name, ok := internal.RuntimeMetricsToProm(&d.Description)
@ -255,8 +267,10 @@ func NewGoCollector(opts ...func(o *internal.GoCollectorOptions)) Collector {
}
var (
msMetrics memStatsMetrics
msDescriptions []metrics.Description
msMetrics memStatsMetrics
msDescriptions []metrics.Description
rmEnvVarMetrics runtimeEnvVarsMetrics
rmEnvVarsDescriptions []metrics.Description
)
if !opt.DisableMemStatsLikeMetrics {
@ -273,14 +287,29 @@ func NewGoCollector(opts ...func(o *internal.GoCollectorOptions)) Collector {
}
}
if !opt.DisableRuntimeEnvVarsMetrics {
rmEnvVarMetrics = goRuntimeEnvVarsMetrics()
rmEnvVarsDescriptions = bestEffortLookupRM(rmNamesForEnvVarsMetrics)
// Check if metric was not exposed before and if not, add to sampleBuf.
for _, rnevDesc := range rmEnvVarsDescriptions {
if _, ok := sampleMap[rnevDesc.Name]; ok {
continue
}
sampleBuf = append(sampleBuf, metrics.Sample{Name: rnevDesc.Name})
sampleMap[rnevDesc.Name] = &sampleBuf[len(sampleBuf)-1]
}
}
return &goCollector{
base: newBaseGoCollector(),
sampleBuf: sampleBuf,
sampleMap: sampleMap,
rmExposedMetrics: metricSet,
rmExactSumMapForHist: opt.RuntimeMetricSumForHist,
msMetrics: msMetrics,
msMetricsEnabled: !opt.DisableMemStatsLikeMetrics,
base: newBaseGoCollector(),
sampleBuf: sampleBuf,
sampleMap: sampleMap,
rmExposedMetrics: metricSet,
rmExactSumMapForHist: opt.RuntimeMetricSumForHist,
msMetrics: msMetrics,
msMetricsEnabled: !opt.DisableMemStatsLikeMetrics,
rmEnvVarMetrics: rmEnvVarMetrics,
rmEnvVarMetricsEnabled: !opt.DisableRuntimeEnvVarsMetrics,
}
}

View File

@ -54,9 +54,6 @@ func expectedBaseMetrics() map[string]struct{} {
b.goroutinesDesc.fqName,
b.gcLastTimeDesc.fqName,
b.threadsDesc.fqName,
b.goMaxProcs.fqName,
b.goGogcPercent.fqName,
b.goMemLimit.fqName,
} {
metrics[m] = struct{}{}
}

View File

@ -26,7 +26,8 @@ type GoCollectorRule struct {
//
// This is internal, so external users only can use it via `collector.WithGoCollector*` methods
type GoCollectorOptions struct {
DisableMemStatsLikeMetrics bool
RuntimeMetricSumForHist map[string]string
RuntimeMetricRules []GoCollectorRule
DisableMemStatsLikeMetrics bool
RuntimeMetricSumForHist map[string]string
RuntimeMetricRules []GoCollectorRule
DisableRuntimeEnvVarsMetrics bool
}