-
Notifications
You must be signed in to change notification settings - Fork 3
/
crawler.go
227 lines (189 loc) · 4.36 KB
/
crawler.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"encoding/json"
"fmt"
"reflect"
"time"
)
type LinePoint struct {
Time int64 `json:"time"`
Y int64 `json:"y"`
}
type LineChartUpdate struct {
ID string `json:"i"`
Points []LinePoint `json:"p"`
}
type GaugeUpdate struct {
ID string `json:"i"`
Value float64 `json:"v"`
}
type TextUpdate struct {
ID string `json:"i"`
Value string `json:"v"`
}
type WidgetsUpdates struct {
Gauges []*GaugeUpdate `json:"g"`
LineCharts []*LineChartUpdate `json:"lc"`
Texts []*TextUpdate `json:"t"`
}
type Crawler struct {
interval time.Duration
fetcher Fetcher
hub *Hub
services []*Service
widgets *Widgets
done chan struct{}
}
type result struct {
service string
vars *Expvars
}
func (c *Crawler) Start() {
for {
select {
case <-time.After(c.interval):
updates := c.ExtractUpdates(c.fetchAll())
data, err := json.Marshal(updates)
if err != nil {
fmt.Println("Error serializing response:", err)
continue
}
c.hub.dataCh <- data
case <-c.done:
return
}
}
}
func (c *Crawler) Stop() {
c.done <- struct{}{}
}
func (c *Crawler) fetchAll() map[string]*Expvars {
vars := map[string]*Expvars{}
resCh := make(chan result, len(c.services))
for _, service := range c.services {
service := service
go func() {
vars, err := c.fetcher.Fetch(service.URL)
if err != nil {
fmt.Printf("Failed to crawl '%s': %s\n", service.Name, err)
}
resCh <- result{service: service.Name, vars: vars}
}()
}
timeout := time.After(time.Second)
for i := 0; i < len(c.services); i++ {
select {
case <-timeout:
fmt.Println("Timed out waiting for all crawling results")
return vars
case r := <-resCh:
if r.vars != nil {
vars[r.service] = r.vars
}
}
}
return vars
}
func (c *Crawler) ExtractUpdates(vars map[string]*Expvars) *WidgetsUpdates {
u := &WidgetsUpdates{
Gauges: []*GaugeUpdate{},
LineCharts: []*LineChartUpdate{},
Texts: []*TextUpdate{},
}
now := Now().Unix()
for _, g := range c.widgets.Gauges {
u.Gauges = append(u.Gauges, &GaugeUpdate{
ID: g.ID(),
Value: GaugeValue(g.Metric, g.MaxValue, vars[g.Service]),
})
}
for _, ch := range c.widgets.LineCharts {
lu := &LineChartUpdate{
ID: ch.ID(),
Points: []LinePoint{},
}
if len(ch.Services) > 0 {
for _, s := range ch.Services {
lu.Points = append(lu.Points, LinePoint{
Time: now,
Y: LineChartValue(ch.Metric, vars[s]),
})
}
} else {
for _, s := range c.services {
lu.Points = append(lu.Points, LinePoint{
Time: now,
Y: LineChartValue(ch.Metric, vars[s.Name]),
})
}
}
u.LineCharts = append(u.LineCharts, lu)
}
for _, t := range c.widgets.Texts {
u.Texts = append(u.Texts, &TextUpdate{
ID: t.ID(),
Value: TextValue(t.Metric, vars[t.Service]),
})
}
return u
}
func GaugeValue(m *Metric, max int64, vars *Expvars) float64 {
if vars == nil {
return 0.0
}
v := ReadMetric(m, vars)
if value, ok := v.(int64); ok {
return float64(value) / float64(max)
}
if value, ok := v.(float64); ok {
return value / float64(max)
}
fmt.Printf("%s: usage of %s with gauge is not supported\n", m, reflect.TypeOf(v))
return 0.0
}
func LineChartValue(m *Metric, vars *Expvars) int64 {
if vars == nil {
return 0
}
v := ReadMetric(m, vars)
if value, ok := v.(int64); ok {
return value
}
fmt.Printf("%s: usage of %s with line-chart is not supported\n", m, reflect.TypeOf(v))
return 0
}
func TextValue(m *Metric, vars *Expvars) string {
if vars == nil {
return "N/A"
}
v := ReadMetric(m, vars)
if value, ok := v.(int64); ok {
return fmt.Sprintf("%d", value)
} else if value, ok := v.(float64); ok {
return fmt.Sprintf("%.2f", value)
} else if value, ok := v.(bool); ok {
return fmt.Sprintf("%t", value)
} else if value, ok := v.(string); ok {
return value
}
fmt.Printf("%s: usage of %s with gauge is not supported\n", m, reflect.TypeOf(v))
return "N/A"
}
func ReadMetric(m *Metric, vars *Expvars) interface{} {
value, err := vars.GetValue(m.Path...)
if err != nil {
return nil
}
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 {
return v
}
return nil
}