Skip to content

Commit

Permalink
ref: define PingMetrics type
Browse files Browse the repository at this point in the history
this cuts down on the long parameter list in the collector function
  • Loading branch information
wbollock committed Dec 13, 2023
1 parent d8c9ed4 commit 1dfd63e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
27 changes: 14 additions & 13 deletions internal/collector/icmp_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/wbollock/ping_exporter/internal/metrics"
)

const (
Expand Down Expand Up @@ -110,7 +111,7 @@ func serveMetricsWithError(w http.ResponseWriter, r *http.Request, registry *pro
}
}

func PingHandler(registry *prometheus.Registry, pingSuccessGauge prometheus.Gauge, pingTimeoutGauge prometheus.Gauge, probeDurationGauge prometheus.Gauge, minGauge prometheus.Gauge, maxGauge prometheus.Gauge, avgGauge prometheus.Gauge, stddevGauge prometheus.Gauge, lossGauge prometheus.Gauge, mutex *sync.Mutex) http.HandlerFunc {
func PingHandler(registry *prometheus.Registry, metrics metrics.PingMetrics, mutex *sync.Mutex) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

p := parseParams(r)
Expand Down Expand Up @@ -146,24 +147,24 @@ func PingHandler(registry *prometheus.Registry, pingSuccessGauge prometheus.Gaug
mutex.Lock()
if pinger.PacketsRecv > 0 && pinger.Timeout > time.Since(start) {
log.Debugf("Ping successful: target=%v", stats.IPAddr)
pingSuccessGauge.Set(1)
pingTimeoutGauge.Set(0)
metrics.PingSuccessGauge.Set(1)
metrics.PingTimeoutGauge.Set(0)
} else if pinger.Timeout < time.Since(start) {
log.Infof("Ping timeout: target=%v, timeout=%v, duration=%v", stats.IPAddr, pinger.Timeout, time.Since(start))
pingTimeoutGauge.Set(1)
pingSuccessGauge.Set(0)
metrics.PingTimeoutGauge.Set(1)
metrics.PingSuccessGauge.Set(0)
} else if pinger.PacketsRecv == 0 {
log.Infof("Ping failed, no packets received: target=%v, packetsRecv=%v, packetsSent=%v", stats.IPAddr, pinger.PacketsRecv, pinger.PacketsSent)
pingSuccessGauge.Set(0)
pingTimeoutGauge.Set(0)
metrics.PingSuccessGauge.Set(0)
metrics.PingTimeoutGauge.Set(0)
}

minGauge.Set(stats.MinRtt.Seconds())
avgGauge.Set(stats.AvgRtt.Seconds())
maxGauge.Set(stats.MaxRtt.Seconds())
stddevGauge.Set(float64(stats.StdDevRtt))
lossGauge.Set(stats.PacketLoss)
probeDurationGauge.Set(time.Since(start).Seconds())
metrics.MinGauge.Set(stats.MinRtt.Seconds())
metrics.AvgGauge.Set(stats.AvgRtt.Seconds())
metrics.MaxGauge.Set(stats.MaxRtt.Seconds())
metrics.StddevGauge.Set(float64(stats.StdDevRtt))
metrics.LossGauge.Set(stats.PacketLoss)
metrics.ProbeDurationGauge.Set(time.Since(start).Seconds())
mutex.Unlock()
}

Expand Down
16 changes: 16 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package metrics

import (
"github.com/prometheus/client_golang/prometheus"
)

type PingMetrics struct {
PingSuccessGauge prometheus.Gauge
PingTimeoutGauge prometheus.Gauge
ProbeDurationGauge prometheus.Gauge
MinGauge prometheus.Gauge
MaxGauge prometheus.Gauge
AvgGauge prometheus.Gauge
StddevGauge prometheus.Gauge
LossGauge prometheus.Gauge
}
17 changes: 15 additions & 2 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/wbollock/ping_exporter/internal/collector"
"github.com/wbollock/ping_exporter/internal/metrics"
)

const (
Expand Down Expand Up @@ -67,8 +68,20 @@ func SetupServer() http.Handler {

var mutex sync.Mutex
registry := prometheus.NewRegistry()
registry.MustRegister(pingSuccessGauge, pingTimeoutGauge, probeDurationGauge, minGauge, maxGauge, avgGauge, stddevGauge, lossGauge)
mux.HandleFunc("/probe", collector.PingHandler(registry, pingSuccessGauge, pingTimeoutGauge, probeDurationGauge, minGauge, maxGauge, avgGauge, stddevGauge, lossGauge, &mutex))

metrics := metrics.PingMetrics{
PingSuccessGauge: pingSuccessGauge,
PingTimeoutGauge: pingTimeoutGauge,
ProbeDurationGauge: probeDurationGauge,
MinGauge: minGauge,
MaxGauge: maxGauge,
AvgGauge: avgGauge,
StddevGauge: stddevGauge,
LossGauge: lossGauge,
}

registry.MustRegister(metrics.PingSuccessGauge, metrics.PingTimeoutGauge, metrics.ProbeDurationGauge, metrics.MinGauge, metrics.MaxGauge, metrics.AvgGauge, metrics.StddevGauge, metrics.LossGauge)
mux.HandleFunc("/probe", collector.PingHandler(registry, metrics, &mutex))

// for non-standard web servers, need to register handlers
mux.HandleFunc("/debug/pprof/", http.HandlerFunc(pprof.Index))
Expand Down

0 comments on commit 1dfd63e

Please sign in to comment.