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

WebfluxSofaTracerFilter should not use thread local to keep SofaTracerSpan #227

Open
shengxiang205 opened this issue May 29, 2019 · 1 comment
Labels
bug Something isn't working

Comments

@shengxiang205
Copy link

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:
@shengxiang205
Copy link
Author

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

@glmapper glmapper added the bug Something isn't working label May 29, 2019
@glmapper glmapper added this to the 2.4.1 milestone Jun 25, 2019
@glmapper glmapper modified the milestones: 2.4.1, 2.4.2 Aug 2, 2019
@glmapper glmapper removed this from the 2.4.2 milestone Nov 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants