-
Notifications
You must be signed in to change notification settings - Fork 166
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
[question] using interceptor to populate coroutine context from listener #196
Comments
i also meet this issue |
This is precisely the function of |
@lowasser I might be missing something but how would I get access to the request message in custom context interceptor? class CustomContextServerInterceptor(private val context: CoroutineContext = EmptyCoroutineContext) : CoroutineContextServerInterceptor() {
override fun coroutineContext(call: ServerCall<*, *>, headers: Metadata): CoroutineContext {
// populate MDC values based on metadata -> this works fine
MDC.put("myKey", headers.get(Metadata.Key.of("myKey", Metadata.ASCII_STRING_MARSHALLER))
// how do i populate MDC based on the incoming request?
MDC.put("request", call?)
return Dispatchers.Default + MDCContext() + context
}
} |
@lowasser I guess you missed the point that the question is how to populate additional MDC elements based on the incoming request, i.e. the request message object, not |
Yes, I missed that, my bad. Given that, your described approach is as good as it gets at this time. There's a related issue, that's kind of skimmed over in your example code's comments:
In this block, |
Thanks for confirming! |
Hello!
Was wondering if there is any way to populate coroutine context from the interceptor listeners, i.e. we would like to populate additional MDC elements based on the incoming request and afaik we can only access those from the listener.
Above doesn't work as listeners are invoked after processing all the interceptors. This means that context created by
CoroutineContextServerInterceptor.interceptCall()
will use snapshot of values up to that point and even if we modify the MDC in the listeners, those changes will be discarded when performing rpc call on the service (as it will start running a coroutine using the context from the interceptor).Using workaround of creating custom coroutine context passed to the constructor (#66) also doesn't work as it appears that bindable service context will be initialized before invoking the listeners as well.
The only other workaround I can think of is to manually populate the MDC from within the RPC methods, e.g.
is there any better way?
The text was updated successfully, but these errors were encountered: