-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathstatus.go
61 lines (54 loc) · 1.67 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"encoding/json"
"fmt"
"os"
"runtime"
"time"
)
// Status represents the status of the running piladb
// instance.
type Status struct {
Code string `json:"status"`
Version string `json:"version"`
GoVersion string `json:"go_version"`
Host string `json:"host"`
PID int `json:"pid"`
StartedAt time.Time `json:"started_at"`
RunningFor float64 `json:"running_for"`
NumberGoroutines int `json:"number_goroutines"`
MemoryAlloc string `json:"memory_alloc"`
}
// NewStatus returns a new piladb status.
func NewStatus(version string, now time.Time, mem *runtime.MemStats) *Status {
status := &Status{}
status.Code = "OK"
status.Version = version
status.GoVersion = runtime.Version()
status.Host = fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH)
status.PID = os.Getpid()
status.StartedAt = now
status.NumberGoroutines = runtime.NumGoroutine()
if mem != nil {
status.MemoryAlloc = MemOutput(mem.Alloc)
}
return status
}
// Update updates the Status given a current time and memory
// stats.
func (s *Status) Update(now time.Time, mem *runtime.MemStats) {
s.RunningFor = now.Sub(s.StartedAt).Seconds()
s.NumberGoroutines = runtime.NumGoroutine()
s.MemoryAlloc = MemOutput(mem.Alloc)
}
// ToJSON returns the Status into a JSON file in []byte
// format.
func (s *Status) ToJSON() []byte {
// Store date in UTC, show it in Local.
s.StartedAt = s.StartedAt.Local()
// Do not check error as the Status type does
// not contain types that could cause such case.
// See http://golang.org/src/encoding/json/encode.go?s=5438:5481#L125
b, _ := json.Marshal(s)
return b
}