Java Code Examples for org.apache.skywalking.apm.agent.core.context.ContextManager#capture()

The following examples show how to use org.apache.skywalking.apm.agent.core.context.ContextManager#capture() . 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: AsyncCommandMethodInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    AsyncCommand asyncCommand = (AsyncCommand) objInst;
    String operationName = "Lettuce/" + asyncCommand.getType().name();
    AbstractSpan span = ContextManager.createLocalSpan(operationName + "/onComplete");
    span.setComponent(ComponentsDefine.LETTUCE);
    Tags.DB_TYPE.set(span, "Redis");
    SpanLayer.asCache(span);
    if (allArguments[0] instanceof Consumer) {
        allArguments[0] = new SWConsumer((Consumer) allArguments[0], ContextManager.capture(), operationName);
    } else {
        allArguments[0] = new SWBiConsumer((BiConsumer) allArguments[0], ContextManager.capture(), operationName);
    }
}
 
Example 2
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 3
Source File: ServerInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public <REQUEST, RESPONSE> ServerCall.Listener<REQUEST> interceptCall(ServerCall<REQUEST, RESPONSE> call,
    Metadata headers, ServerCallHandler<REQUEST, RESPONSE> handler) {
    final ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        String contextValue = headers.get(Metadata.Key.of(next.getHeadKey(), Metadata.ASCII_STRING_MARSHALLER));
        if (!StringUtil.isEmpty(contextValue)) {
            next.setHeadValue(contextValue);
        }
    }

    final AbstractSpan span = ContextManager.createEntrySpan(OperationNameFormatUtil.formatOperationName(call.getMethodDescriptor()), contextCarrier);
    span.setComponent(ComponentsDefine.GRPC);
    span.setLayer(SpanLayer.RPC_FRAMEWORK);
    try {
        return new TracingServerCallListener<>(handler.startCall(new TracingServerCall<>(call, ContextManager.capture()), headers), call
            .getMethodDescriptor(), ContextManager.capture());
    } finally {
        ContextManager.stopSpan();
    }
}
 
Example 4
Source File: TracingRunnable.java    From skywalking with Apache License 2.0 5 votes vote down vote up
/**
 * Wrap {@link Runnable} by {@link TracingRunnable} if active trace context existed.
 *
 * @param delegate {@link Runnable} to wrap.
 * @return Wrapped {@link TracingRunnable} or original {@link Runnable} if trace context not existed.
 */
public static Runnable wrapOrNot(Runnable delegate) {
    // Just wrap continuation with active trace context
    if (ContextManager.isActive()) {
        return new TracingRunnable(ContextManager.capture(), delegate);
    } else {
        return delegate;
    }
}
 
Example 5
Source File: HttpServerExchangeInterceptor.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 {
    if (ContextManager.isActive()) {
        Object argument = allArguments[1];
        if (argument != null) {
            allArguments[1] = new SWRunnable((Runnable) argument, ContextManager.capture());
        }
    }
}
 
Example 6
Source File: DoSubmitMethodInterceptor.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 {
    if (ContextManager.isActive()) {
        allArguments[0] = new SWCallable((Callable) allArguments[0], ContextManager.capture());
    }
}
 
Example 7
Source File: RestExecuteInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    Object ret) throws Throwable {
    Object[] cacheValues = (Object[]) objInst.getSkyWalkingDynamicField();
    cacheValues[2] = ContextManager.capture();
    if (ret != null) {
        String uri = (String) cacheValues[0];
        ((EnhancedInstance) ret).setSkyWalkingDynamicField(new EnhanceCacheObjects(uri, ComponentsDefine.SPRING_REST_TEMPLATE, SpanLayer.HTTP, (ContextSnapshot) cacheValues[2]));
    }
    ContextManager.stopSpan();
    return ret;
}
 
Example 8
Source File: PulsarProducerInterceptor.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 {
    if (allArguments[0] != null) {
        ProducerEnhanceRequiredInfo requiredInfo = (ProducerEnhanceRequiredInfo) objInst.getSkyWalkingDynamicField();
        ContextCarrier contextCarrier = new ContextCarrier();
        String topicName = requiredInfo.getTopic();
        AbstractSpan activeSpan = ContextManager.createExitSpan(OPERATE_NAME_PREFIX + topicName + PRODUCER_OPERATE_NAME_SUFFIX, contextCarrier, requiredInfo
            .getServiceUrl());
        Tags.MQ_BROKER.set(activeSpan, requiredInfo.getServiceUrl());
        Tags.MQ_TOPIC.set(activeSpan, topicName);
        SpanLayer.asMQ(activeSpan);
        activeSpan.setComponent(ComponentsDefine.PULSAR_PRODUCER);
        CarrierItem next = contextCarrier.items();
        MessageImpl msg = (MessageImpl) allArguments[0];
        while (next.hasNext()) {
            next = next.next();
            msg.getMessageBuilder()
               .addProperties(PulsarApi.KeyValue.newBuilder()
                                                .setKey(next.getHeadKey())
                                                .setValue(next.getHeadValue()));
        }
        if (allArguments.length > 1) {
            EnhancedInstance callbackInstance = (EnhancedInstance) allArguments[1];
            if (callbackInstance != null) {
                ContextSnapshot snapshot = ContextManager.capture();
                if (null != snapshot) {
                    SendCallbackEnhanceRequiredInfo callbackRequiredInfo = new SendCallbackEnhanceRequiredInfo();
                    callbackRequiredInfo.setTopic(topicName);
                    callbackRequiredInfo.setContextSnapshot(snapshot);
                    callbackInstance.setSkyWalkingDynamicField(callbackRequiredInfo);
                }
            }
        }
    }
}
 
Example 9
Source File: SessionRequestConstructorInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
    if (ContextManager.isActive()) {
        if (ContextManager.activeSpan().isExit()) {
            CONTEXT_LOCAL.remove();
            return;
        }
        ContextSnapshot snapshot = ContextManager.capture();
        objInst.setSkyWalkingDynamicField(new Object[] {
            snapshot,
            CONTEXT_LOCAL.get()
        });
    }
    CONTEXT_LOCAL.remove();
}
 
Example 10
Source File: TracingClientCall.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void start(Listener<RESPONSE> responseListener, Metadata headers) {
    final AbstractSpan blockingSpan = (AbstractSpan) ContextManager.getRuntimeContext()
                                                                   .get(BLOCKING_CALL_EXIT_SPAN);
    final ContextCarrier contextCarrier = new ContextCarrier();

    // Avoid create ExitSpan repeatedly, ExitSpan of blocking calls will create by BlockingCallInterceptor.
    if (blockingSpan == null) {
        final AbstractSpan span = ContextManager.createExitSpan(serviceName, remotePeer);
        span.setComponent(ComponentsDefine.GRPC);
        span.setLayer(SpanLayer.RPC_FRAMEWORK);
    } else {
        ContextManager.getRuntimeContext().remove(BLOCKING_CALL_EXIT_SPAN);
    }

    ContextManager.inject(contextCarrier);
    CarrierItem contextItem = contextCarrier.items();
    while (contextItem.hasNext()) {
        contextItem = contextItem.next();
        Metadata.Key<String> headerKey = Metadata.Key.of(contextItem.getHeadKey(), Metadata.ASCII_STRING_MARSHALLER);
        headers.put(headerKey, contextItem.getHeadValue());
    }

    snapshot = ContextManager.capture();
    try {
        delegate().start(new TracingClientCallListener(responseListener, snapshot), headers);
    } catch (Throwable t) {
        ContextManager.activeSpan().errorOccurred().log(t);
        throw t;
    } finally {
        if (blockingSpan == null) {
            ContextManager.stopSpan();
        }
    }
}