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 10, 2024
1 parent b6577e8 commit 8d91f8f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/mccutchen/go-httpbin/v2 v2.15.0
github.com/quasilyte/go-ruleguard/dsl v0.3.22
github.com/spf13/afero v1.11.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0
go.opentelemetry.io/otel v1.30.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.30.0
go.opentelemetry.io/otel/sdk v1.30.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 h1:4K4tsIXefpVJtvA/8srF4V4y0akAoPHkIslgAkjixJA=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0/go.mod h1:jjdQuTGVsXV4vSs+CJ2qYDeDPf9yIJV23qlIzBm73Vg=
go.opentelemetry.io/otel v1.30.0 h1:F2t8sK4qf1fAmY9ua4ohFS/K+FUuOPemHUIXHtktrts=
go.opentelemetry.io/otel v1.30.0/go.mod h1:tFw4Br9b7fOS+uEao81PJjVMjW/5fvNCbpsDIXqP0pc=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0 h1:lsInsfvhVIfOI6qHVyysXMNDnjO9Npvl7tlDPJFBVd4=
Expand Down
22 changes: 19 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 @@ -110,8 +113,7 @@ func (r HttpRunner) Run(ctx context.Context, script Script) (*RunResponse, error
select {
case <-ctx.Done():
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:
}
Expand Down Expand Up @@ -165,7 +167,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 8d91f8f

Please sign in to comment.