Skip to content

Commit d037668

Browse files
author
yuqianwei
committed
[Feature] Support for tracking in spring gateway versions 4.1.2 and above #12925
1 parent b358267 commit d037668

File tree

48 files changed

+3536
-1
lines changed

Some content is hidden

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

48 files changed

+3536
-1
lines changed

.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.2x-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

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+
}
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.v412x;
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 HttpClientFinalizerResponseConnectionV412Interceptor 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+
}

0 commit comments

Comments
 (0)