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

Apply contextCapture automatically in await function #3563

Closed
be-hase opened this issue Aug 16, 2023 · 6 comments
Closed

Apply contextCapture automatically in await function #3563

be-hase opened this issue Aug 16, 2023 · 6 comments
Labels
area/kotlin This belongs to the kotlin theme status/invalid We don't feel this issue is valid, or the root cause was found outside of Reactor

Comments

@be-hase
Copy link

be-hase commented Aug 16, 2023

Motivation

This is a similar issue.
#3406

For users using kotlin coroutine, ThreadContextElement is used to propagate MDC, etc.

The block operator is supported by Hooks.automaticContextPropagation, but not the await function. Therefore, we are forced to write it this way.

// e.g. WebClient

webClient.post()
    .uri("...")
    // omitted
    .contextCapture()
    .awaitSingle()

It would be great if the await function is also supported, like the block operator.

...However, the await function is implemented on the coroutine side, so perhaps a request should be made on the coroutine side.
https://github.com/Kotlin/kotlinx.coroutines/blob/1ccc96890d38be52d1bdcd31af5befb54804aa05/reactive/kotlinx-coroutines-reactor/src/Mono.kt#L46

Desired solution

Considered alternatives

Additional context

@reactorbot reactorbot added the ❓need-triage This issue needs triage, hasn't been looked at by a team member yet label Aug 16, 2023
@be-hase
Copy link
Author

be-hase commented Aug 16, 2023

Sorry, I was wrong in my verification and the problem did not occur even with await.
I'll close this issue.

reopend issue.

@be-hase be-hase closed this as completed Aug 16, 2023
@be-hase be-hase reopened this Sep 25, 2023
@be-hase
Copy link
Author

be-hase commented Sep 25, 2023

I have created a sample project.
https://github.com/be-hase/context-propagation-report-202309/tree/main/coroutines-issue

The code is in one file.
https://github.com/be-hase/context-propagation-report-202309/blob/main/coroutines-issue/src/main/kotlin/example/CoroutinesIssueApplication.kt

You can start it with the following command.

./gradlew :coroutines-issue:bootRun 

@be-hase be-hase changed the title Apply contextCapture automatically in await function Apply contextCapture automatically in await function Sep 25, 2023
@be-hase
Copy link
Author

be-hase commented Sep 25, 2023

I found that the following workaround can be done using the ContextInjector mechanism provided by the coroutines side.

package example

import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.reactive.ContextInjector
import org.reactivestreams.Publisher
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import kotlin.coroutines.CoroutineContext

@OptIn(InternalCoroutinesApi::class)
class ContextCaptureInjector : ContextInjector {
    override fun <T> injectCoroutineContext(publisher: Publisher<T>, coroutineContext: CoroutineContext): Publisher<T> {
        return when (publisher) {
            is Mono -> publisher.contextCapture()
            is Flux -> publisher.contextCapture()
            else -> publisher
        }
    }
}

@OlegDokuka
Copy link
Contributor

OlegDokuka commented Oct 2, 2023

@be-hase please move reopen this ticket at kotlin side since we can nothing to do at the reactor one. Alternatively, the mentioned workaround could be landed there -> https://github.com/reactor/reactor-kotlin-extensions.

Feel free to open PR

@OlegDokuka OlegDokuka closed this as not planned Won't fix, can't repro, duplicate, stale Oct 2, 2023
@OlegDokuka OlegDokuka added status/invalid We don't feel this issue is valid, or the root cause was found outside of Reactor area/kotlin This belongs to the kotlin theme and removed ❓need-triage This issue needs triage, hasn't been looked at by a team member yet labels Oct 2, 2023
@be-hase
Copy link
Author

be-hase commented Oct 5, 2023

OK, I'll reopen issue. thanks :)

@be-hase
Copy link
Author

be-hase commented Oct 17, 2023

reopend here: reactor/reactor-kotlin-extensions#59

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kotlin This belongs to the kotlin theme status/invalid We don't feel this issue is valid, or the root cause was found outside of Reactor
Projects
None yet
Development

No branches or pull requests

3 participants