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

OpenTelemetry TraceContext does not propagate through reactive context properly in 3.1.6 / 3.2.0 #38593

Closed
caspianb opened this issue Nov 28, 2023 · 1 comment
Labels
for: external-project For an external project and not something we can fix status: duplicate A duplicate of another issue

Comments

@caspianb
Copy link

caspianb commented Nov 28, 2023

When using webflux in conjunction with open-telemetry with micrometer context propagation, it is expected that the trace context should be accessible throughout the controller. However, after updating to 3.1.6 (or 3.2.0) the traceId is lost within the controller at various spots.

What is even more odd, is simply adding a @RequestBody param to the controller "fixes" the issue in 3.1.6 / 3.2.0.

Run the attached demo application and using postman or similar, call the endpoints using header (or any valid traceparent syntax header):

  • traceparent = 00-11111111111111111111111111111111-2222222222222222-01

In 3.1.5, the traceId will always be available. In 3.1.6 the traceId will not be available in the below root endpoint -- but in the /body endpoint it will be available.

As far as I can tell (via downgrading micrometer, otel-bridge, netty, etc when using spring-boot 3.1.6/3.2.0) the issue does not seem to come from a lower level library.

demo.zip

@Slf4j
@SpringBootApplication
public class DemoApplication {

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

    static String getTraceId(Tracer tracer) {
        return Optional.ofNullable(tracer.currentSpan())
                .map(Span::context)
                .map(TraceContext::traceId)
                .orElse(null);
    }

    @RestController
    @RequiredArgsConstructor
    static class DemoController {
        private final Tracer tracer;

        @GetMapping
        Mono<String> getRoot() {
            // These are null in 3.1.6+
            log.info("Current traceId={}", getTraceId(tracer));
            return Mono.error(() -> {
                log.info("error supplier traceId={}", getTraceId(tracer));
                return new RuntimeException("returning error");
            });
        }

        @GetMapping("body")
        Mono<String> getWithBody(@RequestBody(required=false) String body) {
            // These calls propagate fine
            log.info("Current traceId={}", getTraceId(tracer));
            return Mono.error(() -> {
                log.info("error supplier traceId={}", getTraceId(tracer));
                return new RuntimeException("returning error");
            });
        }
    }

    @RestControllerAdvice
    @RequiredArgsConstructor
    static class ErrorHandler {
        private final Tracer tracer;

        @ExceptionHandler(Exception.class)
        ResponseEntity<String> handleError(Exception ex) {
            log.info("Encountered error: traceId={}", getTraceId(tracer));
            return ResponseEntity.ok("Error: " + ex.getMessage());
        }
    }

}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 28, 2023
@bclozel
Copy link
Member

bclozel commented Nov 28, 2023

Thanks for raising this, I believe this is a duplicate of spring-projects/spring-framework#31706 and I'm closing this issue as a result.

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Nov 28, 2023
@bclozel bclozel added status: duplicate A duplicate of another issue for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants