Skip to content

Commit 3549537

Browse files
authored
[Feature] Support for tracking in spring gateway versions 4.1.2 and above (#747)
1 parent 31bf638 commit 3549537

File tree

53 files changed

+3543
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+3543
-5
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ jobs:
5353
- os: ubuntu
5454
java-version: 21
5555
steps:
56+
- if: matrix.os == 'windows'
57+
name: Support longpaths
58+
run: git config --system core.longpaths true
5659
- uses: actions/checkout@v2
5760
with:
5861
submodules: true

.github/workflows/plugins-jdk17-test.1.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ jobs:
5858
- spring-6.x-scenario
5959
- resteasy-6.x-scenario
6060
- gateway-4.x-scenario
61+
- gateway-4.1.2.x-scenario
6162
- httpexchange-scenario
6263
- activemq-artemis-2.x-scenario
6364
- c3p0-0.9.0.x-0.9.1.x-scenario

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Release Notes.
2727
field as new propagation mechanism, to better support async scenarios.
2828
* Add Caffeine plugin as optional.
2929
* Add Undertow 2.1.7.final+ worker thread pool metrics.
30+
* Support for tracking in spring gateway versions 4.1.2 and above.
3031

3132
All issues and pull requests are [here](https://github.com/apache/skywalking/milestone/222?closed=1)
3233

apm-sniffer/optional-plugins/optional-spring-plugins/optional-spring-cloud/gateway-2.0.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/cloud/gateway/v20x/define/GatewayFilterInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public String getMethodsInterceptor() {
5656

5757
@Override
5858
public boolean isOverrideArgs() {
59-
return true;
59+
return false;
6060
}
6161
}
6262
};

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/define/GatewayFilterInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public String getMethodsInterceptor() {
5656

5757
@Override
5858
public boolean isOverrideArgs() {
59-
return true;
59+
return false;
6060
}
6161
}
6262
};

apm-sniffer/optional-plugins/optional-spring-plugins/optional-spring-cloud/gateway-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/cloud/gateway/v3x/define/GatewayFilterInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public String getMethodsInterceptor() {
5959

6060
@Override
6161
public boolean isOverrideArgs() {
62-
return true;
62+
return false;
6363
}
6464
}
6565
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.skywalking.apm.plugin.spring.cloud.gateway.v412x;
19+
20+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
21+
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
22+
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
23+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
25+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
26+
import org.springframework.web.server.ServerWebExchange;
27+
import org.springframework.web.server.ServerWebExchangeDecorator;
28+
import org.springframework.web.server.adapter.DefaultServerWebExchange;
29+
30+
import java.lang.reflect.Method;
31+
import java.util.concurrent.atomic.AtomicInteger;
32+
33+
import static org.apache.skywalking.apm.network.trace.component.ComponentsDefine.SPRING_CLOUD_GATEWAY;
34+
35+
public class GatewayFilterV412Interceptor implements InstanceMethodsAroundInterceptor {
36+
37+
private static final ThreadLocal<AtomicInteger> STACK_DEEP = ThreadLocal.withInitial(() -> new AtomicInteger(0));
38+
39+
@Override
40+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
41+
MethodInterceptResult result) throws Throwable {
42+
if (isEntry()) {
43+
ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
44+
45+
EnhancedInstance enhancedInstance = getInstance(exchange);
46+
47+
AbstractSpan span = ContextManager.createLocalSpan("SpringCloudGateway/GatewayFilter");
48+
if (enhancedInstance != null && enhancedInstance.getSkyWalkingDynamicField() != null) {
49+
ContextManager.continued((ContextSnapshot) enhancedInstance.getSkyWalkingDynamicField());
50+
}
51+
span.setComponent(SPRING_CLOUD_GATEWAY);
52+
}
53+
}
54+
55+
public static EnhancedInstance getInstance(Object o) {
56+
EnhancedInstance instance = null;
57+
if (o instanceof DefaultServerWebExchange) {
58+
instance = (EnhancedInstance) o;
59+
} else if (o instanceof ServerWebExchangeDecorator) {
60+
ServerWebExchange delegate = ((ServerWebExchangeDecorator) o).getDelegate();
61+
return getInstance(delegate);
62+
}
63+
return instance;
64+
}
65+
66+
@Override
67+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
68+
Object ret) throws Throwable {
69+
if (isExit()) {
70+
if (ContextManager.isActive()) {
71+
ContextManager.stopSpan();
72+
}
73+
}
74+
return ret;
75+
}
76+
77+
@Override
78+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
79+
Class<?>[] argumentsTypes, Throwable t) {
80+
ContextManager.activeSpan().log(t);
81+
}
82+
83+
private boolean isEntry() {
84+
return STACK_DEEP.get().getAndIncrement() == 0;
85+
}
86+
87+
private boolean isExit() {
88+
boolean isExit = STACK_DEEP.get().decrementAndGet() == 0;
89+
if (isExit) {
90+
STACK_DEEP.remove();
91+
}
92+
return isExit;
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.skywalking.apm.plugin.spring.cloud.gateway.v412x;
19+
20+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
21+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
22+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
23+
import org.apache.skywalking.apm.plugin.spring.cloud.gateway.v4x.define.EnhanceObjectCache;
24+
25+
import java.lang.reflect.Method;
26+
27+
public class HttpClientConnectDuplicateV412Interceptor implements InstanceMethodsAroundInterceptor {
28+
29+
@Override
30+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
31+
32+
}
33+
34+
@Override
35+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
36+
if (objInst.getSkyWalkingDynamicField() != null) {
37+
EnhanceObjectCache enhanceObjectCache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField();
38+
if (ret instanceof EnhancedInstance) {
39+
EnhancedInstance retEnhancedInstance = (EnhancedInstance) ret;
40+
retEnhancedInstance.setSkyWalkingDynamicField(enhanceObjectCache);
41+
}
42+
}
43+
return ret;
44+
}
45+
46+
@Override
47+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
48+
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.skywalking.apm.plugin.spring.cloud.gateway.v412x;
19+
20+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
21+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
22+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
23+
import org.apache.skywalking.apm.plugin.spring.cloud.gateway.v4x.define.EnhanceObjectCache;
24+
25+
import java.lang.reflect.Method;
26+
27+
public class HttpClientConnectRequestV412Interceptor implements InstanceMethodsAroundInterceptor {
28+
29+
@Override
30+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
31+
32+
}
33+
34+
@Override
35+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
36+
if (objInst.getSkyWalkingDynamicField() != null) {
37+
if (ret instanceof EnhancedInstance) {
38+
EnhancedInstance retEnhancedInstance = (EnhancedInstance) ret;
39+
Object retSkyWalkingDynamicField = retEnhancedInstance.getSkyWalkingDynamicField();
40+
if (retSkyWalkingDynamicField != null) {
41+
EnhanceObjectCache retEnhanceObjectCache = (EnhanceObjectCache) retSkyWalkingDynamicField;
42+
EnhanceObjectCache objEnhanceObjectCache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField();
43+
retEnhanceObjectCache.setContextSnapshot(objEnhanceObjectCache.getContextSnapshot());
44+
}
45+
}
46+
}
47+
return ret;
48+
}
49+
50+
@Override
51+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
52+
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.skywalking.apm.plugin.spring.cloud.gateway.v412x;
19+
20+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
21+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
22+
import org.apache.skywalking.apm.plugin.spring.cloud.gateway.v4x.define.EnhanceObjectCache;
23+
import reactor.netty.http.client.HttpClientConfig;
24+
25+
/**
26+
* Intercept the constructor and inject {@link EnhanceObjectCache}.
27+
* <p>
28+
* The first constructor argument is {@link HttpClientConfig} class instance which can get the
29+
* request uri string.
30+
*/
31+
public class HttpClientFinalizerConstructorV412Interceptor implements InstanceConstructorInterceptor {
32+
33+
@Override
34+
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
35+
final HttpClientConfig httpClientConfig = (HttpClientConfig) allArguments[0];
36+
if (httpClientConfig == null) {
37+
return;
38+
}
39+
final EnhanceObjectCache enhanceObjectCache = new EnhanceObjectCache();
40+
enhanceObjectCache.setUrl(httpClientConfig.uri());
41+
objInst.setSkyWalkingDynamicField(enhanceObjectCache);
42+
}
43+
}

0 commit comments

Comments
 (0)