From be418913e7f33c6486ac0eec7393dde41d7220db Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sun, 10 Mar 2024 10:15:30 +0000 Subject: [PATCH] Take host label from the pinger for consistency Older versions of pro-bing return the host name as pkt.Addr but newer versions return the stringified version of IPAddr instead. Signed-off-by: Tom Hughes --- collector.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/collector.go b/collector.go index ed0b41e..330dab5 100644 --- a/collector.go +++ b/collector.go @@ -88,20 +88,21 @@ func NewSmokepingCollector(pingers *[]*probing.Pinger, pingResponseSeconds prome for _, pinger := range *pingers { // Init all metrics to 0s. ipAddr := pinger.IPAddr().String() - pingResponseDuplicates.WithLabelValues(ipAddr, pinger.Addr(), pinger.Source) - pingResponseSeconds.WithLabelValues(ipAddr, pinger.Addr(), pinger.Source) - pingResponseTTL.WithLabelValues(ipAddr, pinger.Addr(), pinger.Source) - pingSendErrors.WithLabelValues(ipAddr, pinger.Addr(), pinger.Source) + host := pinger.Addr() + pingResponseDuplicates.WithLabelValues(ipAddr, host, pinger.Source) + pingResponseSeconds.WithLabelValues(ipAddr, host, pinger.Source) + pingResponseTTL.WithLabelValues(ipAddr, host, pinger.Source) + pingSendErrors.WithLabelValues(ipAddr, host, pinger.Source) // Setup handler functions. pinger.OnRecv = func(pkt *probing.Packet) { - pingResponseSeconds.WithLabelValues(pkt.IPAddr.String(), pkt.Addr, pinger.Source).Observe(pkt.Rtt.Seconds()) - pingResponseTTL.WithLabelValues(pkt.IPAddr.String(), pkt.Addr, pinger.Source).Set(float64(pkt.TTL)) + pingResponseSeconds.WithLabelValues(pkt.IPAddr.String(), host, pinger.Source).Observe(pkt.Rtt.Seconds()) + pingResponseTTL.WithLabelValues(pkt.IPAddr.String(), host, pinger.Source).Set(float64(pkt.TTL)) level.Debug(logger).Log("msg", "Echo reply", "ip_addr", pkt.IPAddr, "bytes_received", pkt.Nbytes, "icmp_seq", pkt.Seq, "time", pkt.Rtt, "ttl", pkt.TTL) } pinger.OnDuplicateRecv = func(pkt *probing.Packet) { - pingResponseDuplicates.WithLabelValues(pkt.IPAddr.String(), pkt.Addr, pinger.Source).Inc() + pingResponseDuplicates.WithLabelValues(pkt.IPAddr.String(), host, pinger.Source).Inc() level.Debug(logger).Log("msg", "Echo reply (DUP!)", "ip_addr", pkt.IPAddr, "bytes_received", pkt.Nbytes, "icmp_seq", pkt.Seq, "time", pkt.Rtt, "ttl", pkt.TTL) } @@ -122,7 +123,7 @@ func NewSmokepingCollector(pingers *[]*probing.Pinger, pingResponseSeconds prome level.Debug(logger).Log("msg", "Error receiving packet", "error", err) } pinger.OnSendError = func(pkt *probing.Packet, err error) { - pingSendErrors.WithLabelValues(pkt.IPAddr.String(), pkt.Addr, pinger.Source).Inc() + pingSendErrors.WithLabelValues(pkt.IPAddr.String(), host, pinger.Source).Inc() level.Debug(logger).Log("msg", "Error sending packet", "ip_addr", pkt.IPAddr, "bytes_received", pkt.Nbytes, "icmp_seq", pkt.Seq, "time", pkt.Rtt, "ttl", pkt.TTL, "error", err) }