Skip to content

Commit

Permalink
refactor: inject more metadata into logs
Browse files Browse the repository at this point in the history
Signed-off-by: Niklas Treml <[email protected]>
  • Loading branch information
niklastreml committed Aug 6, 2024
1 parent e137d6a commit 6c83857
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
14 changes: 10 additions & 4 deletions pkg/checks/traceroute/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Target struct {
// The address of the target to traceroute to. Can be a DNS name or an IP address
Addr string `json:"addr" yaml:"addr" mapstructure:"addr"`
// The port to traceroute to
Port uint16 `json:"port" yaml:"port" mapstructure:"port"`
Port int `json:"port" yaml:"port" mapstructure:"port"`
}

func NewCheck() checks.Check {
Expand Down Expand Up @@ -106,22 +106,24 @@ func (tr *Traceroute) check(ctx context.Context) map[string]result {
cResult := make(chan internalResult, len(tr.config.Targets))
var wg sync.WaitGroup

start := time.Now()

wg.Add(len(tr.config.Targets))
for _, t := range tr.config.Targets {
go func(t Target) {
defer wg.Done()
l := log.With("target", t.Addr)
l.Debug("Running traceroute")

start := time.Now()
targetstart := time.Now()
trace, err := tr.traceroute(ctx, tracerouteConfig{
Dest: t.Addr,
Port: int(t.Port),
Port: t.Port,
Timeout: tr.config.Timeout,
MaxHops: tr.config.MaxHops,
Rc: tr.config.Retry,
})
elapsed := time.Since(start)
elapsed := time.Since(targetstart)
if err != nil {
l.Error("Error running traceroute", "error", err)
}
Expand Down Expand Up @@ -153,6 +155,10 @@ func (tr *Traceroute) check(ctx context.Context) map[string]result {
res[r.addr] = r.res
}

elapsed := time.Since(start)

log.Info("Finished traceroute check", "duration", elapsed)

return res
}

Expand Down
20 changes: 11 additions & 9 deletions pkg/checks/traceroute/traceroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,25 @@ func TraceRoute(ctx context.Context, cfg tracerouteConfig) (map[int][]Hop, error
wg.Add(1)
go func(ttl int) {
defer wg.Done()
l := log.With("ttl", ttl)
logctx := logger.IntoContext(ctx, l)
err := helper.Retry(func(ctx context.Context) error {
hop, err := traceroute(ctx, addr, ttl, cfg.Timeout)
if hop != nil {
results <- *hop
}
if err != nil {
log.Error("traceroute failed", "err", err.Error(), "ttl", ttl)
l.Error("traceroute failed", "err", err.Error())
return err
}
if !hop.Reached {
log.Debug("failed to reach target, retrying", "ttl", ttl)
l.Debug("failed to reach target, retrying")
return errors.New("failed to reach target")
}
return nil
}, cfg.Rc)(ctx)
}, cfg.Rc)(logctx)
if err != nil {
log.Debug("traceroute could not reach target", "ttl", ttl)
l.Debug("traceroute could not reach target")
}
}(ttl)
}
Expand Down Expand Up @@ -180,7 +182,7 @@ func traceroute(ctx context.Context, addr net.Addr, ttl int, timeout time.Durati
log := logger.FromContext(ctx)
canIcmp, icmpListener, err := newIcmpListener()
if err != nil {
log.Error("Failed to open ICMP socket", "err", err.Error(), "ttl", ttl)
log.Error("Failed to open ICMP socket", "err", err.Error())
return nil, err
}
defer closeIcmpListener(canIcmp, icmpListener)
Expand All @@ -193,7 +195,7 @@ func traceroute(ctx context.Context, addr net.Addr, ttl int, timeout time.Durati
}

if !canIcmp {
log.Debug("No permission for icmp socket", "ttl", ttl)
log.Debug("No permission for icmp socket")
return &Hop{
Latency: latency,
Ttl: ttl,
Expand Down Expand Up @@ -271,10 +273,10 @@ func handleIcmpResponse(ctx context.Context, icmpListener *icmp.PacketConn, clie
deadline := time.Now().Add(timeout)

for time.Now().Unix() < deadline.Unix() {
log.Debug("Reading ICMP message", "ttl", ttl)
log.Debug("Reading ICMP message")
gotPort, addr, err := readIcmpMessage(ctx, icmpListener, timeout)
if err != nil {
log.Debug("Failed to read ICMP message", "err", err.Error(), "ttl", ttl)
log.Debug("Failed to read ICMP message", "err", err.Error())
continue
}

Expand All @@ -296,7 +298,7 @@ func handleIcmpResponse(ctx context.Context, icmpListener *icmp.PacketConn, clie
}
}

log.Debug("Deadline reached", "ttl", ttl)
log.Debug("Deadline reached")
return Hop{
Ttl: ttl,
}
Expand Down

0 comments on commit 6c83857

Please sign in to comment.