Search in sources :

Example 1 with Span

use of com.creditease.uav.apm.invokechain.span.Span in project uavstack by uavorg.

the class ApacheAsyncHttpClientAdapter method afterDoCap.

@Override
public void afterDoCap(InvokeChainContext context, Object[] args) {
    if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) {
        Span span = (Span) context.get(InvokeChainConstants.PARAM_SPAN_KEY);
        SlowOperContext slowOperContext = new SlowOperContext();
        if (args[0] != null) {
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_EXCEPTION, args[0].toString());
        } else if (Exception.class.isAssignableFrom(args[1].getClass())) {
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_EXCEPTION, args[1].toString());
        } else if (HttpResponse.class.isAssignableFrom(args[1].getClass())) {
            HttpResponse response = (HttpResponse) args[1];
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_HEADER, getResponHeaders(response));
            HttpEntity entity = response.getEntity();
            // 由于存在读取失败和无法缓存的大entity会使套壳失败,故此处添加如下判断
            if (BufferedHttpEntity.class.isAssignableFrom(entity.getClass())) {
                Header header = entity.getContentEncoding();
                String encoding = header == null ? "utf-8" : header.getValue();
                slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, getHttpEntityContent(entity, encoding));
            } else {
                slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, "HttpEntityWrapper failed! Maybe HTTP entity too large to be buffered in memory");
            }
        } else {
            HttpResponse response = (HttpResponse) args[1];
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_HEADER, getResponHeaders(response));
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, "unsupported response");
        }
        Object[] params = { span, slowOperContext };
        UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.SlowOperSupporter", "runCap", span.getEndpointInfo().split(",")[0], InvokeChainConstants.CapturePhase.DOCAP, context, params);
    }
}
Also used : SlowOperContext(com.creditease.uav.apm.slowoper.spi.SlowOperContext) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse) Span(com.creditease.uav.apm.invokechain.span.Span)

Example 2 with Span

use of com.creditease.uav.apm.invokechain.span.Span in project uavstack by uavorg.

the class ApacheHttpClientAdapter method afterDoCap.

@Override
public void afterDoCap(InvokeChainContext context, Object[] args) {
    if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) {
        Span span = (Span) context.get(InvokeChainConstants.PARAM_SPAN_KEY);
        SlowOperContext slowOperContext = new SlowOperContext();
        if (Throwable.class.isAssignableFrom(args[0].getClass())) {
            Throwable e = (Throwable) args[0];
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_EXCEPTION, e.toString());
        } else {
            HttpResponse response = (HttpResponse) args[0];
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_HEADER, getResponHeaders(response));
            HttpEntity entity = response.getEntity();
            // 由于存在读取失败和无法缓存的大entity会使套壳失败,故此处添加如下判断
            if (BufferedHttpEntity.class.isAssignableFrom(entity.getClass())) {
                Header header = entity.getContentEncoding();
                String encoding = header == null ? "utf-8" : header.getValue();
                slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, getHttpEntityContent(entity, encoding));
            } else {
                slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, "HttpEntityWrapper failed! Maybe HTTP entity too large to be buffered in memory");
            }
        }
        Object[] params = { span, slowOperContext };
        UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.SlowOperSupporter", "runCap", span.getEndpointInfo().split(",")[0], InvokeChainConstants.CapturePhase.DOCAP, context, params);
    }
}
Also used : SlowOperContext(com.creditease.uav.apm.slowoper.spi.SlowOperContext) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse) Span(com.creditease.uav.apm.invokechain.span.Span)

Example 3 with Span

use of com.creditease.uav.apm.invokechain.span.Span in project uavstack by uavorg.

the class ApacheHttpClientAdapter method afterPreCap.

@Override
public void afterPreCap(InvokeChainContext context, Object[] args) {
    if (args.length < 2) {
        return;
    }
    /**
     * after precap the client's span is created, set the span meta into http request header
     */
    String url = (String) context.get(InvokeChainConstants.CLIENT_SPAN_THREADLOCAL_STOREKEY);
    Span span = this.spanFactory.getSpanFromContext(url);
    String spanMeta = this.spanFactory.getSpanMeta(span);
    HttpRequest request = (HttpRequest) args[1];
    request.removeHeaders(InvokeChainConstants.PARAM_HTTPHEAD_SPANINFO);
    request.addHeader(InvokeChainConstants.PARAM_HTTPHEAD_SPANINFO, spanMeta);
    handleSlowOperSupporter(request, span, context);
}
Also used : HttpRequest(org.apache.http.HttpRequest) Span(com.creditease.uav.apm.invokechain.span.Span)

Example 4 with Span

use of com.creditease.uav.apm.invokechain.span.Span in project uavstack by uavorg.

the class ApacheHttpClient3Adapter method afterPreCap.

@Override
public void afterPreCap(InvokeChainContext context, Object[] args) {
    /**
     * after precap the client's span is created, set the span meta into http request header
     */
    String url = (String) context.get(InvokeChainConstants.CLIENT_SPAN_THREADLOCAL_STOREKEY);
    Span span = this.spanFactory.getSpanFromContext(url);
    String spanMeta = this.spanFactory.getSpanMeta(span);
    HttpMethod method = (HttpMethod) args[1];
    method.removeRequestHeader(InvokeChainConstants.PARAM_HTTPHEAD_SPANINFO);
    method.addRequestHeader(InvokeChainConstants.PARAM_HTTPHEAD_SPANINFO, spanMeta);
    if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) {
        // httpclient3好像没有encoding参数
        SlowOperContext slowOperContext = new SlowOperContext();
        String quertParams = method.getQueryString();
        if (quertParams == null) {
            quertParams = "{}";
        }
        slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_HEADER, getHeadersContent(method.getRequestHeaders()) + quertParams);
        // 通过此种方式判断是否存在body,通过httpclient类继承确定
        String requestBody = "";
        if (EntityEnclosingMethod.class.isAssignableFrom(method.getClass())) {
            EntityEnclosingMethod entityMethod = (EntityEnclosingMethod) method;
            RequestEntity requestEntity = entityMethod.getRequestEntity();
            // 根据不同类型entity采取不同读取方式(由于httpclient3没有显视encoding,故此处使用默认的编码格式)
            if (ByteArrayRequestEntity.class.isAssignableFrom(requestEntity.getClass())) {
                requestBody = new String(((ByteArrayRequestEntity) requestEntity).getContent());
            } else if (InputStreamRequestEntity.class.isAssignableFrom(requestEntity.getClass())) {
                InputStreamRequestEntity inputStreamRequestEntity = (InputStreamRequestEntity) requestEntity;
                if (!inputStreamRequestEntity.isRepeatable()) {
                    inputStreamRequestEntity.getContentLength();
                }
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStreamRequestEntity.getContent()));
                    StringBuilder body = new StringBuilder();
                    String str;
                    while ((str = reader.readLine()) != null) {
                        body.append(str);
                    }
                    requestBody = body.toString();
                } catch (UnsupportedOperationException e) {
                    logger.warn("body type is not supported!", e);
                    requestBody = "body type is not supported!" + e.toString();
                } catch (IOException e) {
                    logger.error("IOException!", e);
                    requestBody = e.toString();
                }
            } else if (MultipartRequestEntity.class.isAssignableFrom(requestEntity.getClass())) {
                // MultipartRequestEntity暂时不支持
                requestBody = "MultipartRequestEntity is not supported";
            } else if (StringRequestEntity.class.isAssignableFrom(requestEntity.getClass())) {
                requestBody = ((StringRequestEntity) requestEntity).getContent();
            } else {
                requestBody = "unsupported type";
            }
        }
        slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, requestBody);
        Object[] params = { span, slowOperContext };
        UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.SlowOperSupporter", "runCap", span.getEndpointInfo().split(",")[0], InvokeChainConstants.CapturePhase.PRECAP, context, params);
    }
}
Also used : SlowOperContext(com.creditease.uav.apm.slowoper.spi.SlowOperContext) InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) InputStreamReader(java.io.InputStreamReader) EntityEnclosingMethod(org.apache.commons.httpclient.methods.EntityEnclosingMethod) IOException(java.io.IOException) Span(com.creditease.uav.apm.invokechain.span.Span) BufferedReader(java.io.BufferedReader) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity) InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) HttpMethod(org.apache.commons.httpclient.HttpMethod) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Example 5 with Span

use of com.creditease.uav.apm.invokechain.span.Span in project uavstack by uavorg.

the class ApacheHttpClient3Adapter method afterDoCap.

@Override
public void afterDoCap(InvokeChainContext context, Object[] args) {
    if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) {
        Span span = (Span) context.get(InvokeChainConstants.PARAM_SPAN_KEY);
        SlowOperContext slowOperContext = new SlowOperContext();
        if (Throwable.class.isAssignableFrom(args[0].getClass())) {
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_EXCEPTION, args[0].toString());
        } else {
            HttpMethod method = (HttpMethod) args[0];
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_HEADER, getHeadersContent(method.getRequestHeaders()));
            try {
                slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, method.getResponseBodyAsString());
            } catch (IOException e) {
                slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_EXCEPTION, e.toString());
            }
        }
        Object[] params = { span, slowOperContext };
        UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.SlowOperSupporter", "runCap", span.getEndpointInfo().split(",")[0], InvokeChainConstants.CapturePhase.DOCAP, context, params);
    }
}
Also used : SlowOperContext(com.creditease.uav.apm.slowoper.spi.SlowOperContext) IOException(java.io.IOException) Span(com.creditease.uav.apm.invokechain.span.Span) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Aggregations

Span (com.creditease.uav.apm.invokechain.span.Span)37 SlowOperContext (com.creditease.uav.apm.slowoper.spi.SlowOperContext)20 DataLogger (com.creditease.monitor.log.DataLogger)6 SlowOperSpan (com.creditease.uav.apm.slowoper.span.SlowOperSpan)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 Invocation (com.alibaba.dubbo.rpc.Invocation)2 Result (com.alibaba.dubbo.rpc.Result)2 AMQP (com.rabbitmq.client.AMQP)2 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 HttpMethod (org.apache.commons.httpclient.HttpMethod)2 Header (org.apache.http.Header)2 HttpEntity (org.apache.http.HttpEntity)2 HttpRequest (org.apache.http.HttpRequest)2 HttpResponse (org.apache.http.HttpResponse)2 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)2 Message (com.alibaba.rocketmq.common.message.Message)1