Class GrpcClientContextPropagationInterceptor

java.lang.Object
io.micronaut.grpc.client.propagation.GrpcClientContextPropagationInterceptor
All Implemented Interfaces:
io.grpc.ClientInterceptor, io.micronaut.core.order.Ordered

@Singleton @Requires(property="grpc.client.context-propagation.enabled", value="true", defaultValue="true") public class GrpcClientContextPropagationInterceptor extends Object implements io.grpc.ClientInterceptor, io.micronaut.core.order.Ordered
Makes the Micronaut PropagatedContext of the thread that starts a client call current again in the call's response callbacks (onHeaders, onMessage, onClose and onReady).

gRPC runs those callbacks on the channel's executor, so without this interceptor the MDC, tracing and other context elements of the caller are lost in an async stub's StreamObserver. The interceptor does nothing when the thread that starts the call has no propagated context.

The context is captured when the call is started (ClientCall.start(ClientCall.Listener, Metadata)), not when it is created. This interceptor runs first: it captures the context and wraps the application's listener, which also covers calls that another interceptor completes without reaching the transport. GrpcClientContextPropagationInterceptor.Restoring runs last, closest to the transport, so that the listeners of every other interceptor also run inside the restored context.

The captured context is only a fallback: a callback that already runs inside a propagated context, such as one set by another interceptor, keeps it.

Enabled by default; disable with grpc.client.context-propagation.enabled=false.

Since:
5.3.0
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Restores the captured context around the response callbacks coming from the transport.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The property that enables client context propagation.

    Fields inherited from interface io.micronaut.core.order.Ordered

    HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Runs first, so that the context is captured on the thread that starts the call.
    <ReqT,RespT>
    io.grpc.ClientCall<ReqT,RespT>
    interceptCall(io.grpc.MethodDescriptor<ReqT,RespT> method, io.grpc.CallOptions callOptions, io.grpc.Channel next)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • GrpcClientContextPropagationInterceptor

      public GrpcClientContextPropagationInterceptor()
  • Method Details

    • interceptCall

      public <ReqT,RespT> io.grpc.ClientCall<ReqT,RespT> interceptCall(io.grpc.MethodDescriptor<ReqT,RespT> method, io.grpc.CallOptions callOptions, io.grpc.Channel next)
      Specified by:
      interceptCall in interface io.grpc.ClientInterceptor
    • getOrder

      public int getOrder()
      Runs first, so that the context is captured on the thread that starts the call.
      Specified by:
      getOrder in interface io.micronaut.core.order.Ordered
      Returns:
      The order