-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
132 lines (112 loc) · 2.74 KB
/
task.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
package tasks
import (
"time"
"github.com/go-ping/ping"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
)
type Task struct {
address string
pinger *ping.Pinger
stopped bool
// metrics
recvPackets prometheus.Counter
sendPackets prometheus.Counter
rttDuration prometheus.Summary
pingError prometheus.Gauge
}
func (task *Task) Describe(descs chan<- *prometheus.Desc) {
descs <- task.recvPackets.Desc()
descs <- task.sendPackets.Desc()
descs <- task.rttDuration.Desc()
descs <- task.pingError.Desc()
}
func (task *Task) Collect(metrics chan<- prometheus.Metric) {
task.sendPackets.Collect(metrics)
task.recvPackets.Collect(metrics)
task.rttDuration.Collect(metrics)
task.pingError.Collect(metrics)
}
func newTask(addr string, lbs map[string]string) (*Task, error) {
pinger, err := ping.NewPinger(addr)
if err != nil {
return nil, err
}
pinger.SetPrivileged(true)
// extends timeout to 10 years, it's not forever but it should be ok
pinger.Timeout = time.Hour * 24 * 365 * 10
constLabels := make(map[string]string, len(lbs)+1)
for k, v := range lbs {
constLabels[k] = v
}
constLabels["target"] = addr
recvPackets := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "gossiping",
Subsystem: "ping",
Name: "recv_packet_total",
Help: "The Number of received packets",
ConstLabels: constLabels,
})
sendPackets := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "gossiping",
Subsystem: "ping",
Name: "send_packet_total",
Help: "The number of send packets",
ConstLabels: constLabels,
})
rttDuration := prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: "gossiping",
Subsystem: "ping",
Name: "rtt_seconds",
ConstLabels: constLabels,
})
pingError := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "gossiping",
Subsystem: "ping",
Name: "error",
ConstLabels: constLabels,
})
pinger.OnSend = func(pkt *ping.Packet) {
pingError.Set(0)
sendPackets.Inc()
}
pinger.OnRecv = func(pkt *ping.Packet) {
recvPackets.Inc()
rttDuration.Observe(pkt.Rtt.Seconds())
}
return &Task{
address: addr,
pinger: pinger,
recvPackets: recvPackets,
sendPackets: sendPackets,
rttDuration: rttDuration,
pingError: pingError,
}, err
}
func (task *Task) Start(logger *zap.Logger) {
defer func() {
err := recover()
if err != nil {
logger.Error("task panicked",
zap.Stack("task"))
}
}()
task.pingError.Set(1)
for {
err := task.pinger.Run()
if task.stopped {
return
}
task.pingError.Set(1)
if err != nil {
logger.Warn("ping error",
zap.Error(err))
time.Sleep(5 * time.Second)
continue
}
}
}
func (task *Task) Stop() {
task.stopped = true
task.pinger.Stop()
}