Fix webflux sharing span across multiple threads (#5177)

This commit is contained in:
Xin,Zhang 2020-07-27 12:44:17 +08:00 committed by GitHub
parent a49ce988b4
commit 5cf1d122cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 17 deletions

View File

@ -69,34 +69,34 @@ public class DispatcherHandlerHandleMethodInterceptor implements InstanceMethods
span.prepareForAsync();
ContextManager.stopSpan(span);
objInst.setSkyWalkingDynamicField(span);
exchange.getAttributes().put("SKYWALING_SPAN", span);
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
return ((Mono) ret).doFinally(s -> {
try {
Object pathPattern = exchange.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
if (pathPattern != null) {
span.setOperationName(((PathPattern) pathPattern).getPatternString());
}
HttpStatus httpStatus = exchange.getResponse().getStatusCode();
// fix webflux-2.0.0-2.1.0 version have bug. httpStatus is null. not support
if (httpStatus != null) {
Tags.STATUS_CODE.set(span, Integer.toString(httpStatus.value()));
if (httpStatus.isError()) {
span.errorOccurred();
AbstractSpan span = (AbstractSpan) exchange.getAttributes().get("SKYWALING_SPAN");
if (span != null) {
try {
Object pathPattern = exchange.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
if (pathPattern != null) {
span.setOperationName(((PathPattern) pathPattern).getPatternString());
}
HttpStatus httpStatus = exchange.getResponse().getStatusCode();
// fix webflux-2.0.0-2.1.0 version have bug. httpStatus is null. not support
if (httpStatus != null) {
Tags.STATUS_CODE.set(span, Integer.toString(httpStatus.value()));
if (httpStatus.isError()) {
span.errorOccurred();
}
}
} finally {
span.asyncFinish();
}
} finally {
span.asyncFinish();
}
});
}
@Override