You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by "thas1024 (via GitHub)" <gi...@apache.org> on 2023/05/08 09:54:42 UTC

[GitHub] [skywalking] thas1024 opened a new issue, #10776: [Bug]

thas1024 opened a new issue, #10776:
URL: https://github.com/apache/skywalking/issues/10776

   ### Search before asking
   
   - [X] I had searched in the [issues](https://github.com/apache/skywalking/issues?q=is%3Aissue) and found no similar issues.
   
   
   ### Apache SkyWalking Component
   
   Java Agent (apache/skywalking-java)
   
   ### What happened
   
   在使用spring-mvc5和skywalking-java-agent进行链路追踪的过程中,我发现了一个问题:当springmvc中Controller的方法返回ResponseBodyEmitter类型,并通过异步方式发送响应结果时,链路追踪无法正确地传递到最终阶段。例如,以下是一个典型的Controller方法:
   
   @RequestMapping("/test")
   public ResponseBodyEmitter test() {
   
   ResponseBodyEmitter emitter = new ResponseBodyEmitter();
   // gprc invoke 已加入链路追踪
   /** 
   stub.invoke(request, new StreamObserver<String>() {
   
   @Override
   public void onNext(String value) {
   // 异步输出正确响应
   emitter.send(value);
   }
   
   @Override
   public void onError(Throwable t) {
   // 异步输出异常
   emitter.completeWithError(t);
   }
   
   @Override
   public void onComplete() {
   emitter.complete();
   }
   
   })
   **/
   return emitter;
   }
   当异步调用出现异常时,我希望通过spring的全局异常处理器来捕获异常,并将异常信息转换为JSON格式返回给前端。但是,我发现spring的全局异常处理器获取到的TraceId并不是该请求最初生成的TraceId,而是一个全新的ID。
   
   为了找出原因,我阅读了skywalking和springmvc的源码,发现了以下情况:
   
   skywalking通过org.apache.skywalking.apm.plugin.tomcat10x.TomcatInvokeInterceptor拦截了org.apache.catalina.core.StandardHostValve类的invoke方法,在方法调用前创建Span,在方法调用后结束这个Span。
   springmvc对异步响应有特殊的处理逻辑。当DispatcherServlet.dispatch方法遇到异步响应时,会直接返回,不会执行后续的逻辑。当异步响应完成时,会通过Tomcat的Async_Dispatch机制重新触发dispatch的过程,也就是StandardHostValve类的invoke方法会再次执行。因此,请求的生命周期变成了:请求开始->方法处理->请求结束->…->异步响应->请求开始->响应结果/异常处理->请求结束。
   Async_Dispatch产生的第二次请求没有携带第一次请求的链路追踪信息,因此被skywalking视为一个独立的请求,生成了一个全新的TraceID。
   根据上述分析,如果异步响应正常返回结果,则不会有问题,因为没有额外的方法调用过程,只是将结果输出到HTTP响应中。但是如果异步响应出现异常,则无法将异常处理过程加入到链路追踪中,也就无法在JSON格式的错误信息中添加请求的TraceId。
   
   
   
   ### What you expected to happen
   
   在这种异步响应的场景下,通过将链路追踪过程体现完整
   
   ### How to reproduce
   
   要解决这个问题,可能需要参考HttpClient->HttpServer的链路追踪方式,即在第一次请求中将链路追踪信息添加到Request的Header中,在Async_Dispatch时从Header中获取并传递。除了ResponseBodyEmitter外,还有DeferredResultMethodReturnValueHandler也存在类似的问题。
   
   ### Anything else
   
   _No response_
   
   ### Are you willing to submit PR?
   
   - [ ] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [X] I agree to follow this project's [Code of Conduct](https://www.apache.org/foundation/policies/conduct)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [skywalking] wu-sheng closed issue #10776: [Bug]

Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng closed issue #10776: [Bug] 
URL: https://github.com/apache/skywalking/issues/10776


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [skywalking] wu-sheng commented on issue #10776: [Bug]

Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on issue #10776:
URL: https://github.com/apache/skywalking/issues/10776#issuecomment-1538106925

   We only accept issues/PRs in English. And in a generally reading, this seems a discussion, not a bug.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org