-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathservice.go
More file actions
131 lines (111 loc) · 2.72 KB
/
Copy pathservice.go
File metadata and controls
131 lines (111 loc) · 2.72 KB
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
package expvarmon
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
Vars map[VarName]Var
Err error
Restarted bool
UptimeCounter int64
}
// NewService returns new Service object.
func NewService(url url.URL, varNames []VarName) *Service {
vars := make(map[VarName]Var)
for _, name := range varNames {
v := NewVar(name)
vars[name] = v
}
return &Service{
Name: url.Host, // we have only port on start, so use it as name until resolved
URL: url,
Vars: vars,
}
}
// 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
for name, v := range s.Vars {
value, err := expvar.GetValue(name.ToSlice()...)
if err != nil {
v.SetNA()
continue
}
v.Set(value)
}
}
// guessValue attemtps to bruteforce all supported types.
// TODO: FIXME: remove this func
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 varable of this service.
func (s Service) Value(name VarName) string {
v, ok := s.Vars[name]
if !ok {
return ""
}
return v.String()
}