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

Support micrometer context-propagation #5577

Open
wants to merge 43 commits into
base: main
Choose a base branch
from

Conversation

chickenchickenlove
Copy link
Contributor

@chickenchickenlove chickenchickenlove commented Apr 8, 2024

Motivation:

  • Related Issue : Support micrometer-context-propagation #5145
  • Armeria already support context-propagation to maintain RequestContext during executing Reactor code. How it requires maintenance.
  • Reactor integrate micro-meter:context-propagation to do context-propagation during Flux, Mono officially. thus, it would be better to migrate from RequestContextHook to RequestContextPropagationHooks because it can reduce maintenance cost.

Modifications:

  • Add new Hook for Reactor.
  • Add new ThreadLocalAccessor for micro-meter:context-propagation to main RequestContext during executing Reactor code like Mono, Flux.
  • Add new config enableContextPropagation to integrate micro-meter:context-propagation with spring-boot3.

Result:

  • Closes Support micrometer-context-propagation #5145
  • If user want to use micrometer:context-propagation to maintain RequestContext during executing Reactor code like Mono, Flux, just call RequestContextPropagationHook.enable().

@chickenchickenlove
Copy link
Contributor Author

FYI, flow of micro-meter/context-propagation.

image
  1. If we enable context-propagation, hook for context-propagation will be added Reactor's hooks.
image
  1. if subscribe() are called, hook like captureThreadLocals() are called.
image
  1. and then, ContextSnapshot are created to propagate context. in this time, accessor are used to capture state of ThreadLocals.
image
  1. When Mono or Flux are executed in Scheduler's executor, scope are gotten by calling setThreadLocals(). Scope instance are returned, and it is concrete class of AutoClosable.
image
  1. As you can see, Context-Propagation is restore thread local state at end of try ~ resource.
image
  1. On the other hand, setThreadLocals() be about to restore Threadlocal state from ContextSnapshot before runnable.run().

IMHO, it is very similar to RequestContextHook on armeria.

@chickenchickenlove
Copy link
Contributor Author

I investigate that internal of Context-Propagation.

Case 1. publisher.subscribe(...). downstream -> upstream flow

  • When publisher.subscribe(...) are called and publisher is instance of ContextWriteRestoringThreadLocals, it creates ContextSnapshot.Scope.in this sequence, read the Key-Value stored in ReactorContext, then use the ThreadLocalAccessor instance to store the value from ReactorContext into ThreadLocal. Additionally, keep the previously stored values in ThreadLocal as PreviousValues, and use this values to revert the state of ThreadLocal to its previous state when the ContextSnapshot.Scope ends. (FYI, ContextSnapshot.Scope implement AutoClosable).

Case 2. subscriber.onSubscribe(...). upstream -> downstream flow

  • When subscriber.onSubscribe(...) are called and subscriber is instance of ContextWriteRestoringThreadLocal, it creates ContextSnapshot.Scope as well. it means that the values in ReactorContext will be stored to ThreadLocal at the start of subscriber.onSubscribe() and ThreadLocal will be revert to its previous state at the end of the function.

Case 3. subscription.request(...). downstream -> upstream flow

  • When subscription.request(...) are called and subscriptionis instance ofContextWriteRestoringThreadLocal, it creates ContextSnapshot.scope` as well.

ContextWriteRestoringThreadLocals operator are integrated when both [Mono|Flux]#contextCapture() and [Mono|Flux]#contextWrite() are called. It means that Reactor Context are essential to propagate RequestContext to each ThreadLocal during Reactor Operations.

In practice, it works like this:

  1. Reads all values accessible by the KEY of ThreadLocalAccessor in the Reactor Context.
  2. Stores the read values in the Threadlocal by using ThreadLocalAccessor. At this time, the values that previously existed in the Threadllocal are maintained in a Map called PreviousValues.
  3. When the Scope ends, it restores the PreviousValues back to the ThreadLocal.

It means that Reactor Context for writing, ThreadLocals for reading.

@chickenchickenlove
Copy link
Contributor Author

chickenchickenlove commented Apr 10, 2024

@trustin nim, There are also major differences in the test.
I would like to inform you about them.

The test utility function addCallbacks() should be change.

private static <T> Mono<T> addCallbacks(Mono<T> mono, ClientRequestContext ctx) {
        return mono.doFirst(() -> assertThat(ctxExists(ctx)).isTrue())
                   .doOnSubscribe(s -> assertThat(ctxExists(ctx)).isTrue())
                   .doOnRequest(l -> assertThat(ctxExists(ctx)).isTrue())
                   .doOnNext(foo -> assertThat(ctxExists(ctx)).isTrue())
                   .doOnSuccess(t -> assertThat(ctxExists(ctx)).isTrue())
                   .doOnEach(s -> assertThat(ctxExists(ctx)).isTrue())
                   .doOnError(t -> assertThat(ctxExists(ctx)).isTrue())
                   .doAfterTerminate(() -> assertThat(ctxExists(ctx)).isTrue())
                   // I added contextWrite(...)
                   .contextWrite(Context.of(RequestContextAccessor.getInstance().key(), ctx));
        // doOnCancel and doFinally do not have context because we cannot add a hook to the cancel.
    }

contextWrite(Context.of(RequestContextAccessor.getInstance().key(), ctx)); are added. As we know, micro-meter:context-propagation require Reactor Context to propagate context during reactor operations. thus, i added this method.

// Before : StepVerifier.create(mono1)
// After : Add initiali Reactor Context  to StepVerifier. 
StepVerifier.create(mono1, initialReactorContext(ctx))
                    .expectSubscriptionMatches(s -> ctxExists(ctx))
                    .expectNextMatches(s -> ctxExists(ctx) && "baz".equals(s))
                    .verifyComplete();

In previous test code, StepVerifier.create(mono1) is enough. however, it is not enough to micro-meter:context-propagation.
StepVerifier create DefaultVerifySubscriber to subscribe Flux|Mono and valid result is correct. however, DefaultVerifySubscriber has only empty Reactor Context. it means that .expectSubscriptionMatches(s -> ctxExists(ctx)) should be failed.

micro-meter:context-propagation is used to read the values from the Reactor Context and restore the state of ThreadLocal. however, since the DefaultStepVerifierSubscriber has an Empty Reactor Context, the RequestContext stored in ThreadLocal will become Null.

Thus, initial Reactor Context should be include to StepVerifier as well.

@chickenchickenlove chickenchickenlove changed the title Support micrometer context-propagation : Skeleton code Support micrometer context-propagation Apr 10, 2024
@trustin
Copy link
Member

trustin commented Apr 12, 2024

Could you fix the build failures before getting reviews?

@ikhoon ikhoon added new feature sprint Issues for OSS Sprint participants labels Apr 17, 2024
core/build.gradle Outdated Show resolved Hide resolved
chickenchickenlove and others added 3 commits April 24, 2024 12:25
…tContextThreadLocalAccessor.java

Co-authored-by: Trustin Lee <[email protected]>
…tContextThreadLocalAccessorTest.java

Co-authored-by: Trustin Lee <[email protected]>
Copy link
Member

@trustin trustin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@minwoox IIRC you had some comments related with request context hooks you wanted @chickenchickenlove to address. Could you leave some comment about that?

…tContextThreadLocalAccessor.java

Co-authored-by: Trustin Lee <[email protected]>
@Override
@SuppressWarnings("MustBeClosedChecker")
public void setValue(RequestContext value) {
value.push();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just use RequestContextUtil.getAndSet(value);.
value.push() internally invokes a hook but the closeable never be closed because we discard the result of value.push().

final SafeCloseable closeable = invokeHook(current);

This is a limitation of the current design of Mircometer context-proparation.
Because we cannot call the closeable, we should avoid invoking the hook altogether and inform users of the limitation of this API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@minwoox nim, thanks for your comments.
RequestContextUtil.getAndSet(value) seems not get SafeClosable instance.
I make new commit to apply your comments.
Please take another look, when you have free time 🙇‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still looks good. Thanks!

@Override
@SuppressWarnings("MustBeClosedChecker")
public void restore(RequestContext previousValue) {
previousValue.push();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto let's just use RequestContextUtil.getAndSet(previousValue);

@minwoox
Copy link
Member

minwoox commented May 8, 2024

@minwoox IIRC you had some comments related with request context hooks you wanted @chickenchickenlove to address. Could you leave some comment about that?

Thanks! I left my opinion here: #5577 (comment)

Copy link

codecov bot commented May 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.05%. Comparing base (14c5566) to head (e80117c).
Report is 69 commits behind head on main.

Current head e80117c differs from pull request most recent head 1e6cd9b

Please upload reports for the commit 1e6cd9b to get more accurate results.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #5577      +/-   ##
============================================
- Coverage     74.05%   74.05%   -0.01%     
+ Complexity    21253    21242      -11     
============================================
  Files          1850     1850              
  Lines         78600    78540      -60     
  Branches      10032    10020      -12     
============================================
- Hits          58209    58164      -45     
+ Misses        15686    15680       -6     
+ Partials       4705     4696       -9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@trustin trustin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good as a starter. Thank you so much for working on this and I appreciate your patience, @chickenchickenlove 🙇

@chickenchickenlove
Copy link
Contributor Author

Thanks trustin nim.
Thank you for taking the time to review this. 🙇‍♂️

Copy link
Contributor

@jrhee17 jrhee17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for merging once the default method implementation and style issues are addressed 👍 👍 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with the current implementation since it is analogous to the current RequestContextHooks.

Going forward, I'm thinking:

  • The current generalization allows for multiple request contexts and not just RequestContext
  • We and users are already extensively using the RequestContext#[push|pop] APIs - we probably don't want to make a breaking change on this behavior
  • Maybe RequestContextThreadLocalAccessor positioned between the RequestContext and RequestContextStorage implementation is more realistic.

e.g.

class RequestContext {
...
  RequestContext push() {
    return contextSnapshotFactory.captureAll(key -> key.equals(RequestContext.class)).get(RequestContext.class)
  }
...
}
class RequestContextThreadLocalAccessor {
...
  public RequestContext getValue() {
    return RequestContextUtil.get();
  }
...
}

This way, we could 1) keep compatibility with the current RequestContext API 2) Allow an extension point for other context objects to be passed around 3) integration with the context-propagation API can be more first-class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'm down.
Also, we can introduce Context interface to make new scope context extension easy for the future.
(For example, RequestContext is per Request, ConnectionContext is per Connection if armeria supports that features in the future. )

Copy link
Member

@trustin trustin Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarify a little bit: We don't want to use Micrometer API to push or pop the context in our core. What we're trying to achieve with the integration is to allow users to access RequestContext (or the context types from other frameworks like Reactor) using Micrometer API.

I agree that we will eventually want per-connection context, which is gonna be quite useful!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we're trying to achieve with the integration is to allow users to access RequestContext (or the context types from other frameworks like Reactor) using Micrometer API.

I see, I thought the point of integrating micrometer's context-propagation was to introduce ways for users to propagate thread local contexts other than RequestContext when using Armeria server/client.

I'm fine with just letting users access RequestContext via the Micrometer API then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature sprint Issues for OSS Sprint participants
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support micrometer-context-propagation
5 participants