Skip to content

Commit c9a6170

Browse files
darknesstmdarknesstm
andauthored
Support access the tracer context in spring gateway filter (#539)
Co-authored-by: darknesstm <askkoy@163.com>
1 parent 6229b22 commit c9a6170

35 files changed

Lines changed: 1579 additions & 22 deletions

File tree

.github/workflows/plugins-test.3.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ jobs:
9090
- dbcp-2.x-scenario
9191
- jsonrpc4j-1.x-scenario
9292
- gateway-3.x-scenario
93+
- gateway-3.x-filter-context-scenario
9394
- neo4j-4.x-scenario
9495
- oracle-scenario
9596
- druid-1.x-scenario

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ Callable {
133133

134134
* Support Jdk17 ZGC metric collect
135135
* Support Jetty 11.x plugin
136+
* Support access the sky-walking tracer context in spring gateway filter
136137
* Fix the scenario of using the HBase plugin with spring-data-hadoop.
137138
* Add RocketMQ 5.x plugin
138139
* Fix the conflict between the logging kernel and the JDK threadpool plugin.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
19+
package org.apache.skywalking.apm.toolkit.webflux;
20+
21+
import org.springframework.web.server.ServerWebExchange;
22+
23+
import java.util.Optional;
24+
25+
/**
26+
* TraceContext for WebFlux.
27+
*/
28+
public class WebFluxSkyWalkingTraceContext {
29+
/**
30+
* Try to get the traceId of current trace context.
31+
*
32+
* @param serverWebExchange - EnhancedInstance that contains the tracing context
33+
* @return traceId, if it exists, or empty {@link String}.
34+
*/
35+
public static String traceId(ServerWebExchange serverWebExchange) {
36+
return "";
37+
}
38+
39+
/**
40+
* Try to get the segmentId of current trace context.
41+
*
42+
* @param serverWebExchange - EnhancedInstance that contains the tracing context
43+
* @return segmentId, if it exists, or empty {@link String}.
44+
*/
45+
public static String segmentId(ServerWebExchange serverWebExchange) {
46+
return "";
47+
}
48+
49+
/**
50+
* Try to get the spanId of current trace context. The spanId is a negative number when the trace context is
51+
* missing.
52+
*
53+
* @param serverWebExchange - EnhancedInstance that contains the tracing context
54+
* @return spanId, if it exists, or empty {@link String}.
55+
*/
56+
public static int spanId(ServerWebExchange serverWebExchange) {
57+
return -1;
58+
}
59+
60+
/**
61+
* Try to get the custom value from trace context.
62+
*
63+
* @param serverWebExchange - EnhancedInstance that contains the tracing context
64+
* @return custom data value.
65+
*/
66+
public static Optional<String> getCorrelation(ServerWebExchange serverWebExchange, String key) {
67+
return Optional.empty();
68+
}
69+
70+
/**
71+
* Put the custom key/value into trace context.
72+
*
73+
* @param serverWebExchange - EnhancedInstance that contains the tracing context
74+
* @return previous value if it exists.
75+
*/
76+
public static Optional<String> putCorrelation(ServerWebExchange serverWebExchange, String key, String value) {
77+
return Optional.empty();
78+
}
79+
}

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/CorrelationContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
import java.util.ArrayList;
2121
import java.util.Arrays;
22-
import java.util.HashMap;
2322
import java.util.List;
2423
import java.util.Map;
2524
import java.util.Objects;
2625
import java.util.Optional;
26+
import java.util.concurrent.ConcurrentHashMap;
2727
import java.util.stream.Collectors;
2828
import org.apache.skywalking.apm.agent.core.base64.Base64;
2929
import org.apache.skywalking.apm.agent.core.conf.Config;
@@ -49,7 +49,7 @@ public class CorrelationContext {
4949
}
5050

5151
public CorrelationContext() {
52-
this.data = new HashMap<>(Config.Correlation.ELEMENT_MAX_NUMBER);
52+
this.data = new ConcurrentHashMap<>(Config.Correlation.ELEMENT_MAX_NUMBER);
5353
}
5454

5555
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.toolkit.activation.webflux;
19+
20+
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
21+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
22+
23+
import java.lang.reflect.Method;
24+
import java.util.Optional;
25+
26+
public class WebFluxSkyWalkingCorrelationContextGetInterceptor extends WebFluxSkyWalkingStaticMethodsAroundInterceptor {
27+
28+
@Override
29+
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, MethodInterceptResult result) {
30+
final ContextSnapshot contextSnapshot = getContextSnapshot(allArguments[0]);
31+
if (contextSnapshot == null || contextSnapshot.getCorrelationContext() == null) {
32+
return;
33+
}
34+
35+
final String key = (String) allArguments[1];
36+
final Optional<String> data = contextSnapshot.getCorrelationContext().get(key);
37+
38+
result.defineReturnValue(data);
39+
}
40+
41+
@Override
42+
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Object ret) {
43+
return ret;
44+
}
45+
46+
@Override
47+
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Throwable t) {
48+
}
49+
}
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.toolkit.activation.webflux;
19+
20+
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
21+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
22+
23+
import java.lang.reflect.Method;
24+
import java.util.Optional;
25+
26+
public class WebFluxSkyWalkingCorrelationContextPutInterceptor extends WebFluxSkyWalkingStaticMethodsAroundInterceptor {
27+
28+
@Override
29+
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, MethodInterceptResult result) {
30+
final ContextSnapshot contextSnapshot = getContextSnapshot(allArguments[0]);
31+
if (contextSnapshot == null || contextSnapshot.getCorrelationContext() == null) {
32+
return;
33+
}
34+
35+
final String key = (String) allArguments[1];
36+
final String value = (String) allArguments[2];
37+
final Optional<String> previous = contextSnapshot.getCorrelationContext().put(key, value);
38+
39+
result.defineReturnValue(previous);
40+
}
41+
42+
@Override
43+
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Object ret) {
44+
return ret;
45+
}
46+
47+
@Override
48+
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Throwable t) {
49+
}
50+
}

apm-sniffer/apm-toolkit-activation/apm-toolkit-webflux-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/webflux/WebFluxSkyWalkingOperatorsInterceptor.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
2323
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
2424
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
25-
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor;
2625
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
2726
import org.springframework.web.server.ServerWebExchange;
28-
import org.springframework.web.server.ServerWebExchangeDecorator;
29-
import org.springframework.web.server.adapter.DefaultServerWebExchange;
3027
import reactor.util.context.Context;
3128

3229
import java.lang.reflect.Method;
3330

3431
/**
3532
*/
36-
public class WebFluxSkyWalkingOperatorsInterceptor implements StaticMethodsAroundInterceptor {
33+
public class WebFluxSkyWalkingOperatorsInterceptor extends WebFluxSkyWalkingStaticMethodsAroundInterceptor {
3734

3835
@Override
3936
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes,
@@ -67,14 +64,4 @@ public void handleMethodException(Class clazz, Method method, Object[] allArgume
6764
ContextManager.activeSpan().log(t);
6865
}
6966

70-
private static EnhancedInstance getInstance(Object o) {
71-
EnhancedInstance instance = null;
72-
if (o instanceof DefaultServerWebExchange && o instanceof EnhancedInstance) {
73-
instance = (EnhancedInstance) o;
74-
} else if (o instanceof ServerWebExchangeDecorator) {
75-
ServerWebExchange delegate = ((ServerWebExchangeDecorator) o).getDelegate();
76-
return getInstance(delegate);
77-
}
78-
return instance;
79-
}
8067
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
19+
package org.apache.skywalking.apm.toolkit.activation.webflux;
20+
21+
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
22+
import org.apache.skywalking.apm.agent.core.logging.api.ILog;
23+
import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
25+
26+
import java.lang.reflect.Method;
27+
28+
public class WebFluxSkyWalkingSegmentIDInterceptor extends WebFluxSkyWalkingStaticMethodsAroundInterceptor {
29+
30+
private static final ILog LOGGER = LogManager.getLogger(WebFluxSkyWalkingSegmentIDInterceptor.class);
31+
32+
@Override
33+
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes,
34+
MethodInterceptResult result) {
35+
final ContextSnapshot contextSnapshot = getContextSnapshot(allArguments[0]);
36+
if (contextSnapshot == null || contextSnapshot.getCorrelationContext() == null) {
37+
return;
38+
}
39+
result.defineReturnValue(contextSnapshot.getTraceSegmentId());
40+
}
41+
42+
@Override
43+
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes,
44+
Object ret) {
45+
return ret;
46+
}
47+
48+
@Override
49+
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes,
50+
Throwable t) {
51+
LOGGER.error("Failed to get segment Id.", t);
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
19+
package org.apache.skywalking.apm.toolkit.activation.webflux;
20+
21+
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
22+
import org.apache.skywalking.apm.agent.core.logging.api.ILog;
23+
import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
25+
26+
import java.lang.reflect.Method;
27+
28+
public class WebFluxSkyWalkingSpanIDInterceptor extends WebFluxSkyWalkingStaticMethodsAroundInterceptor {
29+
30+
private static final ILog LOGGER = LogManager.getLogger(WebFluxSkyWalkingSpanIDInterceptor.class);
31+
32+
@Override
33+
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes,
34+
MethodInterceptResult result) {
35+
final ContextSnapshot contextSnapshot = getContextSnapshot(allArguments[0]);
36+
if (contextSnapshot == null || contextSnapshot.getCorrelationContext() == null) {
37+
return;
38+
}
39+
result.defineReturnValue(contextSnapshot.getSpanId());
40+
}
41+
42+
@Override
43+
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes,
44+
Object ret) {
45+
return ret;
46+
}
47+
48+
@Override
49+
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes,
50+
Throwable t) {
51+
LOGGER.error("Failed to getDefault span Id.", t);
52+
}
53+
}

0 commit comments

Comments
 (0)