-
Notifications
You must be signed in to change notification settings - Fork 609
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support access the tracer context in spring gateway filter (#539)
Co-authored-by: darknesstm <[email protected]>
- Loading branch information
1 parent
6229b22
commit c9a6170
Showing
35 changed files
with
1,579 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...rc/main/java/org/apache/skywalking/apm/toolkit/webflux/WebFluxSkyWalkingTraceContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.toolkit.webflux; | ||
|
||
import org.springframework.web.server.ServerWebExchange; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* TraceContext for WebFlux. | ||
*/ | ||
public class WebFluxSkyWalkingTraceContext { | ||
/** | ||
* Try to get the traceId of current trace context. | ||
* | ||
* @param serverWebExchange - EnhancedInstance that contains the tracing context | ||
* @return traceId, if it exists, or empty {@link String}. | ||
*/ | ||
public static String traceId(ServerWebExchange serverWebExchange) { | ||
return ""; | ||
} | ||
|
||
/** | ||
* Try to get the segmentId of current trace context. | ||
* | ||
* @param serverWebExchange - EnhancedInstance that contains the tracing context | ||
* @return segmentId, if it exists, or empty {@link String}. | ||
*/ | ||
public static String segmentId(ServerWebExchange serverWebExchange) { | ||
return ""; | ||
} | ||
|
||
/** | ||
* Try to get the spanId of current trace context. The spanId is a negative number when the trace context is | ||
* missing. | ||
* | ||
* @param serverWebExchange - EnhancedInstance that contains the tracing context | ||
* @return spanId, if it exists, or empty {@link String}. | ||
*/ | ||
public static int spanId(ServerWebExchange serverWebExchange) { | ||
return -1; | ||
} | ||
|
||
/** | ||
* Try to get the custom value from trace context. | ||
* | ||
* @param serverWebExchange - EnhancedInstance that contains the tracing context | ||
* @return custom data value. | ||
*/ | ||
public static Optional<String> getCorrelation(ServerWebExchange serverWebExchange, String key) { | ||
return Optional.empty(); | ||
} | ||
|
||
/** | ||
* Put the custom key/value into trace context. | ||
* | ||
* @param serverWebExchange - EnhancedInstance that contains the tracing context | ||
* @return previous value if it exists. | ||
*/ | ||
public static Optional<String> putCorrelation(ServerWebExchange serverWebExchange, String key, String value) { | ||
return Optional.empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...ing/apm/toolkit/activation/webflux/WebFluxSkyWalkingCorrelationContextGetInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.skywalking.apm.toolkit.activation.webflux; | ||
|
||
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Optional; | ||
|
||
public class WebFluxSkyWalkingCorrelationContextGetInterceptor extends WebFluxSkyWalkingStaticMethodsAroundInterceptor { | ||
|
||
@Override | ||
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, MethodInterceptResult result) { | ||
final ContextSnapshot contextSnapshot = getContextSnapshot(allArguments[0]); | ||
if (contextSnapshot == null || contextSnapshot.getCorrelationContext() == null) { | ||
return; | ||
} | ||
|
||
final String key = (String) allArguments[1]; | ||
final Optional<String> data = contextSnapshot.getCorrelationContext().get(key); | ||
|
||
result.defineReturnValue(data); | ||
} | ||
|
||
@Override | ||
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Object ret) { | ||
return ret; | ||
} | ||
|
||
@Override | ||
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Throwable t) { | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ing/apm/toolkit/activation/webflux/WebFluxSkyWalkingCorrelationContextPutInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.skywalking.apm.toolkit.activation.webflux; | ||
|
||
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Optional; | ||
|
||
public class WebFluxSkyWalkingCorrelationContextPutInterceptor extends WebFluxSkyWalkingStaticMethodsAroundInterceptor { | ||
|
||
@Override | ||
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, MethodInterceptResult result) { | ||
final ContextSnapshot contextSnapshot = getContextSnapshot(allArguments[0]); | ||
if (contextSnapshot == null || contextSnapshot.getCorrelationContext() == null) { | ||
return; | ||
} | ||
|
||
final String key = (String) allArguments[1]; | ||
final String value = (String) allArguments[2]; | ||
final Optional<String> previous = contextSnapshot.getCorrelationContext().put(key, value); | ||
|
||
result.defineReturnValue(previous); | ||
} | ||
|
||
@Override | ||
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Object ret) { | ||
return ret; | ||
} | ||
|
||
@Override | ||
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Throwable t) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...ache/skywalking/apm/toolkit/activation/webflux/WebFluxSkyWalkingSegmentIDInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.toolkit.activation.webflux; | ||
|
||
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; | ||
import org.apache.skywalking.apm.agent.core.logging.api.ILog; | ||
import org.apache.skywalking.apm.agent.core.logging.api.LogManager; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
public class WebFluxSkyWalkingSegmentIDInterceptor extends WebFluxSkyWalkingStaticMethodsAroundInterceptor { | ||
|
||
private static final ILog LOGGER = LogManager.getLogger(WebFluxSkyWalkingSegmentIDInterceptor.class); | ||
|
||
@Override | ||
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, | ||
MethodInterceptResult result) { | ||
final ContextSnapshot contextSnapshot = getContextSnapshot(allArguments[0]); | ||
if (contextSnapshot == null || contextSnapshot.getCorrelationContext() == null) { | ||
return; | ||
} | ||
result.defineReturnValue(contextSnapshot.getTraceSegmentId()); | ||
} | ||
|
||
@Override | ||
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, | ||
Object ret) { | ||
return ret; | ||
} | ||
|
||
@Override | ||
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, | ||
Throwable t) { | ||
LOGGER.error("Failed to get segment Id.", t); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
.../apache/skywalking/apm/toolkit/activation/webflux/WebFluxSkyWalkingSpanIDInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.toolkit.activation.webflux; | ||
|
||
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; | ||
import org.apache.skywalking.apm.agent.core.logging.api.ILog; | ||
import org.apache.skywalking.apm.agent.core.logging.api.LogManager; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
public class WebFluxSkyWalkingSpanIDInterceptor extends WebFluxSkyWalkingStaticMethodsAroundInterceptor { | ||
|
||
private static final ILog LOGGER = LogManager.getLogger(WebFluxSkyWalkingSpanIDInterceptor.class); | ||
|
||
@Override | ||
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, | ||
MethodInterceptResult result) { | ||
final ContextSnapshot contextSnapshot = getContextSnapshot(allArguments[0]); | ||
if (contextSnapshot == null || contextSnapshot.getCorrelationContext() == null) { | ||
return; | ||
} | ||
result.defineReturnValue(contextSnapshot.getSpanId()); | ||
} | ||
|
||
@Override | ||
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, | ||
Object ret) { | ||
return ret; | ||
} | ||
|
||
@Override | ||
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, | ||
Throwable t) { | ||
LOGGER.error("Failed to getDefault span Id.", t); | ||
} | ||
} |
Oops, something went wrong.