Search in sources :

Example 1 with CarrierItem

use of org.apache.skywalking.apm.agent.core.context.CarrierItem in project incubator-skywalking by apache.

the class StreamCallClientInterceptor method start.

@Override
public void start(Listener responseListener, Metadata headers) {
    final ContextCarrier contextCarrier = new ContextCarrier();
    final AbstractSpan span = ContextManager.createExitSpan(serviceName, contextCarrier, remotePeer);
    span.setComponent(ComponentsDefine.GRPC);
    SpanLayer.asRPCFramework(span);
    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());
    }
    delegate().start(new CallListener(responseListener, ContextManager.capture()), headers);
    ContextManager.stopSpan();
}
Also used : ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) ForwardingClientCallListener(io.grpc.ForwardingClientCallListener) Metadata(io.grpc.Metadata) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 2 with CarrierItem

use of org.apache.skywalking.apm.agent.core.context.CarrierItem in project incubator-skywalking by apache.

the class HttpClientExecuteInterceptor method beforeMethod.

@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();
    AbstractSpan span = null;
    String remotePeer = httpHost.getHostName() + ":" + (httpHost.getPort() > 0 ? httpHost.getPort() : "https".equals(httpHost.getSchemeName().toLowerCase()) ? 443 : 80);
    try {
        URL url = new URL(httpRequest.getRequestLine().getUri());
        span = ContextManager.createExitSpan(url.getPath(), contextCarrier, remotePeer);
    } catch (MalformedURLException e) {
        throw e;
    }
    span.setComponent(ComponentsDefine.HTTPCLIENT);
    Tags.URL.set(span, httpRequest.getRequestLine().getUri());
    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());
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) MalformedURLException(java.net.MalformedURLException) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) HttpHost(org.apache.http.HttpHost) URL(java.net.URL) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 3 with CarrierItem

use of org.apache.skywalking.apm.agent.core.context.CarrierItem in project incubator-skywalking by apache.

the class KafkaProducerInterceptor method beforeMethod.

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    ContextCarrier contextCarrier = new ContextCarrier();
    ProducerRecord record = (ProducerRecord) allArguments[0];
    String topicName = (String) ((EnhancedInstance) record).getSkyWalkingDynamicField();
    AbstractSpan activeSpan = ContextManager.createExitSpan(OPERATE_NAME_PREFIX + topicName + PRODUCER_OPERATE_NAME_SUFFIX, contextCarrier, (String) objInst.getSkyWalkingDynamicField());
    Tags.MQ_BROKER.set(activeSpan, (String) objInst.getSkyWalkingDynamicField());
    Tags.MQ_TOPIC.set(activeSpan, topicName);
    SpanLayer.asMQ(activeSpan);
    activeSpan.setComponent(ComponentsDefine.KAFKA);
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        record.headers().add(next.getHeadKey(), next.getHeadValue().getBytes());
    }
    EnhancedInstance callbackInstance = (EnhancedInstance) allArguments[1];
    if (callbackInstance != null) {
        callbackInstance.setSkyWalkingDynamicField(ContextManager.capture());
    }
}
Also used : ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) EnhancedInstance(org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 4 with CarrierItem

use of org.apache.skywalking.apm.agent.core.context.CarrierItem in project incubator-skywalking by apache.

the class DubboInterceptor method beforeMethod.

/**
 * <h2>Consumer:</h2> The serialized trace context data will
 * inject to the {@link RpcContext#attachments} for transport to provider side.
 * <p>
 * <h2>Provider:</h2> The serialized trace context data will extract from
 * {@link RpcContext#attachments}. current trace segment will ref if the serialize context data is not null.
 */
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    Invoker invoker = (Invoker) allArguments[0];
    Invocation invocation = (Invocation) allArguments[1];
    RpcContext rpcContext = RpcContext.getContext();
    boolean isConsumer = rpcContext.isConsumerSide();
    URL requestURL = invoker.getUrl();
    AbstractSpan span;
    final String host = requestURL.getHost();
    final int port = requestURL.getPort();
    if (isConsumer) {
        final ContextCarrier contextCarrier = new ContextCarrier();
        span = ContextManager.createExitSpan(generateOperationName(requestURL, invocation), contextCarrier, host + ":" + port);
        // invocation.getAttachments().put("contextData", contextDataStr);
        // @see https://github.com/alibaba/dubbo/blob/dubbo-2.5.3/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/RpcInvocation.java#L154-L161
        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            rpcContext.getAttachments().put(next.getHeadKey(), next.getHeadValue());
        }
    } else {
        ContextCarrier contextCarrier = new ContextCarrier();
        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            next.setHeadValue(rpcContext.getAttachment(next.getHeadKey()));
        }
        span = ContextManager.createEntrySpan(generateOperationName(requestURL, invocation), contextCarrier);
    }
    Tags.URL.set(span, generateRequestURL(requestURL, invocation));
    span.setComponent(ComponentsDefine.DUBBO);
    SpanLayer.asRPCFramework(span);
}
Also used : RpcContext(com.alibaba.dubbo.rpc.RpcContext) ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) Invoker(com.alibaba.dubbo.rpc.Invoker) Invocation(com.alibaba.dubbo.rpc.Invocation) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) URL(com.alibaba.dubbo.common.URL) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 5 with CarrierItem

use of org.apache.skywalking.apm.agent.core.context.CarrierItem in project incubator-skywalking by apache.

the class SkywalkingTracerExtractInterceptor method afterMethod.

@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();
}
Also used : SkywalkingContext(org.apache.skywalking.apm.toolkit.opentracing.SkywalkingContext) ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) Format(io.opentracing.propagation.Format) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) TextMap(io.opentracing.propagation.TextMap) TextMap(io.opentracing.propagation.TextMap) Map(java.util.Map)

Aggregations

CarrierItem (org.apache.skywalking.apm.agent.core.context.CarrierItem)35 ContextCarrier (org.apache.skywalking.apm.agent.core.context.ContextCarrier)35 AbstractSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)29 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 Metadata (io.grpc.Metadata)3 Field (java.lang.reflect.Field)3 URI (java.net.URI)3 URL (java.net.URL)3 EnhancedInstance (org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance)3 Request (com.weibo.api.motan.rpc.Request)2 ForwardingClientCallListener (io.grpc.ForwardingClientCallListener)2 Format (io.opentracing.propagation.Format)2 TextMap (io.opentracing.propagation.TextMap)2 Invocation (io.servicecomb.core.Invocation)2 MalformedURLException (java.net.MalformedURLException)2 Map (java.util.Map)2 Headers (okhttp3.Headers)2 HttpUrl (okhttp3.HttpUrl)2 Request (okhttp3.Request)2 Invocation (org.apache.servicecomb.core.Invocation)2