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

Introduce a variation of contextCapture that captures the context immediately #3501

Open
dstepanov opened this issue Jun 16, 2023 · 5 comments
Labels
area/context This issue is related to the Context for/user-attention This issue needs user attention (feedback, rework, etc...)

Comments

@dstepanov
Copy link

Currently, contextCapture is:

a convenience shortcut to capture thread local values during the subscription phase and put them in the Context that is visible upstream of this operator

Personally, I don't see it much useful as the actual subscription might be way outside of the user code.

It would be nice to have something like currentContextCapture as that would capture the current thread-local context and add it to the reactor context, something like:

var newCtx = capturedContextFromThreadLocal();
mono.contextWrite(ctx -> ctx + newCtx);

One of the use cases would be:

// update MDC context
mono.currentContextCapture()
// delete MDC context
// The MDC context is propagate to all the operators
@reactorbot reactorbot added the ❓need-triage This issue needs triage, hasn't been looked at by a team member yet label Jun 16, 2023
@OlegDokuka
Copy link
Contributor

Hello, @dstepanov!

Can you please provide a certain usecase where contextCapture does not work (test or any reproduceable example with explicit expectations)

Thanks,
Oleh

@OlegDokuka OlegDokuka added the status/need-user-input This needs user input to proceed label Jun 17, 2023
@dstepanov
Copy link
Author

@OlegDokuka
Copy link
Contributor

OlegDokuka commented Jun 19, 2023

@dstepanov, I meant a reproducer that shows a specific scenario where contextCapture does not work as expected (that will help us much better than a link to a pr for an external framework). In your case, it is just a different scenario, which goes beyond the defined capabilities. Also, subscription time happens usually within the same thread as pipeline assembly, hence, all the thread locals should be available (unless you have a certain reproducer that demonstrates different)

Thanks,
Oleh

P.S. As it is demonstrated in the linked PR, contextCapture is the same as contextWrite with minimal integration with the context propagation library, thus technically you can use contextWrite and manually insert required keys. Combining both can do what you need if I get your problem right (or just the pure usage of contextWrite can do it)

@OlegDokuka OlegDokuka added status/invalid We don't feel this issue is valid, or the root cause was found outside of Reactor area/context This issue is related to the Context and removed ❓need-triage This issue needs triage, hasn't been looked at by a team member yet status/need-user-input This needs user input to proceed labels Jun 19, 2023
@dstepanov
Copy link
Author

This is an enhancement request, not a bug report.

The contextCapture works as the JavaDoc describes. I think it should be named contextCaptureOnSubscribe as it is confusing that the capture is postponed and nothing is captured on the method invocation.

My use case differs. I have a try-resources block that modifies the thread-local. This state I want to capture into the Reactor context immediately on some new method invocation. There might be better naming than capture here as I wish to extend it with any possible new thread-local state.

@chemicL
Copy link
Member

chemicL commented Jun 19, 2023

I agree with the sentiment and I think we could consider contextCaptureNow() as a new operator that captures during assembly time, on the spot, when the method is invoked. It feels more intuitive to capture at the time of assembling. Subscription-time capture is out of the control of the person assembling the pipeline and can be useful when an assembled pipeline is reused for more subscribers, but the regular case would be to assume the ThreadLocal values are present when assembling the pipe. @OlegDokuka WDYT?

@chemicL chemicL added for/user-attention This issue needs user attention (feedback, rework, etc...) and removed status/invalid We don't feel this issue is valid, or the root cause was found outside of Reactor labels Jun 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/context This issue is related to the Context for/user-attention This issue needs user attention (feedback, rework, etc...)
Projects
None yet
Development

No branches or pull requests

4 participants