@@ -3,33 +3,65 @@ package main
3
3
import (
4
4
"github.com/prometheus/client_golang/prometheus"
5
5
"github.com/utilitywarehouse/semaphore-wireguard/log"
6
+ "golang.zx2c4.com/wireguard/wgctrl"
6
7
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
7
8
)
8
9
9
10
var (
10
11
syncPeersAttempt = prometheus .NewCounterVec (
11
12
prometheus.CounterOpts {
12
- Name : "semaphore_wg_sync_peers " ,
13
+ Name : "semaphore_wg_sync_peers_total " ,
13
14
Help : "Counts runners' attempts to sync peers." ,
14
15
},
15
16
[]string {"device" , "success" },
16
17
)
17
18
syncQueueFullFailures = prometheus .NewCounterVec (
18
19
prometheus.CounterOpts {
19
- Name : "semaphore_wg_sync_queue_full_failures " ,
20
+ Name : "semaphore_wg_sync_queue_full_failures_total " ,
20
21
Help : "Number of times a sync task was not added to the sync queue in time because the queue was full." ,
21
22
},
22
23
[]string {"device" },
23
24
)
24
25
syncRequeue = prometheus .NewCounterVec (
25
26
prometheus.CounterOpts {
26
- Name : "semaphore_wg_sync_requeue " ,
27
+ Name : "semaphore_wg_sync_requeue_total " ,
27
28
Help : "Number of attempts to requeue a sync." ,
28
29
},
29
30
[]string {"device" },
30
31
)
31
32
)
32
33
34
+ // registerMetrics registers all the prometheus collectors for this package
35
+ func registerMetrics (wgMetricsClient * wgctrl.Client , wgDeviceNames []string ) {
36
+ // Initialize counters with a value of 0
37
+ for _ , d := range wgDeviceNames {
38
+ for _ , s := range []string {"0" , "1" } {
39
+ syncPeersAttempt .With (prometheus.Labels {"device" : d , "success" : s })
40
+ }
41
+ syncQueueFullFailures .With (prometheus.Labels {"device" : d })
42
+ syncRequeue .With (prometheus.Labels {"device" : d })
43
+ }
44
+
45
+ mc := newMetricsCollector (func () ([]* wgtypes.Device , error ) {
46
+ var devices []* wgtypes.Device
47
+ for _ , name := range wgDeviceNames {
48
+ device , err := wgMetricsClient .Device (name )
49
+ if err != nil {
50
+ return nil , err
51
+ }
52
+ devices = append (devices , device )
53
+ }
54
+ return devices , nil
55
+ })
56
+
57
+ prometheus .MustRegister (
58
+ mc ,
59
+ syncPeersAttempt ,
60
+ syncQueueFullFailures ,
61
+ syncRequeue ,
62
+ )
63
+ }
64
+
33
65
// A collector is a prometheus.Collector for a WireGuard device.
34
66
type collector struct {
35
67
DeviceInfo * prometheus.Desc
@@ -42,13 +74,7 @@ type collector struct {
42
74
devices func () ([]* wgtypes.Device , error ) // to allow testing
43
75
}
44
76
45
- func init () {
46
- prometheus .MustRegister (syncPeersAttempt )
47
- prometheus .MustRegister (syncQueueFullFailures )
48
- prometheus .MustRegister (syncRequeue )
49
- }
50
-
51
- // NewMetricsCollector constructs a prometheus.Collector to collect metrics for
77
+ // newMetricsCollector constructs a prometheus.Collector to collect metrics for
52
78
// all present wg devices and correlate with user if possible
53
79
func newMetricsCollector (devices func () ([]* wgtypes.Device , error )) prometheus.Collector {
54
80
// common labels for all metrics
@@ -182,7 +208,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
182
208
}
183
209
}
184
210
185
- func MetricsSyncPeerAttempt (device string , err error ) {
211
+ func metricsSyncPeerAttempt (device string , err error ) {
186
212
s := "1"
187
213
if err != nil {
188
214
s = "0"
@@ -193,13 +219,13 @@ func MetricsSyncPeerAttempt(device string, err error) {
193
219
}).Inc ()
194
220
}
195
221
196
- func MetricsIncSyncQueueFullFailures (device string ) {
222
+ func metricsIncSyncQueueFullFailures (device string ) {
197
223
syncQueueFullFailures .With (prometheus.Labels {
198
224
"device" : device ,
199
225
}).Inc ()
200
226
}
201
227
202
- func MetricsIncSyncRequeue (device string ) {
228
+ func metricsIncSyncRequeue (device string ) {
203
229
syncRequeue .With (prometheus.Labels {
204
230
"device" : device ,
205
231
}).Inc ()
0 commit comments