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

Merged
merged 52 commits into from
Nov 8, 2024

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
@chickenchickenlove
Copy link
Contributor Author

Hi @ikhoon nim, orry to bother you.
When you have time, could you take a look this PR?
Thanks in advanced 🙇‍♂️

@github-actions github-actions bot removed the Stale label Aug 15, 2024
@github-actions github-actions bot added the Stale label Sep 15, 2024
@chickenchickenlove
Copy link
Contributor Author

It has been a long time since the last review.
Please let me know if there is any additional work I should to do. 🙇‍♂️

@minwoox
Copy link
Member

minwoox commented Oct 24, 2024

Hi! @chickenchickenlove. Apologies for the late reply.

The current implementation has a limitation—it doesn’t support the context hook due to the design of the Micrometer context propagation API. Specifically, the setValue() method doesn’t allow returning a Closeable instance. Because of this limitation, we cannot place these classes in the core module.

Before we can address this issue, we need to discuss potential solutions with the Micrometer maintainers. In the meantime, we thought it would be a good idea to move these classes to a separate module, like micrometer-context, so users who don’t need the context hooks can still use them.

Would you mind creating another module for this?

@chickenchickenlove
Copy link
Contributor Author

@minwoox -nim, thanks for your comments 🙇‍♂️
Would it work if I do it this way?

  1. Create new module micrometer-context by usning gradle.
  2. Move these codes from core module to micrometeter-contenxt.

@minwoox
Copy link
Member

minwoox commented Oct 25, 2024

Would it work if I do it this way?
Create new module micrometer-context by usning gradle.
Move these codes from core module to micrometeter-contenxt.

Yeah, please go ahead. 😉

@github-actions github-actions bot removed the Stale label Oct 26, 2024
@CLAassistant
Copy link

CLAassistant commented Oct 26, 2024

CLA assistant check
All committers have signed the CLA.

@chickenchickenlove
Copy link
Contributor Author

@minwoox -nim, Thanks for your comments!
I made a new commit to apply your comments.
When you have time, please take another look. 🙇‍♂️

@@ -1,3 +1,5 @@
dependencies {
api libs.reactor.core
implementation libs.context.propagation
implementation project(':micrometer-context')
Copy link
Member

Choose a reason for hiding this comment

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

Would you exclude this micrometer-context module from reactor3?
micrometer-context has a limitation which isn't supporting the context hook so we cannot include it in reactor3 module by default.

Here's the suggestion:

// reactor3/build.gradle
dependencies {
    api libs.reactor.core
}
// micrometer-context/build.gradle
dependencies {
    implementation libs.context.propagation
    testImplementation project(':reactor3')
}

and move all test classes into micrometer-context module.

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 , thanks for your comments!
I fixed it!
Please take another look. 🙇‍♂️

@minwoox minwoox added this to the 1.31.0 milestone Oct 29, 2024
Copy link
Member

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

Looks great. 👍
Thanks a lot for your hard work. 🙇‍♂️

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.

👍 👍

chickenchickenlove and others added 3 commits November 5, 2024 10:59
…icrometer/context/package-info.java

Co-authored-by: jrhee17 <[email protected]>
…icrometer/context/RequestContextThreadLocalAccessor.java

Co-authored-by: jrhee17 <[email protected]>
Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

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

🙇‍♂️🙇‍♂️

settings.gradle Outdated Show resolved Hide resolved
@ikhoon
Copy link
Contributor

ikhoon commented Nov 8, 2024

It is okay to merge this PR when CI builds pass.

@ikhoon ikhoon merged commit 0019bc7 into line:main Nov 8, 2024
13 of 14 checks passed
@ikhoon ikhoon modified the milestones: 1.31.0, 1.32.0 Nov 11, 2024
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
6 participants