Skip to content

Commit

Permalink
feat: enable cpu/memory process metrics (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
prichrd authored Jan 10, 2023
1 parent 2e9ef0a commit eb2b34a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/do-agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"hash/fnv"
"net/url"
"os"
"strings"
Expand Down Expand Up @@ -63,6 +64,8 @@ const (
defaultAuthURL = internalProxyURL
defaultSonarURL = ""
defaultWebListenAddress = "127.0.0.1:9100"

processScrapingDropletPct = 10
)

var defaultMetadataURL = fmt.Sprintf("%s/metadata", internalProxyURL)
Expand Down Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions cmd/do-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func main() {
log.Fatal("configuration failure: %+v", err)
}

toggleGradualRollouts()
cols := initCollectors()
reg := prometheus.NewRegistry()
reg.MustRegister(cols...)
Expand Down
7 changes: 7 additions & 0 deletions internal/process/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit eb2b34a

Please sign in to comment.