Skip to content

Commit 6c83857

Browse files
committed
refactor: inject more metadata into logs
Signed-off-by: Niklas Treml <[email protected]>
1 parent e137d6a commit 6c83857

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

pkg/checks/traceroute/check.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Target struct {
2020
// The address of the target to traceroute to. Can be a DNS name or an IP address
2121
Addr string `json:"addr" yaml:"addr" mapstructure:"addr"`
2222
// The port to traceroute to
23-
Port uint16 `json:"port" yaml:"port" mapstructure:"port"`
23+
Port int `json:"port" yaml:"port" mapstructure:"port"`
2424
}
2525

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

109+
start := time.Now()
110+
109111
wg.Add(len(tr.config.Targets))
110112
for _, t := range tr.config.Targets {
111113
go func(t Target) {
112114
defer wg.Done()
113115
l := log.With("target", t.Addr)
114116
l.Debug("Running traceroute")
115117

116-
start := time.Now()
118+
targetstart := time.Now()
117119
trace, err := tr.traceroute(ctx, tracerouteConfig{
118120
Dest: t.Addr,
119-
Port: int(t.Port),
121+
Port: t.Port,
120122
Timeout: tr.config.Timeout,
121123
MaxHops: tr.config.MaxHops,
122124
Rc: tr.config.Retry,
123125
})
124-
elapsed := time.Since(start)
126+
elapsed := time.Since(targetstart)
125127
if err != nil {
126128
l.Error("Error running traceroute", "error", err)
127129
}
@@ -153,6 +155,10 @@ func (tr *Traceroute) check(ctx context.Context) map[string]result {
153155
res[r.addr] = r.res
154156
}
155157

158+
elapsed := time.Since(start)
159+
160+
log.Info("Finished traceroute check", "duration", elapsed)
161+
156162
return res
157163
}
158164

pkg/checks/traceroute/traceroute.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,25 @@ func TraceRoute(ctx context.Context, cfg tracerouteConfig) (map[int][]Hop, error
129129
wg.Add(1)
130130
go func(ttl int) {
131131
defer wg.Done()
132+
l := log.With("ttl", ttl)
133+
logctx := logger.IntoContext(ctx, l)
132134
err := helper.Retry(func(ctx context.Context) error {
133135
hop, err := traceroute(ctx, addr, ttl, cfg.Timeout)
134136
if hop != nil {
135137
results <- *hop
136138
}
137139
if err != nil {
138-
log.Error("traceroute failed", "err", err.Error(), "ttl", ttl)
140+
l.Error("traceroute failed", "err", err.Error())
139141
return err
140142
}
141143
if !hop.Reached {
142-
log.Debug("failed to reach target, retrying", "ttl", ttl)
144+
l.Debug("failed to reach target, retrying")
143145
return errors.New("failed to reach target")
144146
}
145147
return nil
146-
}, cfg.Rc)(ctx)
148+
}, cfg.Rc)(logctx)
147149
if err != nil {
148-
log.Debug("traceroute could not reach target", "ttl", ttl)
150+
l.Debug("traceroute could not reach target")
149151
}
150152
}(ttl)
151153
}
@@ -180,7 +182,7 @@ func traceroute(ctx context.Context, addr net.Addr, ttl int, timeout time.Durati
180182
log := logger.FromContext(ctx)
181183
canIcmp, icmpListener, err := newIcmpListener()
182184
if err != nil {
183-
log.Error("Failed to open ICMP socket", "err", err.Error(), "ttl", ttl)
185+
log.Error("Failed to open ICMP socket", "err", err.Error())
184186
return nil, err
185187
}
186188
defer closeIcmpListener(canIcmp, icmpListener)
@@ -193,7 +195,7 @@ func traceroute(ctx context.Context, addr net.Addr, ttl int, timeout time.Durati
193195
}
194196

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

273275
for time.Now().Unix() < deadline.Unix() {
274-
log.Debug("Reading ICMP message", "ttl", ttl)
276+
log.Debug("Reading ICMP message")
275277
gotPort, addr, err := readIcmpMessage(ctx, icmpListener, timeout)
276278
if err != nil {
277-
log.Debug("Failed to read ICMP message", "err", err.Error(), "ttl", ttl)
279+
log.Debug("Failed to read ICMP message", "err", err.Error())
278280
continue
279281
}
280282

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

299-
log.Debug("Deadline reached", "ttl", ttl)
301+
log.Debug("Deadline reached")
300302
return Hop{
301303
Ttl: ttl,
302304
}

0 commit comments

Comments
 (0)