Skip to content

Commit

Permalink
k6runner/http: create spans in k6 http runner
Browse files Browse the repository at this point in the history
  • Loading branch information
roobre committed Oct 4, 2024
1 parent 0decbbf commit c65fdbb
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions internal/k6runner/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"time"

"github.com/rs/zerolog"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"golang.org/x/exp/rand"
)

Expand Down Expand Up @@ -106,14 +109,18 @@ func (r HttpRunner) Run(ctx context.Context, script Script) (*RunResponse, error
waitRemaining := max(0, wait-time.Since(start))
r.logger.Warn().Err(err).Dur("after", waitRemaining).Msg("retrying retryable error")

// Discard context, backoffSpan needs no children.
_, backoffSpan := trace.SpanFromContext(ctx).TracerProvider().Tracer("").Start(ctx, "backoff")

waitTimer := time.NewTimer(waitRemaining)
select {
case <-ctx.Done():
backoffSpan.End()
waitTimer.Stop()
// TODO: Log the returned error in the Processor instead.
r.logger.Error().Err(err).Msg("retries exhausted")
r.logger.Error().Err(err).Msg("retries exhausted") // TODO: Log the returned error in the Processor instead.
return nil, fmt.Errorf("cannot retry further: %w", errors.Join(err, ctx.Err()))
case <-waitTimer.C:
backoffSpan.End()
}

// Backoff linearly, adding some jitter.
Expand Down Expand Up @@ -165,7 +172,21 @@ func (r HttpRunner) request(ctx context.Context, script Script) (*RunResponse, e

req.Header.Add("content-type", "application/json")

resp, err := http.DefaultClient.Do(req)
// Build a tracing-enabled http client.
httpClient := http.Client{
Transport: otelhttp.NewTransport(
http.DefaultTransport,
otelhttp.WithTracerProvider(trace.SpanFromContext(ctx).TracerProvider()),
// Span names do not include method and path by default to avoid cardinality explosion with paths containing
// IDs. As this is not the case with this endpoint, we use a custom formatter that includes both.
otelhttp.WithSpanNameFormatter(func(_ string, r *http.Request) string {
return fmt.Sprintf("%s %s", r.Method, r.URL.Path)
}),
otelhttp.WithPropagators(propagation.TraceContext{}), // Send TraceIDs in outgoing requests.
),
}

resp, err := httpClient.Do(req)
if err != nil {
r.logger.Error().Err(err).Msg("sending request")

Expand Down

0 comments on commit c65fdbb

Please sign in to comment.