Search in sources :

Example 1 with WebRequestStat

use of com.alibaba.druid.support.http.stat.WebRequestStat in project druid by alibaba.

the class WebStatFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    StatHttpServletResponseWrapper responseWrapper = new StatHttpServletResponseWrapper(httpResponse);
    String requestURI = getRequestURI(httpRequest);
    if (isExclusion(requestURI)) {
        chain.doFilter(request, response);
        return;
    }
    long startNano = System.nanoTime();
    long startMillis = System.currentTimeMillis();
    WebRequestStat requestStat = new WebRequestStat(startNano, startMillis);
    WebRequestStat.set(requestStat);
    WebSessionStat sessionStat = getSessionStat(httpRequest);
    webAppStat.beforeInvoke();
    WebURIStat uriStat = webAppStat.getURIStat(requestURI, false);
    if (uriStat == null) {
        int index = requestURI.indexOf(";jsessionid=");
        if (index != -1) {
            requestURI = requestURI.substring(0, index);
            uriStat = webAppStat.getURIStat(requestURI, false);
        }
    }
    if (isProfileEnable()) {
        Profiler.initLocal();
        Profiler.enter(requestURI, Profiler.PROFILE_TYPE_WEB);
    }
    // 第一次访问时,uriStat这里为null,是为了防止404攻击。
    if (uriStat != null) {
        uriStat.beforeInvoke();
    }
    // 第一次访问时,sessionId为null,如果缺省sessionCreate=false,sessionStat就为null。
    if (sessionStat != null) {
        sessionStat.beforeInvoke();
    }
    Throwable error = null;
    try {
        chain.doFilter(request, responseWrapper);
    } catch (IOException e) {
        error = e;
        throw e;
    } catch (ServletException e) {
        error = e;
        throw e;
    } catch (RuntimeException e) {
        error = e;
        throw e;
    } catch (Error e) {
        error = e;
        throw e;
    } finally {
        long endNano = System.nanoTime();
        requestStat.setEndNano(endNano);
        long nanos = endNano - startNano;
        webAppStat.afterInvoke(error, nanos);
        if (sessionStat == null) {
            sessionStat = getSessionStat(httpRequest);
            if (sessionStat != null) {
                // 补偿
                sessionStat.beforeInvoke();
            }
        }
        if (sessionStat != null) {
            sessionStat.afterInvoke(error, nanos);
            sessionStat.setPrincipal(getPrincipal(httpRequest));
        }
        if (uriStat == null) {
            int status = responseWrapper.getStatus();
            if (status == HttpServletResponse.SC_NOT_FOUND) {
                String errorUrl = contextPath + "error_" + status;
                uriStat = webAppStat.getURIStat(errorUrl, true);
            } else {
                uriStat = webAppStat.getURIStat(requestURI, true);
            }
            if (uriStat != null) {
                // 补偿调用
                uriStat.beforeInvoke();
            }
        }
        if (uriStat != null) {
            uriStat.afterInvoke(error, nanos);
        }
        WebRequestStat.set(null);
        if (isProfileEnable()) {
            Profiler.release(nanos);
            Map<ProfileEntryKey, ProfileEntryReqStat> requestStatsMap = Profiler.getStatsMap();
            if (uriStat != null) {
                uriStat.getProfiletat().record(requestStatsMap);
            }
            Profiler.removeLocal();
        }
    }
}
Also used : ProfileEntryReqStat(com.alibaba.druid.support.profile.ProfileEntryReqStat) WebRequestStat(com.alibaba.druid.support.http.stat.WebRequestStat) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) WebSessionStat(com.alibaba.druid.support.http.stat.WebSessionStat) ProfileEntryKey(com.alibaba.druid.support.profile.ProfileEntryKey) WebURIStat(com.alibaba.druid.support.http.stat.WebURIStat)

Example 2 with WebRequestStat

use of com.alibaba.druid.support.http.stat.WebRequestStat in project druid by alibaba.

the class StatHandlerInterceptor method afterCompletion.

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception error) throws Exception {
    WebRequestStat requestStat = WebRequestStat.current();
    long endNano = System.nanoTime();
    requestStat.setEndNano(endNano);
    WebSessionStat sessionStat = getSessionStat(request);
    WebURIStat uriStat = WebURIStat.current();
    long nanos = endNano - requestStat.getStartNano();
    webAppStat.afterInvoke(null, nanos);
    if (sessionStat == null) {
        sessionStat = getSessionStat(request);
        if (sessionStat != null) {
            // 补偿
            sessionStat.beforeInvoke();
        }
    }
    if (sessionStat != null) {
        sessionStat.afterInvoke(error, nanos);
        sessionStat.setPrincipal(getPrincipal(request));
    }
    if (uriStat != null) {
        uriStat.afterInvoke(error, nanos);
    }
    WebRequestStat.set(null);
    if (isProfileEnable()) {
        Profiler.release(nanos);
        Map<ProfileEntryKey, ProfileEntryReqStat> requestStatsMap = Profiler.getStatsMap();
        if (uriStat != null) {
            uriStat.getProfiletat().record(requestStatsMap);
        }
        Profiler.removeLocal();
    }
}
Also used : ProfileEntryReqStat(com.alibaba.druid.support.profile.ProfileEntryReqStat) WebSessionStat(com.alibaba.druid.support.http.stat.WebSessionStat) ProfileEntryKey(com.alibaba.druid.support.profile.ProfileEntryKey) WebRequestStat(com.alibaba.druid.support.http.stat.WebRequestStat) WebURIStat(com.alibaba.druid.support.http.stat.WebURIStat)

Example 3 with WebRequestStat

use of com.alibaba.druid.support.http.stat.WebRequestStat in project druid by alibaba.

the class StatHandlerInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    final WebAppStat webAppStat = getWebAppStat(request);
    String requestURI = getRequestURI(request);
    long startNano = System.nanoTime();
    long startMillis = System.currentTimeMillis();
    WebRequestStat requestStat = new WebRequestStat(startNano, startMillis);
    WebRequestStat.set(requestStat);
    WebSessionStat sessionStat = getSessionStat(request);
    webAppStat.beforeInvoke();
    WebURIStat uriStat = webAppStat.getURIStat(requestURI, false);
    if (uriStat == null) {
        int index = requestURI.indexOf(";jsessionid=");
        if (index != -1) {
            requestURI = requestURI.substring(0, index);
            uriStat = webAppStat.getURIStat(requestURI, false);
        }
    }
    if (isProfileEnable()) {
        Profiler.initLocal();
        Profiler.enter(requestURI, Profiler.PROFILE_TYPE_WEB);
    }
    // 第一次访问时,uriStat这里为null,是为了防止404攻击。
    if (uriStat != null) {
        uriStat.beforeInvoke();
    }
    // 第一次访问时,sessionId为null,如果缺省sessionCreate=false,sessionStat就为null。
    if (sessionStat != null) {
        sessionStat.beforeInvoke();
    }
    return true;
}
Also used : WebSessionStat(com.alibaba.druid.support.http.stat.WebSessionStat) WebRequestStat(com.alibaba.druid.support.http.stat.WebRequestStat) WebAppStat(com.alibaba.druid.support.http.stat.WebAppStat) WebURIStat(com.alibaba.druid.support.http.stat.WebURIStat)

Aggregations

WebRequestStat (com.alibaba.druid.support.http.stat.WebRequestStat)3 WebSessionStat (com.alibaba.druid.support.http.stat.WebSessionStat)3 WebURIStat (com.alibaba.druid.support.http.stat.WebURIStat)3 ProfileEntryKey (com.alibaba.druid.support.profile.ProfileEntryKey)2 ProfileEntryReqStat (com.alibaba.druid.support.profile.ProfileEntryReqStat)2 WebAppStat (com.alibaba.druid.support.http.stat.WebAppStat)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1