-
Notifications
You must be signed in to change notification settings - Fork 101
/
service.go
169 lines (142 loc) · 3.39 KB
/
service.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"net/url"
"strings"
"sync"
"github.com/antonholmquist/jason"
)
var (
// uptimeCounter is a variable used for tracking uptime status.
// It should be always incrementing and included into default expvar vars.
// Could be replaced with something different or made configurable in
// the future.
uptimeCounter = VarName("memstats.PauseTotalNs").ToSlice()
)
// Service represents constantly updating info about single service.
type Service struct {
URL url.URL
Name string
Cmdline string
stacks map[VarName]*Stack
Err error
Restarted bool
UptimeCounter int64
}
// NewService returns new Service object.
func NewService(url url.URL, vars []VarName) *Service {
values := make(map[VarName]*Stack)
for _, name := range vars {
values[VarName(name)] = NewStack()
}
return &Service{
Name: url.Host, // we have only port on start, so use it as name until resolved
URL: url,
stacks: values,
}
}
// Update updates Service info from Expvar variable.
func (s *Service) Update(wg *sync.WaitGroup) {
defer wg.Done()
expvar, err := FetchExpvar(s.URL)
// check for restart
if s.Err != nil && err == nil {
s.Restarted = true
}
s.Err = err
// if memstat.PauseTotalNs less than s.UptimeCounter
// then service was restarted
c, err := expvar.GetInt64(uptimeCounter...)
if err != nil {
s.Err = err
} else {
if s.UptimeCounter > c {
s.Restarted = true
}
s.UptimeCounter = c
}
// Update Cmdline & Name only once
if len(s.Cmdline) == 0 {
cmdline, err := expvar.GetStringArray("cmdline")
if err != nil {
s.Err = err
} else {
s.Cmdline = strings.Join(cmdline, " ")
s.Name = BaseCommand(cmdline)
}
}
// For all vars, fetch desired value from Json and push to it's own stack.
for name, stack := range s.stacks {
value, err := expvar.GetValue(name.ToSlice()...)
if err != nil {
stack.Push(nil)
continue
}
v := guessValue(value)
if v != nil {
stack.Push(v)
}
}
}
// guessValue attemtps to bruteforce all supported types.
func guessValue(value *jason.Value) interface{} {
if v, err := value.Int64(); err == nil {
return v
} else if v, err := value.Float64(); err == nil {
return v
} else if v, err := value.Boolean(); err == nil {
return v
} else if v, err := value.String(); err == nil {
return v
} else if v, err := value.Array(); err == nil {
// if we get an array, calculate average
// empty array, treat as zero
if len(v) == 0 {
return 0
}
avg := averageJason(v)
// cast to int64 for Int64 values
if _, err := v[0].Int64(); err == nil {
return int64(avg)
}
return avg
}
return nil
}
// Value returns current value for the given var of this service.
//
// It also formats value, if kind is specified.
func (s Service) Value(name VarName) string {
if s.Err != nil {
return "N/A"
}
val, ok := s.stacks[name]
if !ok {
return "N/A"
}
v := val.Front()
if v == nil {
return "N/A"
}
return Format(v, name.Kind())
}
// Values returns slice of ints with recent
// values of the given var, to be used with sparkline.
func (s Service) Values(name VarName) []int {
stack, ok := s.stacks[name]
if !ok {
return nil
}
return stack.IntValues()
}
// Max returns maximum recorded value for given service and var.
func (s Service) Max(name VarName) interface{} {
val, ok := s.stacks[name]
if !ok {
return nil
}
v := val.Max
if v == nil {
return nil
}
return Format(v, name.Kind())
}