Skip to content

Commit 82a4e8c

Browse files
committed
refactor: addition of some golang metrics, cpu, memory, number of gc and time in gc
1 parent 308a881 commit 82a4e8c

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Diff for: cmd/kar-controllers/app/metrics.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package app
2+
3+
import (
4+
"runtime"
5+
"time"
6+
7+
"github.com/prometheus/client_golang/prometheus"
8+
"github.com/prometheus/client_golang/prometheus/promauto"
9+
)
10+
11+
var (
12+
goroutines = promauto.NewGauge(prometheus.GaugeOpts{
13+
Name: "mcad_goroutines_total",
14+
Help: "Current number of goroutines",
15+
})
16+
memory = promauto.NewGauge(prometheus.GaugeOpts{
17+
Name: "mcad_memory_usage_bytes",
18+
Help: "Current memory usage",
19+
})
20+
gc = promauto.NewCounter(prometheus.CounterOpts{
21+
Name: "mcad_gc_operations_total",
22+
Help: "Total number of GC operations",
23+
})
24+
gcTime = promauto.NewCounter(prometheus.CounterOpts{
25+
Name: "mcad_gc_time_seconds",
26+
Help: "Total time spent in GC",
27+
})
28+
)
29+
30+
func RecordMetrics() {
31+
go func() {
32+
for {
33+
m := &runtime.MemStats{}
34+
runtime.ReadMemStats(m)
35+
36+
// runtime.NumGoroutine() returns the number of goroutines that currently exist
37+
goroutines.Set(float64(runtime.NumGoroutine()))
38+
// m.Sys is the total bytes of memory obtained from the OS
39+
memory.Set(float64(m.Sys))
40+
// m.NumGC is the number of completed GarbageCollection cycles since the program started
41+
gc.Add(float64(m.NumGC))
42+
// m.PauseTotalNs is the total GarbageCollection pause time since the program started
43+
gcTime.Add(float64(m.PauseTotalNs) / float64(time.Second))
44+
45+
time.Sleep(2 * time.Second)
46+
}
47+
}()
48+
}

0 commit comments

Comments
 (0)