diff --git a/src/main/java/com/alibaba/druid/support/http/StatServlet.java b/src/main/java/com/alibaba/druid/support/http/StatServlet.java index 4b8f342066..ed9fa0968c 100644 --- a/src/main/java/com/alibaba/druid/support/http/StatServlet.java +++ b/src/main/java/com/alibaba/druid/support/http/StatServlet.java @@ -7,7 +7,7 @@ import java.sql.Driver; import java.sql.DriverManager; import java.util.ArrayList; import java.util.Enumeration; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -20,6 +20,7 @@ import com.alibaba.druid.VERSION; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.stat.DruidDataSourceStatManager; import com.alibaba.druid.stat.JdbcSqlStat; +import com.alibaba.druid.stat.JdbcStatManager; import com.alibaba.druid.util.IOUtils; import com.alibaba.druid.util.JdbcUtils; import com.alibaba.druid.util.StringUtils; @@ -58,15 +59,18 @@ public class StatServlet extends HttpServlet { returnJSONBasicStat(request, response); return; } - - if (path.equals("/datasource.html")) { - returnResourceFile(path, response); + + if (path.equals("/reset-all.json")) { + resetAllStat(); + returnJSONResult(request, response, RESULT_CODE_SUCCESS, null); return; } + if (path.equals("/datasource.json")) { returnJSONResult(request, response, RESULT_CODE_SUCCESS, getJSONDataSourceStatList()); return; } + if (path.startsWith("/datasource-")) { Integer id = StringUtils.subStringToInteger(path, "datasource-", "."); Object result = getJSONDataSourceStat(id); @@ -78,6 +82,7 @@ public class StatServlet extends HttpServlet { returnJSONResult(request, response, RESULT_CODE_SUCCESS, getJSONSqlStat()); return; } + if (path.startsWith("/sql-")) { Integer id = StringUtils.subStringToInteger(path, "sql-", "."); Object result = getJSONSqlStat(id); @@ -89,6 +94,11 @@ public class StatServlet extends HttpServlet { returnResourceFile(path, response); } + private void resetAllStat() { + JdbcStatManager.getInstance().reset(); + DruidDataSourceStatManager.getInstance().reset(); + } + private List getJSONDrivers() { List drivers = new ArrayList(); for (Enumeration e = DriverManager.getDrivers(); e.hasMoreElements();) { @@ -99,7 +109,7 @@ public class StatServlet extends HttpServlet { } private void returnJSONBasicStat(HttpServletRequest request, HttpServletResponse response) throws IOException { - Map json = new HashMap(); + Map json = new LinkedHashMap(); json.put("Version", VERSION.getVersionNumber()); json.put("Drivers", getJSONDrivers()); @@ -143,9 +153,9 @@ public class StatServlet extends HttpServlet { if (identity == null) { return null; } - for (DruidDataSource ds : DruidDataSourceStatManager.getDruidDataSourceInstances()) { - if (System.identityHashCode(ds) == identity) { - return ds; + for (DruidDataSource datasource : DruidDataSourceStatManager.getDruidDataSourceInstances()) { + if (System.identityHashCode(datasource) == identity) { + return datasource; } } return null; @@ -162,19 +172,23 @@ public class StatServlet extends HttpServlet { } private Map toJSONSqlStat(JdbcSqlStat sqlStat) { - Map json = new HashMap(); + Map json = new LinkedHashMap(); + json.put("SQL", sqlStat.getSql()); json.put("File", sqlStat.getFile()); json.put("Name", sqlStat.getName()); + json.put("ExecuteCount", sqlStat.getExecuteCount()); json.put("ExecuteMillisTotal", sqlStat.getExecuteMillisTotal()); json.put("ExecuteMillisMax", sqlStat.getExecuteMillisMax()); + json.put("InTxnCount", sqlStat.getInTransactionCount()); json.put("ErrorCount", sqlStat.getErrorCount()); json.put("UpdateCount", sqlStat.getUpdateCount()); json.put("FetchRowCount", sqlStat.getFetchRowCount()); json.put("RunningCount", sqlStat.getRunningCount()); json.put("ConcurrentMax", sqlStat.getConcurrentMax()); + json.put("ExecHistogram", sqlStat.getHistogram().toArray()); json.put("FetchRowHistogram", sqlStat.getFetchRowCountHistogram().toArray()); json.put("UpdateCountHistogram", sqlStat.getUpdateCountHistogram().toArray()); @@ -185,24 +199,30 @@ public class StatServlet extends HttpServlet { private Map toJSONDataSource(DruidDataSource dataSource) { - Map json = new HashMap(); + Map json = new LinkedHashMap(); json.put("Identity", System.identityHashCode(dataSource)); json.put("Name", dataSource.getName()); - json.put("URL", dataSource.getUrl()); json.put("DbType", dataSource.getDbType()); - json.put("UserName", dataSource.getUsername()); json.put("DriverClassName", dataSource.getDriverClassName()); + + json.put("URL", dataSource.getUrl()); + json.put("UserName", dataSource.getUsername()); + json.put("InitialSize", dataSource.getInitialSize()); json.put("MinIdle", dataSource.getMinIdle()); json.put("MaxActive", dataSource.getMaxActive()); + json.put("TestOnBorrow", dataSource.isTestOnBorrow()); json.put("TestWhileIdle", dataSource.isTestWhileIdle()); + json.put("LogicConnectCount", dataSource.getConnectCount()); json.put("LogicCloseCount", dataSource.getCloseCount()); json.put("LogicConnectErrorCount", dataSource.getConnectErrorCount()); + json.put("PhysicalConnectCount", dataSource.getCreateCount()); json.put("PhysicalCloseCount", dataSource.getDestroyCount()); json.put("PhysicalConnectErrorCount", dataSource.getCreateErrorCount()); + json.put("PSCacheAccessCount", dataSource.getCachedPreparedStatementAccessCount()); json.put("PSCacheHitCount", dataSource.getCachedPreparedStatementHitCount()); json.put("PSCacheMissCount", dataSource.getCachedPreparedStatementMissCount()); @@ -213,7 +233,7 @@ public class StatServlet extends HttpServlet { Object content) throws IOException { PrintWriter out = response.getWriter(); - Map json = new HashMap(); + Map json = new LinkedHashMap(); json.put("ResultCode", resultCode); json.put("Content", content); diff --git a/src/main/java/com/alibaba/druid/util/StringUtils.java b/src/main/java/com/alibaba/druid/util/StringUtils.java index da79134951..9c5420c64a 100644 --- a/src/main/java/com/alibaba/druid/util/StringUtils.java +++ b/src/main/java/com/alibaba/druid/util/StringUtils.java @@ -28,11 +28,10 @@ public class StringUtils { public static String subString(String src, String start, String to) { int indexFrom = src.indexOf(start); int indexTo = src.indexOf(to); - if (indexFrom < 0 || indexTo < 0 || indexFrom < indexTo) { + if (indexFrom < 0 || indexTo < 0 || indexFrom > indexTo) { return null; } indexFrom += start.length(); - indexTo += to.length(); return src.substring(indexFrom, indexTo); } diff --git a/src/main/resources/support/http/resources/api.html b/src/main/resources/support/http/resources/api.html new file mode 100644 index 0000000000..6093f675b4 --- /dev/null +++ b/src/main/resources/support/http/resources/api.html @@ -0,0 +1,74 @@ + + + + Druid Stat JSON API + + + + +
+ +
+
+

JSON API

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameDescription
+ basic.json + + basic info. +
+ datasource.json + + DatsSourceStat List +
+ datasource-{id}.json + + DatasourceStat for id={id},id is DatsSourceStat.ID
+ Example: druid/datasource-1.json +
+ sql.json + + SqlStat info List. +
+ sql-{id}.json + + SqlStat for id={id} ,id is SqlStat.ID
+ Example: druid/sql-1.json +
+ reset-all.json + + Reset all stat +
+
+ + \ No newline at end of file diff --git a/src/main/resources/support/http/resources/datasource.html b/src/main/resources/support/http/resources/datasource.html index 0594a9caf8..a555ccf6ce 100644 --- a/src/main/resources/support/http/resources/datasource.html +++ b/src/main/resources/support/http/resources/datasource.html @@ -1,7 +1,7 @@ - Druid DataSource Stat List + Druid DataSourceStat