3
0

upd(sam): update monitor, add new fields, restructure

This commit is contained in:
Tit Petric
2018-08-03 19:16:12 +02:00
parent 0dd0b44d96
commit dd2f377a48
2 changed files with 32 additions and 26 deletions

View File

@@ -30,7 +30,7 @@ func main() {
// log to stdout not stderr
log.SetOutput(os.Stdout)
NewMonitor(config.monitorInterval)
go NewMonitor(config.monitorInterval)
// set up database connection
factory.Database.Add("default", config.dbDSN)

View File

@@ -8,40 +8,46 @@ import (
)
type Monitor struct {
runtime.MemStats
Alloc,
TotalAlloc,
Sys,
Mallocs,
Frees,
LiveObjects,
PauseTotalNs uint64
NumGC uint32
NumGoroutine int
}
func (m Monitor) MarshalJSON() ([]byte, error) {
result := struct {
Alloc,
TotalAlloc,
Sys,
Mallocs,
Frees uint64
NumGC uint32
NumGoroutine int
}{
m.MemStats.Alloc,
m.MemStats.TotalAlloc,
m.MemStats.Sys,
m.MemStats.Mallocs,
m.MemStats.Frees,
m.MemStats.NumGC,
m.NumGoroutine,
}
return json.Marshal(result)
}
func NewMonitor(duration int) {
var m Monitor
var rtm runtime.MemStats
var interval = time.Duration(duration) * time.Second
for {
<-time.After(interval)
runtime.ReadMemStats(&m.MemStats)
// Read full mem stats
runtime.ReadMemStats(&rtm)
// Number of goroutines
m.NumGoroutine = runtime.NumGoroutine()
// Misc memory stats
m.Alloc = rtm.Alloc
m.TotalAlloc = rtm.TotalAlloc
m.Sys = rtm.Sys
m.Mallocs = rtm.Mallocs
m.Frees = rtm.Frees
// Live objects = Mallocs - Frees
m.LiveObjects = m.Mallocs - m.Frees
// GC Stats
m.PauseTotalNs = rtm.PauseTotalNs
m.NumGC = rtm.NumGC
// Just encode to json and print
b, _ := json.Marshal(m)
fmt.Println(string(b))
}