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 not working correctly with Reactive Spring-Boot #4982

Closed
frommeyerc opened this issue Dec 28, 2021 · 3 comments
Closed

Opentelemetry not working correctly with Reactive Spring-Boot #4982

frommeyerc opened this issue Dec 28, 2021 · 3 comments
Labels
bug Something isn't working needs author feedback Waiting for additional feedback from the author repro provided stale

Comments

@frommeyerc
Copy link
Contributor

Describe the bug
The current implementation is not working correctly in a reactive Spring-Boot application. The provided auto-configuration does set up a somewhat working tracing instrumentation, but context propagation doesn't work. This seems to be due to the use of a ThreadLocal where this is not appropriate in a reactive application that is not bound to a single thread per request.

Steps to reproduce
I created a minimal configuration here that shows the issue.

What did you expect to see?
I expected to have a working tracing setup where nested spans carry the same traceId.

What did you see instead?
Every span gets a new traceId.

What version are you using?
The repo uses 1.7.0 but I believe nothing relevant changed meanwhile.

Environment
Compiler: AdoptOpenJDK 11.0.2
OS: Ubuntu 20.04

Additional context
There is an open discussion on this topic: #4471 (reply in thread)

@frommeyerc frommeyerc added the bug Something isn't working label Dec 28, 2021
@laurit
Copy link
Contributor

laurit commented Jan 7, 2022

@frommeyerc I believe the issue isn't really due to usage of ThreadLocal but because there is no instrumentation for reactor netty that is used as http server in your test application. When your application makes a http request it adds a traceparent header for context propagation but as far as I can tell on the receiving end nothing reads that header so context doesn't propagate. If you'd modify your application to manually restore context with something like

  @Autowired
  private OpenTelemetry openTelemetry;

  @GetMapping("{no}")
  public Mono<String> get(@PathVariable int no, @RequestHeader HttpHeaders httpHeader) {
    Context extractedContext = openTelemetry.getPropagators().getTextMapPropagator()
            .extract(Context.current(), httpHeader, new TextMapGetter<>() {
              @Override
              public Iterable<String> keys(HttpHeaders carrier) {
                return carrier.keySet();
              }

              @Override
              public String get(HttpHeaders carrier, String key) {
                return carrier.getFirst(key);
              }
            });

    try (Scope scope = extractedContext.makeCurrent()) {
      log.info("Called with {}", no);
      Mono<String> mono = no < 10 ? callDown(no + 1) : Mono.just("Hi");
      return ContextPropagationOperator.runWithContext(mono, Context.current());
    }
  }

it should behave a bit better. I changed opentelemetry version to 1.9.0, ContextPropagationOperator.runWithContext was missing in 1.7.0 that you used.
When using javaagent context propagation seems to work without any extra steps needed.

@frommeyerc
Copy link
Contributor Author

@laurit this seems to work. Movin the code into a WebFilter this even seems to work as a library. I'll do a couple more checks and if this turns out good, I'll close this one here. Thank you very much for that hint. Would there have been some fine documentation for me to read this up (and to find even more information now)?

@trask trask added the needs author feedback Waiting for additional feedback from the author label Aug 15, 2023
@github-actions
Copy link
Contributor

This has been automatically marked as stale because it has been marked as needing author feedback and has not had any activity for 7 days. It will be closed automatically if there is no response from the author within 7 additional days from this comment.

@github-actions github-actions bot added the stale label Aug 25, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs author feedback Waiting for additional feedback from the author repro provided stale
Projects
None yet
Development

No branches or pull requests

4 participants