You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by "yqw570994511 (via GitHub)" <gi...@apache.org> on 2024/12/10 13:50:50 UTC
[PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
yqw570994511 opened a new pull request, #736:
URL: https://github.com/apache/skywalking-java/pull/736
[…g-cloud-gateway-plugin #12839](https://github.com/apache/skywalking/discussions/12839)
<!--
⚠️ Please make sure to read this template first, pull requests that don't accord with this template
maybe closed without notice.
Texts surrounded by `<` and `>` are meant to be replaced by you, e.g. <framework name>, <issue number>.
Put an `x` in the `[ ]` to mark the item as CHECKED. `[x]`
-->
<!-- ==== 🐛 Remove this line WHEN AND ONLY WHEN you're fixing a bug, follow the checklist 👇 ====
### Fix <bug description or the bug issue number or bug issue link>
- [ ] Add a unit test to verify that the fix works.
- [ ] Explain briefly why the bug exists and how to fix it.
==== 🐛 Remove this line WHEN AND ONLY WHEN you're fixing a bug, follow the checklist 👆 ==== -->
<!-- ==== 🔌 Remove this line WHEN AND ONLY WHEN you're adding a new plugin, follow the checklist 👇 ====
### Add an agent plugin to support <framework name>
- [ ] Add a test case for the new plugin, refer to [the doc](https://github.com/apache/skywalking-java/blob/main/docs/en/setup/service-agent/java-agent/Plugin-test.md)
- [ ] Add a component id in [the component-libraries.yml](https://github.com/apache/skywalking/blob/master/oap-server/server-starter/src/main/resources/component-libraries.yml)
- [ ] Add a logo in [the UI repo](https://github.com/apache/skywalking-rocketbot-ui/tree/master/src/views/components/topology/assets)
==== 🔌 Remove this line WHEN AND ONLY WHEN you're adding a new plugin, follow the checklist 👆 ==== -->
<!-- ==== 📈 Remove this line WHEN AND ONLY WHEN you're improving the performance, follow the checklist 👇 ====
### Improve the performance of <class or module or ...>
- [ ] Add a benchmark for the improvement, refer to [the existing ones](https://github.com/apache/skywalking-java/blob/main/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/LinkedArrayBenchmark.java)
- [ ] The benchmark result.
```text
<Paste the benchmark results here>
```
- [ ] Links/URLs to the theory proof or discussion articles/blogs. <links/URLs here>
==== 📈 Remove this line WHEN AND ONLY WHEN you're improving the performance, follow the checklist 👆 ==== -->
<!-- ==== 🆕 Remove this line WHEN AND ONLY WHEN you're adding a new feature, follow the checklist 👇 ====
### <Feature description>
- [ ] If this is non-trivial feature, paste the links/URLs to the design doc.
- [ ] Update the documentation to include this new feature.
- [ ] Tests(including UT, IT, E2E) are added to verify the new feature.
- [ ] If it's UI related, attach the screenshots below.
==== 🆕 Remove this line WHEN AND ONLY WHEN you're adding a new feature, follow the checklist 👆 ==== -->
- [ ] If this pull request closes/resolves/fixes an existing issue, replace the issue number. Closes #<issue number>.
- [ ] Update the [`CHANGES` log](https://github.com/apache/skywalking-java/blob/main/CHANGES.md).
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2543911510
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.
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2533572414
@yqw570994511 Could you verify this new way? If it works, we don't need extra filter interceptor.
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2543909100
Could you reformat your comment? It is a little chaos from reading. I tried to edit but still not clear.
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "yqw570994511 (via GitHub)" <gi...@apache.org>.
yqw570994511 commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2543949526
> 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.
> Is there any filter not supposed?
The GlobalFilter and GatewayFilter loaded by default by the Spring Gateway will also be intercepted. Do you think this is an unnecessary filter?
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wuwen5 (via GitHub)" <gi...@apache.org>.
wuwen5 commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2532149016
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);
}
```
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "yqw570994511 (via GitHub)" <gi...@apache.org>.
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: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2543923797
Is there any filter not supposed?
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2536215534
If this targets to support custom filters, I think it makes sense.
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2537797860
I am not following. How to debug Agent and plugins are common knowledge.
There is nothing special at my side.
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2542668125
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?
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2544269443
I posted this discussion according to your comments, https://github.com/apache/skywalking/discussions/12860
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2544188027
I didn't have a concept which interceptor is necessary or unnecessary.
I just wanted to setup a feature boundaries.
It is good enough now.
Could you polish the codes by review? I think we are almost good.
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2537799001
Here is a pull request discussion. Please don't mix things here.
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "yqw570994511 (via GitHub)" <gi...@apache.org>.
yqw570994511 commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2531885662
> Please update the Spring Gateway cases, UT is not enough to verify plugins codes. There are many test scenarios [here](https://github.com/apache/skywalking-java/tree/main/test/plugin/scenarios), such as [this](https://github.com/apache/skywalking-java/tree/main/test/plugin/scenarios/gateway-3.x-scenario) is for 3.x GW.
>
> If you don't know how to run this, please follow [Plugin automatic test framework docs](https://skywalking.apache.org/docs/skywalking-java/latest/en/setup/service-agent/java-agent/plugin-test/). You could change codes of those cases, and verify them [locally](https://skywalking.apache.org/docs/skywalking-java/latest/en/setup/service-agent/java-agent/plugin-test/#local-test-and-pull-request-to-the-upstream) with new expectation files.
Sure, I'll take a look at the documentation you provided first.
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "huicunjun (via GitHub)" <gi...@apache.org>.
huicunjun commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2537796645
> Why ping me?
The tid of the log printed in the spring gateway interceptor is inconsistent with the tid of the log printed in the downstream service interceptor after forwarding. Is there any way to troubleshoot?
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "yqw570994511 (via GitHub)" <gi...@apache.org>.
yqw570994511 commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2543912442
> 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:

--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "yqw570994511 (via GitHub)" <gi...@apache.org>.
yqw570994511 commented on code in PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#discussion_r1886107389
##########
apm-sniffer/optional-plugins/optional-spring-plugins/optional-spring-cloud/gateway-2.1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/cloud/gateway/v21x/GatewayFilterInterceptor.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x;
+
+import org.apache.skywalking.apm.agent.core.context.ContextManager;
+import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
+import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
+import org.springframework.web.server.ServerWebExchange;
+import org.springframework.web.server.ServerWebExchangeDecorator;
+import org.springframework.web.server.adapter.DefaultServerWebExchange;
+
+import java.lang.reflect.Method;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.apache.skywalking.apm.network.trace.component.ComponentsDefine.SPRING_CLOUD_GATEWAY;
+
+public class GatewayFilterInterceptor implements InstanceMethodsAroundInterceptor {
+
+ private static final ThreadLocal<AtomicInteger> STACK_DEEP = ThreadLocal.withInitial(() -> new AtomicInteger(0));
+
+ @Override
+ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
+ MethodInterceptResult result) throws Throwable {
+ STACK_DEEP.get().getAndIncrement();
+ if (isEntry()) {
Review Comment:
> @yqw570994511 Could you check this and polish?
Thank you, I will adjust my code as follows
```
public class GatewayFilterInterceptor implements InstanceMethodsAroundInterceptor {
private static final ThreadLocal<AtomicInteger> STACK_DEEP = ThreadLocal.withInitial(() -> new AtomicInteger(0));
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
if (isEntry()) {
ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
EnhancedInstance enhancedInstance = getInstance(exchange);
AbstractSpan span = ContextManager.createLocalSpan("SpringCloudGateway/GatewayFilter");
if (enhancedInstance != null && enhancedInstance.getSkyWalkingDynamicField() != null) {
ContextManager.continued((ContextSnapshot) enhancedInstance.getSkyWalkingDynamicField());
}
span.setComponent(SPRING_CLOUD_GATEWAY);
}
}
public static EnhancedInstance getInstance(Object o) {
EnhancedInstance instance = null;
if (o instanceof DefaultServerWebExchange) {
instance = (EnhancedInstance) o;
} else if (o instanceof ServerWebExchangeDecorator) {
ServerWebExchange delegate = ((ServerWebExchangeDecorator) o).getDelegate();
return getInstance(delegate);
}
return instance;
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
if (isExit()) {
if (ContextManager.isActive()) {
ContextManager.stopSpan();
}
}
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().log(t);
}
private boolean isEntry() {
return STACK_DEEP.get().getAndIncrement() == 0;
}
private boolean isExit() {
boolean isExit = STACK_DEEP.get().decrementAndGet() == 0;
if (isExit) {
STACK_DEEP.remove();
}
return isExit;
}
}
```
If you think it is feasible, can I submit the code accordingly?
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2531730618
Please update the Spring Gateway cases, UT is not enough to verify plugins codes. There are many test scenarios [here](https://github.com/apache/skywalking-java/tree/main/test/plugin/scenarios), such as [this](https://github.com/apache/skywalking-java/tree/main/test/plugin/scenarios/gateway-3.x-scenario) is for 3.x GW.
If you don't know how to run this, please follow [Plugin automatic test framework docs](https://skywalking.apache.org/docs/skywalking-java/latest/en/setup/service-agent/java-agent/plugin-test/). You could change codes of those cases, and verify them [locally](https://skywalking.apache.org/docs/skywalking-java/latest/en/setup/service-agent/java-agent/plugin-test/#local-test-and-pull-request-to-the-upstream) with new expectation files.
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2531819371
You can see, your new codes break [gateway-3.x-filter-context-scenario](https://github.com/apache/skywalking-java/actions/runs/12257583733/job/34196578548?pr=736#logs). That is another thing you should fix about the test scenarios.
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng merged PR #736:
URL: https://github.com/apache/skywalking-java/pull/736
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on code in PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#discussion_r1886108579
##########
apm-sniffer/optional-plugins/optional-spring-plugins/optional-spring-cloud/gateway-2.1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/cloud/gateway/v21x/GatewayFilterInterceptor.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x;
+
+import org.apache.skywalking.apm.agent.core.context.ContextManager;
+import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
+import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
+import org.springframework.web.server.ServerWebExchange;
+import org.springframework.web.server.ServerWebExchangeDecorator;
+import org.springframework.web.server.adapter.DefaultServerWebExchange;
+
+import java.lang.reflect.Method;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.apache.skywalking.apm.network.trace.component.ComponentsDefine.SPRING_CLOUD_GATEWAY;
+
+public class GatewayFilterInterceptor implements InstanceMethodsAroundInterceptor {
+
+ private static final ThreadLocal<AtomicInteger> STACK_DEEP = ThreadLocal.withInitial(() -> new AtomicInteger(0));
+
+ @Override
+ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
+ MethodInterceptResult result) throws Throwable {
+ STACK_DEEP.get().getAndIncrement();
+ if (isEntry()) {
Review Comment:
Yes, please go ahead
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "hyhyf (via GitHub)" <gi...@apache.org>.
hyhyf commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2563634906
> > Why ping me?
>
> The tid of the log printed in the spring gateway interceptor is inconsistent with the tid of the log printed in the downstream service interceptor after forwarding. Is there any way to troubleshoot?
Maybe you need to add spring-cloud-gateway plugin to your projcet.
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on code in PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#discussion_r1884735909
##########
apm-sniffer/optional-plugins/optional-spring-plugins/optional-spring-cloud/gateway-2.1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/cloud/gateway/v21x/GatewayFilterInterceptor.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x;
+
+import org.apache.skywalking.apm.agent.core.context.ContextManager;
+import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
+import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
+import org.springframework.web.server.ServerWebExchange;
+import org.springframework.web.server.ServerWebExchangeDecorator;
+import org.springframework.web.server.adapter.DefaultServerWebExchange;
+
+import java.lang.reflect.Method;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.apache.skywalking.apm.network.trace.component.ComponentsDefine.SPRING_CLOUD_GATEWAY;
+
+public class GatewayFilterInterceptor implements InstanceMethodsAroundInterceptor {
+
+ private static final ThreadLocal<AtomicInteger> STACK_DEEP = ThreadLocal.withInitial(() -> new AtomicInteger(0));
+
+ @Override
+ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
+ MethodInterceptResult result) throws Throwable {
+ STACK_DEEP.get().getAndIncrement();
+ if (isEntry()) {
Review Comment:
I think you have some duplicate executions here and same as `isExit`.
`STACK_DEEP.get().getAndIncrement()` will return 0 for the first time(as Entry), and `STACK_DEEP.get().decrementAndGet()` will return 0 too for the last time(as Exit).
Your codes call `getThreadLocal` and `getIntValue` twice. You could polish codes about this.
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "yqw570994511 (via GitHub)" <gi...@apache.org>.
yqw570994511 commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2533630006
> @yqw570994511 Could you verify this new way? If it works, we don't need extra filter interceptor.
Thank you, I'll try this way
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "yqw570994511 (via GitHub)" <gi...@apache.org>.
yqw570994511 commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2533630404
> 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);
> }
> ```
Thank you, I'll try this way
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "huicunjun (via GitHub)" <gi...@apache.org>.
huicunjun commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2537775801
My situation is similar to yours, the issue of logback not outputting tid can be resolved through
WebFluxSkyWalkingOperators.continueTracing(serverWebExchange, callable);
Solution to the interceptor code for the package gateway
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "huicunjun (via GitHub)" <gi...@apache.org>.
huicunjun commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2537777397
The problem I found is that when the gateway forwards to downstream web services, the traceId changes and the entire process is not linked together
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "yqw570994511 (via GitHub)" <gi...@apache.org>.
yqw570994511 commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2536090005
> 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:
<img width="818" alt="image" src="https://github.com/user-attachments/assets/9813626b-f864-4a2f-8d19-5e31020c810b" />
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:
<img width="743" alt="image" src="https://github.com/user-attachments/assets/67060889-6f1c-4a77-97ab-48329e76773a" />
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.
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2537782151
Why ping me?
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2532121274
@wuwen5 Could you take a look at this?
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on code in PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#discussion_r1886075859
##########
apm-sniffer/optional-plugins/optional-spring-plugins/optional-spring-cloud/gateway-2.1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/cloud/gateway/v21x/GatewayFilterInterceptor.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x;
+
+import org.apache.skywalking.apm.agent.core.context.ContextManager;
+import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
+import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
+import org.springframework.web.server.ServerWebExchange;
+import org.springframework.web.server.ServerWebExchangeDecorator;
+import org.springframework.web.server.adapter.DefaultServerWebExchange;
+
+import java.lang.reflect.Method;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.apache.skywalking.apm.network.trace.component.ComponentsDefine.SPRING_CLOUD_GATEWAY;
+
+public class GatewayFilterInterceptor implements InstanceMethodsAroundInterceptor {
+
+ private static final ThreadLocal<AtomicInteger> STACK_DEEP = ThreadLocal.withInitial(() -> new AtomicInteger(0));
+
+ @Override
+ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
+ MethodInterceptResult result) throws Throwable {
+ STACK_DEEP.get().getAndIncrement();
+ if (isEntry()) {
Review Comment:
@yqw570994511 Could you check this and polish?
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2543910544
The reason I am asking about, is not doubting your PR. The key is, I want to set up a good boundary for community users.
How much does this PR resolve the issues about the manual codes required? In which cases, they still need to consider adding those codes.
Could you focus on that?
--
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
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "yqw570994511 (via GitHub)" <gi...@apache.org>.
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: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway [skywalking-java]
Posted by "yqw570994511 (via GitHub)" <gi...@apache.org>.
yqw570994511 commented on PR #736:
URL: https://github.com/apache/skywalking-java/pull/736#issuecomment-2544246940
> I didn't have a concept which interceptor is necessary or unnecessary. I just wanted to setup a feature boundaries. It is good enough now.
>
> Could you polish the codes by review? I think we are almost good.
Thank you, I will adjust my code as follows
```
public class GatewayFilterInterceptor implements InstanceMethodsAroundInterceptor {
private static final ThreadLocal<AtomicInteger> STACK_DEEP = ThreadLocal.withInitial(() -> new AtomicInteger(0));
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
if (isEntry()) {
ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
EnhancedInstance enhancedInstance = getInstance(exchange);
AbstractSpan span = ContextManager.createLocalSpan("SpringCloudGateway/GatewayFilter");
if (enhancedInstance != null && enhancedInstance.getSkyWalkingDynamicField() != null) {
ContextManager.continued((ContextSnapshot) enhancedInstance.getSkyWalkingDynamicField());
}
span.setComponent(SPRING_CLOUD_GATEWAY);
}
}
public static EnhancedInstance getInstance(Object o) {
EnhancedInstance instance = null;
if (o instanceof DefaultServerWebExchange) {
instance = (EnhancedInstance) o;
} else if (o instanceof ServerWebExchangeDecorator) {
ServerWebExchange delegate = ((ServerWebExchangeDecorator) o).getDelegate();
return getInstance(delegate);
}
return instance;
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
if (isExit()) {
if (ContextManager.isActive()) {
ContextManager.stopSpan();
}
}
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().log(t);
}
private boolean isEntry() {
return STACK_DEEP.get().getAndIncrement() == 0;
}
private boolean isExit() {
boolean isExit = STACK_DEEP.get().decrementAndGet() == 0;
if (isExit) {
STACK_DEEP.remove();
}
return isExit;
}
}
```
--
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