-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.go
321 lines (259 loc) · 8.14 KB
/
collector.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"runtime"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"
)
const metricNamePrefix = "ai_on_the_edge_device_"
var errRequestFailed = errors.New("request failed")
var errEmptyResponse = errors.New("empty response")
var descriptors = struct {
error *prometheus.Desc
firmwareInfo *prometheus.Desc
netInfo *prometheus.Desc
cpuTemp *prometheus.Desc
rssi *prometheus.Desc
memHeapFree *prometheus.Desc
flowValue *prometheus.Desc
flowSuccess *prometheus.Desc
flowErrorInfo *prometheus.Desc
flowTimestamp *prometheus.Desc
}{
error: prometheus.NewDesc(metricNamePrefix+"error",
"Metrics collection failed.",
nil, nil),
firmwareInfo: prometheus.NewDesc(metricNamePrefix+"firmware_info",
"Firmware metadata.",
[]string{"version", "gittag", "gitrevision"}, nil),
netInfo: prometheus.NewDesc(metricNamePrefix+"network_info",
"Network metadata.",
[]string{"hostname", "ipv4"}, nil),
rssi: prometheus.NewDesc(metricNamePrefix+"rssi_dbm",
"WiFi signal strength in dBm.",
nil, nil),
cpuTemp: prometheus.NewDesc(metricNamePrefix+"cpu_temperature_celsius",
"CPU temperature in degrees celsius.",
nil, nil),
memHeapFree: prometheus.NewDesc(metricNamePrefix+"memory_heap_free_bytes",
"Bytes available on the heap.",
nil, nil),
flowValue: prometheus.NewDesc(metricNamePrefix+"flow_value",
"Most recent value.",
[]string{"name"}, nil),
flowSuccess: prometheus.NewDesc(metricNamePrefix+"flow_success",
"Whether digitization was successful.",
[]string{"name"}, nil),
flowErrorInfo: prometheus.NewDesc(metricNamePrefix+"flow_error_info",
"Error encountered during digitization.",
[]string{"name", "message"}, nil),
flowTimestamp: prometheus.NewDesc(metricNamePrefix+"flow_timestamp_seconds",
"Timestamp of the most recent digitization.",
[]string{"name"}, nil),
}
var client = func() *http.Client {
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 10 * time.Second,
}).DialContext,
DisableCompression: true,
ExpectContinueTimeout: 1 * time.Second,
IdleConnTimeout: 90 * time.Second,
MaxIdleConns: 10,
MaxIdleConnsPerHost: 1,
Proxy: http.ProxyFromEnvironment,
TLSHandshakeTimeout: 10 * time.Second,
// The HTTP server is single-threaded.
MaxConnsPerHost: 1,
}
return &http.Client{
Transport: transport,
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
}
}()
func collectJsonNumber(ch chan<- prometheus.Metric, name string,
desc *prometheus.Desc, valueType prometheus.ValueType, raw json.RawMessage,
labelValues ...string,
) error {
if len(raw) == 0 {
// Message is unset
return nil
}
var s string
if err := json.Unmarshal(raw, &s); err != nil {
return fmt.Errorf("%s: %w", name, err)
}
if s == "" {
// Number is missing
return nil
}
value, err := strconv.ParseFloat(s, 64)
if err != nil {
return fmt.Errorf("%s: %w", name, err)
}
ch <- prometheus.MustNewConstMetric(desc, valueType, value, labelValues...)
return nil
}
type collector struct {
ctx context.Context
target *url.URL
}
func newCollector(ctx context.Context, target *url.URL) *collector {
return &collector{ctx, target}
}
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
ch <- descriptors.firmwareInfo
ch <- descriptors.netInfo
ch <- descriptors.rssi
ch <- descriptors.cpuTemp
ch <- descriptors.memHeapFree
ch <- descriptors.flowValue
ch <- descriptors.flowSuccess
ch <- descriptors.flowErrorInfo
ch <- descriptors.flowTimestamp
}
func (c *collector) newRequest(ctx context.Context, path string) (*http.Request, error) {
u := c.target.JoinPath(path)
return http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
}
func (c *collector) doRequest(ctx context.Context, path string) (*http.Response, error) {
if req, err := c.newRequest(ctx, path); err != nil {
return nil, err
} else if resp, err := client.Do(req); err != nil {
return nil, err
} else if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%w: %q: %v", errRequestFailed, req.URL.String(), resp.Status)
} else {
return resp, nil
}
}
func (c *collector) doJsonRequest(ctx context.Context, path string, v any) error {
resp, err := c.doRequest(ctx, path)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.ContentLength == 0 {
return fmt.Errorf("%q: %w", resp.Request.URL.String(), errEmptyResponse)
}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(v); err != nil {
return fmt.Errorf("JSON decoder: %w", err)
}
return nil
}
type sysinfoData struct {
Firmware string `json:"firmware"`
GitTag string `json:"gittag"`
GitRevision string `json:"gitrevision"`
CpuTemp json.RawMessage `json:"cputemp"`
Hostname string `json:"hostname"`
IPv4 string `json:"ipv4"`
FreeHeapMem json.RawMessage `json:"freeHeapMem"`
}
func (c *collector) collectSysinfo(ctx context.Context, ch chan<- prometheus.Metric) error {
var payload []sysinfoData
if err := c.doJsonRequest(ctx, "/sysinfo", &payload); err != nil {
return err
}
if len(payload) < 1 {
return errors.New("sysinfo missing from response")
}
data := payload[0]
ch <- prometheus.MustNewConstMetric(descriptors.firmwareInfo, prometheus.GaugeValue, 1,
data.Firmware, data.GitTag, data.GitRevision)
ch <- prometheus.MustNewConstMetric(descriptors.netInfo, prometheus.GaugeValue, 1,
data.Hostname, data.IPv4)
if err := collectJsonNumber(ch, "cputemp", descriptors.cpuTemp, prometheus.GaugeValue, data.CpuTemp); err != nil {
return err
}
if err := collectJsonNumber(ch, "freeHeapMem", descriptors.memHeapFree, prometheus.GaugeValue, data.FreeHeapMem); err != nil {
return err
}
return nil
}
func (c *collector) collectRssi(ctx context.Context, ch chan<- prometheus.Metric) error {
resp, err := c.doRequest(ctx, "/rssi")
if err != nil {
return err
}
defer resp.Body.Close()
var value float64
if n, err := fmt.Fscanf(resp.Body, "%f\n", &value); err != nil {
return fmt.Errorf("RSSI: %w", err)
} else if n < 1 {
return errors.New("RSSI value missing")
} else {
ch <- prometheus.MustNewConstMetric(descriptors.rssi, prometheus.GaugeValue, value)
}
return nil
}
type flowData struct {
Value json.RawMessage `json:"value"`
Error string `json:"error"`
Timestamp string `json:"timestamp"`
}
func (d flowData) collect(ch chan<- prometheus.Metric, name string) error {
if err := collectJsonNumber(ch, "value", descriptors.flowValue, prometheus.CounterValue, d.Value, name); err != nil {
return err
}
var successValue float64
errMsg := strings.TrimSpace(d.Error)
switch errMsg {
case "", "no error":
successValue = 1
errMsg = ""
}
ch <- prometheus.MustNewConstMetric(descriptors.flowSuccess, prometheus.GaugeValue, successValue, name)
ch <- prometheus.MustNewConstMetric(descriptors.flowErrorInfo, prometheus.GaugeValue, 1, name, errMsg)
if d.Timestamp != "" {
ts, err := time.Parse("2006-01-02T15:04:05-0700", d.Timestamp)
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(descriptors.flowTimestamp, prometheus.GaugeValue, float64(ts.Unix()), name)
}
return nil
}
func (c *collector) collectFlows(ctx context.Context, ch chan<- prometheus.Metric) error {
var payload map[string]flowData
if err := c.doJsonRequest(ctx, "/json", &payload); err != nil {
return err
}
for name, data := range payload {
if err := data.collect(ch, name); err != nil {
return fmt.Errorf("flow %q: %w", name, err)
}
}
return nil
}
func (c *collector) collect(ch chan<- prometheus.Metric) error {
g, ctx := errgroup.WithContext(c.ctx)
g.SetLimit(runtime.GOMAXPROCS(0))
for _, fn := range []func(context.Context, chan<- prometheus.Metric) error{
c.collectSysinfo,
c.collectRssi,
c.collectFlows,
} {
fn := fn
g.Go(func() error { return fn(ctx, ch) })
}
return g.Wait()
}
func (c *collector) Collect(ch chan<- prometheus.Metric) {
if err := c.collect(ch); err != nil {
ch <- prometheus.NewInvalidMetric(descriptors.error, err)
}
}