rewrite-spring icon indicating copy to clipboard operation
rewrite-spring copied to clipboard

SB 3.2 - Use Reactor Context-Propagation property

Open nmck257 opened this issue 1 year ago • 6 comments

What problem are you trying to solve?

Use this shiny new property: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.2-Release-Notes#properties ...as a replacement for calling Hooks.enableAutomaticContextPropagation(); before booting the app, as mentioned in this blog post: https://spring.io/blog/2023/03/30/context-propagation-with-project-reactor-3-unified-bridging-between-reactive#automatic-context-propagation

What precondition(s) should be checked before applying this recipe?

Probably UsesType for Hooks and SpringApplication

Describe the situation before applying the recipe

@SpringBootApplication
public class MyApp{

    public static void main(final String[] args) {
        Hooks.enableAutomaticContextPropagation();
        SpringApplication.run(MyApp.class, args);
    }
}

Describe the situation after applying the recipe

@SpringBootApplication
public class MyApp{

    public static void main(final String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}

...combined with a spring property addition:

spring:
  reactor:
    context-propagation: auto

Have you considered any alternatives or workarounds?

Any additional context

Are you interested in contributing this recipe to OpenRewrite?

Maybe eventually; eager contributors are welcome to grab it first

nmck257 avatar Dec 15 '23 18:12 nmck257

Seems like a good first issue indeed ; we can have a JavaIsoVisitor return null from visitMethodInvocation when Hooks.enableAutomaticContextPropagation() is found in a @SpringBootApplication main method. Then call doAfterVisit(new AddSpringProperty("spring.reactor.context-propagation", "true", null, null).getVisitor()) to add the property and done. Couple tests and we should be good. Anyone welcome to pick this up!

timtebeek avatar Dec 15 '23 20:12 timtebeek

I think it might have to be a ScanningRecipe, such that it can make the choice to adjust a property file while visiting a java file, but still feels within the realm of a first issue

nmck257 avatar Dec 15 '23 22:12 nmck257

Hello, My name is prem and I would like to work on this issue. Is it still active ?

Shiroo77 avatar Jan 26 '24 10:01 Shiroo77

Hi! Yes sure; if you open up a draft pull request with the tests I'll assign the issue to you.

timtebeek avatar Jan 26 '24 11:01 timtebeek

We need to replace the use of Hooks.enableAutomaticContextPropagation() with the spring.reactor.context-propagation property as a part of Spring Boot 3.2 ! I've been searching the entire project to locate the file or code snippet where Hooks.enableAutomaticContextPropagation() is used, but I haven't been successful so far. Could you please provide some guidance on where I should be looking or any specific classes or configurations.

Shiroo77 avatar Jan 27 '24 18:01 Shiroo77

@Shiroo77 The original issue description shows what I believe to be the most common pattern: calling the Hooks... method within main before calling SpringApplication::run. I'm not aware of any common patterns where the method would appear somewhere else; I suspect it may not work properly if applied late in the spring app lifecycle

nmck257 avatar Jan 27 '24 19:01 nmck257