sofastack / sofa-tracer

SOFATracer is a component for the distributed system call trace. And through a unified traceId logging the logs of various network calls in the invoking link. These logs can be used for quick discovery of faults, service governance, etc.
Apache License 2.0
1.11k stars 375 forks source link

WebfluxSofaTracerFilter should not use thread local to keep SofaTracerSpan #227

Open shengxiang205 opened 5 years ago

shengxiang205 commented 5 years ago

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

shengxiang205 commented 5 years ago

code in WebfluxSofaTracrFilter.java using code:

SofaTracerSpan springMvcSpan = springMvcTracer.serverReceive(spanContext);

to create first SofaTracerSpan,

but serverReceive in AbstractTracer using

SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan serverSpan = sofaTraceContext.pop();

to keep SofaTracerSpan in ThreadLocal

as we known, in concurrent mode, multi reactor stream may share same thread, it will lead to unknown bug with thread local variables