-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
handler.go
50 lines (41 loc) · 1.11 KB
/
handler.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
package gather
import (
"bytes"
"context"
"encoding/json"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/nats"
"go.uber.org/zap"
)
// handler implents nats Handler interface.
type handler struct {
Scraper Scraper
Publisher nats.Publisher
log *zap.Logger
}
// Process consumes scraper target from scraper target queue,
// call the scraper to gather, and publish to metrics queue.
func (h *handler) Process(s nats.Subscription, m nats.Message) {
defer m.Ack()
req := new(influxdb.ScraperTarget)
err := json.Unmarshal(m.Data(), req)
if err != nil {
h.log.Error("Unable to unmarshal json", zap.Error(err))
return
}
ms, err := h.Scraper.Gather(context.TODO(), *req)
if err != nil {
h.log.Error("Unable to gather", zap.Error(err))
return
}
// send metrics to recorder queue
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(ms); err != nil {
h.log.Error("Unable to marshal json", zap.Error(err))
return
}
if err := h.Publisher.Publish(MetricsSubject, buf); err != nil {
h.log.Error("Unable to publish scraper metrics", zap.Error(err))
return
}
}