Search in sources :

Example 31 with CarrierItem

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

the class SenderSendInterceptor method beforeMethod.

@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());
    }
}
Also used : ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) Request(org.nutz.http.Request) METHOD(org.nutz.http.Request.METHOD) URI(java.net.URI) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 32 with CarrierItem

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

the class AsyncCallInterceptor method beforeMethod.

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    EnhanceRequiredInfo enhanceRequiredInfo = (EnhanceRequiredInfo) objInst.getSkyWalkingDynamicField();
    Request request = (Request) enhanceRequiredInfo.getRealCallEnhance().getSkyWalkingDynamicField();
    HttpUrl requestUrl = request.url();
    AbstractSpan span = ContextManager.createExitSpan(requestUrl.uri().getPath(), requestUrl.host() + ":" + requestUrl.port());
    ContextManager.continued(enhanceRequiredInfo.getContextSnapshot());
    ContextCarrier contextCarrier = new ContextCarrier();
    ContextManager.inject(contextCarrier);
    span.setComponent(ComponentsDefine.OKHTTP);
    Tags.HTTP.METHOD.set(span, request.method());
    Tags.URL.set(span, requestUrl.uri().toString());
    SpanLayer.asHttp(span);
    Field headersField = Request.class.getDeclaredField("headers");
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(headersField, headersField.getModifiers() & ~Modifier.FINAL);
    headersField.setAccessible(true);
    Headers.Builder headerBuilder = request.headers().newBuilder();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        headerBuilder.add(next.getHeadKey(), next.getHeadValue());
    }
    headersField.set(request, headerBuilder.build());
}
Also used : Field(java.lang.reflect.Field) ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) Headers(okhttp3.Headers) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 33 with CarrierItem

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

the class DefaultHttpClientInterceptor method beforeMethod.

/**
 * Get the {@link feign.Request} from {@link EnhancedInstance}, then create {@link AbstractSpan} and set host, port,
 * kind, component, url from {@link feign.Request}. Through the reflection of the way, set the http header of
 * context data into {@link feign.Request#headers}.
 *
 * @param method
 * @param result change this result, if you want to truncate the method.
 * @throws Throwable
 */
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    Request request = (Request) allArguments[0];
    URL url = new URL(request.url());
    ContextCarrier contextCarrier = new ContextCarrier();
    int port = url.getPort() == -1 ? 80 : url.getPort();
    String remotePeer = url.getHost() + ":" + port;
    String operationName = url.getPath();
    if (operationName == null || operationName.length() == 0) {
        operationName = "/";
    }
    AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, remotePeer);
    span.setComponent(ComponentsDefine.FEIGN);
    Tags.HTTP.METHOD.set(span, request.method());
    Tags.URL.set(span, request.url());
    SpanLayer.asHttp(span);
    Field headersField = Request.class.getDeclaredField("headers");
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(headersField, headersField.getModifiers() & ~Modifier.FINAL);
    headersField.setAccessible(true);
    Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        List<String> contextCollection = new LinkedList<String>();
        contextCollection.add(next.getHeadValue());
        headers.put(next.getHeadKey(), contextCollection);
    }
    headers.putAll(request.headers());
    headersField.set(request, Collections.unmodifiableMap(headers));
}
Also used : ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) Request(feign.Request) URL(java.net.URL) LinkedList(java.util.LinkedList) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan) LinkedHashMap(java.util.LinkedHashMap) Field(java.lang.reflect.Field) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) Collection(java.util.Collection)

Example 34 with CarrierItem

use of org.apache.skywalking.apm.agent.core.context.CarrierItem 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 35 with CarrierItem

use of org.apache.skywalking.apm.agent.core.context.CarrierItem 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

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