Skip to content

Commit

Permalink
remove span
Browse files Browse the repository at this point in the history
  • Loading branch information
宋潇岳 committed Oct 9, 2019
1 parent 025d8ae commit 6d6fd72
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,24 @@

import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
import org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x.context.SWTransmitter;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebExchangeDecorator;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.core.publisher.SignalType;

import java.lang.reflect.Method;
import java.util.function.Consumer;

import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;

Expand Down Expand Up @@ -66,7 +73,32 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Object ret) throws Throwable {
return ret;
EnhancedInstance instance = getInstance(allArguments[0]);
if (instance == null) {
return ret;
}
final SWTransmitter swTransmitter = (SWTransmitter) instance.getSkyWalkingDynamicField();
if (swTransmitter == null) {
return ret;
}
final ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
Mono<Void> mono = (Mono) ret;
return mono.doFinally(new Consumer<SignalType>() {
@Override
public void accept(SignalType signalType) {
HttpStatus statusCode = exchange.getResponse().getStatusCode();
if (statusCode == HttpStatus.TOO_MANY_REQUESTS) {
AbstractSpan localSpan = ContextManager.createLocalSpan(swTransmitter.getOperationName());
Tags.STATUS_CODE.set(localSpan,statusCode.toString());
SpanLayer.asHttp(localSpan);
localSpan.setComponent(ComponentsDefine.SPRING_CLOUD_GATEWAY);
ContextManager.continued(swTransmitter.getSnapshot());
ContextManager.stopSpan(localSpan);
AbstractSpan spanWebflux = swTransmitter.getSpanWebflux();
spanWebflux.asyncFinish();
}
}
});
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allA
Tags.STATUS_CODE.set(transmitter.getSpanWebflux().errorOccurred(), String.valueOf(response.code()));
}
transmitter.getSpanGateway().asyncFinish();
transmitter.getSpanFilter().asyncFinish();
transmitter.getSpanWebflux().asyncFinish();
objInst.setSkyWalkingDynamicField(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
package org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x;

import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
import org.apache.skywalking.apm.agent.core.context.RuntimeContext;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
import org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x.context.Constants;
import org.apache.skywalking.apm.plugin.spring.cloud.gateway.v21x.context.SWTransmitter;
import org.springframework.web.server.ServerWebExchange;
Expand All @@ -40,24 +38,14 @@
*/
public class NettyRoutingFilterInterceptor implements InstanceMethodsAroundInterceptor {

private static final String SPRING_CLOUD_GATEWAY_ROUTE_PREFIX = "FilteringWebHandler";

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
EnhancedInstance instance = NettyRoutingFilterInterceptor.getInstance(allArguments[0]);
if (instance != null) {
SWTransmitter swTransmitter = (SWTransmitter) instance.getSkyWalkingDynamicField();
if (swTransmitter != null) {
AbstractSpan filteringWebHandler = ContextManager.createLocalSpan(SPRING_CLOUD_GATEWAY_ROUTE_PREFIX);
filteringWebHandler.setComponent(ComponentsDefine.SPRING_CLOUD_GATEWAY);
SpanLayer.asHttp(filteringWebHandler);
ContextManager.continued(swTransmitter.getSnapshot());
swTransmitter.setSnapshot(ContextManager.capture());
swTransmitter.setSpanFilter(filteringWebHandler.prepareForAsync());
ContextManager.stopSpan(filteringWebHandler);
ContextManager.getRuntimeContext().put(Constants.SPRING_CLOUD_GATEWAY_TRANSMITTER, swTransmitter);
}
RuntimeContext runtimeContext = ContextManager.getRuntimeContext();
runtimeContext.put(Constants.SPRING_CLOUD_GATEWAY_TRANSMITTER, swTransmitter);
}
}

Expand All @@ -79,24 +67,13 @@ public void handleMethodException(EnhancedInstance objInst, Method method, Objec
public static EnhancedInstance getInstance(Object o) {
EnhancedInstance instance = null;
if (o instanceof ServerWebExchangeDecorator) {
instance = getEnhancedInstance((ServerWebExchangeDecorator) o);
ServerWebExchange delegate = ((ServerWebExchangeDecorator) o).getDelegate();
if (delegate instanceof DefaultServerWebExchange) {
instance = (EnhancedInstance) delegate;
}
} else if (o instanceof DefaultServerWebExchange) {
instance = (EnhancedInstance) o;
}
return instance;
}


private static EnhancedInstance getEnhancedInstance(ServerWebExchangeDecorator serverWebExchangeDecorator) {
Object o = serverWebExchangeDecorator.getDelegate();
if (o instanceof ServerWebExchangeDecorator) {
return getEnhancedInstance((ServerWebExchangeDecorator) o);
} else if (o instanceof DefaultServerWebExchange) {
return (EnhancedInstance) o;
} else if (o == null) {
throw new NullPointerException("The expected class DefaultServerWebExchange is null");
} else {
throw new RuntimeException("Unknown parameter types:" + o.getClass());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class SWTransmitter {

private AbstractSpan spanWebflux;
private AbstractSpan spanGateway;
private AbstractSpan spanFilter;
private ContextSnapshot snapshot;
private String operationName;

Expand All @@ -39,14 +38,6 @@ public SWTransmitter(AbstractSpan spanWebflux, ContextSnapshot snapshot, String
this.operationName = operationName;
}

public AbstractSpan getSpanFilter() {
return spanFilter;
}

public void setSpanFilter(AbstractSpan spanFilter) {
this.spanFilter = spanFilter;
}

public AbstractSpan getSpanWebflux() {
return spanWebflux;
}
Expand Down

0 comments on commit 6d6fd72

Please sign in to comment.