package main: Prevent disco.Disco generating OTel traces

The disco.Disco API isn't yet set up to pass through context.Context, and
so if we give it a HTTP client that has the OpenTelemetry instrumentation
on it then any HTTP request causes an orphan trace span disconnected from
the main trace, which causes annoying noise in some trace viewers.

As a temporary solution so we can ship v1.10 soon without making large
changes to the svchost library, we'll prevent the HTTP client constructor
function from detecting that tracing is enabled by passing it
context.TODO() instead of the actual context. This would not be acceptable
in the long run but is safe for this temporary workaround because currently
httpclient.New doesn't use the given context for anything except detecting
whether tracing is enabled.

We will address this in a more complete way during the v1.11 development
period by modernizing svchost to take context.Context arguments on all
functions that can potentially make external requests.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
This commit is contained in:
Martin Atkins
2025-05-15 17:51:56 -07:00
parent 6c14802e20
commit d079da6e9e

View File

@@ -43,14 +43,25 @@ const (
// object should obtain authentication credentials for service discovery // object should obtain authentication credentials for service discovery
// requests. Passing a nil credSrc is acceptable and means that all discovery // requests. Passing a nil credSrc is acceptable and means that all discovery
// requests are to be made anonymously. // requests are to be made anonymously.
func newServiceDiscovery(ctx context.Context, credSrc auth.CredentialsSource) *disco.Disco { func newServiceDiscovery(_ context.Context, credSrc auth.CredentialsSource) *disco.Disco {
services := disco.NewWithCredentialsSource(credSrc) services := disco.NewWithCredentialsSource(credSrc)
services.SetUserAgent(httpclient.OpenTofuUserAgent(version.String())) services.SetUserAgent(httpclient.OpenTofuUserAgent(version.String()))
// For historical reasons, the registry request retry policy also applies // For historical reasons, the registry request retry policy also applies
// to all service discovery requests, which we implement by using transport // to all service discovery requests, which we implement by using transport
// from a HTTP client that is configured for registry client use. // from a HTTP client that is configured for registry client use.
client := newRegistryHTTPClient(ctx) //
// TEMP: The disco.Disco API isn't yet set up to pass through
// context.Context, so we're intentionally ignoring the passed-in ctx
// here to prevent the created client from having OpenTelemetry
// instrumentation added to it. This is just a low-risk temporary trick
// for the v1.10 release; we intend to update disco.Disco to properly
// support context.Context at some point during the v1.11 development
// period. This relies on the fact that httpclient.New uses the context
// we're (indirectly) passing it only to find out if there's an active
// OpenTelemetry span, which should be a valid assumption for as long as
// this very temporary workaround lasts.
client := newRegistryHTTPClient(context.TODO())
services.Transport = client.HTTPClient.Transport services.Transport = client.HTTPClient.Transport
return services return services