From 5d9b680e77388aae7cbdcf6ba6a5549d5c45d111 Mon Sep 17 00:00:00 2001 From: Arianna Vespri Date: Sun, 21 Jul 2024 18:15:48 +0200 Subject: [PATCH] Simplify new metrics reading Signed-off-by: Arianna Vespri --- prometheus/go_collector.go | 8 ------- prometheus/go_collector_latest.go | 35 +++++++++++++++++-------------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/prometheus/go_collector.go b/prometheus/go_collector.go index 6e368b2..5d6fe60 100644 --- a/prometheus/go_collector.go +++ b/prometheus/go_collector.go @@ -16,7 +16,6 @@ package prometheus import ( "runtime" "runtime/debug" - runmetr "runtime/metrics" "time" ) @@ -314,10 +313,3 @@ type runtimeEnvVarsMetrics []struct { // I couldn't come up with a better name. desc *Desc origMetricName string } - -func readRunMetrSampleBuf(metricName string) []runmetr.Sample { - sampleBuf := make([]runmetr.Sample, 1) - sampleBuf[0].Name = metricName - runmetr.Read(sampleBuf) - return sampleBuf -} diff --git a/prometheus/go_collector_latest.go b/prometheus/go_collector_latest.go index 7896503..9d2ce1d 100644 --- a/prometheus/go_collector_latest.go +++ b/prometheus/go_collector_latest.go @@ -126,8 +126,8 @@ type goCollector struct { msMetrics memStatsMetrics msMetricsEnabled bool - rmEnvVarMetrics runtimeEnvVarsMetrics // how to call them??? - rmEnvVarMetricsEnabled bool + rmEnvVarsMetrics runtimeEnvVarsMetrics // how to call them??? + rmEnvVarsMetricsEnabled bool } type rmMetricDesc struct { @@ -269,7 +269,7 @@ func NewGoCollector(opts ...func(o *internal.GoCollectorOptions)) Collector { var ( msMetrics memStatsMetrics msDescriptions []metrics.Description - rmEnvVarMetrics runtimeEnvVarsMetrics + rmEnvVarsMetrics runtimeEnvVarsMetrics rmEnvVarsDescriptions []metrics.Description ) @@ -288,7 +288,7 @@ func NewGoCollector(opts ...func(o *internal.GoCollectorOptions)) Collector { } if !opt.DisableRuntimeEnvVarsMetrics { - rmEnvVarMetrics = goRuntimeEnvVarsMetrics() + rmEnvVarsMetrics = goRuntimeEnvVarsMetrics() rmEnvVarsDescriptions = bestEffortLookupRM(rmNamesForEnvVarsMetrics) // 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{ - base: newBaseGoCollector(), - sampleBuf: sampleBuf, - sampleMap: sampleMap, - rmExposedMetrics: metricSet, - rmExactSumMapForHist: opt.RuntimeMetricSumForHist, - msMetrics: msMetrics, - msMetricsEnabled: !opt.DisableMemStatsLikeMetrics, - rmEnvVarMetrics: rmEnvVarMetrics, - rmEnvVarMetricsEnabled: !opt.DisableRuntimeEnvVarsMetrics, + base: newBaseGoCollector(), + sampleBuf: sampleBuf, + sampleMap: sampleMap, + rmExposedMetrics: metricSet, + rmExactSumMapForHist: opt.RuntimeMetricSumForHist, + msMetrics: msMetrics, + msMetricsEnabled: !opt.DisableMemStatsLikeMetrics, + rmEnvVarsMetrics: rmEnvVarsMetrics, + rmEnvVarsMetricsEnabled: !opt.DisableRuntimeEnvVarsMetrics, } } @@ -390,9 +390,12 @@ func (c *goCollector) Collect(ch chan<- Metric) { } } - if c.rmEnvVarMetricsEnabled { - for _, v := range c.rmEnvVarMetrics { - ch <- MustNewConstMetric(v.desc, GaugeValue, float64(readRunMetrSampleBuf(v.origMetricName)[0].Value.Uint64())) + if c.rmEnvVarsMetricsEnabled { + sampleBuf := make([]metrics.Sample, len(c.rmEnvVarsMetrics)) + for i, v := range c.rmEnvVarsMetrics { + sampleBuf[i].Name = v.origMetricName + metrics.Read(sampleBuf) + ch <- MustNewConstMetric(v.desc, GaugeValue, unwrapScalarRMValue(sampleBuf[i].Value)) } } }