temporalio / sdk-go

Temporal Go SDK
https://docs.temporal.io/application-development?lang=go
MIT License
481 stars 197 forks source link

Add otel Carrier implementation for Temporal headers. #1534

Open mcbadger88 opened 4 days ago

mcbadger88 commented 4 days ago

What was changed

Adds a Opentelemetry 'Carrier' implementation for Temporal Headers. This can be used by third party libraries to propagate Opentelemetry data via the Temporal headers. For example to propagate trace ID from the starter to workers.

Similar to implementations for other protocols here https://github.com/open-telemetry/opentelemetry-go/blob/0485de287ec48767a58e9fc2eda30b3dc1836668/propagation/propagation.go#L104 and here https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/instrumentation/google.golang.org/grpc/otelgrpc/metadata_supplier.go#L21.

Why?

I have made these changes in order for external libraries to be able to add Opentelemetry trace info to the Temporal Headers. Specifically my use case is a custom temporal interceptor that we have written.

Example usage:

import  (
    "go.opentelemetry.io/otel/propagation"
    "go.temporal.io/sdk/interceptor"
)

carrier := temporal_propagator.TemporalHeaderCarrier(interceptor.WorkflowHeader(ctx)

// In order to copy otel TextMap data from context into Temporal Headers
propagation.TextMapPropagator.Inject(ctx, carrier)

// In order to copy the otel TextMap data from Temporal Headers into context
propagation.TextMapPropagator.Extract(ctx, carrier)

Checklist

  1. Closes

  2. How was this tested: Unit test I was unable to figure out where in the temporal interceptor code the trace ID is copied into the temporal headers, but perhaps that could be updated to use this and then it will be covered by an integration test? Appreciate any pointers on that.

  3. Any docs updates needed? not sure?

CLAassistant commented 4 days ago

CLA assistant check
All committers have signed the CLA.