Skip to content

Commit bf0a1df

Browse files
committed
k6runner/http: create spans in k6 http runner
1 parent 8b2a3fd commit bf0a1df

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-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: 19 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

@@ -110,8 +113,7 @@ func (r HttpRunner) Run(ctx context.Context, script Script) (*RunResponse, error
110113
select {
111114
case <-ctx.Done():
112115
waitTimer.Stop()
113-
// TODO: Log the returned error in the Processor instead.
114-
r.logger.Error().Err(err).Msg("retries exhausted")
116+
r.logger.Error().Err(err).Msg("retries exhausted") // TODO: Log the returned error in the Processor instead.
115117
return nil, fmt.Errorf("cannot retry further: %w", errors.Join(err, ctx.Err()))
116118
case <-waitTimer.C:
117119
}
@@ -165,7 +167,21 @@ func (r HttpRunner) request(ctx context.Context, script Script) (*RunResponse, e
165167

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

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

0 commit comments

Comments
 (0)