-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: addition of some golang metrics, cpu, memory, number of gc …
…and time in gc
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}() | ||
} |