Java Code Examples for org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan#prepareForAsync()

The following examples show how to use org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan#prepareForAsync() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpClientRequestImplInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
                         MethodInterceptResult result) {
    HttpClientRequestContext requestContext = (HttpClientRequestContext) objInst.getSkyWalkingDynamicField();
    if (!requestContext.sent) {
        HttpClientRequest request = (HttpClientRequest) objInst;
        ContextCarrier contextCarrier = new ContextCarrier();
        AbstractSpan span = ContextManager.createExitSpan(toPath(request.uri()), contextCarrier,
                requestContext.remotePeer);
        span.setComponent(ComponentsDefine.VERTX);
        SpanLayer.asHttp(span);
        Tags.HTTP.METHOD.set(span, request.method().toString());
        Tags.URL.set(span, request.uri());

        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            request.headers().add(next.getHeadKey(), next.getHeadValue());
        }
        requestContext.vertxContext = new VertxContext(ContextManager.capture(), span.prepareForAsync());
    }
}
 
Example 2
Source File: HttpClientFinalizerSendInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
                         MethodInterceptResult result) throws Throwable {
    EnhanceObjectCache enhanceObjectCache = (EnhanceObjectCache) objInst.getSkyWalkingDynamicField();
    AbstractSpan span = ContextManager.activeSpan();
    span.prepareForAsync();

    if (!StringUtil.isEmpty(enhanceObjectCache.getUrl())) {
        URL url = new URL(enhanceObjectCache.getUrl());

        ContextCarrier contextCarrier = new ContextCarrier();
        AbstractSpan abstractSpan = ContextManager.createExitSpan(
            "SpringCloudGateway/sendRequest", contextCarrier, getPeer(url));
        Tags.URL.set(abstractSpan, enhanceObjectCache.getUrl());
        abstractSpan.prepareForAsync();
        abstractSpan.setComponent(SPRING_CLOUD_GATEWAY);

        ContextManager.stopSpan(abstractSpan);
        ContextManager.stopSpan(span);

        BiFunction<? super HttpClientRequest, ? super NettyOutbound, ? extends Publisher<Void>> finalSender = (BiFunction<? super HttpClientRequest, ? super NettyOutbound, ? extends Publisher<Void>>) allArguments[0];
        allArguments[0] = new BiFunction<HttpClientRequest, NettyOutbound, Publisher<Void>>() {
            @Override
            public Publisher<Void> apply(HttpClientRequest request, NettyOutbound outbound) {
                Publisher publisher = finalSender.apply(request, outbound);

                CarrierItem next = contextCarrier.items();
                while (next.hasNext()) {
                    next = next.next();
                    request.requestHeaders().remove(next.getHeadKey());
                    request.requestHeaders().set(next.getHeadKey(), next.getHeadValue());
                }
                return publisher;
            }
        };
        enhanceObjectCache.setCacheSpan(abstractSpan);
    }

    enhanceObjectCache.setSpan1(span);
}
 
Example 3
Source File: NettyRoutingFilterInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
                         MethodInterceptResult result) throws Throwable {
    ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
    EnhancedInstance enhancedInstance = getInstance(exchange);

    AbstractSpan span = ContextManager.createLocalSpan("SpringCloudGateway/RoutingFilter");
    if (enhancedInstance != null && enhancedInstance.getSkyWalkingDynamicField() != null) {
        ContextManager.continued((ContextSnapshot) enhancedInstance.getSkyWalkingDynamicField());
    }
    span.setComponent(SPRING_CLOUD_GATEWAY);
    span.prepareForAsync();
}
 
Example 4
Source File: HttpClientRequestInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeMethod(final EnhancedInstance objInst,
                         final Method method,
                         final Object[] allArguments,
                         final Class<?>[] argumentsTypes,
                         final MethodInterceptResult result) throws Throwable {
    AbstractSpan span = ContextManager.activeSpan();

    URL url = new URL((String) allArguments[1]);
    ContextCarrier contextCarrier = new ContextCarrier();
    AbstractSpan abstractSpan = ContextManager.createExitSpan(
        "SpringCloudGateway/sendRequest", contextCarrier, getPeer(url));
    abstractSpan.prepareForAsync();
    Tags.URL.set(abstractSpan, String.valueOf(allArguments[1]));

    abstractSpan.setComponent(SPRING_CLOUD_GATEWAY);
    ContextManager.stopSpan(abstractSpan);

    Function<? super HttpClientRequest, ? extends Publisher<Void>> handler = (Function<? super HttpClientRequest, ? extends Publisher<Void>>) allArguments[2];
    allArguments[2] = new Function<HttpClientRequest, Publisher<Void>>() {
        @Override
        public Publisher<Void> apply(final HttpClientRequest httpClientRequest) {
            //
            CarrierItem next = contextCarrier.items();
            if (httpClientRequest instanceof EnhancedInstance) {
                ((EnhancedInstance) httpClientRequest).setSkyWalkingDynamicField(next);
            }
            return handler.apply(httpClientRequest);
        }
    };

    objInst.setSkyWalkingDynamicField(new EnhanceCacheObject(span, abstractSpan));
}
 
Example 5
Source File: DispatcherHandlerHandleMethodInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
                         MethodInterceptResult result) throws Throwable {
    EnhancedInstance instance = getInstance(allArguments[0]);

    ServerWebExchange exchange = (ServerWebExchange) allArguments[0];

    ContextCarrier carrier = new ContextCarrier();
    CarrierItem next = carrier.items();
    HttpHeaders headers = exchange.getRequest().getHeaders();
    while (next.hasNext()) {
        next = next.next();
        List<String> header = headers.get(next.getHeadKey());
        if (header != null && header.size() > 0) {
            next.setHeadValue(header.get(0));
        }
    }

    AbstractSpan span = ContextManager.createEntrySpan(exchange.getRequest().getURI().getPath(), carrier);
    span.setComponent(ComponentsDefine.SPRING_WEBFLUX);
    SpanLayer.asHttp(span);
    Tags.URL.set(span, exchange.getRequest().getURI().toString());
    HTTP.METHOD.set(span, exchange.getRequest().getMethodValue());
    instance.setSkyWalkingDynamicField(ContextManager.capture());
    span.prepareForAsync();
    ContextManager.stopSpan(span);

    objInst.setSkyWalkingDynamicField(span);
}
 
Example 6
Source File: ServerTracingFilterInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public Object afterMethodImpl(EnhancedInstance enhancedInstance, Method method, Object[] objects, Class<?>[] classes, Object ret) throws Throwable {
    final AbstractSpan finagleSpan = getSpan();
    getLocalContextHolder().remove(FinagleCtxs.SW_SPAN);

    /*
     * If the intercepted method throws exception, the ret will be null
     */
    if (ret == null) {
        ContextManager.stopSpan(finagleSpan);
    } else {
        finagleSpan.prepareForAsync();
        ContextManager.stopSpan(finagleSpan);
        ((Future<?>) ret).addEventListener(new FutureEventListener<Object>() {
            @Override
            public void onSuccess(Object value) {
                finagleSpan.asyncFinish();
            }

            @Override
            public void onFailure(Throwable cause) {
                finagleSpan.errorOccurred();
                finagleSpan.log(cause);
                finagleSpan.asyncFinish();
            }
        });
    }
    return ret;
}
 
Example 7
Source File: ClientTracingFilterInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public Object afterMethodImpl(EnhancedInstance enhancedInstance, Method method, Object[] objects, Class<?>[] classes, Object ret) throws Throwable {

    final AbstractSpan finagleSpan = getSpan();
    getLocalContextHolder().remove(SW_SPAN);
    getMarshalledContextHolder().remove(SWContextCarrier$.MODULE$);

    /*
     * If the intercepted method throws exception, the ret will be null
     */
    if (ret == null) {
        ContextManager.stopSpan(finagleSpan);
    } else {
        finagleSpan.prepareForAsync();
        ContextManager.stopSpan(finagleSpan);

        ((Future<?>) ret).addEventListener(new FutureEventListener<Object>() {
            @Override
            public void onSuccess(Object value) {
                finagleSpan.asyncFinish();
            }

            @Override
            public void onFailure(Throwable cause) {
                finagleSpan.errorOccurred();
                finagleSpan.log(cause);
                finagleSpan.asyncFinish();
            }
        });
    }
    return ret;
}