Java Code Examples for org.apache.skywalking.apm.agent.core.context.ContextCarrier#items()

The following examples show how to use org.apache.skywalking.apm.agent.core.context.ContextCarrier#items() . 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: 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 2
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 3
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 4
Source File: SenderSendInterceptor.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 {
    Request req = (Request) objInst.getSkyWalkingDynamicField();
    final URI requestURL = req.getUrl().toURI();
    final METHOD httpMethod = req.getMethod();
    final ContextCarrier contextCarrier = new ContextCarrier();
    String remotePeer = requestURL.getHost() + ":" + requestURL.getPort();
    AbstractSpan span = ContextManager.createExitSpan(requestURL.getPath(), contextCarrier, remotePeer);

    span.setComponent(ComponentsDefine.NUTZ_HTTP);
    Tags.URL.set(span, requestURL.getScheme() + "://" + requestURL.getHost() + ":" + requestURL.getPort() + requestURL
        .getPath());
    Tags.HTTP.METHOD.set(span, httpMethod.toString());
    SpanLayer.asHttp(span);

    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        req.getHeader().set(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: ServerInterceptor.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];
    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    if (request.getKvAttachment() == null) {
        request.setKvAttachment(new HashMap<>());
    }
    while (next.hasNext()) {
        next = next.next();
        next.setHeadValue((String) request.getKvAttachment().get(next.getHeadKey()));
    }

    AbstractSpan span = ContextManager.createEntrySpan(generateOperationName(request), contextCarrier);
    SpanLayer.asRPCFramework(span);
    span.setComponent(ComponentsDefine.BRPC_JAVA);
}
 
Example 7
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 8
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 9
Source File: SWServerRPCPlugin.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void serverReceiveRequest(RPCContext context) {
    Map meta = context.requestCallMeta();

    ContextCarrier carrier = new ContextCarrier();
    CarrierItem items = carrier.items();
    while (items.hasNext()) {
        items = items.next();
        ByteBuffer buffer = (ByteBuffer) meta.get(new Utf8(items.getHeadKey()));
        items.setHeadValue(new String(buffer.array()));
    }

    String operationName = prefix + context.getMessage().getName();
    AbstractSpan span = ContextManager.createEntrySpan(operationName, carrier);
    SpanLayer.asRPCFramework(span);
    span.setComponent(ComponentsDefine.AVRO_SERVER);
}
 
Example 10
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 11
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 12
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 13
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 14
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 15
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 16
Source File: ActionMethodInterceptor.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 {
    PathMappingCache pathMappingCache = (PathMappingCache) objInst.getSkyWalkingDynamicField();
    String requestURL = pathMappingCache.findPathMapping(method);
    if (requestURL == null) {
        At methodRequestMapping = method.getAnnotation(At.class);
        if (methodRequestMapping.value().length > 0) {
            requestURL = methodRequestMapping.value()[0];
        } else {
            requestURL = "";
        }
        pathMappingCache.addPathMapping(method, requestURL);
        requestURL = pathMappingCache.findPathMapping(method);
    }

    HttpServletRequest request = Mvcs.getReq();
    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        next.setHeadValue(request.getHeader(next.getHeadKey()));
    }
    AbstractSpan span = ContextManager.createEntrySpan(requestURL, contextCarrier);
    Tags.URL.set(span, request.getRequestURL().toString());
    Tags.HTTP.METHOD.set(span, request.getMethod());
    span.setComponent(ComponentsDefine.NUTZ_MVC_ANNOTATION);
    SpanLayer.asHttp(span);
}
 
Example 17
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 18
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 19
Source File: HttpClientRequestInterceptor.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 {
    AbstractSpan span = ContextManager.activeSpan();

    URL url = new URL((String) allArguments[1]);
    ContextCarrier contextCarrier = new ContextCarrier();
    AbstractSpan abstractSpan = ContextManager.createExitSpan(
        "SpringCloudGateway/sendRequest", contextCarrier, getPeer(url));
    abstractSpan.prepareForAsync();
    Tags.URL.set(abstractSpan, String.valueOf(allArguments[1]));

    abstractSpan.setComponent(SPRING_CLOUD_GATEWAY);
    ContextManager.stopSpan(abstractSpan);

    Function<? super HttpClientRequest, ? extends Publisher<Void>> handler = (Function<? super HttpClientRequest, ? extends Publisher<Void>>) allArguments[2];
    allArguments[2] = new Function<HttpClientRequest, Publisher<Void>>() {
        @Override
        public Publisher<Void> apply(final HttpClientRequest httpClientRequest) {
            //
            CarrierItem next = contextCarrier.items();
            if (httpClientRequest instanceof EnhancedInstance) {
                ((EnhancedInstance) httpClientRequest).setSkyWalkingDynamicField(next);
            }
            return handler.apply(httpClientRequest);
        }
    };

    objInst.setSkyWalkingDynamicField(new EnhanceCacheObject(span, abstractSpan));
}
 
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);
}