From 1c80ad5f5a7926b7ed1dd0892899e88aa0868a41 Mon Sep 17 00:00:00 2001 From: Eduard Marbach Date: Wed, 30 Oct 2024 11:07:26 +0100 Subject: [PATCH] feat: add tick feature --- README.md | 23 ++++++++++++++++++++++- main.go | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 9dc7ef9..4bf67c8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,23 @@ # prometheus_turbotstat_exporter -An prometheus exporter for turbotstat (for monitoring different C-states and pkg-states) +An prometheus exporter for turbotstat (for monitoring different C-states and pkg-states). + +## Example scrape output + +Part of the output from the scrape: + +```txt +... +turbostat_cpu_states_percent{num_cpu="6",type="c1e"} 0.03 +turbostat_cpu_states_percent{num_cpu="6",type="c3"} 0.05 +turbostat_cpu_states_percent{num_cpu="6",type="c6"} 1.37 +turbostat_cpu_states_percent{num_cpu="6",type="c7s"} 0 +turbostat_cpu_states_percent{num_cpu="6",type="c8"} 1.11 +turbostat_cpu_states_percent{num_cpu="6",type="c9"} 0.13 +turbostat_cpu_states_percent{num_cpu="6",type="poll"} 0 +turbostat_cpu_states_percent{num_cpu="7",type="c1"} 0.01 +turbostat_cpu_states_percent{num_cpu="7",type="c10"} 96.91 +turbostat_cpu_states_percent{num_cpu="7",type="c1e"} 0.03 +turbostat_cpu_states_percent{num_cpu="7",type="c3"} 0.01 +turbostat_cpu_states_percent{num_cpu="7",type="c6"} 1.36 +... +``` diff --git a/main.go b/main.go index 03f1dc3..fcf65b9 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "slices" "strconv" "strings" + "time" "bufio" @@ -400,7 +401,6 @@ func parseOutput(input io.Reader) ([]string, []map[string]interface{}) { } func Update() { - // TODO maybe run the execution in the background and just return the current values? reader := executeProgram(defaultSleepTimer) _, data := parseOutput(&reader) @@ -437,12 +437,14 @@ func Update() { type helloWorldhandler struct{} func (h helloWorldhandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - Update() + if !isBackgroundMode { + Update() + } promhttp.Handler().ServeHTTP(w, r) } var listOfMetrics []metricMapping = nil -var defaultSleepTimer int = 5 +var defaultSleepTimer = 5 var isCommandCat = false var isBackgroundMode = false var backgroundCollectSeconds = 30 @@ -514,6 +516,31 @@ func main() { listOfMetrics = buildMetricList(&reader) + if isBackgroundMode { + log.Debugf("Starting ticker") + ticker := time.NewTicker(time.Duration(backgroundCollectSeconds) * time.Second) + quit := make(chan struct{}) + manualTick := make(chan bool) + + go func() { + for { + select { + case <-ticker.C: + log.Debugf("Ticker update") + Update() + case <-manualTick: + log.Debugf("Manual tick update") + Update() + case <-quit: + log.Debugf("Stop background updater") + ticker.Stop() + return + } + } + }() + manualTick <- true + } + //foo := newFooCollector() http.Handle("/metrics", helloWorldhandler{}) @@ -555,10 +582,6 @@ func test1() { panic(err) } - if err != nil { // Load clients from file - panic(err) - } - for _, client := range maps { fmt.Println("Hello", client) }