Skip to content

Commit f8f659e

Browse files
committed
Add HTTP client update function to tracing library
Signed-off-by: Derek McGowan <[email protected]> (cherry picked from commit 8203bb9) Signed-off-by: Derek McGowan <[email protected]>
1 parent 807ddd6 commit f8f659e

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

remotes/docker/resolver.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import (
3939
"github.com/containerd/containerd/version"
4040
"github.com/opencontainers/go-digest"
4141
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
42-
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
4342
)
4443

4544
var (
@@ -586,12 +585,9 @@ func (r *request) do(ctx context.Context) (*http.Response, error) {
586585
return nil
587586
}
588587
}
589-
client.Transport = otelhttp.NewTransport(
590-
client.Transport,
591-
otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
592-
return tracing.Name("remotes.docker.resolver", "HTTPRequest")
593-
}),
594-
)
588+
589+
tracing.UpdateHTTPClient(client, tracing.Name("remotes.docker.resolver", "HTTPRequest"))
590+
595591
resp, err := client.Do(req)
596592
if err != nil {
597593
return nil, fmt.Errorf("failed to do request: %w", err)

tracing/tracing.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"net/http"
2222

23+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
2324
"go.opentelemetry.io/otel"
2425
"go.opentelemetry.io/otel/attribute"
2526
"go.opentelemetry.io/otel/codes"
@@ -47,6 +48,16 @@ func WithHTTPRequest(_ *http.Request) SpanOpt {
4748
}
4849
}
4950

51+
// UpdateHTTPClient updates the http client with the necessary otel transport
52+
func UpdateHTTPClient(client *http.Client, name string) {
53+
client.Transport = otelhttp.NewTransport(
54+
client.Transport,
55+
otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
56+
return name
57+
}),
58+
)
59+
}
60+
5061
// StartSpan starts child span in a context.
5162
func StartSpan(ctx context.Context, opName string, opts ...SpanOpt) (context.Context, *Span) {
5263
config := StartConfig{}

0 commit comments

Comments
 (0)