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

The following examples show how to use org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan#setLayer() . 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: 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 2
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 3
Source File: TracingServerCall.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void sendMessage(RESPONSE message) {
    // We just create the request on message span for server stream calls.
    if (!getMethodDescriptor().getType().serverSendsOneMessage()) {
        final AbstractSpan span = ContextManager.createLocalSpan(operationPrefix + RESPONSE_ON_MESSAGE_OPERATION_NAME);
        span.setComponent(ComponentsDefine.GRPC);
        span.setLayer(SpanLayer.RPC_FRAMEWORK);
        ContextManager.continued(contextSnapshot);

        try {
            super.sendMessage(message);
        } catch (Throwable t) {
            ContextManager.activeSpan().errorOccurred().log(t);
            throw t;
        } finally {
            ContextManager.stopSpan();
        }
    } else {
        super.sendMessage(message);
    }
}
 
Example 4
Source File: TracingServerCallListener.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void onHalfClose() {
    final AbstractSpan span = ContextManager.createLocalSpan(operationPrefix + REQUEST_ON_COMPLETE_OPERATION_NAME);
    span.setComponent(ComponentsDefine.GRPC);
    span.setLayer(SpanLayer.RPC_FRAMEWORK);
    ContextManager.continued(contextSnapshot);

    try {
        super.onHalfClose();
    } catch (Throwable t) {
        ContextManager.activeSpan().errorOccurred().log(t);
        throw t;
    } finally {
        ContextManager.stopSpan();
    }
}
 
Example 5
Source File: TracingServerCallListener.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void onCancel() {
    final AbstractSpan span = ContextManager.createLocalSpan(operationPrefix + REQUEST_ON_CANCEL_OPERATION_NAME);
    span.setComponent(ComponentsDefine.GRPC);
    span.setLayer(SpanLayer.RPC_FRAMEWORK);
    ContextManager.continued(contextSnapshot);

    try {
        super.onCancel();
    } catch (Throwable t) {
        ContextManager.activeSpan().errorOccurred().log(t);
        throw t;
    } finally {
        ContextManager.stopSpan();
    }
}
 
Example 6
Source File: TracingServerCallListener.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void onMessage(REQUEST message) {
    // We just create the request on message span for client stream calls.
    if (!methodType.clientSendsOneMessage()) {
        final AbstractSpan span = ContextManager.createLocalSpan(operationPrefix + REQUEST_ON_MESSAGE_OPERATION_NAME);
        span.setComponent(ComponentsDefine.GRPC);
        span.setLayer(SpanLayer.RPC_FRAMEWORK);
        ContextManager.continued(contextSnapshot);
        try {
            super.onMessage(message);
        } catch (Throwable t) {
            ContextManager.activeSpan().errorOccurred().log(t);
            throw t;
        } finally {
            ContextManager.stopSpan();
        }
    } else {
        super.onMessage(message);
    }
}
 
Example 7
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 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: ArmeriaClientInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
protected void beforeMethod(final URI uri, final HttpMethod httpMethod, final String path) {
    final ContextCarrier contextCarrier = new ContextCarrier();
    final String remotePeer = uri.getHost() + ":" + uri.getPort();

    final AbstractSpan exitSpan = ContextManager.createExitSpan(path, contextCarrier, remotePeer);

    exitSpan.setComponent(ComponentsDefine.ARMERIA);
    exitSpan.setLayer(SpanLayer.HTTP);
    Tags.HTTP.METHOD.set(exitSpan, httpMethod.name());

    ContextManager.getRuntimeContext().put(KEY_SAFE_CLOSEABLE, Clients.withHttpHeaders(headers -> {
        HttpHeadersBuilder builder = headers.toBuilder();
        for (CarrierItem item = contextCarrier.items(); item.hasNext(); ) {
            item = item.next();
            builder.add(AsciiString.of(item.getHeadKey()), item.getHeadValue());
        }
        return builder.build();
    }));
}
 
Example 10
Source File: TracingClientCall.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void halfClose() {
    final AbstractSpan span = ContextManager.createLocalSpan(operationPrefix + REQUEST_ON_COMPLETE_OPERATION_NAME);
    span.setComponent(ComponentsDefine.GRPC);
    span.setLayer(SpanLayer.RPC_FRAMEWORK);
    ContextManager.continued(snapshot);

    try {
        super.halfClose();
    } catch (Throwable t) {
        ContextManager.activeSpan().errorOccurred().log(t);
        throw t;
    } finally {
        ContextManager.stopSpan();
    }
}
 
Example 11
Source File: TracingClientCall.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void sendMessage(REQUEST message) {
    if (methodDescriptor.getType().clientSendsOneMessage()) {
        super.sendMessage(message);
        return;
    }

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

    try {
        super.sendMessage(message);
    } catch (Throwable t) {
        ContextManager.activeSpan().errorOccurred().log(t);
        throw t;
    } finally {
        ContextManager.stopSpan();
    }
}
 
Example 12
Source File: AbstractMessageConsumeInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public final void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
    Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    List<MessageExt> msgs = (List<MessageExt>) allArguments[0];

    ContextCarrier contextCarrier = getContextCarrierFromMessage(msgs.get(0));
    AbstractSpan span = ContextManager.createEntrySpan(CONSUMER_OPERATION_NAME_PREFIX + msgs.get(0)
                                                                                            .getTopic() + "/Consumer", contextCarrier);

    span.setComponent(ComponentsDefine.ROCKET_MQ_CONSUMER);
    span.setLayer(SpanLayer.MQ);
    for (int i = 1; i < msgs.size(); i++) {
        ContextManager.extract(getContextCarrierFromMessage(msgs.get(i)));
    }

}
 
Example 13
Source File: Armeria084ServerInterceptor.java    From skywalking with Apache License 2.0 6 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 {

    DefaultHttpRequest httpRequest = (DefaultHttpRequest) allArguments[1];
    HttpHeaders headers = httpRequest.headers();

    ContextCarrier carrier = new ContextCarrier();
    for (CarrierItem item = carrier.items(); item.hasNext(); ) {
        item = item.next();
        item.setHeadValue(headers.get(AsciiString.of(item.getHeadKey())));
    }

    AbstractSpan entrySpan = ContextManager.createEntrySpan(httpRequest.path(), carrier);
    entrySpan.setComponent(ComponentsDefine.ARMERIA);
    entrySpan.setLayer(SpanLayer.HTTP);
    entrySpan.setPeer(httpRequest.authority());
    Tags.URL.set(entrySpan, httpRequest.path());
    Tags.HTTP.METHOD.set(entrySpan, httpRequest.method().name());
}
 
Example 14
Source File: Armeria085ServerInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeMethod(final EnhancedInstance objInst, final Method method, final Object[] allArguments,
                         final Class<?>[] argumentsTypes, final MethodInterceptResult result) {

    DefaultHttpRequest httpRequest = (DefaultHttpRequest) allArguments[1];
    HttpHeaders headers = httpRequest.headers();

    ContextCarrier carrier = new ContextCarrier();
    for (CarrierItem item = carrier.items(); item.hasNext(); ) {
        item = item.next();
        item.setHeadValue(headers.get(AsciiString.of(item.getHeadKey())));
    }

    AbstractSpan entrySpan = ContextManager.createEntrySpan(httpRequest.path(), carrier);
    entrySpan.setComponent(ComponentsDefine.ARMERIA);
    entrySpan.setLayer(SpanLayer.HTTP);
    entrySpan.setPeer(httpRequest.authority());
    Tags.URL.set(entrySpan, httpRequest.path());
    Tags.HTTP.METHOD.set(entrySpan, httpRequest.method().name());
}
 
Example 15
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();
        }
    }
}
 
Example 16
Source File: BlockingCallInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes,
    MethodInterceptResult result) {
    Channel channel = (Channel) allArguments[0];
    MethodDescriptor<?, ?> methodDescriptor = (MethodDescriptor<?, ?>) allArguments[1];
    final AbstractSpan span = ContextManager.createExitSpan(formatOperationName(methodDescriptor), channel.authority());
    span.setComponent(ComponentsDefine.GRPC);
    span.setLayer(SpanLayer.RPC_FRAMEWORK);
    ContextManager.getRuntimeContext().put(BLOCKING_CALL_EXIT_SPAN, span);
}
 
Example 17
Source File: Armeria084ClientInterceptor.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 {

    final UserClient userClient = (UserClient) objInst;
    final URI uri = userClient.uri();
    final HttpMethod httpMethod = (HttpMethod) allArguments[1];
    final String path = (String) allArguments[2];
    final Object req = allArguments[5];

    if (!(req instanceof HttpRequest)) {
        return;
    }

    final HttpRequest httpReq = (HttpRequest) req;

    final ContextCarrier contextCarrier = new ContextCarrier();
    final String remotePeer = uri.getHost() + ":" + uri.getPort();

    final AbstractSpan exitSpan = ContextManager.createExitSpan(path, contextCarrier, remotePeer);

    exitSpan.setComponent(ComponentsDefine.ARMERIA);
    exitSpan.setLayer(SpanLayer.HTTP);
    Tags.HTTP.METHOD.set(exitSpan, httpMethod.name());

    HttpHeaders headers = httpReq.headers();
    for (CarrierItem item = contextCarrier.items(); item.hasNext(); ) {
        item = item.next();
        headers.add(AsciiString.of(item.getHeadKey()), item.getHeadValue());
    }
}
 
Example 18
Source File: TracingServerCall.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Override
public void close(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);
    switch (status.getCode()) {
        case OK:
            break;
        // UNKNOWN/INTERNAL status code will case error in this span.
        // Those status code means some unexpected error occurred in server.
        // Similar to 5XX in HTTP status.
        case UNKNOWN:
        case INTERNAL:
            if (status.getCause() == null) {
                span.errorOccurred().log(status.asRuntimeException());
            } else {
                span.errorOccurred().log(status.getCause());
            }
            break;
        // Other status code means some predictable error occurred in server.
        // Like PERMISSION_DENIED or UNAUTHENTICATED somethings.
        // Similar to 4XX in HTTP status.
        default:
            // But if the status still has cause exception, we will log it too.
            if (status.getCause() != null) {
                span.errorOccurred().log(status.getCause());
            }
            break;
    }
    Tags.STATUS_CODE.set(span, status.getCode().name());

    try {
        super.close(status, trailers);
    } catch (Throwable t) {
        ContextManager.activeSpan().errorOccurred().log(t);
        throw t;
    } finally {
        ContextManager.stopSpan();
    }
}