Skip to content

Commit

Permalink
refactor: addition of some golang metrics, cpu, memory, number of gc …
Browse files Browse the repository at this point in the history
…and time in gc
  • Loading branch information
dimakis committed Jul 18, 2023
1 parent e8a9df1 commit 516027b
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions cmd/kar-controllers/app/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package app

import (
"runtime"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
goroutines = promauto.NewGauge(prometheus.GaugeOpts{
Name: "mcad_goroutines_total",
Help: "Current number of goroutines",
})
memory = promauto.NewGauge(prometheus.GaugeOpts{
Name: "mcad_memory_usage_bytes",
Help: "Current memory usage",
})
gc = promauto.NewCounter(prometheus.CounterOpts{
Name: "mcad_gc_operations_total",
Help: "Total number of GC operations",
})
gcTime = promauto.NewCounter(prometheus.CounterOpts{
Name: "mcad_gc_time_seconds",
Help: "Total time spent in GC",
})
)

func RecordMetrics() {
go func() {
for {
m := &runtime.MemStats{}
runtime.ReadMemStats(m)

// runtime.NumGoroutine() returns the number of goroutines that currently exist
goroutines.Set(float64(runtime.NumGoroutine()))
// m.Sys is the total bytes of memory obtained from the OS
memory.Set(float64(m.Sys))
// m.NumGC is the number of completed GarbageCollection cycles since the program started
gc.Add(float64(m.NumGC))
// m.PauseTotalNs is the total GarbageCollection pause time since the program started
gcTime.Add(float64(m.PauseTotalNs) / float64(time.Second))

time.Sleep(2 * time.Second)
}
}()
}

0 comments on commit 516027b

Please sign in to comment.