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

The following examples show how to use org.apache.skywalking.apm.agent.core.context.CarrierItem#next() . 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: CodecUtilsTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private void assertSwContextCarrier(SWContextCarrier expected, SWContextCarrier actual) {
    assertThat(expected.getOperationName(), is(actual.getOperationName()));
    Map<String, String> data = new HashMap<>();
    if (actual.getCarrier() == null) {
        assertNull(expected.getCarrier());
    } else {
        CarrierItem next = expected.getCarrier().items();
        while (next.hasNext()) {
            next = next.next();
            data.put(next.getHeadKey(), next.getHeadValue());
        }
        next = actual.getCarrier().items();
        while (next.hasNext()) {
            next = next.next();
            assertThat(next.getHeadValue(), is(data.get(next.getHeadKey())));
        }
    }

}
 
Example 3
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 4
Source File: MotanConsumerInterceptor.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 {

    URL url = (URL) objInst.getSkyWalkingDynamicField();
    Request request = (Request) allArguments[0];
    if (url != null) {
        ContextCarrier contextCarrier = new ContextCarrier();
        String remotePeer = url.getHost() + ":" + url.getPort();
        AbstractSpan span = ContextManager.createExitSpan(generateOperationName(url, request), contextCarrier, remotePeer);
        span.setComponent(ComponentsDefine.MOTAN);
        Tags.URL.set(span, url.getIdentity());
        SpanLayer.asRPCFramework(span);
        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            request.setAttachment(next.getHeadKey(), next.getHeadValue());
        }
    }
}
 
Example 5
Source File: ServerTracingFilterInterceptorTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private void runWithContext(final TestFunction function) {
    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        if (next.getHeadKey().equals(SW8CarrierItem.HEADER_NAME)) {
            next.setHeadValue("1-My40LjU=-MS4yLjM=-3-c2VydmljZQ==-aW5zdGFuY2U=-L2FwcA==-MTI3LjAuMC4xOjgwODA=");
        }
    }
    SWContextCarrier swContextCarrier = new SWContextCarrier();
    swContextCarrier.setContextCarrier(contextCarrier);
    swContextCarrier.setOperationName(rpc);
    Contexts.broadcast().let(SWContextCarrier$.MODULE$, swContextCarrier, new AbstractFunction0<Void>() {
        @Override
        public Void apply() {
            try {
                function.apply();
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
            return null;
        }
    });
}
 
Example 6
Source File: CodecUtils.java    From skywalking with Apache License 2.0 6 votes vote down vote up
/**
 * Encodes the swContextCarrier to byte array.
 *
 * SWContextCarrier consists of some strings, such as operation name, {@link CarrierItem#getHeadKey()},
 * {@link CarrierItem#getHeadValue()}. We encode each string to byte array by
 * {@link #encodeStringToBytes(String)}, then assemble these byte arrays in a certain order, each byte array has
 * a fixed length of 4 bytes before it to record the length. The format as follow:
 *
 * |length of following byte array,4 byte|byte array|length of following byte array,4 byte|byte array|...
 *
 * The first byte array is operation name, followed by byte array of the key of first {@code CarrierItem},
 * followed by byte array of the value of first {@code CarrierItem}, followed by byte array of second {@code
 * CarrierItem}, and so on.
 *
 * @param swContextCarrier the swContextCarrier to encode
 * @return
 */
static Buf encode(SWContextCarrier swContextCarrier) {
    if (StringUtil.isNotEmpty(swContextCarrier.getOperationName())
            && swContextCarrier.getCarrier() != null) {
        ByteArrayOutputStream bos = getBos();
        try (DataOutputStream dos = new DataOutputStream(bos)) {
            putString(dos, swContextCarrier.getOperationName());
            CarrierItem next = swContextCarrier.getCarrier().items();
            while (next.hasNext()) {
                next = next.next();
                if (next.getHeadKey() != null && next.getHeadValue() != null) {
                    putString(dos, next.getHeadKey());
                    putString(dos, next.getHeadValue());
                }
            }
            bos.flush();
            return Bufs.ownedBuf(bos.toByteArray());
        } catch (Exception e) {
            LOGGER.error("encode swContextCarrier exception.", e);
        }
    }
    return Bufs.EMPTY;
}
 
Example 7
Source File: SolrConnectorInterceptor.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 (ContextManager.isActive()) {
        HttpRequestBase request = (HttpRequestBase) allArguments[0];

        ContextCarrier carrier = new ContextCarrier();
        ContextManager.inject(carrier);

        CarrierItem items = carrier.items();
        while (items.hasNext()) {
            items = items.next();
            request.setHeader(items.getHeadKey(), items.getHeadValue());
        }
    }
}
 
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: 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 10
Source File: Struts2Interceptor.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 {
    HttpServletRequest request = ServletActionContext.getRequest();
    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.getRequestURI(), contextCarrier);
    Tags.URL.set(span, request.getRequestURL().toString());
    Tags.HTTP.METHOD.set(span, request.getMethod());
    span.setComponent(ComponentsDefine.STRUTS2);
    SpanLayer.asHttp(span);
}
 
Example 11
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 12
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 13
Source File: AbstractMessageConsumeInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private ContextCarrier getContextCarrierFromMessage(MessageExt message) {
    ContextCarrier contextCarrier = new ContextCarrier();

    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        next.setHeadValue(message.getUserProperty(next.getHeadKey()));
    }

    return contextCarrier;
}
 
Example 14
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 15
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 16
Source File: ActiveMQProducerInterceptor.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 {
    ContextCarrier contextCarrier = new ContextCarrier();
    ActiveMQDestination activeMQDestination = (ActiveMQDestination) allArguments[0];
    Message message = (Message) allArguments[1];
    String url = (String) objInst.getSkyWalkingDynamicField();
    AbstractSpan activeSpan = null;
    if (activeMQDestination.getDestinationType() == QUEUE_TYPE || activeMQDestination.getDestinationType() == TEMP_QUEUE_TYPE) {
        activeSpan = ContextManager.createExitSpan(OPERATE_NAME_PREFIX + "Queue/" + activeMQDestination.getPhysicalName() + PRODUCER_OPERATE_NAME_SUFFIX, contextCarrier, url);
        Tags.MQ_BROKER.set(activeSpan, url);
        Tags.MQ_QUEUE.set(activeSpan, activeMQDestination.getPhysicalName());

    } else if (activeMQDestination.getDestinationType() == TOPIC_TYPE || activeMQDestination.getDestinationType() == TEMP_TOPIC_TYPE) {
        activeSpan = ContextManager.createExitSpan(OPERATE_NAME_PREFIX + "Topic/" + activeMQDestination.getPhysicalName() + PRODUCER_OPERATE_NAME_SUFFIX, contextCarrier, url);
        Tags.MQ_BROKER.set(activeSpan, url);
        Tags.MQ_TOPIC.set(activeSpan, activeMQDestination.getPhysicalName());
    }
    SpanLayer.asMQ(activeSpan);
    activeSpan.setComponent(ComponentsDefine.ACTIVEMQ_PRODUCER);
    CarrierItem next = contextCarrier.items();

    while (next.hasNext()) {
        next = next.next();
        message.setStringProperty(next.getHeadKey(), next.getHeadValue());
    }

}
 
Example 17
Source File: ActiveMQConsumerInterceptor.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 {
    ContextCarrier contextCarrier = new ContextCarrier();
    String url = (String) objInst.getSkyWalkingDynamicField();
    MessageDispatch messageDispatch = (MessageDispatch) allArguments[0];
    AbstractSpan activeSpan = null;
    if (messageDispatch.getDestination().getDestinationType() == QUEUE_TYPE || messageDispatch.getDestination()
                                                                                              .getDestinationType() == TEMP_QUEUE_TYPE) {
        activeSpan = ContextManager.createEntrySpan(OPERATE_NAME_PREFIX + "Queue/" + messageDispatch.getDestination()
                                                                                                    .getPhysicalName() + CONSUMER_OPERATE_NAME_SUFFIX, null)
                                   .start(System.currentTimeMillis());
        Tags.MQ_BROKER.set(activeSpan, url);
        Tags.MQ_QUEUE.set(activeSpan, messageDispatch.getDestination().getPhysicalName());
    } else if (messageDispatch.getDestination()
                              .getDestinationType() == TOPIC_TYPE || messageDispatch.getDestination()
                                                                                    .getDestinationType() == TEMP_TOPIC_TYPE) {
        activeSpan = ContextManager.createEntrySpan(OPERATE_NAME_PREFIX + "Topic/" + messageDispatch.getDestination()
                                                                                                    .getPhysicalName() + CONSUMER_OPERATE_NAME_SUFFIX, null)
                                   .start(System.currentTimeMillis());
        Tags.MQ_BROKER.set(activeSpan, url);
        Tags.MQ_TOPIC.set(activeSpan, messageDispatch.getDestination().getPhysicalName());
    }
    activeSpan.setComponent(ComponentsDefine.ACTIVEMQ_CONSUMER);
    SpanLayer.asMQ(activeSpan);
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        Object propertyValue = messageDispatch.getMessage().getProperty(next.getHeadKey());
        if (propertyValue != null) {
            next.setHeadValue(propertyValue.toString());
        }
    }
    ContextManager.extract(contextCarrier);

}
 
Example 18
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 19
Source File: SofaRpcConsumerInterceptor.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 {
    SofaRequest sofaRequest = (SofaRequest) allArguments[0];
    RpcInternalContext rpcContext = RpcInternalContext.getContext();

    ProviderInfo providerInfo = rpcContext.getProviderInfo();

    AbstractSpan span = null;

    final String host = providerInfo.getHost();
    final int port = providerInfo.getPort();
    final ContextCarrier contextCarrier = new ContextCarrier();
    final String operationName = generateOperationName(providerInfo, sofaRequest);
    span = ContextManager.createExitSpan(operationName, contextCarrier, host + ":" + port);
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        String key = next.getHeadKey();
        String skyWalkingKey = SKYWALKING_PREFIX + key;
        sofaRequest.addRequestProp(skyWalkingKey, next.getHeadValue());
    }

    Tags.URL.set(span, generateRequestURL(providerInfo, sofaRequest));
    span.setComponent(ComponentsDefine.SOFARPC);
    SpanLayer.asRPCFramework(span);
}
 
Example 20
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 {
    AsyncClientHttpRequest clientHttpRequest = (AsyncClientHttpRequest) ret;
    if (ret != null) {
        Object[] cacheValues = (Object[]) objInst.getSkyWalkingDynamicField();
        ContextCarrier contextCarrier = (ContextCarrier) cacheValues[1];
        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            clientHttpRequest.getHeaders().set(next.getHeadKey(), next.getHeadValue());
        }
    }
    return ret;
}