Search in sources :

Example 41 with ContextCarrier

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

the class CallServerInterceptor method interceptCall.

@Override
public ServerCall.Listener interceptCall(ServerCall call, Metadata headers, ServerCallHandler handler) {
    Map<String, String> headerMap = new HashMap<String, String>();
    for (String key : headers.keys()) {
        if (!key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
            String value = headers.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
            headerMap.put(key, value);
        }
    }
    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        String contextValue = headerMap.get(next.getHeadKey());
        if (!StringUtil.isEmpty(contextValue)) {
            next.setHeadValue(contextValue);
        }
    }
    final AbstractSpan span = ContextManager.createEntrySpan(OperationNameFormatUtil.formatOperationName(call.getMethodDescriptor()), contextCarrier);
    span.setComponent(ComponentsDefine.GRPC);
    return new ServerCallListener(handler.startCall(new ForwardingServerCall.SimpleForwardingServerCall(call) {

        @Override
        public void sendHeaders(Metadata responseHeaders) {
            delegate().sendHeaders(responseHeaders);
        }
    }, headers), call.getMethodDescriptor());
}
Also used : ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) HashMap(java.util.HashMap) Metadata(io.grpc.Metadata) ForwardingServerCallListener(io.grpc.ForwardingServerCallListener) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 42 with ContextCarrier

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

the class StateInterceptor method beforeMethod.

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    if (null == allArguments[0]) {
        return;
    }
    HttpRequestWrapper httpRequest = (HttpRequestWrapper) allArguments[0];
    String uri = httpRequest.getOriginal().getRequestLine().getUri();
    AbstractSpan span = null;
    final ContextCarrier contextCarrier = new ContextCarrier();
    try {
        URL url = new URL(httpRequest.getOriginal().getRequestLine().getUri());
        String remotePeer = url.getHost() + ":" + url.getPort();
        span = ContextManager.createExitSpan(url.getPath(), contextCarrier, remotePeer);
    } catch (MalformedURLException e) {
        throw e;
    }
    span.setComponent(ComponentsDefine.HTTP_ASYNC_CLIENT);
    Tags.URL.set(span, uri);
    Tags.HTTP.METHOD.set(span, httpRequest.getOriginal().getRequestLine().getMethod());
    SpanLayer.asHttp(span);
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        httpRequest.setHeader(next.getHeadKey(), next.getHeadValue());
    }
}
Also used : ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) MalformedURLException(java.net.MalformedURLException) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) HttpRequestWrapper(org.apache.http.client.methods.HttpRequestWrapper) URL(java.net.URL) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Aggregations

ContextCarrier (org.apache.skywalking.apm.agent.core.context.ContextCarrier)42 AbstractSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)36 CarrierItem (org.apache.skywalking.apm.agent.core.context.CarrierItem)35 URI (java.net.URI)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 Metadata (io.grpc.Metadata)3 Field (java.lang.reflect.Field)3 URL (java.net.URL)3 List (java.util.List)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