Skip to content

Commit

Permalink
Use correct source label for metrics
Browse files Browse the repository at this point in the history
As go captures by reference we need to ensure each pinger's callbacks
uses the source the current pinger or they will all wind up reporting
the source from the last pinger.

Signed-off-by: Tom Hughes <[email protected]>
  • Loading branch information
tomhughes committed Mar 10, 2024
1 parent be41891 commit dd7c10f
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,21 @@ func NewSmokepingCollector(pingers *[]*probing.Pinger, pingResponseSeconds prome
// Init all metrics to 0s.
ipAddr := pinger.IPAddr().String()
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)
source := pinger.Source
pingResponseDuplicates.WithLabelValues(ipAddr, host, source)
pingResponseSeconds.WithLabelValues(ipAddr, host, source)
pingResponseTTL.WithLabelValues(ipAddr, host, source)
pingSendErrors.WithLabelValues(ipAddr, host, source)

// Setup handler functions.
pinger.OnRecv = func(pkt *probing.Packet) {
pingResponseSeconds.WithLabelValues(pkt.IPAddr.String(), host, pinger.Source).Observe(pkt.Rtt.Seconds())
pingResponseTTL.WithLabelValues(pkt.IPAddr.String(), host, pinger.Source).Set(float64(pkt.TTL))
pingResponseSeconds.WithLabelValues(pkt.IPAddr.String(), host, source).Observe(pkt.Rtt.Seconds())
pingResponseTTL.WithLabelValues(pkt.IPAddr.String(), host, 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(), host, pinger.Source).Inc()
pingResponseDuplicates.WithLabelValues(pkt.IPAddr.String(), host, 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)
}
Expand All @@ -123,7 +124,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(), host, pinger.Source).Inc()
pingSendErrors.WithLabelValues(pkt.IPAddr.String(), host, 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)
}
Expand Down

0 comments on commit dd7c10f

Please sign in to comment.