From eb2b34a45a79d98ff7cc36f4bff6b5b374546275 Mon Sep 17 00:00:00 2001 From: Philippe Richard Date: Tue, 10 Jan 2023 13:04:19 -0500 Subject: [PATCH] feat: enable cpu/memory process metrics (#293) --- cmd/do-agent/config.go | 21 +++++++++++++++++++++ cmd/do-agent/main.go | 1 + internal/process/collector.go | 7 +++++++ 3 files changed, 29 insertions(+) diff --git a/cmd/do-agent/config.go b/cmd/do-agent/config.go index 339894d1..5ac9c2a9 100644 --- a/cmd/do-agent/config.go +++ b/cmd/do-agent/config.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "hash/fnv" "net/url" "os" "strings" @@ -63,6 +64,8 @@ const ( defaultAuthURL = internalProxyURL defaultSonarURL = "" defaultWebListenAddress = "127.0.0.1:9100" + + processScrapingDropletPct = 10 ) var defaultMetadataURL = fmt.Sprintf("%s/metadata", internalProxyURL) @@ -165,6 +168,24 @@ func checkConfig() error { return nil } +func toggleGradualRollouts() { + hostname, err := os.Hostname() + if err != nil { + return + } + + hash := fnv.New64a() + _, err = hash.Write([]byte(hostname)) + if err != nil { + return + } + + if hash.Sum64()%100 <= processScrapingDropletPct { + log.Debug("Enabling process scraping") + config.noProcesses = false + } +} + func initWriter(wc *prometheus.CounterVec) (metricWriter, limiter) { if config.stdoutOnly { return writer.NewFile(os.Stdout, wc), &constThrottler{wait: 10 * time.Second} diff --git a/cmd/do-agent/main.go b/cmd/do-agent/main.go index b9d73e51..01e27520 100644 --- a/cmd/do-agent/main.go +++ b/cmd/do-agent/main.go @@ -26,6 +26,7 @@ func main() { log.Fatal("configuration failure: %+v", err) } + toggleGradualRollouts() cols := initCollectors() reg := prometheus.NewRegistry() reg.MustRegister(cols...) diff --git a/internal/process/collector.go b/internal/process/collector.go index b270d30c..070f6506 100644 --- a/internal/process/collector.go +++ b/internal/process/collector.go @@ -11,6 +11,7 @@ import ( type processCollector struct { collectFn func(chan<- prometheus.Metric) rss *prometheus.Desc + cpuTime *prometheus.Desc } // NewProcessCollector returns a collector which exports the current state of @@ -23,6 +24,11 @@ func NewProcessCollector() prometheus.Collector { "Resident memory size in bytes.", []string{"process", "pid"}, nil, ), + cpuTime: prometheus.NewDesc( + "sonar_process_cpu_time_seconds", + "CPU time in seconds.", + []string{"process", "pid"}, nil, + ), } if _, err := procfs.NewStat(); err == nil { @@ -66,5 +72,6 @@ func (c *processCollector) processCollect(ch chan<- prometheus.Metric) { pid := strconv.Itoa(stat.PID) ch <- prometheus.MustNewConstMetric(c.rss, prometheus.GaugeValue, float64(stat.ResidentMemory()), name, pid) + ch <- prometheus.MustNewConstMetric(c.cpuTime, prometheus.GaugeValue, stat.CPUTime(), name, pid) } }