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

The following examples show how to use org.apache.skywalking.apm.agent.core.context.CarrierItem#setHeadValue() . 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: 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 2
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 3
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 4
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 5
Source File: PulsarConsumerInterceptor.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 (allArguments[0] != null) {
        ConsumerEnhanceRequiredInfo requiredInfo = (ConsumerEnhanceRequiredInfo) objInst.getSkyWalkingDynamicField();
        Message msg = (Message) allArguments[0];
        ContextCarrier carrier = new ContextCarrier();
        CarrierItem next = carrier.items();
        while (next.hasNext()) {
            next = next.next();
            next.setHeadValue(msg.getProperty(next.getHeadKey()));
        }
        AbstractSpan activeSpan = ContextManager.createEntrySpan(OPERATE_NAME_PREFIX + requiredInfo.getTopic() + CONSUMER_OPERATE_NAME + requiredInfo
            .getSubscriptionName(), carrier);
        activeSpan.setComponent(ComponentsDefine.PULSAR_CONSUMER);
        SpanLayer.asMQ(activeSpan);
        Tags.MQ_BROKER.set(activeSpan, requiredInfo.getServiceUrl());
        Tags.MQ_TOPIC.set(activeSpan, requiredInfo.getTopic());
    }
}
 
Example 6
Source File: SynchronousDispatcherInterceptor.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 {
    HttpRequest request = (HttpRequest) allArguments[0];

    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        next.setHeadValue(request.getHttpHeaders().getHeaderString(next.getHeadKey()));
    }

    AbstractSpan span = ContextManager.createEntrySpan(request.getUri().getPath(), contextCarrier);
    Tags.URL.set(span, toPath(request.getUri().getRequestUri().toString()));
    Tags.HTTP.METHOD.set(span, request.getHttpMethod());
    span.setComponent(ComponentsDefine.RESTEASY);
    SpanLayer.asHttp(span);
}
 
Example 7
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 8
Source File: RabbitMQConsumerInterceptor.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 {
    ContextCarrier contextCarrier = new ContextCarrier();
    String url = (String) objInst.getSkyWalkingDynamicField();
    Envelope envelope = (Envelope) allArguments[1];
    AMQP.BasicProperties properties = (AMQP.BasicProperties) allArguments[2];
    AbstractSpan activeSpan = ContextManager.createEntrySpan(OPERATE_NAME_PREFIX + "Topic/" + envelope.getExchange() + "Queue/" + envelope
        .getRoutingKey() + CONSUMER_OPERATE_NAME_SUFFIX, null).start(System.currentTimeMillis());
    Tags.MQ_BROKER.set(activeSpan, url);
    Tags.MQ_TOPIC.set(activeSpan, envelope.getExchange());
    Tags.MQ_QUEUE.set(activeSpan, envelope.getRoutingKey());
    activeSpan.setComponent(ComponentsDefine.RABBITMQ_CONSUMER);
    SpanLayer.asMQ(activeSpan);
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        if (properties.getHeaders() != null && properties.getHeaders().get(next.getHeadKey()) != null) {
            next.setHeadValue(properties.getHeaders().get(next.getHeadKey()).toString());
        }
    }
    ContextManager.extract(contextCarrier);

}
 
Example 9
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 10
Source File: ServerConnectionHandleMessageInterceptor.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 (allArguments[0] instanceof HttpRequest) {
        HttpRequest request = (HttpRequest) allArguments[0];
        ContextCarrier contextCarrier = new ContextCarrier();
        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            next.setHeadValue(request.headers().get(next.getHeadKey()));
            request.headers().remove(next.getHeadKey());
        }

        AbstractSpan span = ContextManager.createEntrySpan(toPath(request.getUri()), contextCarrier);
        span.setComponent(ComponentsDefine.VERTX);
        SpanLayer.asHttp(span);
        Tags.HTTP.METHOD.set(span, request.getMethod().toString());
        Tags.URL.set(span, request.getUri());

        objInst.setSkyWalkingDynamicField(new VertxContext(ContextManager.capture(), span.prepareForAsync()));
    }
}
 
Example 11
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 12
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 13
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);
}
 
Example 14
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 15
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 16
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 17
Source File: ResinV3Interceptor.java    From java-plugin-extensions 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 {
    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 18
Source File: CodecUtils.java    From skywalking with Apache License 2.0 5 votes vote down vote up
/**
 * Decodes byte array to SWContextCarrier.
 *
 * First we read 4 bytes from byte array, composing them into an int value n, and read the following n bytes,
 * decodes them to a string by {@link #decodeStringFromBytes(byte[])}, and so on.
 *
 * The first string is operation name, other strings are a list pair of key and value, can be translated to
 * {@link ContextCarrier}
 *
 * For convenient, we use {@link ByteBuffer} to wrap byte array.
 *
 * @param buf the byte array to decode
 * @return
 */
static SWContextCarrier decode(Buf buf) {
    try {
        byte[] bytes = Bufs.ownedByteArray(buf);
        if (bytes == null || bytes.length == 0) {
            return EMPTY_SWCONTEXTCARRIER;
        }
        ContextCarrier contextCarrier = new ContextCarrier();
        SWContextCarrier swContextCarrier = new SWContextCarrier();
        swContextCarrier.setContextCarrier(contextCarrier);

        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        String operationName = getNextString(byteBuffer);
        if (operationName != null) {
            swContextCarrier.setOperationName(operationName);
        }

        Map<String, String> data = readToMap(byteBuffer);
        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            next.setHeadValue(data.get(next.getHeadKey()));
        }
        return swContextCarrier;
    } catch (Exception e) {
        LOGGER.error("decode swContextCarrier exception.", e);
    }
    return EMPTY_SWCONTEXTCARRIER;
}
 
Example 19
Source File: MotanProviderInterceptor.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 {
    Request request = (Request) allArguments[0];
    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        next.setHeadValue(request.getAttachments().get(next.getHeadKey()));
    }

    AbstractSpan span = ContextManager.createEntrySpan(generateViewPoint(request), contextCarrier);
    SpanLayer.asRPCFramework(span);
    span.setComponent(ComponentsDefine.MOTAN);
}
 
Example 20
Source File: KafkaConsumerInterceptor.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    Object ret) throws Throwable {
    /*
     * If the intercepted method throws exception, the ret will be null
     */
    if (ret == null) {
        return ret;
    }
    Map<TopicPartition, List<ConsumerRecord<?, ?>>> records = (Map<TopicPartition, List<ConsumerRecord<?, ?>>>) ret;
    //
    // The entry span will only be created when the consumer received at least one message.
    //
    if (records.size() > 0) {
        ConsumerEnhanceRequiredInfo requiredInfo = (ConsumerEnhanceRequiredInfo) objInst.getSkyWalkingDynamicField();
        AbstractSpan activeSpan = ContextManager.createEntrySpan(OPERATE_NAME_PREFIX + requiredInfo.getTopics() + CONSUMER_OPERATE_NAME + requiredInfo
            .getGroupId(), null).start(requiredInfo.getStartTime());

        activeSpan.setComponent(ComponentsDefine.KAFKA_CONSUMER);
        SpanLayer.asMQ(activeSpan);
        Tags.MQ_BROKER.set(activeSpan, requiredInfo.getBrokerServers());
        Tags.MQ_TOPIC.set(activeSpan, requiredInfo.getTopics());

        for (List<ConsumerRecord<?, ?>> consumerRecords : records.values()) {
            for (ConsumerRecord<?, ?> record : consumerRecords) {
                ContextCarrier contextCarrier = new ContextCarrier();

                CarrierItem next = contextCarrier.items();
                while (next.hasNext()) {
                    next = next.next();
                    Iterator<Header> iterator = record.headers().headers(next.getHeadKey()).iterator();
                    if (iterator.hasNext()) {
                        next.setHeadValue(new String(iterator.next().value()));
                    }
                }
                ContextManager.extract(contextCarrier);
            }
        }
        ContextManager.stopSpan();
    }
    return ret;
}