Search in sources :

Example 1 with RewriteIvcRequestWrapper

use of com.creditease.uav.apm.RewriteIvcRequestWrapper in project uavstack by uavorg.

the class ServerSpanAdapter method afterDoCap.

@Override
public void afterDoCap(InvokeChainContext context, Object[] args) {
    if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) {
        String url = (String) context.get(CaptureConstants.INFO_APPSERVER_CONNECTOR_REQUEST_URL);
        Span span = this.spanFactory.getRemoveSpanFromContext(url);
        if (span == null) {
            return;
        }
        SlowOperContext slowOperContext = new SlowOperContext();
        // 防止有没有拦截住的请求
        if (RewriteIvcRequestWrapper.class.isAssignableFrom(args[0].getClass()) && RewriteIvcResponseWrapper.class.isAssignableFrom(args[1].getClass())) {
            RewriteIvcRequestWrapper request = (RewriteIvcRequestWrapper) args[0];
            // 在最后取parameter,放入Header末尾
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_REQ_HEADER, getRequestHeaders(request) + generateParameterString(request.getAllParameters()));
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_REQ_BODY, request.getContent().toString());
            request.clearBodyContent();
            RewriteIvcResponseWrapper response = (RewriteIvcResponseWrapper) args[1];
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_RSP_HEADER, getResponHeaders(response));
            if ((Integer) context.get(CaptureConstants.INFO_APPSERVER_CONNECTOR_RESPONSECODE) == 500) {
                String exceptionStr = ((Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION)).toString();
                slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_RSP_BODY, exceptionStr);
            } else {
                slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_RSP_BODY, response.getContent().toString());
            }
            response.clearBodyContent();
        } else {
            HttpServletRequest request = (HttpServletRequest) args[0];
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_REQ_HEADER, getRequestHeaders(request));
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_REQ_BODY, "unsupported request");
            HttpServletResponse response = (HttpServletResponse) args[1];
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_RSP_HEADER, getResponHeaders(response));
            slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_RSP_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 : HttpServletRequest(javax.servlet.http.HttpServletRequest) SlowOperContext(com.creditease.uav.apm.slowoper.spi.SlowOperContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) Span(com.creditease.uav.apm.invokechain.span.Span) RewriteIvcRequestWrapper(com.creditease.uav.apm.RewriteIvcRequestWrapper) RewriteIvcResponseWrapper(com.creditease.uav.apm.RewriteIvcResponseWrapper)

Example 2 with RewriteIvcRequestWrapper

use of com.creditease.uav.apm.RewriteIvcRequestWrapper in project uavstack by uavorg.

the class JEEServiceRunGlobalFilterHandler method doRequest.

@Override
protected void doRequest(HttpServletRequest request, HttpServletResponse response, InterceptContext context) {
    StringBuffer bf = request.getRequestURL();
    if (bf == null) {
        return;
    }
    String urlInfo = bf.toString();
    if ((!MonitorServerUtil.isIncludeMonitorURLForService(urlInfo) && !MonitorServerUtil.isIncludeMonitorURLForPage(urlInfo)) || "/com.creditease.uav".equals(getReqContextPath(request))) {
        return;
    }
    if (logger.isDebugable()) {
        logger.debug("JEEServiceStartInvokeChainHandler called for url:" + urlInfo, null);
    }
    Object[] args;
    // 重调用链开启时添加warpper
    if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) {
        HttpServletRequest reqs = (HttpServletRequest) context.get(InterceptConstants.HTTPREQUEST);
        RewriteIvcRequestWrapper requestWrapper = new RewriteIvcRequestWrapper(reqs, "IVC_DAT");
        context.put(InterceptConstants.HTTPREQUEST, requestWrapper);
        HttpServletResponse resp = (HttpServletResponse) context.get(InterceptConstants.HTTPRESPONSE);
        RewriteIvcResponseWrapper responseWrapper = new RewriteIvcResponseWrapper(resp, "IVC_DAT");
        context.put(InterceptConstants.HTTPRESPONSE, responseWrapper);
        args = new Object[] { requestWrapper, responseWrapper };
    } else {
        args = new Object[] { request, response };
    }
    Map<String, Object> params = new HashMap<String, Object>();
    /**
     * try to get SPAN_KEY_HEAD from http request header
     */
    HttpServletRequest httprequest = (HttpServletRequest) context.get(InterceptConstants.HTTPREQUEST);
    String traceContext = httprequest.getHeader(InvokeChainConstants.PARAM_HTTPHEAD_SPANINFO);
    String method = httprequest.getHeader(InvokeChainConstants.CLIENT_IT_METHOD);
    if (!StringHelper.isEmpty(traceContext)) {
        params.put(InvokeChainConstants.PARAM_HTTPHEAD_SPANINFO, traceContext);
    }
    if (!StringHelper.isEmpty(method)) {
        params.put(InvokeChainConstants.CLIENT_IT_METHOD, method);
    }
    String clientip = MonitorServerUtil.getClientIP(httprequest.getRemoteAddr(), httprequest.getHeader("X-Forwarded-For"));
    // put intercept context
    params.put(InvokeChainConstants.PARAM_INTECEPTCONTEXT, context);
    params.put(InvokeChainConstants.PARAM_REMOTE_SRC_INFO, clientip);
    // put request url
    params.put(CaptureConstants.INFO_APPSERVER_CONNECTOR_REQUEST_URL, request.getRequestURL().toString());
    // register adapter
    UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "registerAdapter", ServerSpanAdapter.class);
    UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "runCap", InvokeChainConstants.CHAIN_APP_SERVICE, InvokeChainConstants.CapturePhase.PRECAP, params, ServerSpanAdapter.class, args);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) RewriteIvcRequestWrapper(com.creditease.uav.apm.RewriteIvcRequestWrapper) RewriteIvcResponseWrapper(com.creditease.uav.apm.RewriteIvcResponseWrapper)

Aggregations

RewriteIvcRequestWrapper (com.creditease.uav.apm.RewriteIvcRequestWrapper)2 RewriteIvcResponseWrapper (com.creditease.uav.apm.RewriteIvcResponseWrapper)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Span (com.creditease.uav.apm.invokechain.span.Span)1 SlowOperContext (com.creditease.uav.apm.slowoper.spi.SlowOperContext)1 HashMap (java.util.HashMap)1