From cd8445b70b627d5f5e91e44d13106a224fb39d09 Mon Sep 17 00:00:00 2001 From: Arianna Vespri Date: Thu, 11 Jul 2024 16:36:45 +0200 Subject: [PATCH] Add go_gomaxprocs, go_gogc_percent and go_gomemlimit to the default Go runtime metrics Signed-off-by: Arianna Vespri --- go.mod | 1 + go.sum | 2 ++ .../collectors/go_collector_latest_test.go | 3 ++ prometheus/go_collector.go | 31 +++++++++++++++++++ prometheus/go_collector_latest_test.go | 3 ++ 5 files changed, 40 insertions(+) diff --git a/go.mod b/go.mod index 60f8dc1..afe7cd1 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( require ( github.com/golang/protobuf v1.5.3 // indirect + github.com/hashicorp/go-version v1.7.0 github.com/jpillora/backoff v1.0.0 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect diff --git a/go.sum b/go.sum index 743a74f..1d048bd 100644 --- a/go.sum +++ b/go.sum @@ -14,6 +14,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY= +github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= diff --git a/prometheus/collectors/go_collector_latest_test.go b/prometheus/collectors/go_collector_latest_test.go index 76673f3..da2e89f 100644 --- a/prometheus/collectors/go_collector_latest_test.go +++ b/prometheus/collectors/go_collector_latest_test.go @@ -31,6 +31,9 @@ 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", diff --git a/prometheus/go_collector.go b/prometheus/go_collector.go index ad9a71a..289a37d 100644 --- a/prometheus/go_collector.go +++ b/prometheus/go_collector.go @@ -16,6 +16,7 @@ package prometheus import ( "runtime" "runtime/debug" + runmetr "runtime/metrics" "time" ) @@ -211,6 +212,9 @@ type baseGoCollector struct { gcDesc *Desc gcLastTimeDesc *Desc goInfoDesc *Desc + goMaxProcs *Desc + goGogcPercent *Desc + goMemLimit *Desc } func newBaseGoCollector() baseGoCollector { @@ -235,6 +239,18 @@ 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), } } @@ -245,6 +261,9 @@ 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. @@ -266,6 +285,18 @@ 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 { diff --git a/prometheus/go_collector_latest_test.go b/prometheus/go_collector_latest_test.go index 551f497..f2a3177 100644 --- a/prometheus/go_collector_latest_test.go +++ b/prometheus/go_collector_latest_test.go @@ -54,6 +54,9 @@ 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{}{} }