use of com.alibaba.druid.support.http.stat.WebURIStat 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();
}
}
}
use of com.alibaba.druid.support.http.stat.WebURIStat 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();
}
}
use of com.alibaba.druid.support.http.stat.WebURIStat 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;
}
use of com.alibaba.druid.support.http.stat.WebURIStat in project druid by alibaba.
the class WebURIStatMemoryTest method test_0.
public void test_0() throws Exception {
WebURIStat item = new WebURIStat("");
gc();
long memoryStart = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
final int COUNT = 1024 * 1024;
WebURIStat[] items = new WebURIStat[COUNT];
for (int i = 0; i < COUNT; ++i) {
items[i] = new WebURIStat("");
items[i].getProfiletat();
// items[i] = Histogram.makeHistogram(20);
}
long memoryEnd = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
System.out.println("memory used : " + NumberFormat.getInstance().format(memoryEnd - memoryStart));
}
Aggregations