Skip to content

Commit 7aec682

Browse files
authored
Support for tracing webflux-6.x and gateway-4.x (#661)
1 parent c5d62cb commit 7aec682

File tree

52 files changed

+2766
-6
lines changed

Some content is hidden

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

52 files changed

+2766
-6
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ jobs:
5757
case:
5858
- spring-6.x-scenario
5959
- resteasy-6.x-scenario
60+
- gateway-4.x-scenario
6061
steps:
6162
- uses: actions/checkout@v2
6263
with:

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Release Notes.
77

88
* Fix NoSuchMethodError in mvc-annotation-commons and change deprecated method.
99
* fix forkjoinpool plugin in JDK11。
10+
* Support for tracing spring-cloud-gateway 4.x in gateway-4.x-plugin.
11+
1012

1113
#### Documentation
1214

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
2121

2222
/**
23-
* This abstract class defines the <code>witnessClasses()</code> method,
24-
* and other plugin define classes need to inherit from this class
23+
* This abstract class defines the <code>witnessClasses()</code> method, and other plugin define classes need to inherit
24+
* from this class
2525
*/
2626
public abstract class AbstractGatewayV3EnhancePluginDefine extends ClassInstanceMethodsEnhancePluginDefine {
2727

2828
@Override
2929
protected String[] witnessClasses() {
30-
return new String[]{"org.springframework.cloud.gateway.config.GatewayLoadBalancerProperties"};
30+
return new String[] {
31+
"org.springframework.cloud.gateway.config.GatewayLoadBalancerProperties",
32+
"org.springframework.web.client.AsyncRestTemplate"
33+
};
3134
}
3235
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to You under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
~
18+
-->
19+
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<parent>
22+
<artifactId>optional-spring-cloud</artifactId>
23+
<groupId>org.apache.skywalking</groupId>
24+
<version>9.2.0-SNAPSHOT</version>
25+
</parent>
26+
<modelVersion>4.0.0</modelVersion>
27+
28+
<artifactId>apm-spring-cloud-gateway-4.x-plugin</artifactId>
29+
<packaging>jar</packaging>
30+
<url>http://maven.apache.org</url>
31+
32+
<properties>
33+
<spring-cloud-starter-gateway.version>4.0.0</spring-cloud-starter-gateway.version>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>org.springframework.cloud</groupId>
39+
<artifactId>spring-cloud-starter-gateway</artifactId>
40+
<version>${spring-cloud-starter-gateway.version}</version>
41+
<scope>provided</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.apache.skywalking</groupId>
45+
<artifactId>apm-spring-webflux-6.x-plugin</artifactId>
46+
<version>${project.version}</version>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-shade-plugin</artifactId>
55+
<version>3.2.4</version>
56+
<executions>
57+
<execution>
58+
<phase>package</phase>
59+
<goals>
60+
<goal>shade</goal>
61+
</goals>
62+
<configuration>
63+
<filters>
64+
<filter>
65+
<artifact>org.apache.skywalking:apm-spring-webflux-6.x-plugin</artifact>
66+
<excludes>
67+
<exclude>skywalking-plugin.def</exclude>
68+
</excludes>
69+
</filter>
70+
</filters>
71+
</configuration>
72+
</execution>
73+
</executions>
74+
</plugin>
75+
</plugins>
76+
</build>
77+
78+
</project>
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.v4x;
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 reactor.netty.http.client.HttpClientConfig} class instance which can get the
29+
* request uri string.
30+
*/
31+
public class HttpClientFinalizerConstructorInterceptor 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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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.v4x;
19+
20+
import io.netty.handler.codec.http.HttpResponseStatus;
21+
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
22+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
23+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
25+
import org.apache.skywalking.apm.plugin.spring.cloud.gateway.v4x.define.EnhanceObjectCache;
26+
import org.reactivestreams.Publisher;
27+
import reactor.core.publisher.Flux;
28+
import reactor.core.publisher.SignalType;
29+
import reactor.netty.Connection;
30+
import reactor.netty.http.client.HttpClientResponse;
31+
32+
import java.lang.reflect.Method;
33+
import java.util.function.BiFunction;
34+
35+
/**
36+
* This class intercept <code>responseConnection</code> method.
37+
* <p>
38+
* After downstream service response, finish the span in the {@link EnhanceObjectCache}.
39+
*/
40+
public class HttpClientFinalizerResponseConnectionInterceptor implements InstanceMethodsAroundInterceptor {
41+
42+
@Override
43+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
44+
MethodInterceptResult result) {
45+
BiFunction<? super HttpClientResponse, ? super Connection, ? extends Publisher> finalReceiver = (BiFunction<? super HttpClientResponse, ? super Connection, ? extends Publisher>) allArguments[0];
46+
EnhanceObjectCache cache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField();
47+
allArguments[0] = (BiFunction<HttpClientResponse, Connection, Publisher>) (response, connection) -> {
48+
Publisher publisher = finalReceiver.apply(response, connection);
49+
if (cache == null) {
50+
return publisher;
51+
}
52+
// receive the response.
53+
if (cache.getSpan() != null) {
54+
if (response.status().code() >= HttpResponseStatus.BAD_REQUEST.code()) {
55+
cache.getSpan().errorOccurred();
56+
}
57+
Tags.HTTP_RESPONSE_STATUS_CODE.set(cache.getSpan(), response.status().code());
58+
}
59+
60+
return publisher;
61+
};
62+
}
63+
64+
@Override
65+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
66+
Object ret) {
67+
Flux<?> responseFlux = (Flux<?>) ret;
68+
69+
responseFlux = responseFlux
70+
.doOnError(e -> {
71+
EnhanceObjectCache cache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField();
72+
if (cache == null) {
73+
return;
74+
}
75+
76+
if (cache.getSpan() != null) {
77+
cache.getSpan().errorOccurred();
78+
cache.getSpan().log(e);
79+
}
80+
})
81+
.doFinally(signalType -> {
82+
EnhanceObjectCache cache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField();
83+
if (cache == null) {
84+
return;
85+
}
86+
// do finally. Finish the span.
87+
if (cache.getSpan() != null) {
88+
if (signalType == SignalType.CANCEL) {
89+
cache.getSpan().errorOccurred();
90+
}
91+
cache.getSpan().asyncFinish();
92+
}
93+
94+
if (cache.getSpan1() != null) {
95+
cache.getSpan1().asyncFinish();
96+
}
97+
});
98+
99+
return responseFlux;
100+
}
101+
102+
@Override
103+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
104+
Class<?>[] argumentsTypes, Throwable t) {
105+
106+
}
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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.v4x;
19+
20+
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
21+
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
22+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
23+
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
24+
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
25+
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
26+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
27+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
28+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
29+
import org.apache.skywalking.apm.plugin.spring.cloud.gateway.v4x.define.EnhanceObjectCache;
30+
import org.apache.skywalking.apm.util.StringUtil;
31+
import org.reactivestreams.Publisher;
32+
import reactor.netty.NettyOutbound;
33+
import reactor.netty.http.client.HttpClientRequest;
34+
35+
import java.lang.reflect.Method;
36+
import java.net.URL;
37+
import java.util.function.BiFunction;
38+
39+
import static org.apache.skywalking.apm.network.trace.component.ComponentsDefine.SPRING_CLOUD_GATEWAY;
40+
41+
/**
42+
* This class intercept <code>send</code> method.
43+
* <p>
44+
* In before method, create a new BiFunction lambda expression for setting <code>ContextCarrier</code> to http header
45+
* and replace the original lambda in argument
46+
*/
47+
public class HttpClientFinalizerSendInterceptor implements InstanceMethodsAroundInterceptor {
48+
49+
@Override
50+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
51+
MethodInterceptResult result) throws Throwable {
52+
EnhanceObjectCache enhanceObjectCache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField();
53+
if (enhanceObjectCache == null) {
54+
return;
55+
}
56+
57+
/*
58+
In this plug-in, the HttpClientFinalizerSendInterceptor depends on the NettyRoutingFilterInterceptor
59+
When the NettyRoutingFilterInterceptor is not executed, the HttpClientFinalizerSendInterceptor has no meaning to be executed independently
60+
and using ContextManager.activeSpan() method would cause NPE as active span does not exist.
61+
*/
62+
if (!ContextManager.isActive()) {
63+
return;
64+
}
65+
66+
AbstractSpan span = ContextManager.activeSpan();
67+
span.prepareForAsync();
68+
69+
if (StringUtil.isNotEmpty(enhanceObjectCache.getUrl())) {
70+
URL url = new URL(enhanceObjectCache.getUrl());
71+
72+
ContextCarrier contextCarrier = new ContextCarrier();
73+
AbstractSpan abstractSpan = ContextManager.createExitSpan(
74+
"SpringCloudGateway/sendRequest", contextCarrier, getPeer(url));
75+
Tags.URL.set(abstractSpan, enhanceObjectCache.getUrl());
76+
abstractSpan.prepareForAsync();
77+
abstractSpan.setComponent(SPRING_CLOUD_GATEWAY);
78+
abstractSpan.setLayer(SpanLayer.HTTP);
79+
ContextManager.stopSpan(abstractSpan);
80+
81+
BiFunction<? super HttpClientRequest, ? super NettyOutbound, ? extends Publisher<Void>> finalSender = (BiFunction<? super HttpClientRequest, ? super NettyOutbound, ? extends Publisher<Void>>) allArguments[0];
82+
allArguments[0] = (BiFunction<HttpClientRequest, NettyOutbound, Publisher<Void>>) (request, outbound) -> {
83+
Publisher publisher = finalSender.apply(request, outbound);
84+
85+
CarrierItem next = contextCarrier.items();
86+
while (next.hasNext()) {
87+
next = next.next();
88+
request.requestHeaders().remove(next.getHeadKey());
89+
request.requestHeaders().set(next.getHeadKey(), next.getHeadValue());
90+
}
91+
return publisher;
92+
};
93+
enhanceObjectCache.setCacheSpan(abstractSpan);
94+
}
95+
ContextManager.stopSpan(span);
96+
enhanceObjectCache.setSpan1(span);
97+
}
98+
99+
private String getPeer(URL url) {
100+
return url.getHost() + ":" + url.getPort();
101+
}
102+
103+
@Override
104+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
105+
Object ret) {
106+
((EnhancedInstance) ret).setSkyWalkingDynamicField(objInst.getSkyWalkingDynamicField());
107+
return ret;
108+
}
109+
110+
@Override
111+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
112+
Class<?>[] argumentsTypes, Throwable t) {
113+
114+
}
115+
}

0 commit comments

Comments
 (0)