yqw570994511 commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2543928498

   > Because in this 
[doc](https://skywalking.apache.org/docs/skywalking-java/next/en/setup/service-agent/java-agent/application-toolkit-webflux/#fetch-trace-context-relative-ids),
 it mentioned about fetching trace IDs, I had known they are not able to output 
in the same place of the logs. We don't need to talk about that part.
   > 
   > The question is, are all filters logs carrying logs automatically now? Or 
some fliters can, others can't. If it is later, then which filters still can't 
have the automatic injected trace contexts in their logs.
   
   Any filter that implements the GlobalFilter and GatewayFilter interfaces can 
be supported. If the user only calls the code chain.filter(exchange) in each 
filter, the traceId will be fully recorded. However, if the user writes this 
code chain.filter(exchange).then(), the code in then() will not be able to 
obtain the traceId. In this case, you need to manually call 
WebFluxSkyWalkingTraceContext.traceId(exchange) in then() to obtain it. Here is 
an example:
   
   ```
   @Component
   public class Filter1 implements GlobalFilter, Ordered {
   
       private static final Logger log = LoggerFactory.getLogger(Filter1.class);
       @Override
       public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain 
chain) {
           log.info("filter1 running");
   
           // fetch trace ID
           String traceId = TraceContext.traceId();
           log.info("filter1 traceId: {}", traceId);
   
           // fetch segment ID
           String segmentId = TraceContext.segmentId();
           log.info("filter1 segmentId: {}", segmentId);
   
           // fetch span ID
           int spanId = TraceContext.spanId();
           log.info("filter1 spanId: {}", spanId);
   
           return chain.filter(exchange).then(Mono.fromRunnable(() -> {
               log.info("filter1 then running");
   
               // fetch trace ID
               String traceId2 = TraceContext.traceId();
               log.info("filter1 then traceId: {}", traceId2);
   
               // fetch segment ID
               String segmentId2 = TraceContext.segmentId();
               log.info("filter1 then segmentId: {}", segmentId2);
   
               // fetch span ID
               int spanId2 = TraceContext.spanId();
               log.info("filter1 then spanId: {}", spanId2);
           }));
       }
       @Override
       public int getOrder() {
           return -100;
       }
   }
   
   ```
   result:
   <img width="1336" alt="image" 
src="https://github.com/user-attachments/assets/34103862-811a-4fa3-ac6a-93a645bcd3a1";
 />
   
   ```
   @Component
   public class Filter2 implements GlobalFilter, Ordered {
   
       private static final Logger log = LoggerFactory.getLogger(Filter2.class);
       @Override
       public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain 
chain) {
           log.info("filter2 running");
   
           // fetch trace ID
           String traceId = TraceContext.traceId();
           log.info("filter2 traceId: {}", traceId);
   
           // fetch segment ID
           String segmentId = TraceContext.segmentId();
           log.info("filter2 segmentId: {}", segmentId);
   
           // fetch span ID
           int spanId = TraceContext.spanId();
           log.info("filter2 spanId: {}", spanId);
           return chain.filter(exchange).then(Mono.fromRunnable(() -> {
               String traceId2 = 
WebFluxSkyWalkingTraceContext.traceId(exchange);
               log.info("filter2 then traceId: {}", traceId2);
   
               String segmentId2 = 
WebFluxSkyWalkingTraceContext.segmentId(exchange);
               log.info("filter2 then segmentId: {}", segmentId2);
   
               int spanId2 = WebFluxSkyWalkingTraceContext.spanId(exchange);
               log.info("filter2 then spanId: {}", spanId2);
           }));
       }
   
       @Override
       public int getOrder() {
           return 10;
       }
   }
   ```
   result:
   <img width="1364" alt="image" 
src="https://github.com/user-attachments/assets/587edde5-f774-40ff-9b94-c8387646e83f";
 />
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to