Java Code Examples for org.apache.skywalking.apm.agent.core.context.CarrierItem#hasNext()

The following examples show how to use org.apache.skywalking.apm.agent.core.context.CarrierItem#hasNext() . 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: SkywalkingTraceFactory.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
@Override
public void begin(final String name, final String component, final Map<String, String> tags) {
    Map<String, String> ctx = (Map<String, String>) invocation.removeAttachment(HIDDEN_KEY_TRACE_SKYWALKING);
    ContextCarrier contextCarrier = new ContextCarrier();
    if (ctx != null) {
        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            next.setHeadValue(ctx.get(next.getHeadKey()));
        }
    }
    span = ContextManager.createEntrySpan(name, contextCarrier);
    span.setComponent(new OfficialComponent(componentId, component));
    tag(tags);
    SpanLayer.asRPCFramework(span);
}
 
Example 2
Source File: SofaRpcProviderInterceptor.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 {
    SofaRequest sofaRequest = (SofaRequest) allArguments[0];

    AbstractSpan span = null;

    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        final String headKey = next.getHeadKey();
        final Object attachment = sofaRequest.getRequestProp(SKYWALKING_PREFIX + headKey);
        if (attachment != null) {
            next.setHeadValue(attachment.toString());
        } else {
            next.setHeadValue("");
        }
    }
    span = ContextManager.createEntrySpan(generateViewPoint(sofaRequest), contextCarrier);

    span.setComponent(ComponentsDefine.SOFARPC);
    SpanLayer.asRPCFramework(span);
}
 
Example 3
Source File: ClientInterceptor.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 {
    Request request = (Request) allArguments[0];
    InetSocketAddress remoteAddress = (InetSocketAddress) request.getChannel().remoteAddress();
    InetAddress address = remoteAddress.getAddress();

    final ContextCarrier contextCarrier = new ContextCarrier();
    AbstractSpan span = ContextManager.createExitSpan(generateOperationName(request), contextCarrier, address.getHostAddress() + ":" + remoteAddress.getPort());
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        if (request.getKvAttachment() == null) {
            request.setKvAttachment(new HashMap<>());
        }
        request.getKvAttachment().put(next.getHeadKey(), next.getHeadValue());
    }
    span.setComponent(ComponentsDefine.BRPC_JAVA);
    SpanLayer.asRPCFramework(span);
}
 
Example 4
Source File: TransportClientHandlerInterceptor.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 {
    Invocation invocation = (Invocation) allArguments[0];
    if (!checkRegisterStatus(invocation)) {
        return;
    }
    URI uri = new URI(invocation.getEndpoint().toString());
    String peer = uri.getHost() + ":" + uri.getPort();
    String operationName = invocation.getMicroserviceQualifiedName();
    final ContextCarrier contextCarrier = new ContextCarrier();
    AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, peer);
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        invocation.getContext().put(next.getHeadKey(), next.getHeadValue());
    }
    String url = invocation.getOperationMeta().getOperationPath();
    Tags.URL.set(span, url);
    span.setComponent(ComponentsDefine.SERVICECOMB);
    SpanLayer.asRPCFramework(span);
}
 
Example 5
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 6
Source File: HandleInterceptor.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 {
    HttpChannel httpChannel = (HttpChannel) objInst;
    HttpServletRequest servletRequest = httpChannel.getRequest();

    ContextCarrier contextCarrier = new ContextCarrier();

    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        next.setHeadValue(servletRequest.getHeader(next.getHeadKey()));
    }

    AbstractSpan span = ContextManager.createEntrySpan(servletRequest.getRequestURI(), contextCarrier);
    Tags.URL.set(span, servletRequest.getRequestURL().toString());
    Tags.HTTP.METHOD.set(span, servletRequest.getMethod());
    span.setComponent(ComponentsDefine.JETTY_SERVER);
    SpanLayer.asHttp(span);
}
 
Example 7
Source File: SkywalkingTracerExtractInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    Object ret) throws Throwable {
    Format format = (Format) allArguments[0];
    if (Format.Builtin.TEXT_MAP.equals(format) || Format.Builtin.HTTP_HEADERS.equals(format)) {
        TextMap textMapCarrier = (TextMap) allArguments[1];

        ContextCarrier contextCarrier = new ContextCarrier();
        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            Iterator<Map.Entry<String, String>> iterator = textMapCarrier.iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                if (next.getHeadKey().equals(entry.getKey())) {
                    next.setHeadValue(entry.getValue());
                    break;
                }
            }
        }
        ContextManager.extract(contextCarrier);
    }
    return new SkywalkingContext();
}
 
Example 8
Source File: SkywalkingTracerInjectInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    Object ret) throws Throwable {
    Format format = (Format) allArguments[1];
    if (Format.Builtin.TEXT_MAP.equals(format) || Format.Builtin.HTTP_HEADERS.equals(format)) {
        TextMap carrier = (TextMap) allArguments[2];
        ContextCarrier contextCarrier = new ContextCarrier();
        ContextManager.inject(contextCarrier);
        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            carrier.put(next.getHeadKey(), next.getHeadValue());
        }
    } else {
        //Don't support other format yet.
    }

    return null;
}
 
Example 9
Source File: ProducerOperationHandlerInterceptor.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 {
    Invocation invocation = (Invocation) allArguments[0];
    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        next.setHeadValue(invocation.getContext().get(next.getHeadKey()));
    }
    String operationName = invocation.getMicroserviceQualifiedName();
    AbstractSpan span = ContextManager.createEntrySpan(operationName, contextCarrier);
    String url = invocation.getOperationMeta().getOperationPath();
    Tags.URL.set(span, url);
    span.setComponent(ComponentsDefine.SERVICECOMB);
    SpanLayer.asRPCFramework(span);
}
 
Example 10
Source File: ResinV4Interceptor.java    From java-plugin-extensions 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 {
    CauchoRequest request = (CauchoRequest)allArguments[0];
    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        next.setHeadValue(request.getHeader(next.getHeadKey()));
    }
    AbstractSpan span = ContextManager.createEntrySpan(request.getPageURI(), contextCarrier);
    span.setComponent(ComponentsDefine.RESIN);
    Tags.URL.set(span, appendRequestURL(request));
    SpanLayer.asHttp(span);

}
 
Example 11
Source File: HttpClientExecuteInterceptor.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 || allArguments[1] == null) {
        // illegal args, can't trace. ignore.
        return;
    }
    final HttpHost httpHost = (HttpHost) allArguments[0];
    HttpRequest httpRequest = (HttpRequest) allArguments[1];
    final ContextCarrier contextCarrier = new ContextCarrier();

    String remotePeer = httpHost.getHostName() + ":" + port(httpHost);

    String uri = httpRequest.getRequestLine().getUri();
    String requestURI = getRequestURI(uri);
    String operationName = requestURI;
    AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, remotePeer);

    span.setComponent(ComponentsDefine.HTTPCLIENT);
    Tags.URL.set(span, buildSpanValue(httpHost, uri));
    Tags.HTTP.METHOD.set(span, httpRequest.getRequestLine().getMethod());
    SpanLayer.asHttp(span);

    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        httpRequest.setHeader(next.getHeadKey(), next.getHeadValue());
    }
}
 
Example 12
Source File: ClusteredEventBusSendRemoteInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    ContextManager.getRuntimeContext().remove(VertxContext.STOP_SPAN_NECESSARY + "." + getClass().getName());

    ClusteredMessage message = (ClusteredMessage) allArguments[1];
    if (VertxContext.hasContext(message.address())) {
        VertxContext context = VertxContext.popContext(message.address());
        context.getSpan().asyncFinish();
    } else {
        ServerID sender = (ServerID) allArguments[0];
        ContextCarrier contextCarrier = new ContextCarrier();
        AbstractSpan span = ContextManager.createExitSpan(message.address(), contextCarrier, sender.toString());
        span.setComponent(ComponentsDefine.VERTX);
        SpanLayer.asRPCFramework(span);

        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            message.headers().add(next.getHeadKey(), next.getHeadValue());
        }

        if (message.replyAddress() != null) {
            VertxContext.pushContext(message.replyAddress(), new VertxContext(ContextManager.capture(), span.prepareForAsync()));
        }
        ContextManager.getRuntimeContext().put(VertxContext.STOP_SPAN_NECESSARY + "." + getClass().getName(), true);
    }
}
 
Example 13
Source File: HttpAsyncRequestExecutorInterceptor.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 {
    HttpContext context = CONTEXT_LOCAL.get();
    CONTEXT_LOCAL.remove();
    if (context == null) {
        return;
    }
    final ContextCarrier contextCarrier = new ContextCarrier();
    HttpRequestWrapper requestWrapper = (HttpRequestWrapper) context.getAttribute(HttpClientContext.HTTP_REQUEST);
    HttpHost httpHost = (HttpHost) context.getAttribute(HttpClientContext.HTTP_TARGET_HOST);

    RequestLine requestLine = requestWrapper.getRequestLine();
    String uri = requestLine.getUri();
    String operationName = uri.startsWith("http") ? new URL(uri).getPath() : uri;
    int port = httpHost.getPort();
    AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, httpHost.getHostName() + ":" + (port == -1 ? 80 : port));
    span.setComponent(ComponentsDefine.HTTP_ASYNC_CLIENT);
    Tags.URL.set(span, requestWrapper.getOriginal().getRequestLine().getUri());
    Tags.HTTP.METHOD.set(span, requestLine.getMethod());
    SpanLayer.asHttp(span);
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        requestWrapper.setHeader(next.getHeadKey(), next.getHeadValue());
    }
}
 
Example 14
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 15
Source File: MessageSendInterceptor.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 {
    Message message = (Message) allArguments[2];
    ContextCarrier contextCarrier = new ContextCarrier();
    String namingServiceAddress = String.valueOf(objInst.getSkyWalkingDynamicField());
    AbstractSpan span = ContextManager.createExitSpan(buildOperationName(message.getTopic()), contextCarrier, namingServiceAddress);
    span.setComponent(ComponentsDefine.ROCKET_MQ_PRODUCER);
    Tags.MQ_BROKER.set(span, (String) allArguments[0]);
    Tags.MQ_TOPIC.set(span, message.getTopic());
    SpanLayer.asMQ(span);

    SendMessageRequestHeader requestHeader = (SendMessageRequestHeader) allArguments[3];
    StringBuilder properties = new StringBuilder(requestHeader.getProperties());
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        if (!StringUtil.isEmpty(next.getHeadValue())) {
            properties.append(next.getHeadKey());
            properties.append(NAME_VALUE_SEPARATOR);
            properties.append(next.getHeadValue());
            properties.append(PROPERTY_SEPARATOR);
        }
    }
    requestHeader.setProperties(properties.toString());

    if (allArguments[6] != null) {
        ((EnhancedInstance) allArguments[6]).setSkyWalkingDynamicField(new SendCallBackEnhanceInfo(message.getTopic(), ContextManager
            .capture()));
    }
}
 
Example 16
Source File: SWClientRPCPlugin.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void clientSendRequest(RPCContext context) {
    ContextCarrier carrier = new ContextCarrier();
    ContextManager.inject(carrier);

    CarrierItem items = carrier.items();
    while (items.hasNext()) {
        items = items.next();
        context.requestCallMeta().put(items.getHeadKey(), ByteBuffer.wrap(items.getHeadValue().getBytes()));
    }
}
 
Example 17
Source File: SkywalkingTraceFactory.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
public void begin(final String name, final String component, final Map<String, String> tags) {
    Map<String, String> ctx = new HashMap<>();
    ContextCarrier contextCarrier = new ContextCarrier();
    span = ContextManager.createExitSpan(name, contextCarrier, "unknown");
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        ctx.put(next.getHeadKey(), next.getHeadValue());
    }
    invocation.addAttachment(HIDDEN_KEY_TRACE_SKYWALKING, ctx);
    span.setComponent(new OfficialComponent(componentId, component));
    tag(tags);
    SpanLayer.asRPCFramework(span);
}
 
Example 18
Source File: RestRequestInterceptor.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 {
    ClientHttpRequest clientHttpRequest = (ClientHttpRequest) ret;
    if (clientHttpRequest instanceof AbstractClientHttpRequest) {
        AbstractClientHttpRequest httpRequest = (AbstractClientHttpRequest) clientHttpRequest;
        ContextCarrier contextCarrier = (ContextCarrier) objInst.getSkyWalkingDynamicField();
        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            httpRequest.getHeaders().set(next.getHeadKey(), next.getHeadValue());
        }
    }
    return ret;
}
 
Example 19
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 20
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);
}