Skip to content

WebfluxSofaTracerFilter should not use thread local to keep SofaTracerSpan #227

Open
@shengxiang205

Description

Describe the bug

when we using webflux with sofa tracer, we have controller code like this:

@RestController
public class SampleRestController {

    private static Logger logger = LoggerFactory.getLogger(SampleRestController.class);
    private static final String TEMPLATE = "Hello, %s!";

    private final AtomicLong counter  = new AtomicLong();

    /***
     * http://localhost:8080/springwebflux
     * @param name name
     * @return map
     */
    @RequestMapping("/springwebflux")
    public Mono<Map<String, Object>> springwebflux(@RequestParam(value = "name", defaultValue = "SOFATracer SpringWebFlux DEMO") String name) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("success", true);
        resultMap.put("id", counter.incrementAndGet());
        resultMap.put("content", String.format(TEMPLATE, name));
        logger.info("result: {}", resultMap);
        return Mono.delay(Duration.ofSeconds(3)).map(i -> resultMap);
    }
}

application.properties:

# Application Name
spring.application.name=SOFATracerSpringWebFlux
# logging path
logging.path=./logs
com.alipay.sofa.tracer.samplerPercentage=100
com.alipay.sofa.tracer.samplerName=PercentageBasedSampler
com.alipay.sofa.tracer.zipkin.base-url=http://127.0.0.1:9411
com.alipay.sofa.tracer.zipkin.enabled=true

we visit http://localhost:8080/springwebflux in concurrent mode with env -Dreactor.netty.ioWorkerCount=1

Expected behavior

logs/tracelog/spring-mvc-digest.log should have output lines for every request

Actual behavior

logs/tracelog/spring-mvc-digest.log lost some logs

Steps to reproduce

Minimal yet complete reproducer code (or GitHub URL to code)

Environment

  • SOFATracer version: 3.0.6
  • JVM version (e.g. java -version):
  • OS version (e.g. uname -a):
  • Maven version:
  • IDE version:

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions