@@ -12,48 +12,66 @@ import (
12
12
"github.com/czerwonk/ping_exporter/config"
13
13
)
14
14
15
- var (
16
- labelNames []string
15
+ type pingCollector struct {
16
+ monitor * mon.Monitor
17
+ enableDeprecatedMetrics bool
18
+ rttUnit rttUnit
19
+
20
+ cfg * config.Config
21
+
22
+ mutex sync.RWMutex
23
+
24
+ customLabels * customLabelSet
25
+ metrics map [string ]* mon.Metrics
26
+
17
27
rttDesc scaledMetrics
18
28
bestDesc scaledMetrics
19
29
worstDesc scaledMetrics
20
30
meanDesc scaledMetrics
21
31
stddevDesc scaledMetrics
22
32
lossDesc * prometheus.Desc
23
33
progDesc * prometheus.Desc
24
- mutex * sync.Mutex
25
- )
34
+ }
26
35
27
- type pingCollector struct {
28
- cfg * config.Config
29
- customLabels * customLabelSet
30
- monitor * mon.Monitor
31
- metrics map [string ]* mon.Metrics
36
+ func NewPingCollector (enableDeprecatedMetrics bool , unit rttUnit , monitor * mon.Monitor , cfg * config.Config ) * pingCollector {
37
+ ret := & pingCollector {
38
+ monitor : monitor ,
39
+ enableDeprecatedMetrics : enableDeprecatedMetrics ,
40
+ rttUnit : unit ,
41
+ cfg : cfg ,
42
+ }
43
+ ret .customLabels = newCustomLabelSet (cfg .Targets )
44
+ ret .createDesc ()
45
+ return ret
32
46
}
33
47
34
- func (p * pingCollector ) Describe (ch chan <- * prometheus.Desc ) {
35
- p .createDesc ()
48
+ func (p * pingCollector ) UpdateConfig (cfg * config.Config ) {
49
+ p .mutex .Lock ()
50
+ defer p .mutex .Unlock ()
51
+ p .cfg .Targets = cfg .Targets
52
+ }
36
53
37
- if enableDeprecatedMetrics {
38
- rttDesc .Describe (ch )
54
+ func (p * pingCollector ) Describe (ch chan <- * prometheus.Desc ) {
55
+ if p .enableDeprecatedMetrics {
56
+ p .rttDesc .Describe (ch )
39
57
}
40
- bestDesc .Describe (ch )
41
- worstDesc .Describe (ch )
42
- meanDesc .Describe (ch )
43
- stddevDesc .Describe (ch )
44
- ch <- lossDesc
45
- ch <- progDesc
58
+ p . bestDesc .Describe (ch )
59
+ p . worstDesc .Describe (ch )
60
+ p . meanDesc .Describe (ch )
61
+ p . stddevDesc .Describe (ch )
62
+ ch <- p . lossDesc
63
+ ch <- p . progDesc
46
64
}
47
65
48
66
func (p * pingCollector ) Collect (ch chan <- prometheus.Metric ) {
49
- mutex .Lock ()
50
- defer mutex .Unlock ()
67
+ p . mutex .Lock ()
68
+ defer p . mutex .Unlock ()
51
69
52
70
if m := p .monitor .Export (); len (m ) > 0 {
53
71
p .metrics = m
54
72
}
55
73
56
- ch <- prometheus .MustNewConstMetric (progDesc , prometheus .GaugeValue , 1 )
74
+ ch <- prometheus .MustNewConstMetric (p . progDesc , prometheus .GaugeValue , 1 )
57
75
58
76
for target , metrics := range p .metrics {
59
77
l := strings .SplitN (target , " " , 3 )
@@ -63,35 +81,34 @@ func (p *pingCollector) Collect(ch chan<- prometheus.Metric) {
63
81
64
82
if metrics .PacketsSent > metrics .PacketsLost {
65
83
if enableDeprecatedMetrics {
66
- rttDesc .Collect (ch , metrics .Best , append (l , "best" )... )
67
- rttDesc .Collect (ch , metrics .Worst , append (l , "worst" )... )
68
- rttDesc .Collect (ch , metrics .Mean , append (l , "mean" )... )
69
- rttDesc .Collect (ch , metrics .StdDev , append (l , "std_dev" )... )
84
+ p . rttDesc .Collect (ch , metrics .Best , append (l , "best" )... )
85
+ p . rttDesc .Collect (ch , metrics .Worst , append (l , "worst" )... )
86
+ p . rttDesc .Collect (ch , metrics .Mean , append (l , "mean" )... )
87
+ p . rttDesc .Collect (ch , metrics .StdDev , append (l , "std_dev" )... )
70
88
}
71
89
72
- bestDesc .Collect (ch , metrics .Best , l ... )
73
- worstDesc .Collect (ch , metrics .Worst , l ... )
74
- meanDesc .Collect (ch , metrics .Mean , l ... )
75
- stddevDesc .Collect (ch , metrics .StdDev , l ... )
90
+ p . bestDesc .Collect (ch , metrics .Best , l ... )
91
+ p . worstDesc .Collect (ch , metrics .Worst , l ... )
92
+ p . meanDesc .Collect (ch , metrics .Mean , l ... )
93
+ p . stddevDesc .Collect (ch , metrics .StdDev , l ... )
76
94
}
77
95
78
96
loss := float64 (metrics .PacketsLost ) / float64 (metrics .PacketsSent )
79
- ch <- prometheus .MustNewConstMetric (lossDesc , prometheus .GaugeValue , loss , l ... )
97
+ ch <- prometheus .MustNewConstMetric (p . lossDesc , prometheus .GaugeValue , loss , l ... )
80
98
}
81
99
}
82
100
83
101
func (p * pingCollector ) createDesc () {
84
- labelNames = []string {"target" , "ip" , "ip_version" }
102
+ labelNames : = []string {"target" , "ip" , "ip_version" }
85
103
labelNames = append (labelNames , p .customLabels .labelNames ()... )
86
104
87
- rttDesc = newScaledDesc ("rtt" , "Round trip time" , append (labelNames , "type" ))
88
- bestDesc = newScaledDesc ("rtt_best" , "Best round trip time" , labelNames )
89
- worstDesc = newScaledDesc ("rtt_worst" , "Worst round trip time" , labelNames )
90
- meanDesc = newScaledDesc ("rtt_mean" , "Mean round trip time" , labelNames )
91
- stddevDesc = newScaledDesc ("rtt_std_deviation" , "Standard deviation" , labelNames )
92
- lossDesc = newDesc ("loss_ratio" , "Packet loss from 0.0 to 1.0" , labelNames , nil )
93
- progDesc = newDesc ("up" , "ping_exporter version" , nil , prometheus.Labels {"version" : version })
94
- mutex = & sync.Mutex {}
105
+ p .rttDesc = newScaledDesc ("rtt" , "Round trip time" , p .rttUnit , append (labelNames , "type" ))
106
+ p .bestDesc = newScaledDesc ("rtt_best" , "Best round trip time" , p .rttUnit , labelNames )
107
+ p .worstDesc = newScaledDesc ("rtt_worst" , "Worst round trip time" , p .rttUnit , labelNames )
108
+ p .meanDesc = newScaledDesc ("rtt_mean" , "Mean round trip time" , p .rttUnit , labelNames )
109
+ p .stddevDesc = newScaledDesc ("rtt_std_deviation" , "Standard deviation" , p .rttUnit , labelNames )
110
+ p .lossDesc = newDesc ("loss_ratio" , "Packet loss from 0.0 to 1.0" , labelNames , nil )
111
+ p .progDesc = newDesc ("up" , "ping_exporter version" , nil , prometheus.Labels {"version" : version })
95
112
}
96
113
97
114
func newDesc (name , help string , variableLabels []string , constLabels prometheus.Labels ) * prometheus.Desc {
0 commit comments