Skip to content

Commit

Permalink
Merge pull request #15 from utilitywarehouse/init-counters
Browse files Browse the repository at this point in the history
metrics: initialize counters
  • Loading branch information
ribbybibby authored Apr 30, 2021
2 parents 45eade4 + 1d0f317 commit 13026b0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
20 changes: 1 addition & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import (
"regexp"
"strings"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"

"github.com/utilitywarehouse/semaphore-wireguard/kube"
"github.com/utilitywarehouse/semaphore-wireguard/log"
Expand Down Expand Up @@ -107,7 +105,7 @@ func main() {
}
}()

makeMetricsCollector(wgMetricsClient, wgDeviceNames)
registerMetrics(wgMetricsClient, wgDeviceNames)
listenAndServe(runners)

// Stop runners before finishing
Expand Down Expand Up @@ -158,22 +156,6 @@ func makeRunner(homeClient kubernetes.Interface, localName string, rConf *remote
return r, wgDeviceName, nil
}

func makeMetricsCollector(wgMetricsClient *wgctrl.Client, wgDeviceNames []string) {
mc := newMetricsCollector(func() ([]*wgtypes.Device, error) {
var devices []*wgtypes.Device
for _, name := range wgDeviceNames {
device, err := wgMetricsClient.Device(name)
if err != nil {
return nil, err
}
devices = append(devices, device)
}
return devices, nil
})
prometheus.MustRegister(mc)

}

func listenAndServe(runners []*Runner) {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
Expand Down
52 changes: 39 additions & 13 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,65 @@ package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/utilitywarehouse/semaphore-wireguard/log"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)

var (
syncPeersAttempt = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "semaphore_wg_sync_peers",
Name: "semaphore_wg_sync_peers_total",
Help: "Counts runners' attempts to sync peers.",
},
[]string{"device", "success"},
)
syncQueueFullFailures = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "semaphore_wg_sync_queue_full_failures",
Name: "semaphore_wg_sync_queue_full_failures_total",
Help: "Number of times a sync task was not added to the sync queue in time because the queue was full.",
},
[]string{"device"},
)
syncRequeue = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "semaphore_wg_sync_requeue",
Name: "semaphore_wg_sync_requeue_total",
Help: "Number of attempts to requeue a sync.",
},
[]string{"device"},
)
)

// registerMetrics registers all the prometheus collectors for this package
func registerMetrics(wgMetricsClient *wgctrl.Client, wgDeviceNames []string) {
// Initialize counters with a value of 0
for _, d := range wgDeviceNames {
for _, s := range []string{"0", "1"} {
syncPeersAttempt.With(prometheus.Labels{"device": d, "success": s})
}
syncQueueFullFailures.With(prometheus.Labels{"device": d})
syncRequeue.With(prometheus.Labels{"device": d})
}

mc := newMetricsCollector(func() ([]*wgtypes.Device, error) {
var devices []*wgtypes.Device
for _, name := range wgDeviceNames {
device, err := wgMetricsClient.Device(name)
if err != nil {
return nil, err
}
devices = append(devices, device)
}
return devices, nil
})

prometheus.MustRegister(
mc,
syncPeersAttempt,
syncQueueFullFailures,
syncRequeue,
)
}

// A collector is a prometheus.Collector for a WireGuard device.
type collector struct {
DeviceInfo *prometheus.Desc
Expand All @@ -42,13 +74,7 @@ type collector struct {
devices func() ([]*wgtypes.Device, error) // to allow testing
}

func init() {
prometheus.MustRegister(syncPeersAttempt)
prometheus.MustRegister(syncQueueFullFailures)
prometheus.MustRegister(syncRequeue)
}

// NewMetricsCollector constructs a prometheus.Collector to collect metrics for
// newMetricsCollector constructs a prometheus.Collector to collect metrics for
// all present wg devices and correlate with user if possible
func newMetricsCollector(devices func() ([]*wgtypes.Device, error)) prometheus.Collector {
// common labels for all metrics
Expand Down Expand Up @@ -182,7 +208,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
}
}

func MetricsSyncPeerAttempt(device string, err error) {
func metricsSyncPeerAttempt(device string, err error) {
s := "1"
if err != nil {
s = "0"
Expand All @@ -193,13 +219,13 @@ func MetricsSyncPeerAttempt(device string, err error) {
}).Inc()
}

func MetricsIncSyncQueueFullFailures(device string) {
func metricsIncSyncQueueFullFailures(device string) {
syncQueueFullFailures.With(prometheus.Labels{
"device": device,
}).Inc()
}

func MetricsIncSyncRequeue(device string) {
func metricsIncSyncRequeue(device string) {
syncRequeue.With(prometheus.Labels{
"device": device,
}).Inc()
Expand Down
6 changes: 3 additions & 3 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (r *Runner) syncLoop() {
continue
}
err := r.syncPeers()
MetricsSyncPeerAttempt(r.device.Name(), err)
metricsSyncPeerAttempt(r.device.Name(), err)
if err != nil {
log.Logger.Warn("Failed to sync wg peers", "err", err)
r.requeuePeersSync()
Expand Down Expand Up @@ -165,7 +165,7 @@ func (r *Runner) enqueuePeersSync() {
log.Logger.Debug("Sync task queued")
case <-time.After(5 * time.Second):
log.Logger.Error("Timed out trying to queue a sync action for netset, sync queue is full")
MetricsIncSyncQueueFullFailures(r.device.Name())
metricsIncSyncQueueFullFailures(r.device.Name())
r.requeuePeersSync()
}
}
Expand All @@ -174,7 +174,7 @@ func (r *Runner) requeuePeersSync() {
log.Logger.Debug("Requeueing peers sync task")
go func() {
time.Sleep(1)
MetricsIncSyncRequeue(r.device.Name())
metricsIncSyncRequeue(r.device.Name())
r.enqueuePeersSync()
}()
}
Expand Down

0 comments on commit 13026b0

Please sign in to comment.