Search in sources :

Example 1 with SlowOperContext

use of com.creditease.uav.apm.slowoper.spi.SlowOperContext in project uavstack by uavorg.

the class AbstractHttpClientAdapter method handleSlowOperSupporter.

protected void handleSlowOperSupporter(HttpRequest request, Span span, InvokeChainContext context) {
    if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) {
        SlowOperContext slowOperContext = new SlowOperContext();
        String queryParams = getParamsFromUri(request.getRequestLine().getUri());
        slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_HEADER, getRequestHeaders(request) + queryParams);
        try {
            // 通过此种方式判断是否存在body,通过httpclient类继承确定
            if (HttpEntityEnclosingRequestBase.class.isAssignableFrom(request.getClass())) {
                HttpEntityEnclosingRequestBase req = (HttpEntityEnclosingRequestBase) request;
                HttpEntity entity = req.getEntity();
                // 兼容不正当使用
                if (entity == null) {
                    slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, "");
                } else {
                    Header header = entity.getContentEncoding();
                    String encoding = header == null ? "utf-8" : header.getValue();
                    try {
                        BufferedHttpEntity httpEntityWrapper = new BufferedHttpEntity(entity);
                        req.setEntity(httpEntityWrapper);
                        slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, getHttpEntityContent(httpEntityWrapper, encoding));
                    } catch (IOException e) {
                        logger.warn("HttpEntityWrapper failed!", e);
                        slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, e.toString());
                    }
                }
            } else {
                slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, "");
            }
        } catch (Exception e) {
            // 由于会存在无法缓冲的情况,故此处添加捕获
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, e.toString());
        }
        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) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) Header(org.apache.http.Header) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 2 with SlowOperContext

use of com.creditease.uav.apm.slowoper.spi.SlowOperContext 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 3 with SlowOperContext

use of com.creditease.uav.apm.slowoper.spi.SlowOperContext 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 4 with SlowOperContext

use of com.creditease.uav.apm.slowoper.spi.SlowOperContext 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 SlowOperContext

use of com.creditease.uav.apm.slowoper.spi.SlowOperContext 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

SlowOperContext (com.creditease.uav.apm.slowoper.spi.SlowOperContext)21 Span (com.creditease.uav.apm.invokechain.span.Span)20 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 IOException (java.io.IOException)3 Header (org.apache.http.Header)3 HttpEntity (org.apache.http.HttpEntity)3 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)3 Invocation (com.alibaba.dubbo.rpc.Invocation)2 Result (com.alibaba.dubbo.rpc.Result)2 SlowOperSpan (com.creditease.uav.apm.slowoper.span.SlowOperSpan)2 AMQP (com.rabbitmq.client.AMQP)2 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)2 HttpMethod (org.apache.commons.httpclient.HttpMethod)2 HttpResponse (org.apache.http.HttpResponse)2 Message (com.alibaba.rocketmq.common.message.Message)1 MessageExt (com.alibaba.rocketmq.common.message.MessageExt)1 DataLogger (com.creditease.monitor.log.DataLogger)1 RewriteIvcRequestWrapper (com.creditease.uav.apm.RewriteIvcRequestWrapper)1 RewriteIvcResponseWrapper (com.creditease.uav.apm.RewriteIvcResponseWrapper)1 BufferedReader (java.io.BufferedReader)1