Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transport): add OpenTelemetry context propagation #2127

Merged
merged 4 commits into from
Dec 5, 2023

Conversation

quartzmo
Copy link
Member

@quartzmo quartzmo commented Aug 31, 2023

OpenCensus sunsetting and OpenTelemetry support

The OpenCensus project is obsolete and was archived on July 31st, 2023. This means that any security vulnerabilities that are found will not be patched. We recommend that you begin migrating to OpenCensus tracing to OpenTelemetry, the successor project.

Using the OpenTelemetry-Go - OpenCensus Bridge, you can immediately begin exporting your traces with OpenTelemetry, even while dependencies such as the Google Cloud client libraries for Go remain instrumented with OpenCensus. If you do not use the bridge, you will need to migrate your entire application and all of its instrumented dependencies at once. For simple applications, this may be possible, but we expect the bridge to be helpful if multiple libraries with instrumentation are used.

One year after the Google Cloud Go client libraries release the experimental, opt-in OpenTelemetry tracing support in this PR (mid-Q4 2023), the prior experimental OpenCensus tracing will be removed.

Please refer to the following resources:

OpenTelemetry trace context propagation

The context propagation support for OpenCensus tracing (based on the Cloud Trace header) must be supplemented with new and separate context propagation support for OpenTelemetry tracing (based on the W3C Trace Context header). The google.golang.org/api/transport package currently configures context propagation for the Cloud Trace header (X-Cloud-Trace-Context). This is used in all Go client libraries, both HTTP and gRPC, Discovery (Apiary) and GAPIC.

This PR adds support for by wrapping the underlying transport as follows:

func addOpenTelemetryTransport(trans http.RoundTripper, settings *internal.DialSettings) http.RoundTripper {
	if settings.TelemetryDisabled {
		return trans
	}
	return otelhttp.NewTransport(trans)
}

Passing options to otelhttp.NewTransport in HTTP-based clients

Users who want to pass options to otelhttp.NewTransport can disable the library support in the transport package and configure the transport and client themselves.

ctx := context.Background()
trans, err := htransport.NewTransport(ctx,
        http.DefaultTransport,
        option.WithScopes(storage.ScopeFullControl),
)
if err != nil {
        log.Fatal(err)
}
// An example of passing options to the otelhttp.Transport.
otelOpts := otelhttp.WithFilter(func(r *http.Request) bool {
	return r.URL.Path != "/ping"
})
hc := &http.Client{
	Transport: otelhttp.NewTransport(trans, otelOpts),
}
client, err := storage.NewClient(ctx, option.WithHTTPClient(hc))

Note that scopes must be set manually in the user-configured solution.

Passing options to otelgrpc.NewClientHandler in gRPC-based clients

The setup is pretty similar in gRPC using the otelgrpc.NewClientHandler.

projectID := "..."
ctx := context.Background()

// An example of passing options to grpc.WithStatsHandler.
otelOpts := otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents)
dialOpts := grpc.WithStatsHandler(otelgrpc.NewClientHandler(otelOpts))

ctx := context.Background()
c, err := datastore.NewClient(ctx, projectID, option.WithGRPCDialOption(dialOpts))
if err != nil {
	log.Fatal(err)
}
defer c.Close()

@tmc
Copy link

tmc commented Sep 22, 2023

As most of the ecosystem has moved on to opentelemetry, it'd be interesting to hear how otel support here is going to evolve (and what the deprecation path of the opencensus instrumentation looks like). Any input there?

@quartzmo quartzmo force-pushed the transport-open-telemetry branch from 29124f7 to f491ead Compare November 7, 2023 16:34
@quartzmo quartzmo marked this pull request as ready for review November 7, 2023 16:35
@quartzmo quartzmo requested a review from a team as a code owner November 7, 2023 16:35
@quartzmo quartzmo force-pushed the transport-open-telemetry branch from 6d1aaf9 to 7b91188 Compare November 13, 2023 23:53
@quartzmo quartzmo marked this pull request as draft November 13, 2023 23:55
@quartzmo quartzmo force-pushed the transport-open-telemetry branch from 7b91188 to 35c3d65 Compare November 17, 2023 21:48
@quartzmo quartzmo force-pushed the transport-open-telemetry branch from 35c3d65 to 41c4009 Compare November 17, 2023 23:04
@quartzmo quartzmo marked this pull request as ready for review November 17, 2023 23:05
quartzmo added a commit to quartzmo/google-cloud-go that referenced this pull request Dec 4, 2023
Copy link
Contributor

@shollyman shollyman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, modulo a bit of feedback on the dialcontext testing.

transport/grpc/dial_test.go Show resolved Hide resolved
@quartzmo quartzmo merged commit 46421d4 into googleapis:main Dec 5, 2023
5 checks passed
@quartzmo quartzmo deleted the transport-open-telemetry branch December 5, 2023 23:07
quartzmo added a commit to googleapis/google-cloud-go that referenced this pull request Jan 11, 2024
quartzmo added a commit to quartzmo/google-cloud-go that referenced this pull request Oct 8, 2024
quartzmo added a commit to quartzmo/google-cloud-go that referenced this pull request Oct 8, 2024
quartzmo added a commit to googleapis/google-cloud-go that referenced this pull request Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants