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

The following examples show how to use org.apache.skywalking.apm.agent.core.context.ContextManager#createLocalSpan() . 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: HystrixCommandRunInterceptor.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) throws Throwable {
    // create a local span, and continued, The `execution method` running in other thread if the
    // hystrix strategy is `THREAD`.
    EnhanceRequireObjectCache enhanceRequireObjectCache = (EnhanceRequireObjectCache) objInst.getSkyWalkingDynamicField();
    ContextSnapshot snapshot = enhanceRequireObjectCache.getContextSnapshot();

    AbstractSpan activeSpan = ContextManager.createLocalSpan(enhanceRequireObjectCache.getOperationNamePrefix() + "/Execution");
    activeSpan.setComponent(ComponentsDefine.HYSTRIX);
    if (snapshot != null) {
        ContextManager.continued(snapshot);
    }
    // Because of `fall back` method running in other thread. so we need capture concurrent span for tracing.
    enhanceRequireObjectCache.setContextSnapshot(ContextManager.capture());
}
 
Example 2
Source File: TracingClientCall.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void onClose(Status status, Metadata trailers) {
    final AbstractSpan span = ContextManager.createLocalSpan(operationPrefix + RESPONSE_ON_CLOSE_OPERATION_NAME);
    span.setComponent(ComponentsDefine.GRPC);
    span.setLayer(SpanLayer.RPC_FRAMEWORK);
    ContextManager.continued(contextSnapshot);
    if (!status.isOk()) {
        span.errorOccurred().log(status.asRuntimeException());
        Tags.STATUS_CODE.set(span, status.getCode().name());
    }

    try {
        delegate().onClose(status, trailers);
    } catch (Throwable t) {
        ContextManager.activeSpan().errorOccurred().log(t);
    } finally {
        ContextManager.stopSpan();
    }
}
 
Example 3
Source File: HttpContextHandleDispatchResponseInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
                         MethodInterceptResult result) throws Throwable {
    HttpContext httpContext = (HttpContext) objInst;
    HttpClientRequest clientRequest = httpContext.clientRequest();
    VertxContext context = ((HttpClientRequestContext) ((EnhancedInstance) clientRequest)
            .getSkyWalkingDynamicField()).vertxContext;
    Tags.STATUS_CODE.set(context.getSpan(), Integer.toString(httpContext.clientResponse().statusCode()));
    context.getSpan().asyncFinish();

    AbstractSpan span = ContextManager.createLocalSpan("#" + context.getSpan().getOperationName());
    span.setComponent(ComponentsDefine.VERTX);
    SpanLayer.asHttp(span);
    ContextManager.continued(context.getContextSnapshot());
}
 
Example 4
Source File: RouteStateInterceptor.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 {
    RoutingContextImplBase routingContext = (RoutingContextImplBase) allArguments[0];
    List<Handler<RoutingContext>> contextHandlers = (List<Handler<RoutingContext>>) objInst.getSkyWalkingDynamicField();
    AtomicInteger currentContextIndex = (AtomicInteger) ((EnhancedInstance) routingContext).getSkyWalkingDynamicField();
    int handlerContextIndex = currentContextIndex.get();
    if (VertxContext.VERTX_VERSION >= 35 && contextHandlers.size() > 1) {
        currentContextIndex.getAndIncrement(); //3.5+ has possibility for multiple handlers
    }
    String contextName = contextHandlers.get(handlerContextIndex).getClass().getCanonicalName();
    int lambdaOffset = contextName.indexOf("$$Lambda$");
    if (lambdaOffset > 0) contextName = contextName.substring(0, lambdaOffset + 9);
    AbstractSpan span = ContextManager.createLocalSpan(String.format("%s.handle(RoutingContext)", contextName));

    Object connection = ((EnhancedInstance) routingContext.request()).getSkyWalkingDynamicField();
    VertxContext vertxContext = (VertxContext) ((EnhancedInstance) connection).getSkyWalkingDynamicField();
    ContextManager.continued(vertxContext.getContextSnapshot());
    span.setComponent(ComponentsDefine.VERTX);
    SpanLayer.asHttp(span);
}
 
Example 5
Source File: TracingRunnable.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    if (ContextManager.isActive() && snapshot.isFromCurrent()) {
        // Thread not switched, skip restore snapshot.
        delegate.run();
        return;
    }

    // Create local coroutine span
    AbstractSpan span = ContextManager.createLocalSpan(COROUTINE);
    span.setComponent(ComponentsDefine.KT_COROUTINE);

    // Recover with snapshot
    ContextManager.continued(snapshot);

    try {
        delegate.run();
    } finally {
        ContextManager.stopSpan(span);
    }
}
 
Example 6
Source File: ConstructorWithSpanBuilderInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
    SkywalkingSpanBuilder spanBuilder = (SkywalkingSpanBuilder) allArguments[0];

    AbstractSpan span;
    if (spanBuilder.isEntry()) {
        span = ContextManager.createEntrySpan(spanBuilder.getOperationName(), null);
    } else if (spanBuilder.isExit() && (!StringUtil.isEmpty(spanBuilder.getPeer()))) {
        span = ContextManager.createExitSpan(spanBuilder.getOperationName(), buildRemotePeer(spanBuilder));
    } else {
        span = ContextManager.createLocalSpan(spanBuilder.getOperationName());
    }

    for (Tag tag : spanBuilder.getTags()) {
        span.tag(Tags.ofKey(tag.getKey()), tag.getValue());
    }
    if (spanBuilder.isError()) {
        span.errorOccurred();
    }

    objInst.setSkyWalkingDynamicField(span);
}
 
Example 7
Source File: HttpClientRequestImplHandleResponseInterceptor.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) throws Throwable {
    if (VertxContext.VERTX_VERSION < 38 || allArguments.length == 2) {
        HttpClientRequestContext requestContext = (HttpClientRequestContext) objInst.getSkyWalkingDynamicField();
        if (!requestContext.usingWebClient) {
            VertxContext context = requestContext.vertxContext;
            Tags.STATUS_CODE.set(context.getSpan(), Integer.toString(((HttpClientResponse) allArguments[0]).statusCode()));
            context.getSpan().asyncFinish();

            AbstractSpan span = ContextManager.createLocalSpan("#" + context.getSpan().getOperationName());
            span.setComponent(ComponentsDefine.VERTX);
            SpanLayer.asHttp(span);
            ContextManager.continued(context.getContextSnapshot());
        }
    }
}
 
Example 8
Source File: TracingClientCall.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void onMessage(RESPONSE message) {
    if (methodDescriptor.getType().serverSendsOneMessage()) {
        super.onMessage(message);
        return;
    }

    final AbstractSpan span = ContextManager.createLocalSpan(operationPrefix + RESPONSE_ON_MESSAGE_OPERATION_NAME);
    span.setComponent(ComponentsDefine.GRPC);
    span.setLayer(SpanLayer.RPC_FRAMEWORK);
    ContextManager.continued(contextSnapshot);

    try {
        delegate().onMessage(message);
    } catch (Throwable t) {
        ContextManager.activeSpan().errorOccurred().log(t);
    } finally {
        ContextManager.stopSpan();
    }
}
 
Example 9
Source File: TracingClientCall.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void cancel(@Nullable String message, @Nullable Throwable cause) {
    final AbstractSpan span = ContextManager.createLocalSpan(operationPrefix + REQUEST_ON_CANCEL_OPERATION_NAME);
    span.setComponent(ComponentsDefine.GRPC);
    span.setLayer(SpanLayer.RPC_FRAMEWORK);
    ContextManager.continued(snapshot);

    if (cause != null) {
        span.log(cause);
    }

    try {
        super.cancel(message, cause);
    } catch (Throwable t) {
        ContextManager.activeSpan().errorOccurred().log(t);
        throw t;
    } finally {
        ContextManager.stopSpan();
    }
}
 
Example 10
Source File: SWCallable.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public V call() throws Exception {
    AbstractSpan span = ContextManager.createLocalSpan(SWCallable.OPERATION_NAME);
    span.setComponent(ComponentsDefine.SPRING_ASYNC);
    try {
        ContextManager.continued(snapshot);
        return callable.call();
    } catch (Exception e) {
        ContextManager.activeSpan().errorOccurred().log(e);
        throw e;
    } finally {
        ContextManager.stopSpan();
    }
}
 
Example 11
Source File: HystrixCommandGetFallbackInterceptor.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 {
    EnhanceRequireObjectCache enhanceRequireObjectCache = (EnhanceRequireObjectCache) objInst.getSkyWalkingDynamicField();
    ContextSnapshot snapshot = enhanceRequireObjectCache.getContextSnapshot();

    AbstractSpan activeSpan = ContextManager.createLocalSpan(enhanceRequireObjectCache.getOperationNamePrefix() + "/Fallback");
    activeSpan.setComponent(ComponentsDefine.HYSTRIX);
    if (snapshot != null) {
        ContextManager.continued(snapshot);
    }
}
 
Example 12
Source File: OnSuccessInterceptor.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 {
    SendCallBackEnhanceInfo enhanceInfo = (SendCallBackEnhanceInfo) objInst.getSkyWalkingDynamicField();
    AbstractSpan activeSpan = ContextManager.createLocalSpan(CALLBACK_OPERATION_NAME_PREFIX + enhanceInfo.getTopicId() + "/Producer/Callback");
    activeSpan.setComponent(ComponentsDefine.ROCKET_MQ_PRODUCER);
    SendStatus sendStatus = ((SendResult) allArguments[0]).getSendStatus();
    if (sendStatus != SendStatus.SEND_OK) {
        activeSpan.errorOccurred();
        Tags.STATUS_CODE.set(activeSpan, sendStatus.name());
    }
    ContextManager.continued(enhanceInfo.getContextSnapshot());
}
 
Example 13
Source File: JobExecutorInterceptor.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 {
    ShardingContexts shardingContexts = (ShardingContexts) allArguments[0];
    Integer item = (Integer) allArguments[1];
    ShardingContext shardingContext = new ShardingContext(shardingContexts, item);
    String operateName = shardingContext.getJobName();
    if (!Strings.isNullOrEmpty(shardingContext.getShardingParameter())) {
        operateName += "-" + shardingContext.getShardingParameter();
    }
    AbstractSpan span = ContextManager.createLocalSpan(operateName);
    span.setComponent(ComponentsDefine.ELASTIC_JOB);
    span.tag(Tags.ofKey("sharding_context"), shardingContext.toString());
}
 
Example 14
Source File: TagAnnotationTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    methodInterceptor = new TagAnnotationMethodInterceptor();
    tagInterceptor = new ActiveSpanTagInterceptor();
    tagParameters = new Object[] {"testTagKey", "testTagValue"};
    tagParameterTypes = new Class[] {String.class, String.class};

    String operationName = "testMethod";
    ContextManager.createLocalSpan(operationName);
}
 
Example 15
Source File: GsonToJsonInterceptor.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 {

    AbstractSpan span = ContextManager.createLocalSpan("Gson/ToJson");
    span.setComponent(ComponentsDefine.GSON);
    int length = allArguments[0].toString().length();
    span.tag(Tags.ofKey("length"), Integer.toString(length));

}
 
Example 16
Source File: GsonFromJsonInterceptor.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 {

    AbstractSpan span = ContextManager.createLocalSpan("Gson/FromJson");
    span.setComponent(ComponentsDefine.GSON);
    int length = allArguments[0].toString().length();
    span.tag(Tags.ofKey("length"), Integer.toString(length));

}
 
Example 17
Source File: EhcacheOperateElementInterceptor.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 {
    EhcacheEnhanceInfo enhanceInfo = (EhcacheEnhanceInfo) objInst.getSkyWalkingDynamicField();

    AbstractSpan span = ContextManager.createLocalSpan("Ehcache/" + method.getName() + "/" + enhanceInfo.getCacheName());
    span.setComponent(ComponentsDefine.EHCACHE);
    SpanLayer.asCache(span);

    Element element = (Element) allArguments[0];
    if (element != null && element.getObjectKey() != null) {
        Tags.DB_STATEMENT.set(span, element.getObjectKey().toString());
    }
}
 
Example 18
Source File: SendCallbackInterceptor.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 {
    SendCallbackEnhanceRequiredInfo requiredInfo = (SendCallbackEnhanceRequiredInfo) objInst.getSkyWalkingDynamicField();
    if (null != requiredInfo.getContextSnapshot()) {
        AbstractSpan activeSpan = ContextManager.createLocalSpan(OPERATION_NAME);
        activeSpan.setComponent(ComponentsDefine.PULSAR_PRODUCER);
        Tags.MQ_TOPIC.set(activeSpan, requiredInfo.getTopic());
        SpanLayer.asMQ(activeSpan);
        ContextManager.continued(requiredInfo.getContextSnapshot());
    }
}
 
Example 19
Source File: FutureGetInterceptor.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    EnhanceCacheObjects cacheValues = (EnhanceCacheObjects) objInst.getSkyWalkingDynamicField();
    ContextManager.createLocalSpan("future/get:" + cacheValues.getOperationName());
}
 
Example 20
Source File: SpringAnnotationInterceptor.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    String operationName = objInst.getClass().getName() + "." + method.getName();
    ContextManager.createLocalSpan(operationName);
}