open-telemetry / opentelemetry-java-instrumentation

OpenTelemetry auto-instrumentation and instrumentation libraries for Java
https://opentelemetry.io
Apache License 2.0
1.96k stars 860 forks source link

Ability to extract current otel context from grpc context #11965

Open svametcalf opened 3 months ago

svametcalf commented 3 months ago

Is your feature request related to a problem? Please describe.

We are currently using grpc and the reactive-grpc generators in Spring. To make the tracing infrastructure all gel together, we are needing to provide a bridge between the gRPC context and Reactor Context, so that other Spring libraries can correlate started spans.

Currently, there is not a way to access the otel context value that has been stored in the gRPC context, because the gRPC Context uses reference equality for access and the key instances that have been used for storage are private.

Describe the solution you'd like

We would need a public Context.Key somewhere, so that the current otel context could be accessed. For example:

ContextStorageBridge.OTEL_CONTEXT.get(Context.current())  // this could be anywhere, it just needs to be `public`

This way, we can pass this context value to the Reactor pipeline

Describe alternatives you've considered

No response

Additional context

No response

heyams commented 2 months ago

Check out this code sample using request header to inject current context.. then you can retrieve it out to be used in other libraries?

// Inject the request with the current Context, which contains our current Span. openTelemetry.getPropagators().getTextMapPropagator().inject(Context.current(), transportLayer, setter);