Merge pull request #49156 from vvoland/distribution-http-otel

distribution: Pass `Traceparent` OTEL header
This commit is contained in:
Sebastiaan van Stijn
2025-01-03 01:06:50 +01:00
committed by GitHub
3 changed files with 33 additions and 12 deletions

View File

@@ -12,7 +12,6 @@ import (
"github.com/docker/distribution/manifest/schema2"
"github.com/docker/distribution/registry/client"
"github.com/docker/distribution/registry/client/auth"
"github.com/docker/distribution/registry/client/transport"
registrytypes "github.com/docker/docker/api/types/registry"
"github.com/docker/docker/dockerversion"
"github.com/docker/docker/registry"
@@ -95,7 +94,7 @@ func newRepository(
}
modifiers := registry.Headers(dockerversion.DockerUserAgent(ctx), metaHeaders)
authTransport := transport.NewTransport(base, modifiers...)
authTransport := newTransport(base, modifiers...)
challengeManager, err := registry.PingV2Registry(endpoint.URL, authTransport)
if err != nil {
@@ -126,7 +125,8 @@ func newRepository(
basicHandler := auth.NewBasicHandler(creds)
modifiers = append(modifiers, auth.NewAuthorizer(challengeManager, tokenHandler, basicHandler))
}
tr := transport.NewTransport(base, modifiers...)
tr := newTransport(base, modifiers...)
// FIXME(thaJeztah): should this just take the original repoInfo.Name instead of converting the remote name back to a named reference?
repoNameRef, err := reference.WithName(repoName)

18
distribution/transport.go Normal file
View File

@@ -0,0 +1,18 @@
package distribution
import (
"net/http"
"github.com/docker/distribution/registry/client/transport"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
// newTransport creates a new transport which will apply modifiers to
// the request on a RoundTrip call.
func newTransport(base http.RoundTripper, modifiers ...transport.RequestModifier) http.RoundTripper {
tr := transport.NewTransport(base, modifiers...)
// Wrap the transport with OpenTelemetry instrumentation
// This propagates the Traceparent header.
return otelhttp.NewTransport(tr)
}

View File

@@ -14,6 +14,7 @@ import (
"github.com/containerd/log"
"github.com/docker/distribution/registry/client/transport"
"github.com/docker/go-connections/tlsconfig"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
// HostCertsDir returns the config directory for a specific host.
@@ -115,7 +116,7 @@ func Headers(userAgent string, metaHeaders http.Header) []transport.RequestModif
// newTransport returns a new HTTP transport. If tlsConfig is nil, it uses the
// default TLS configuration.
func newTransport(tlsConfig *tls.Config) *http.Transport {
func newTransport(tlsConfig *tls.Config) http.RoundTripper {
if tlsConfig == nil {
tlsConfig = tlsconfig.ServerDefault()
}
@@ -125,12 +126,14 @@ func newTransport(tlsConfig *tls.Config) *http.Transport {
KeepAlive: 30 * time.Second,
}
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: direct.DialContext,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: tlsConfig,
// TODO(dmcgowan): Call close idle connections when complete and use keep alive
DisableKeepAlives: true,
}
return otelhttp.NewTransport(
&http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: direct.DialContext,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: tlsConfig,
// TODO(dmcgowan): Call close idle connections when complete and use keep alive
DisableKeepAlives: true,
},
)
}