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

   I tried it according to the method you gave, and it achieved the same effect 
as my previous code
   `
   @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) {
           // fetch trace ID
           String traceId = WebFluxSkyWalkingTraceContext.traceId(exchange);
           // fetch segment ID
           String segmentId = WebFluxSkyWalkingTraceContext.segmentId(exchange);
           // fetch span ID
           int spanId = WebFluxSkyWalkingTraceContext.spanId(exchange);
           log.info("filter1 traceId: {}, segmentId: {}, spanId: {}", traceId, 
segmentId, spanId);
           return chain.filter(exchange);
       }
       @Override
       public int getOrder() {
           return -100;
       }
   }
   `
   
   I got the following result
   
   <img width="1370" alt="image" 
src="https://github.com/user-attachments/assets/5876fb36-f436-4b29-80c9-f31d24144561";
 />
   
   But when I am in another situation, it can't meet my needs. My project is 
configured with logback to output skywalking's traceId to help me track 
problems, but using the above method, I can see that skywalking's traceId is 
not output. In the production environment, I really need my log to print out 
the traceId, and I also hope that I can achieve this goal by writing business 
code as much as possible. For example, I hope my filter code can output the 
traceId in the log like this:
   
   `
   @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");
           return chain.filter(exchange);
       }
       @Override
       public int getOrder() {
           return -100;
       }
   }
   `
   
   The result of using the new solution is as follows:
   <img width="1420" alt="image" 
src="https://github.com/user-attachments/assets/208ce4f1-997b-4207-947a-b383b7b76421";
 />
   
   I think the new scheme can support more scenarios and should help many 
people like me who have similar needs
   
   > I think the issue seems to be repetitive with this #539 , and we can 
support it in this way.
   > 
   > 
https://github.com/apache/skywalking-java/blob/main/docs/en/setup/service-agent/java-agent/Application-toolkit-webflux.md#fetch-trace-context-relative-ids
   > 
   > ```
   > @Override
   >     public Mono<Void> filter(ServerWebExchange exchange, 
GatewayFilterChain chain){
   >         // fetch trace ID
   >         String traceId = WebFluxSkyWalkingTraceContext.traceId(exchange);
   >         
   >         // fetch segment ID
   >         String segmentId = 
WebFluxSkyWalkingTraceContext.segmentId(exchange);
   >         
   >         // fetch span ID
   >         int spanId = WebFluxSkyWalkingTraceContext.spanId(exchange);
   >         
   >         return chain.filter(exchange);
   >     }
   > ```
   
   
   
   > I think the issue seems to be repetitive with this #539 , and we can 
support it in this way.
   > 
   > 
https://github.com/apache/skywalking-java/blob/main/docs/en/setup/service-agent/java-agent/Application-toolkit-webflux.md#fetch-trace-context-relative-ids
   > 
   > ```
   > @Override
   >     public Mono<Void> filter(ServerWebExchange exchange, 
GatewayFilterChain chain){
   >         // fetch trace ID
   >         String traceId = WebFluxSkyWalkingTraceContext.traceId(exchange);
   >         
   >         // fetch segment ID
   >         String segmentId = 
WebFluxSkyWalkingTraceContext.segmentId(exchange);
   >         
   >         // fetch span ID
   >         int spanId = WebFluxSkyWalkingTraceContext.spanId(exchange);
   >         
   >         return chain.filter(exchange);
   >     }
   > ```
   
   I tried it according to the method you gave, and it achieved the same effect 
as my previous code
   `@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) {
           // fetch trace ID
           String traceId = WebFluxSkyWalkingTraceContext.traceId(exchange);
           // fetch segment ID
           String segmentId = WebFluxSkyWalkingTraceContext.segmentId(exchange);
           // fetch span ID
           int spanId = WebFluxSkyWalkingTraceContext.spanId(exchange);
           log.info("filter1 traceId: {}, segmentId: {}, spanId: {}", traceId, 
segmentId, spanId);
           return chain.filter(exchange);
       }
       @Override
       public int getOrder() {
           return -100;
       }
   }`
   
   I got the following result
   
   <img width="1370" alt="image" 
src="https://github.com/user-attachments/assets/5876fb36-f436-4b29-80c9-f31d24144561";
 />
   
   But when I am in another situation, it can't meet my needs. My project is 
configured with logback to output skywalking's traceId to help me track 
problems, but using the above method, I can see that skywalking's traceId is 
not output. In the production environment, I really need my log to print out 
the traceId, and I also hope that I can achieve this goal by writing business 
code as much as possible. For example, I hope my filter code can output the 
traceId in the log like this:
   
   `@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");
           return chain.filter(exchange);
       }
       @Override
       public int getOrder() {
           return -100;
       }
   }`
   
   The result of using the new solution is as follows:
   <img width="1420" alt="image" 
src="https://github.com/user-attachments/assets/208ce4f1-997b-4207-947a-b383b7b76421";
 />
   
   I think the new scheme can support more scenarios and should help many 
people like me who have similar needs
   
   > It is good to see tests are passed. Thanks.
   > 
   > One question about this 
[doc](https://skywalking.apache.org/docs/skywalking-java/next/en/setup/service-agent/java-agent/application-toolkit-webflux/#fetch-trace-context-relative-ids),
 with your new plugin gets merged, does user still need to add manual codes? 
Which scenarios are automatically working, which are still not?
   
   Writing code similar to this is enough:
   `
   @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);
       }
       @Override
       public int getOrder() {
           return -100;
       }
   }
   `
   `
   @Component
   public class GatewayFilter1 implements GatewayFilter {
   
       private static final Logger log = 
LoggerFactory.getLogger(GatewayFilter1.class);
       @Override
       public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain 
chain) {
           log.info("gatewayFilter1 running");
           return chain.filter(exchange);
       }
   }
   `
   The results obtained are as follows:
   <img width="1427" alt="image" 
src="https://github.com/user-attachments/assets/a1718d55-b008-4543-99d8-d5f5c52108c6";
 />
   


-- 
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