add(sam): basic memory/goroutine monitor
This commit is contained in:
@@ -6,8 +6,9 @@ import (
|
||||
)
|
||||
|
||||
type configuration struct {
|
||||
httpAddr string
|
||||
dbDSN string
|
||||
httpAddr string
|
||||
dbDSN string
|
||||
monitorInterval int
|
||||
}
|
||||
|
||||
func flags(prefix string, mountFlags ...func()) configuration {
|
||||
@@ -19,6 +20,7 @@ func flags(prefix string, mountFlags ...func()) configuration {
|
||||
|
||||
flag.StringVar(&config.httpAddr, p("http-addr"), ":3000", "Listen address for HTTP server")
|
||||
flag.StringVar(&config.dbDSN, p("db-dsn"), "crust:crust@tcp(db1:3306)/crust?collation=utf8mb4_general_ci", "DSN for database connection")
|
||||
flag.IntVar(&config.monitorInterval, "monitor-interval", 300, "Monitor interval (seconds, 0 = disable)")
|
||||
|
||||
for _, mount := range mountFlags {
|
||||
mount()
|
||||
|
||||
@@ -30,6 +30,7 @@ func main() {
|
||||
|
||||
// log to stdout not stderr
|
||||
log.SetOutput(os.Stdout)
|
||||
NewMonitor(config.monitorInterval)
|
||||
|
||||
// set up database connection
|
||||
factory.Database.Add("default", config.dbDSN)
|
||||
|
||||
48
cmd/sam/monitor.go
Normal file
48
cmd/sam/monitor.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Monitor struct {
|
||||
runtime.MemStats
|
||||
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 interval = time.Duration(duration) * time.Second
|
||||
for {
|
||||
<-time.After(interval)
|
||||
runtime.ReadMemStats(&m.MemStats)
|
||||
m.NumGoroutine = runtime.NumGoroutine()
|
||||
b, _ := json.Marshal(m)
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user