Simplify new metrics reading

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
This commit is contained in:
Arianna Vespri 2024-07-21 18:15:48 +02:00
parent 17b306559d
commit 5d9b680e77
2 changed files with 19 additions and 24 deletions

View File

@ -16,7 +16,6 @@ package prometheus
import ( import (
"runtime" "runtime"
"runtime/debug" "runtime/debug"
runmetr "runtime/metrics"
"time" "time"
) )
@ -314,10 +313,3 @@ type runtimeEnvVarsMetrics []struct { // I couldn't come up with a better name.
desc *Desc desc *Desc
origMetricName string origMetricName string
} }
func readRunMetrSampleBuf(metricName string) []runmetr.Sample {
sampleBuf := make([]runmetr.Sample, 1)
sampleBuf[0].Name = metricName
runmetr.Read(sampleBuf)
return sampleBuf
}

View File

@ -126,8 +126,8 @@ type goCollector struct {
msMetrics memStatsMetrics msMetrics memStatsMetrics
msMetricsEnabled bool msMetricsEnabled bool
rmEnvVarMetrics runtimeEnvVarsMetrics // how to call them??? rmEnvVarsMetrics runtimeEnvVarsMetrics // how to call them???
rmEnvVarMetricsEnabled bool rmEnvVarsMetricsEnabled bool
} }
type rmMetricDesc struct { type rmMetricDesc struct {
@ -269,7 +269,7 @@ func NewGoCollector(opts ...func(o *internal.GoCollectorOptions)) Collector {
var ( var (
msMetrics memStatsMetrics msMetrics memStatsMetrics
msDescriptions []metrics.Description msDescriptions []metrics.Description
rmEnvVarMetrics runtimeEnvVarsMetrics rmEnvVarsMetrics runtimeEnvVarsMetrics
rmEnvVarsDescriptions []metrics.Description rmEnvVarsDescriptions []metrics.Description
) )
@ -288,7 +288,7 @@ func NewGoCollector(opts ...func(o *internal.GoCollectorOptions)) Collector {
} }
if !opt.DisableRuntimeEnvVarsMetrics { if !opt.DisableRuntimeEnvVarsMetrics {
rmEnvVarMetrics = goRuntimeEnvVarsMetrics() rmEnvVarsMetrics = goRuntimeEnvVarsMetrics()
rmEnvVarsDescriptions = bestEffortLookupRM(rmNamesForEnvVarsMetrics) rmEnvVarsDescriptions = bestEffortLookupRM(rmNamesForEnvVarsMetrics)
// Check if metric was not exposed before and if not, add to sampleBuf. // Check if metric was not exposed before and if not, add to sampleBuf.
@ -301,15 +301,15 @@ func NewGoCollector(opts ...func(o *internal.GoCollectorOptions)) Collector {
} }
} }
return &goCollector{ return &goCollector{
base: newBaseGoCollector(), base: newBaseGoCollector(),
sampleBuf: sampleBuf, sampleBuf: sampleBuf,
sampleMap: sampleMap, sampleMap: sampleMap,
rmExposedMetrics: metricSet, rmExposedMetrics: metricSet,
rmExactSumMapForHist: opt.RuntimeMetricSumForHist, rmExactSumMapForHist: opt.RuntimeMetricSumForHist,
msMetrics: msMetrics, msMetrics: msMetrics,
msMetricsEnabled: !opt.DisableMemStatsLikeMetrics, msMetricsEnabled: !opt.DisableMemStatsLikeMetrics,
rmEnvVarMetrics: rmEnvVarMetrics, rmEnvVarsMetrics: rmEnvVarsMetrics,
rmEnvVarMetricsEnabled: !opt.DisableRuntimeEnvVarsMetrics, rmEnvVarsMetricsEnabled: !opt.DisableRuntimeEnvVarsMetrics,
} }
} }
@ -390,9 +390,12 @@ func (c *goCollector) Collect(ch chan<- Metric) {
} }
} }
if c.rmEnvVarMetricsEnabled { if c.rmEnvVarsMetricsEnabled {
for _, v := range c.rmEnvVarMetrics { sampleBuf := make([]metrics.Sample, len(c.rmEnvVarsMetrics))
ch <- MustNewConstMetric(v.desc, GaugeValue, float64(readRunMetrSampleBuf(v.origMetricName)[0].Value.Uint64())) for i, v := range c.rmEnvVarsMetrics {
sampleBuf[i].Name = v.origMetricName
metrics.Read(sampleBuf)
ch <- MustNewConstMetric(v.desc, GaugeValue, unwrapScalarRMValue(sampleBuf[i].Value))
} }
} }
} }