Skip to content

Regression: sleuth.propagation-keys aren't available in filter with WebFlux #1199

Closed
@anatoliy-balakirev

Description

@anatoliy-balakirev

Bug report:

Propagation keys are not available in the filter with WebFlux.

Spring Boot version: 2.1.2.RELEASE
Spring Cloud version: Greenwich.RELEASE. No issue with Finchley.RELEASE
Sample project: https://github.com/anatoliy-balakirev/sleuth-test

So basically if I set propagation key:

spring.sleuth.propagation-keys=x-b3-user-id

Within my first controller:

@Slf4j
@RestController
@RequiredArgsConstructor
public class DemoController {

    private final WebClient webClient;

    @GetMapping("/demo")
    public Mono<String> getMessageFromContext() {
        ExtraFieldPropagation.set(USER_ID_KEY, "123");
        log.info("Sending request to another controller. Set userId as an extra field: {}", USER_ID_KEY);

        return webClient.get().uri("/userId").retrieve().bodyToMono(String.class).doOnNext(userId -> {
            log.info("User id from another controller is: {}", userId);
        });
    }

}

And then try to access it within filter, which I register in the WebClient:

    @Bean
    public WebClient myWebClient(final WebClient.Builder webClientBuilder) {
        return webClientBuilder.baseUrl("http://localhost:8080")
            .filter((request, exchangeFunction) -> {
                final String userId = request.headers().getFirst(USER_ID_KEY);
                log.info("User id in filter is: {}", userId);
                return exchangeFunction.exchange(request);
            })
            .build();
    }

I'm getting null there. Still that key is propagated to my next controller, I can access it there and send back:

@Slf4j
@RestController
@RequiredArgsConstructor
public class UserIdController {


    @GetMapping("/userId")
    public Mono<String> getMessage() {
        return Mono.fromCallable(() -> {
            log.info("Got into userId controller");
            return ExtraFieldPropagation.get(USER_ID_KEY);
        });
    }

}

Here is the log with Greenwich.RELEASE:

2019-01-28 13:37:02.098  INFO [-,a2058a4c791cf166,a2058a4c791cf166,false] 87989 --- [ctor-http-nio-3] com.sleuth.test.demo.DemoController      : Sending request to another controller. Set userId as an extra field: x-b3-user-id
2019-01-28 13:37:02.140  INFO [-,a2058a4c791cf166,a2058a4c791cf166,false] 87989 --- [ctor-http-nio-3] com.sleuth.test.demo.DemoApplication     : User id in filter is: null
2019-01-28 13:37:02.229  INFO [-,a2058a4c791cf166,6f2a3567a3746bcb,false] 87989 --- [ctor-http-nio-4] com.sleuth.test.demo.UserIdController    : Got into userId controller
2019-01-28 13:37:02.277  INFO [-,a2058a4c791cf166,a2058a4c791cf166,false] 87989 --- [ctor-http-nio-3] com.sleuth.test.demo.DemoController      : User id from another controller is: 123

And here is with Finchley.RELEASE (you can uncomment it in the sample project's build.gradle to test):

2019-01-28 13:38:42.714  INFO [-,ea901756ef83f1bd,ea901756ef83f1bd,false] 88004 --- [ctor-http-nio-3] com.sleuth.test.demo.DemoController      : Sending request to another controller. Set userId as an extra field: x-b3-user-id
2019-01-28 13:38:42.758  INFO [-,ea901756ef83f1bd,ea901756ef83f1bd,false] 88004 --- [ctor-http-nio-3] com.sleuth.test.demo.DemoApplication     : User id in filter is: 123
2019-01-28 13:38:42.854  INFO [-,ea901756ef83f1bd,8c544927d480688e,false] 88004 --- [ctor-http-nio-4] com.sleuth.test.demo.UserIdController    : Got into userId controller
2019-01-28 13:38:42.897  INFO [-,ea901756ef83f1bd,ea901756ef83f1bd,false] 88004 --- [ctor-http-nio-3] com.sleuth.test.demo.DemoController      : User id from another controller is: 123

Regression seems to be introduced here: #1126

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions