forked from schweikert/fping-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target.go
130 lines (109 loc) · 2.41 KB
/
target.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
package main
import (
"fmt"
"log"
"math"
"sync"
"github.com/prometheus/client_golang/prometheus"
)
// Target is the internal state about a target
type Target struct {
sync.Mutex
spec TargetSpec
registry *prometheus.Registry
measurements *Measurements
}
func NewTarget(ts TargetSpec) *Target {
t := Target{
spec: ts,
registry: prometheus.NewRegistry()}
log.Println("new target:", ts.host)
//prometheus.WrapRegistererWith(prometheus.Labels{"zone": "aaa"}, t.registry).MustRegister(t)
t.registry.MustRegister(&t)
return &t
}
var (
fpingSentCountDesc = prometheus.NewDesc(
"fping_sent_count",
"Number of sent probes",
nil, nil,
)
fpingLostCountDesc = prometheus.NewDesc(
"fping_lost_count",
"Number of lost probes",
nil, nil,
)
fpingRTTSumDesc = prometheus.NewDesc(
"fping_rtt_sum",
"Sum of measured latencies",
nil, nil,
)
fpingRTTCountDesc = prometheus.NewDesc(
"fping_rtt_count",
"Number of measured latencies (successful probes)",
nil, nil,
)
fpingRTTDesc = prometheus.NewDesc(
"fping_rtt",
"Summary of measured latencies",
[]string{"quantile"}, nil,
)
)
func (t *Target) AddMeasurements(m Measurements) {
t.Lock()
t.measurements = &m
t.Unlock()
}
func (t *Target) Collect(ch chan<- prometheus.Metric) {
t.Lock()
defer t.Unlock()
if t.measurements == nil {
return
}
// fping_sent_count
ch <- prometheus.MustNewConstMetric(
fpingSentCountDesc,
prometheus.GaugeValue,
float64(t.measurements.GetSentCount()),
)
// fping_lost_count
ch <- prometheus.MustNewConstMetric(
fpingLostCountDesc,
prometheus.GaugeValue,
float64(t.measurements.GetLostCount()),
)
// sum
ch <- prometheus.MustNewConstMetric(
fpingRTTSumDesc,
prometheus.GaugeValue,
t.measurements.GetRTTSum(),
)
// count
ch <- prometheus.MustNewConstMetric(
fpingRTTCountDesc,
prometheus.GaugeValue,
float64(t.measurements.GetRTTCount()),
)
count := float64(len(t.measurements.rtt))
for i := range t.measurements.rtt {
quantile := fmt.Sprintf("%.3f", (float64(i)+1.0)/count)
if t.measurements.lost[i] {
ch <- prometheus.MustNewConstMetric(
fpingRTTDesc,
prometheus.GaugeValue,
math.NaN(),
quantile,
)
} else {
ch <- prometheus.MustNewConstMetric(
fpingRTTDesc,
prometheus.GaugeValue,
t.measurements.rtt[i],
quantile,
)
}
}
}
func (t *Target) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(t, ch)
}