question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Regression in 3.0.2: tracer context lost when used with spring-data-mongodb

See original GitHub issue

Describe the bug

Hi, I think there’s a regression introduced in 3.0.2. Given an endpoint like:

import org.springframework.cloud.sleuth.Tracer

@GetMapping("chain-with-mongo")
fun chainMongo(): Mono<SpanWrapper> {
    val result = SpanWrapper()
    return tracer.spanId()
           .map { result.add(it) }
           .flatMap { repo.insert(Person(it)) }
           .flatMap { tracer.spanId() } // <-- spanId is empty here
           .map { result.add(it) }
           .map { result }
}

fun Tracer.spanId() = currentSpan()?.context()?.spanId().orEmpty().toMono()

After the reactive repository, traceId or spanId are empty.
This happens from 3.0.2. Previous versions (3.0.1 & 3.0.0) are fine.
Other versions:

  • Spring Cloud 2020.0.2
  • Spring Boot 2.4.4

If I were to guess where this might come from I would point at the recent optimizations in Reactor operator decoration.

Sample

You can find a reproducer here. You can sample the issue by executing ./gradlew test

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
marcingrzejszczakcommented, May 11, 2021

BTW - if you bump mongodb to version <mongodb.version>4.2.3</mongodb.version> then things work out of the box without any changes! That’s beacuse Mongo started using project reactor under the hood. I can close this issue then since the recommended approach is to use version 4.2 of the mongodb driver

1reaction
marcingrzejszczakcommented, May 11, 2021

Hi! The fact that it used to work had to be accidental. We have done a big analysis with @mp911de and we need to talk to Mongo folks about providing hook points to allow the reactive context propagation. Until now as a workaround you have to IMO use the WebFluxSleuthOperators and the like:


@RestController
@RequestMapping("/spans")
public class TestController {

    private static final Logger log = LoggerFactory.getLogger(TestController.class);
    
    private final Tracer tracer;
    private final CurrentTraceContext currentTraceContext;
    private final PersonRepository repo;

    public TestController(Tracer tracer, CurrentTraceContext currentTraceContext, PersonRepository repo) {
        this.tracer = tracer;
        this.currentTraceContext = currentTraceContext;
        this.repo = repo;
    }
    
    @GetMapping("chain-with-mongo")
    Mono<SpanWrapper> chainMongo(ServerWebExchange serverWebExchange) {
        SpanWrapper result = new SpanWrapper().setSpans(new ArrayList<>());
        return spanId(tracer)
                .map(result::add)
                .doOnNext(s -> log.info("ASD1: [{}]", s))
                .flatMap(s -> repo.insert(new Person(s)))
// log stuff with tracing context
                .doOnNext(person -> WebFluxSleuthOperators.withSpanInScope(tracer, currentTraceContext, serverWebExchange, () -> log.info("ASD 2")))
// retrieve the root span and put it in thread local (the scope should be cleared by the TraceFilter later on)
                .doOnNext(person -> tracer.withSpan(serverWebExchange.getAttribute(Span.class.getName())))
                .flatMap(p -> spanId(tracer)) // <-- spanId is empty here
                .map(result::add)
                .map(it -> result);
    }
}

I’ll mark this as in progress cause we can’t do much about it ATM.

Read more comments on GitHub >

github_iconTop Results From Across the Web

I have upgraded sping-data-mongodb version 3.3.2 and now ...
MongoCollection collection = mongoTemplate.getCollection(“testdb”); i have upgraded sping-data-mongodb version 3.3.2 and now getting errors.
Read more >
Trace context lost after mongo call #1204 - GitHub
Hi, using latest Spring versions (2.1.2.RELEASE and Cloud Greenwich.RELEASE) it looks like after calling mongo with reactive operators trace ...
Read more >
Spring Data MongoDB - Reference Documentation
The Spring Data MongoDB project applies core Spring concepts to the development of solutions that use the MongoDB document style data store.
Read more >
Release Notes Red Hat Fuse 7.0 | Red Hat Customer Portal
Welcome to the Fuse 7.0 GA release! This release continues the evolution of Red Hat Fuse with major component upgrades and expanded range...
Read more >
Bug listing with status CONFIRMED as at 2022/12/14 03:46:34
splunk - the search engine for IT data" status:CONFIRMED resolution: severity:enhancement · Bug:128982 - "sys-process/cryopid process freezer (new ebuild)" ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found