Skip to content

Commit be3d092

Browse files
authored
[Feature] Support Tracing for GlobalFilter and GatewayFilter in Gateway (#736)
1 parent 26600e7 commit be3d092

File tree

24 files changed

+2110
-39
lines changed

24 files changed

+2110
-39
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Release Notes.
1616
* Bump up gRPC to 1.68.1
1717
* Bump up netty to 4.1.115.Final
1818
* Fix the `CreateAopProxyInterceptor` in the Spring core-patch to prevent it from changing the implementation of the Spring AOP proxy
19+
* Support Tracing for GlobalFilter and GatewayFilter in Spring Gateway
1920

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

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.v20x;
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 GatewayFilterInterceptor 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+
}

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/Constants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ public interface Constants {
2929
// HttpClientRequest
3030
String INTERCEPT_CLASS_HTTP_CLIENT_REQUEST = "reactor.ipc.netty.http.client.HttpClientRequest";
3131
String HTTPCLIENT_REQUEST_HEADERS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.spring.cloud.gateway.v20x.HttpclientRequestHeadersInterceptor";
32+
33+
// GatewayFilter
34+
String INTERCEPT_CLASS_GATEWAY_FILTER = "org.springframework.cloud.gateway.filter.GatewayFilter";
35+
String GATEWAY_FILTER_INTERCEPTOR = "org.apache.skywalking.apm.plugin.spring.cloud.gateway.v20x.GatewayFilterInterceptor";
3236
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.v20x.define;
19+
20+
import net.bytebuddy.description.method.MethodDescription;
21+
import net.bytebuddy.matcher.ElementMatcher;
22+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
23+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
24+
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
25+
import org.apache.skywalking.apm.agent.core.plugin.match.HierarchyMatch;
26+
27+
import static net.bytebuddy.matcher.ElementMatchers.named;
28+
import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType;
29+
30+
public class GatewayFilterInstrumentation extends AbstractGateway200EnhancePluginDefine {
31+
32+
@Override
33+
protected ClassMatch enhanceClass() {
34+
return HierarchyMatch.byHierarchyMatch(Constants.INTERCEPT_CLASS_GATEWAY_FILTER);
35+
}
36+
37+
@Override
38+
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
39+
return new ConstructorInterceptPoint[0];
40+
}
41+
42+
@Override
43+
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
44+
return new InstanceMethodsInterceptPoint[] {
45+
new InstanceMethodsInterceptPoint() {
46+
@Override
47+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
48+
return named("filter").and(
49+
takesArgumentWithType(0, "org.springframework.web.server.ServerWebExchange"));
50+
}
51+
52+
@Override
53+
public String getMethodsInterceptor() {
54+
return Constants.GATEWAY_FILTER_INTERCEPTOR;
55+
}
56+
57+
@Override
58+
public boolean isOverrideArgs() {
59+
return true;
60+
}
61+
}
62+
};
63+
}
64+
}

apm-sniffer/optional-plugins/optional-spring-plugins/optional-spring-cloud/gateway-2.0.x-plugin/src/main/resources/skywalking-plugin.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ spring-cloud-gateway-2.0.x=org.apache.skywalking.apm.plugin.spring.cloud.gateway
1818
spring-cloud-gateway-2.0.x=org.apache.skywalking.apm.plugin.spring.cloud.gateway.v20x.define.HttpClientInstrumentation
1919
spring-cloud-gateway-2.0.x=org.apache.skywalking.apm.plugin.spring.cloud.gateway.v20x.define.HttpClientRequestInstrumentation
2020
spring-cloud-gateway-2.0.x=org.apache.skywalking.apm.plugin.spring.cloud.gateway.v20x.define.ServerWebExchangeInstrumentation
21-
spring-cloud-gateway-2.0.x=org.apache.skywalking.apm.plugin.spring.cloud.gateway.v20x.define.DispatcherHandlerInstrumentation
21+
spring-cloud-gateway-2.0.x=org.apache.skywalking.apm.plugin.spring.cloud.gateway.v20x.define.DispatcherHandlerInstrumentation
22+
spring-cloud-gateway-2.0.x=org.apache.skywalking.apm.plugin.spring.cloud.gateway.v20x.define.GatewayFilterInstrumentation

0 commit comments

Comments
 (0)