Skip to content

Commit 09c7724

Browse files
committed
k6runner/http: create spans in k6 http runner
1 parent 3371d63 commit 09c7724

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ require (
3838
github.com/mccutchen/go-httpbin/v2 v2.15.0
3939
github.com/quasilyte/go-ruleguard/dsl v0.3.22
4040
github.com/spf13/afero v1.11.0
41+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0
4142
go.opentelemetry.io/otel v1.30.0
4243
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.30.0
4344
go.opentelemetry.io/otel/sdk v1.30.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
172172
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
173173
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
174174
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
175+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 h1:4K4tsIXefpVJtvA/8srF4V4y0akAoPHkIslgAkjixJA=
176+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0/go.mod h1:jjdQuTGVsXV4vSs+CJ2qYDeDPf9yIJV23qlIzBm73Vg=
175177
go.opentelemetry.io/otel v1.30.0 h1:F2t8sK4qf1fAmY9ua4ohFS/K+FUuOPemHUIXHtktrts=
176178
go.opentelemetry.io/otel v1.30.0/go.mod h1:tFw4Br9b7fOS+uEao81PJjVMjW/5fvNCbpsDIXqP0pc=
177179
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0 h1:lsInsfvhVIfOI6qHVyysXMNDnjO9Npvl7tlDPJFBVd4=

internal/k6runner/http.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
"time"
1111

1212
"github.com/rs/zerolog"
13+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
14+
"go.opentelemetry.io/otel/propagation"
15+
"go.opentelemetry.io/otel/trace"
1316
"golang.org/x/exp/rand"
1417
)
1518

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

112+
// Discard context, backoffSpan needs no children.
113+
_, backoffSpan := trace.SpanFromContext(ctx).TracerProvider().Tracer("").Start(ctx, "backoff")
114+
109115
waitTimer := time.NewTimer(waitRemaining)
110116
select {
111117
case <-ctx.Done():
118+
backoffSpan.End()
112119
waitTimer.Stop()
113-
// TODO: Log the returned error in the Processor instead.
114-
r.logger.Error().Err(err).Msg("retries exhausted")
120+
r.logger.Error().Err(err).Msg("retries exhausted") // TODO: Log the returned error in the Processor instead.
115121
return nil, fmt.Errorf("cannot retry further: %w", errors.Join(err, ctx.Err()))
116122
case <-waitTimer.C:
123+
backoffSpan.End()
117124
}
118125

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

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

168-
resp, err := http.DefaultClient.Do(req)
175+
// Build a tracing-enabled http client.
176+
httpClient := http.Client{
177+
Transport: otelhttp.NewTransport(
178+
http.DefaultTransport,
179+
otelhttp.WithTracerProvider(trace.SpanFromContext(ctx).TracerProvider()),
180+
// Span names do not include method and path by default to avoid cardinality explosion with paths containing
181+
// IDs. As this is not the case with this endpoint, we use a custom formatter that includes both.
182+
otelhttp.WithSpanNameFormatter(func(_ string, r *http.Request) string {
183+
return fmt.Sprintf("%s %s", r.Method, r.URL.Path)
184+
}),
185+
otelhttp.WithPropagators(propagation.TraceContext{}), // Send TraceIDs in outgoing requests.
186+
),
187+
}
188+
189+
resp, err := httpClient.Do(req)
169190
if err != nil {
170191
r.logger.Error().Err(err).Msg("sending request")
171192

0 commit comments

Comments
 (0)