profile support

This commit is contained in:
wenshao
2012-10-07 17:51:11 +08:00
parent 7a7af668f3
commit e1823c2f2c
12 changed files with 415 additions and 82 deletions
@@ -18,6 +18,7 @@ package com.alibaba.druid.support.http;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.servlet.Filter;
@@ -41,6 +42,9 @@ import com.alibaba.druid.support.http.stat.WebSessionStat;
import com.alibaba.druid.support.http.stat.WebURIStat;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.support.profile.ProfileEntryKey;
import com.alibaba.druid.support.profile.ProfileEntryReqStat;
import com.alibaba.druid.support.profile.Profiler;
import com.alibaba.druid.util.DruidWebUtils;
import com.alibaba.druid.util.PatternMatcher;
import com.alibaba.druid.util.ServletPathMatcher;
@@ -55,6 +59,7 @@ public class WebStatFilter implements Filter {
private final static Log LOG = LogFactory.getLog(WebStatFilter.class);
public final static String PARAM_NAME_PORFILE_ENABLE = "profileEnable";
public final static String PARAM_NAME_SESSION_STAT_ENABLE = "sessionStatEnable";
public final static String PARAM_NAME_SESSION_STAT_MAX_COUNT = "sessionStatMaxCount";
public static final String PARAM_NAME_EXCLUSIONS = "exclusions";
@@ -75,6 +80,7 @@ public class WebStatFilter implements Filter {
private boolean sessionStatEnable = true;
private int sessionStatMaxCount = DEFAULT_MAX_STAT_SESSION_COUNT;
private boolean createSession = false;
private boolean profileEnable = false;
private String contextPath;
@@ -114,6 +120,10 @@ public class WebStatFilter implements Filter {
}
}
if (isProfileEnable()) {
Profiler.initLocal();
}
// 第一次访问时,uriStat这里为null,是为了防止404攻击。
if (uriStat != null) {
uriStat.beforeInvoke();
@@ -177,6 +187,12 @@ public class WebStatFilter implements Filter {
}
WebRequestStat.set(null);
if (isProfileEnable()) {
Map<ProfileEntryKey, ProfileEntryReqStat> requestStatsMap = Profiler.getStatsMap();
uriStat.getProfiletat().record(requestStatsMap);
Profiler.removeLocal();
}
}
}
@@ -339,6 +355,20 @@ public class WebStatFilter implements Filter {
}
}
}
{
String param = config.getInitParameter(PARAM_NAME_PORFILE_ENABLE);
if (param != null && param.trim().length() != 0) {
param = param.trim();
if ("true".equals(param)) {
this.profileEnable = true;
} else if ("false".equals(param)) {
this.profileEnable = false;
} else {
LOG.error("WebStatFilter Parameter '" + PARAM_NAME_PORFILE_ENABLE + "' config error");
}
}
}
{
String param = config.getInitParameter(PARAM_NAME_SESSION_STAT_MAX_COUNT);
if (param != null && param.trim().length() != 0) {
@@ -376,6 +406,14 @@ public class WebStatFilter implements Filter {
this.sessionStatEnable = sessionStatEnable;
}
public boolean isProfileEnable() {
return profileEnable;
}
public void setProfileEnable(boolean profileEnable) {
this.profileEnable = profileEnable;
}
public WebAppStat getWebAppStat() {
return webAppStat;
}
@@ -21,6 +21,9 @@ import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import com.alibaba.druid.support.profile.ProfileStat;
import com.alibaba.druid.support.profile.Profiler;
public class WebURIStat {
private final String uri;
@@ -54,6 +57,8 @@ public class WebURIStat {
private volatile long lastAccessTimeMillis = -1L;
private ProfileStat profiletat = new ProfileStat();
private final static ThreadLocal<WebURIStat> currentLocal = new ThreadLocal<WebURIStat>();
public WebURIStat(String uri){
@@ -93,6 +98,8 @@ public class WebURIStat {
if (requestStat != null) {
this.setLastAccessTimeMillis(requestStat.getStartMillis());
}
Profiler.enter(uri, Profiler.PROFILE_TYPE_WEB);
}
public void afterInvoke(Throwable error, long nanos) {
@@ -154,16 +161,18 @@ public class WebURIStat {
this.jdbcExecuteErrorCount.addAndGet(localStat.getJdbcExecuteErrorCount());
this.jdbcExecuteTimeNano.addAndGet(localStat.getJdbcExecuteTimeNano());
this.addJdbcPoolConnectionOpenCount(localStat.getJdbcPoolConnectionOpenCount());
this.addJdbcPoolConnectionCloseCount(localStat.getJdbcPoolConnectionCloseCount());
this.addJdbcResultSetOpenCount(localStat.getJdbcResultSetOpenCount());
this.addJdbcResultSetCloseCount(localStat.getJdbcResultSetCloseCount());
}
}
currentLocal.set(null);
Profiler.release(nanos);
}
public int getRunningCount() {
@@ -301,7 +310,7 @@ public class WebURIStat {
public long getJdbcResultSetOpenCount() {
return jdbcResultSetOpenCount.get();
}
public void addJdbcResultSetOpenCount(long delta) {
jdbcResultSetOpenCount.addAndGet(delta);
}
@@ -309,11 +318,15 @@ public class WebURIStat {
public long getJdbcResultSetCloseCount() {
return jdbcResultSetCloseCount.get();
}
public void addJdbcResultSetCloseCount(long delta) {
jdbcResultSetCloseCount.addAndGet(delta);
}
public ProfileStat getProfiletat() {
return profiletat;
}
public Map<String, Object> getStatData() {
Map<String, Object> data = new LinkedHashMap<String, Object>();
@@ -341,10 +354,12 @@ public class WebURIStat {
data.put("JdbcPoolConnectionOpenCount", this.getJdbcPoolConnectionOpenCount());
data.put("JdbcPoolConnectionCloseCount", this.getJdbcPoolConnectionCloseCount());
data.put("JdbcResultSetOpenCount", this.getJdbcResultSetOpenCount());
data.put("JdbcResultSetCloseCount", this.getJdbcResultSetCloseCount());
data.put("Profiles", this.getProfiletat().getStatData());
return data;
}
}