Skip to content

Commit

Permalink
feat: instrument traceroute implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
lvlcn-t committed Sep 9, 2024
1 parent 65a4aa9 commit a66acc9
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 44 deletions.
48 changes: 33 additions & 15 deletions pkg/checks/traceroute/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/getkin/kin-openapi/openapi3"
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -74,11 +76,11 @@ func (tr *Traceroute) Run(ctx context.Context, cResult chan checks.ResultDTO) er
defer cancel()
log := logger.FromContext(ctx)

log.Info("Starting traceroute check", "interval", tr.config.Interval.String())
log.InfoContext(ctx, "Starting traceroute check", "interval", tr.config.Interval.String())
for {
select {
case <-ctx.Done():
log.Error("Context canceled", "error", ctx.Err())
log.ErrorContext(ctx, "Context canceled", "error", ctx.Err())
return ctx.Err()
case <-tr.DoneChan:
return nil
Expand All @@ -92,7 +94,7 @@ func (tr *Traceroute) Run(ctx context.Context, cResult chan checks.ResultDTO) er
Timestamp: time.Now(),
},
}
log.Debug("Successfully finished traceroute check run")
log.DebugContext(ctx, "Successfully finished traceroute check run")
}
}
}
Expand All @@ -115,39 +117,51 @@ 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.String())
l.Debug("Running traceroute")
c, span := tr.tracer.Start(ctx, t.String())
l.DebugContext(ctx, "Running traceroute")

c, span := tr.tracer.Start(ctx, t.String(), trace.WithAttributes(
attribute.String("target.addr", t.Addr),
attribute.Int("target.port", t.Port),
attribute.Stringer("config.interval", tr.config.Interval),
attribute.Stringer("config.timeout", tr.config.Timeout),
attribute.Int("config.max_hops", tr.config.MaxHops),
attribute.Int("config.retry.count", tr.config.Retry.Count),
attribute.Stringer("config.retry.delay", tr.config.Retry.Delay),
))
defer span.End()

targetstart := time.Now()
s := time.Now()
hops, err := tr.traceroute(c, tracerouteConfig{
Dest: t.Addr,
Port: t.Port,
Timeout: tr.config.Timeout,
MaxHops: tr.config.MaxHops,
Rc: tr.config.Retry,
})
elapsed := time.Since(targetstart)
elapsed := time.Since(s)

if err != nil {
l.Error("Error running traceroute", "error", err)
l.ErrorContext(ctx, "Error running traceroute", "error", err)
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
} else {
span.SetStatus(codes.Ok, "success")
}

tr.metrics.CheckDuration(t.Addr, elapsed)
l.DebugContext(ctx, "Ran traceroute", "result", hops, "duration", elapsed)

l.Debug("Ran traceroute", "result", hops, "duration", elapsed)
res := result{
Hops: hops,
MinHops: tr.config.MaxHops,
}

for ttl, hop := range hops {
for _, attempt := range hop {
if attempt.Reached && attempt.Ttl < res.MinHops {
Expand All @@ -156,6 +170,12 @@ func (tr *Traceroute) check(ctx context.Context) map[string]result {
}
}

span.AddEvent("Traceroute completed", trace.WithAttributes(
attribute.Int("result.min_hops", res.MinHops),
attribute.Int("result.hop_count", len(hops)),
attribute.Stringer("result.elapsed_time", elapsed),
))

cResult <- internalResult{addr: t.Addr, res: res}
}(t)
}
Expand All @@ -168,9 +188,7 @@ func (tr *Traceroute) check(ctx context.Context) map[string]result {
}

elapsed := time.Since(start)

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

log.InfoContext(ctx, "Finished traceroute check", "duration", elapsed)
return res
}

Expand Down
Loading

0 comments on commit a66acc9

Please sign in to comment.