Skip to content

Commit

Permalink
Add HTTP client update function to tracing library
Browse files Browse the repository at this point in the history
Signed-off-by: Derek McGowan <derek@mcg.dev>
(cherry picked from commit 8203bb9)
Signed-off-by: Derek McGowan <derek@mcg.dev>
  • Loading branch information
dmcgowan committed Dec 6, 2023
1 parent 807ddd6 commit f8f659e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
10 changes: 3 additions & 7 deletions remotes/docker/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
"github.com/containerd/containerd/version"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

var (
Expand Down Expand Up @@ -586,12 +585,9 @@ func (r *request) do(ctx context.Context) (*http.Response, error) {
return nil
}
}
client.Transport = otelhttp.NewTransport(
client.Transport,
otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
return tracing.Name("remotes.docker.resolver", "HTTPRequest")
}),
)

tracing.UpdateHTTPClient(client, tracing.Name("remotes.docker.resolver", "HTTPRequest"))

resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to do request: %w", err)
Expand Down
11 changes: 11 additions & 0 deletions tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"net/http"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
Expand Down Expand Up @@ -47,6 +48,16 @@ func WithHTTPRequest(_ *http.Request) SpanOpt {
}
}

// UpdateHTTPClient updates the http client with the necessary otel transport
func UpdateHTTPClient(client *http.Client, name string) {
client.Transport = otelhttp.NewTransport(
client.Transport,
otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
return name
}),
)
}

// StartSpan starts child span in a context.
func StartSpan(ctx context.Context, opName string, opts ...SpanOpt) (context.Context, *Span) {
config := StartConfig{}
Expand Down

0 comments on commit f8f659e

Please sign in to comment.