From 73a267f721d806a9b8c506c67aa0b3c208f944b6 Mon Sep 17 00:00:00 2001 From: wenshao Date: Thu, 15 Aug 2013 19:39:49 +0800 Subject: [PATCH] improve monitor-client support --- .../druid/pool/DruidDataSourceStatValue.java | 96 ++++++--- .../com/alibaba/druid/stat/JdbcSqlStat.java | 8 +- .../alibaba/druid/stat/JdbcSqlStatValue.java | 129 ++++++------ .../support/http/stat/WebAppStatValue.java | 131 ++++++------ .../support/http/stat/WebURIStatValue.java | 81 ++++---- .../monitor/{ => annotation}/MField.java | 10 +- .../monitor/{ => annotation}/MTable.java | 2 +- .../monitor/dao/MonitorDaoJdbcImpl.java | 196 +++++++++++++++++- .../spring/stat/SpringMethodStatValue.java | 65 +++--- .../druid/wall/WallFunctionStatValue.java | 9 +- .../druid/wall/WallProviderStatValue.java | 21 +- .../alibaba/druid/wall/WallSqlStatValue.java | 19 +- .../druid/wall/WallTableStatValue.java | 35 ++-- .../support/monitor/mysql/datasource.sql | 16 +- .../support}/monitor/mysql/springmethod.sql | 7 +- .../resources/support}/monitor/mysql/sql.sql | 8 +- .../support}/monitor/mysql/webapp.sql | 7 +- .../support/monitor/mysql/weburi.sql | 9 +- .../monitor/MonitorDaoJdbcImplTest.java | 54 ++--- 19 files changed, 553 insertions(+), 350 deletions(-) rename src/main/java/com/alibaba/druid/support/monitor/{ => annotation}/MField.java (85%) rename src/main/java/com/alibaba/druid/support/monitor/{ => annotation}/MTable.java (94%) rename doc/monitor/mysql/dataSource.sql => src/main/resources/support/monitor/mysql/datasource.sql (76%) rename {doc => src/main/resources/support}/monitor/mysql/springmethod.sql (90%) rename {doc => src/main/resources/support}/monitor/mysql/sql.sql (94%) rename {doc => src/main/resources/support}/monitor/mysql/webapp.sql (95%) rename doc/monitor/mysql/webrui.sql => src/main/resources/support/monitor/mysql/weburi.sql (89%) diff --git a/src/main/java/com/alibaba/druid/pool/DruidDataSourceStatValue.java b/src/main/java/com/alibaba/druid/pool/DruidDataSourceStatValue.java index f344c25718..60ca505676 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidDataSourceStatValue.java +++ b/src/main/java/com/alibaba/druid/pool/DruidDataSourceStatValue.java @@ -19,19 +19,20 @@ import java.util.Date; import java.util.List; import com.alibaba.druid.stat.JdbcSqlStatValue; -import com.alibaba.druid.support.monitor.MField; -import com.alibaba.druid.support.monitor.MTable; +import com.alibaba.druid.support.monitor.annotation.AggregateType; +import com.alibaba.druid.support.monitor.annotation.MField; +import com.alibaba.druid.support.monitor.annotation.MTable; @MTable(name = "druid_datasource") public class DruidDataSourceStatValue { - @MField(groupBy = true) + @MField(groupBy = true, aggregate = AggregateType.None) protected String name; - @MField + @MField(aggregate = AggregateType.None) protected String dbType; - @MField + @MField(aggregate = AggregateType.None) protected String driverClassName; protected String url; @@ -54,75 +55,93 @@ public class DruidDataSourceStatValue { protected boolean defaultReadOnly; protected Integer defaultTransactionIsolation; - @MField + @MField(aggregate = AggregateType.Last) protected int activeCount; - @MField + @MField(aggregate = AggregateType.Max) protected int activePeak; - @MField + @MField(aggregate = AggregateType.Max) protected long activePeakTime; - @MField + @MField(aggregate = AggregateType.Last) protected int poolingCount; - @MField + @MField(aggregate = AggregateType.Max) protected int poolingPeak; - @MField + @MField(aggregate = AggregateType.Max) protected long poolingPeakTime; - @MField + @MField(aggregate = AggregateType.Sum) protected long connectCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long closeCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long waitThreadCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long notEmptyWaitCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long notEmptyWaitNanos; - @MField + @MField(aggregate = AggregateType.Sum) protected long logicConnectErrorCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long physicalConnectCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long physicalCloseCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long physicalConnectErrorCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long executeCount; - @MField + + @MField(aggregate = AggregateType.Sum) protected long errorCount; - @MField + + @MField(aggregate = AggregateType.Sum) protected long commitCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long rollbackCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long pstmtCacheHitCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long pstmtCacheMissCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long startTransactionCount; - protected long[] transactionHistogram; + // protected long[] transactionHistogram; protected long[] connectionHoldTimeHistogram; - @MField + @MField(aggregate = AggregateType.Sum) + protected long txn_0_1; + @MField(aggregate = AggregateType.Sum) + protected long txn_1_10; + @MField(aggregate = AggregateType.Sum) + protected long txn_10_100; + @MField(aggregate = AggregateType.Sum) + protected long txn_100_1000; + @MField(aggregate = AggregateType.Sum) + protected long txn_1000_10000; + @MField(aggregate = AggregateType.Sum) + protected long txn_10000_100000; + @MField(aggregate = AggregateType.Sum) + protected long txn_more; + + @MField(aggregate = AggregateType.Sum) protected long clobOpenCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long blobOpenCount; + @MField(aggregate = AggregateType.Sum) protected long sqlSkipCount; protected List sqlList; @@ -484,11 +503,22 @@ public class DruidDataSourceStatValue { } public long[] getTransactionHistogram() { - return transactionHistogram; + return new long[] { txn_0_1,// txn_0_1 + txn_10_100, // + txn_100_1000, // + txn_1000_10000, // + txn_10000_100000, // + txn_more, // + }; } - public void setTransactionHistogram(long[] transactionHistogram) { - this.transactionHistogram = transactionHistogram; + public void setTransactionHistogram(long[] values) { + txn_0_1 = values[0]; + txn_10_100 = values[1]; + txn_100_1000 = values[2]; + txn_1000_10000 = values[3]; + txn_10000_100000 = values[4]; + txn_more = values[5]; } public long[] getConnectionHoldTimeHistogram() { diff --git a/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java b/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java index 34a449a8ee..fd0829da4c 100644 --- a/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java +++ b/src/main/java/com/alibaba/druid/stat/JdbcSqlStat.java @@ -866,12 +866,15 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { SimpleType.LONG, // SimpleType.LONG, // - // 35 - + // 35 - 39 SimpleType.LONG, // SimpleType.LONG, // SimpleType.LONG, // SimpleType.LONG, // SimpleType.LONG, // + + // 40 - + SimpleType.LONG, // }; @@ -931,6 +934,9 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean { "ReadBytesLength", // "InputStreamOpenCount", // "ReaderOpenCount", // + + // 40 + "HASH", // // }; diff --git a/src/main/java/com/alibaba/druid/stat/JdbcSqlStatValue.java b/src/main/java/com/alibaba/druid/stat/JdbcSqlStatValue.java index 2bc0343a99..5ec4ccecf2 100644 --- a/src/main/java/com/alibaba/druid/stat/JdbcSqlStatValue.java +++ b/src/main/java/com/alibaba/druid/stat/JdbcSqlStatValue.java @@ -21,8 +21,9 @@ import java.util.Map; import javax.management.JMException; -import com.alibaba.druid.support.monitor.MField; -import com.alibaba.druid.support.monitor.MTable; +import com.alibaba.druid.support.monitor.annotation.AggregateType; +import com.alibaba.druid.support.monitor.annotation.MField; +import com.alibaba.druid.support.monitor.annotation.MTable; import com.alibaba.druid.util.JMXUtils; import com.alibaba.druid.util.Utils; @@ -33,129 +34,129 @@ public class JdbcSqlStatValue { protected String sql; - @MField(groupBy = true) + @MField(groupBy = true, aggregate = AggregateType.None, hashFor = "sql", hashForType="sql") private long sqlHash; - @MField + @MField(aggregate = AggregateType.None) protected String dataSource; - @MField(name = "lastStartTime") + @MField(name = "lastStartTime", aggregate = AggregateType.Max) protected long executeLastStartTime; - @MField(name = "batchTotal") + @MField(name = "batchTotal", aggregate = AggregateType.Sum) protected long executeBatchSizeTotal; - @MField(name = "batchToMax") + @MField(name = "batchToMax", aggregate = AggregateType.Max) protected int executeBatchSizeMax; - @MField(name = "execSuccessCount") + @MField(name = "execSuccessCount", aggregate = AggregateType.Sum) protected long executeSuccessCount; - @MField(name = "execNanoTotal") + @MField(name = "execNanoTotal", aggregate = AggregateType.Sum) protected long executeSpanNanoTotal; - @MField(name = "execNanoMax") + @MField(name = "execNanoMax", aggregate = AggregateType.Sum) protected long executeSpanNanoMax; - @MField(name = "running") + @MField(name = "running", aggregate = AggregateType.Last) protected int runningCount; - @MField + @MField(aggregate = AggregateType.Max) protected int concurrentMax; - @MField(name = "rsHoldTime") + @MField(name = "rsHoldTime", aggregate = AggregateType.Sum) protected long resultSetHoldTimeNano; - @MField(name = "execRsHoldTime") + @MField(name = "execRsHoldTime", aggregate = AggregateType.Sum) protected long executeAndResultSetHoldTime; - @MField + @MField(aggregate = AggregateType.None) protected String name; - @MField + @MField(aggregate = AggregateType.None) protected String file; - @MField + @MField(aggregate = AggregateType.None) protected String dbType; - @MField(name = "execNanoMaxOccurTime") + @MField(name = "execNanoMaxOccurTime", aggregate = AggregateType.Max) protected long executeNanoSpanMaxOccurTime; - @MField(name = "errorCount") + @MField(name = "errorCount", aggregate = AggregateType.Sum) protected long executeErrorCount; protected Throwable executeErrorLast; - @MField(name = "errorLastMsg") + @MField(name = "errorLastMsg", aggregate = AggregateType.Last) protected String executeErrorLastMessage; - @MField(name = "errorLastClass") + @MField(name = "errorLastClass", aggregate = AggregateType.Last) protected String executeErrorLastClass; - @MField(name = "errorLastStackTrace") + @MField(name = "errorLastStackTrace", aggregate = AggregateType.Last) protected String executeErrorLastStackTrace; - @MField(name = "errorLastTime") + @MField(name = "errorLastTime", aggregate = AggregateType.Last) protected long executeErrorLastTime; - @MField + @MField(aggregate = AggregateType.Sum) protected long updateCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long updateCountMax; - @MField + @MField(aggregate = AggregateType.Sum) protected long fetchRowCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long fetchRowCountMax; - @MField(name = "inTxnCount") + @MField(name = "inTxnCount", aggregate = AggregateType.Sum) protected long inTransactionCount; - @MField + @MField(aggregate = AggregateType.Last) protected String lastSlowParameters; - @MField + @MField(aggregate = AggregateType.Sum) protected long clobOpenCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long blobOpenCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long readStringLength; - @MField + @MField(aggregate = AggregateType.Sum) protected long readBytesLength; - @MField + @MField(aggregate = AggregateType.Sum) protected long inputStreamOpenCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long readerOpenCount; - @MField(name = "h1") + @MField(name = "h1", aggregate = AggregateType.Sum) protected long histogram_0_1; - @MField(name = "h10") + @MField(name = "h10", aggregate = AggregateType.Sum) protected long histogram_1_10; - @MField(name = "h100") + @MField(name = "h100", aggregate = AggregateType.Sum) protected int histogram_10_100; - @MField(name = "h1000") + @MField(name = "h1000", aggregate = AggregateType.Sum) protected int histogram_100_1000; - @MField(name = "h10000") + @MField(name = "h10000", aggregate = AggregateType.Sum) protected int histogram_1000_10000; - @MField(name = "h100000") + @MField(name = "h100000", aggregate = AggregateType.Sum) protected int histogram_10000_100000; - @MField(name = "h1000000") + @MField(name = "h1000000", aggregate = AggregateType.Sum) protected int histogram_100000_1000000; - @MField(name = "hmore") + @MField(name = "hmore", aggregate = AggregateType.Sum) protected int histogram_1000000_more; public long[] getExecuteHistogram() { @@ -170,28 +171,28 @@ public class JdbcSqlStatValue { }; } - @MField(name = "eh1") + @MField(name = "eh1", aggregate = AggregateType.Sum) protected long executeAndResultHoldTime_0_1; - @MField(name = "eh10") + @MField(name = "eh10", aggregate = AggregateType.Sum) protected long executeAndResultHoldTime_1_10; - @MField(name = "eh100") + @MField(name = "eh100", aggregate = AggregateType.Sum) protected int executeAndResultHoldTime_10_100; - @MField(name = "eh1000") + @MField(name = "eh1000", aggregate = AggregateType.Sum) protected int executeAndResultHoldTime_100_1000; - @MField(name = "eh10000") + @MField(name = "eh10000", aggregate = AggregateType.Sum) protected int executeAndResultHoldTime_1000_10000; - @MField(name = "eh100000") + @MField(name = "eh100000", aggregate = AggregateType.Sum) protected int executeAndResultHoldTime_10000_100000; - @MField(name = "eh1000000") + @MField(name = "eh1000000", aggregate = AggregateType.Sum) protected int executeAndResultHoldTime_100000_1000000; - @MField(name = "ehmore") + @MField(name = "ehmore", aggregate = AggregateType.Sum) protected int executeAndResultHoldTime_1000000_more; public long[] getExecuteAndResultHoldHistogram() { @@ -206,22 +207,22 @@ public class JdbcSqlStatValue { }; } - @MField(name = "f1") + @MField(name = "f1", aggregate = AggregateType.Sum) protected long fetchRowCount_0_1; - @MField(name = "f10") + @MField(name = "f10", aggregate = AggregateType.Sum) protected long fetchRowCount_1_10; - @MField(name = "f100") + @MField(name = "f100", aggregate = AggregateType.Sum) protected long fetchRowCount_10_100; - @MField(name = "f1000") + @MField(name = "f1000", aggregate = AggregateType.Sum) protected int fetchRowCount_100_1000; - @MField(name = "f10000") + @MField(name = "f10000", aggregate = AggregateType.Sum) protected int fetchRowCount_1000_10000; - @MField(name = "fmore") + @MField(name = "fmore", aggregate = AggregateType.Sum) protected int fetchRowCount_10000_more; public long[] getFetchRowHistogram() { @@ -234,22 +235,22 @@ public class JdbcSqlStatValue { }; } - @MField(name = "u1") + @MField(name = "u1", aggregate = AggregateType.Sum) protected long updateCount_0_1; - @MField(name = "u10") + @MField(name = "u10", aggregate = AggregateType.Sum) protected long updateCount_1_10; - @MField(name = "u100") + @MField(name = "u100", aggregate = AggregateType.Sum) protected long updateCount_10_100; - @MField(name = "u1000") + @MField(name = "u1000", aggregate = AggregateType.Sum) protected int updateCount_100_1000; - @MField(name = "u10000") + @MField(name = "u10000", aggregate = AggregateType.Sum) protected int updateCount_1000_10000; - @MField(name = "umore") + @MField(name = "umore", aggregate = AggregateType.Sum) protected int updateCount_10000_more; public long[] getUpdateHistogram() { @@ -690,6 +691,8 @@ public class JdbcSqlStatValue { map.put("InputStreamOpenCount", getInputStreamOpenCount()); // 38 map.put("ReaderOpenCount", getReaderOpenCount()); // 39 + map.put("HASH", getSqlHash()); // 40 + return map; } diff --git a/src/main/java/com/alibaba/druid/support/http/stat/WebAppStatValue.java b/src/main/java/com/alibaba/druid/support/http/stat/WebAppStatValue.java index d506140f35..109b9ce48d 100644 --- a/src/main/java/com/alibaba/druid/support/http/stat/WebAppStatValue.java +++ b/src/main/java/com/alibaba/druid/support/http/stat/WebAppStatValue.java @@ -18,165 +18,166 @@ package com.alibaba.druid.support.http.stat; import java.util.LinkedHashMap; import java.util.Map; -import com.alibaba.druid.support.monitor.MField; -import com.alibaba.druid.support.monitor.MTable; +import com.alibaba.druid.support.monitor.annotation.AggregateType; +import com.alibaba.druid.support.monitor.annotation.MField; +import com.alibaba.druid.support.monitor.annotation.MTable; @MTable(name = "druid_webapp") public class WebAppStatValue { - @MField(groupBy = true) + @MField(groupBy = true, aggregate=AggregateType.None) String contextPath; - @MField + @MField(aggregate = AggregateType.Last) int runningCount; - @MField + @MField(aggregate = AggregateType.Max) int concurrentMax; - @MField + @MField(aggregate = AggregateType.Sum) long requestCount; - @MField + @MField(aggregate = AggregateType.Last) long sessionCount; - @MField + @MField(aggregate = AggregateType.Sum) long jdbcFetchRowCount; - @MField + @MField(aggregate = AggregateType.Sum) long jdbcUpdateCount; - @MField + @MField(aggregate = AggregateType.Sum) long jdbcExecuteCount; - @MField + @MField(aggregate = AggregateType.Sum) long jdbcExecuteTimeNano; - @MField + @MField(aggregate = AggregateType.Sum) long jdbcCommitCount; - @MField + @MField(aggregate = AggregateType.Sum) long jdbcRollbackCount; - @MField + @MField(aggregate = AggregateType.Sum) long osMacOSXCount; - @MField + @MField(aggregate = AggregateType.Sum) long osWindowsCount; - @MField + @MField(aggregate = AggregateType.Sum) long osLinuxCount; - @MField + @MField(aggregate = AggregateType.Sum) long osSymbianCount; - @MField + @MField(aggregate = AggregateType.Sum) long osFreeBSDCount; - @MField + @MField(aggregate = AggregateType.Sum) long osOpenBSDCount; - @MField + @MField(aggregate = AggregateType.Sum) long osAndroidCount; - @MField + @MField(aggregate = AggregateType.Sum) long osWindows98Count; - @MField + @MField(aggregate = AggregateType.Sum) long osWindowsXPCount; - @MField + @MField(aggregate = AggregateType.Sum) long osWindows2000Count; - @MField + @MField(aggregate = AggregateType.Sum) long osWindowsVistaCount; - @MField + @MField(aggregate = AggregateType.Sum) long osWindows7Count; - @MField + @MField(aggregate = AggregateType.Sum) long osWindows8Count; - @MField + @MField(aggregate = AggregateType.Sum) long osAndroid15Count; - @MField + @MField(aggregate = AggregateType.Sum) long osAndroid16Count; - @MField + @MField(aggregate = AggregateType.Sum) long osAndroid20Count; - @MField + @MField(aggregate = AggregateType.Sum) long osAndroid21Count; - @MField + @MField(aggregate = AggregateType.Sum) long osAndroid22Count; - @MField + @MField(aggregate = AggregateType.Sum) long osAndroid23Count; - @MField + @MField(aggregate = AggregateType.Sum) long osAndroid30Count; - @MField + @MField(aggregate = AggregateType.Sum) long osAndroid31Count; - @MField + @MField(aggregate = AggregateType.Sum) long osAndroid32Count; - @MField + @MField(aggregate = AggregateType.Sum) long osAndroid40Count; - @MField + @MField(aggregate = AggregateType.Sum) long osAndroid41Count; - @MField + @MField(aggregate = AggregateType.Sum) long osAndroid42Count; - @MField + @MField(aggregate = AggregateType.Sum) long osAndroid43Count; - @MField + @MField(aggregate = AggregateType.Sum) long osLinuxUbuntuCount; - @MField + @MField(aggregate = AggregateType.Sum) long browserIECount; - @MField + @MField(aggregate = AggregateType.Sum) long browserFirefoxCount; - @MField + @MField(aggregate = AggregateType.Sum) long browserChromeCount; - @MField + @MField(aggregate = AggregateType.Sum) long browserSafariCount; - @MField + @MField(aggregate = AggregateType.Sum) long browserOperaCount; - @MField + @MField(aggregate = AggregateType.Sum) long browserIE5Count; - @MField + @MField(aggregate = AggregateType.Sum) long browserIE6Count; - @MField + @MField(aggregate = AggregateType.Sum) long browserIE7Count; - @MField + @MField(aggregate = AggregateType.Sum) long browserIE8Count; - @MField + @MField(aggregate = AggregateType.Sum) long browserIE9Count; - @MField + @MField(aggregate = AggregateType.Sum) long browserIE10Count; - @MField + @MField(aggregate = AggregateType.Sum) long browser360SECount; - @MField + @MField(aggregate = AggregateType.Sum) long deviceAndroidCount; - @MField + @MField(aggregate = AggregateType.Sum) long deviceIpadCount; - @MField + @MField(aggregate = AggregateType.Sum) long deviceIphoneCount; - @MField + @MField(aggregate = AggregateType.Sum) long deviceWindowsPhoneCount; - @MField + @MField(aggregate = AggregateType.Sum) long botCount; - @MField + @MField(aggregate = AggregateType.Sum) long botBaiduCount; - @MField + @MField(aggregate = AggregateType.Sum) long botYoudaoCount; - @MField + @MField(aggregate = AggregateType.Sum) long botGoogleCount; - @MField + @MField(aggregate = AggregateType.Sum) long botMsnCount; - @MField + @MField(aggregate = AggregateType.Sum) long botBingCount; - @MField + @MField(aggregate = AggregateType.Sum) long botSosoCount; - @MField + @MField(aggregate = AggregateType.Sum) long botSogouCount; - @MField + @MField(aggregate = AggregateType.Sum) long botYahooCount; public String getContextPath() { diff --git a/src/main/java/com/alibaba/druid/support/http/stat/WebURIStatValue.java b/src/main/java/com/alibaba/druid/support/http/stat/WebURIStatValue.java index 812b3b9aae..7b6b7baac7 100644 --- a/src/main/java/com/alibaba/druid/support/http/stat/WebURIStatValue.java +++ b/src/main/java/com/alibaba/druid/support/http/stat/WebURIStatValue.java @@ -21,99 +21,100 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import com.alibaba.druid.support.monitor.MField; -import com.alibaba.druid.support.monitor.MTable; +import com.alibaba.druid.support.monitor.annotation.AggregateType; +import com.alibaba.druid.support.monitor.annotation.MField; +import com.alibaba.druid.support.monitor.annotation.MTable; import com.alibaba.druid.support.profile.ProfileEntryStatValue; @MTable(name = "druid_weburi") public class WebURIStatValue { - @MField(groupBy = true) + @MField(groupBy = true, aggregate = AggregateType.None) protected String uri; - @MField + @MField(aggregate = AggregateType.Last) protected int runningCount; - @MField + @MField(aggregate = AggregateType.Max) protected int concurrentMax; - @MField + @MField(aggregate = AggregateType.Sum) protected long requestCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long requestTimeNano; - @MField + @MField(aggregate = AggregateType.Sum) protected long jdbcFetchRowCount; - @MField + @MField(aggregate = AggregateType.Max) protected long jdbcFetchRowPeak; - @MField + @MField(aggregate = AggregateType.Sum) protected long jdbcUpdateCount; - @MField + @MField(aggregate = AggregateType.Max) protected long jdbcUpdatePeak; - @MField + @MField(aggregate = AggregateType.Sum) protected long jdbcExecuteCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long jdbcExecuteErrorCount; - @MField + @MField(aggregate = AggregateType.Max) protected long jdbcExecutePeak; // 单次请求执行SQL次数的峰值 - @MField + @MField(aggregate = AggregateType.Sum) protected long jdbcExecuteTimeNano; - @MField + @MField(aggregate = AggregateType.Sum) protected long jdbcCommitCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long jdbcRollbackCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long jdbcPoolConnectionOpenCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long jdbcPoolConnectionCloseCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long jdbcResultSetOpenCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long jdbcResultSetCloseCount; - @MField + @MField(aggregate = AggregateType.Sum) protected long errorCount; - @MField - protected long lastAccessTimeMillis = -1L; + @MField(aggregate = AggregateType.Last) + protected Date lastAccessTime = null; private List profileEntryStatValueList; - @MField(name = "h1") + @MField(name = "h1", aggregate = AggregateType.Sum) protected long histogram_0_1; - @MField(name = "h10") + @MField(name = "h10", aggregate = AggregateType.Sum) protected long histogram_1_10; - @MField(name = "h100") + @MField(name = "h100", aggregate = AggregateType.Sum) protected long histogram_10_100; - @MField(name = "h1000") + @MField(name = "h1000", aggregate = AggregateType.Sum) protected long histogram_100_1000; - @MField(name = "h10000") + @MField(name = "h10000", aggregate = AggregateType.Sum) protected int histogram_1000_10000; - @MField(name = "h100000") + @MField(name = "h100000", aggregate = AggregateType.Sum) protected int histogram_10000_100000; - @MField(name = "h1000000") + @MField(name = "h1000000", aggregate = AggregateType.Sum) protected int histogram_100000_1000000; - @MField(name = "hmore") + @MField(name = "hmore", aggregate = AggregateType.Sum) protected int histogram_1000000_more; public long[] getHistogram() { @@ -296,12 +297,12 @@ public class WebURIStatValue { this.errorCount = errorCount; } - public long getLastAccessTimeMillis() { - return lastAccessTimeMillis; - } - public void setLastAccessTimeMillis(long lastAccessTimeMillis) { - this.lastAccessTimeMillis = lastAccessTimeMillis; + if (lastAccessTimeMillis > 0) { + this.lastAccessTime = new Date(lastAccessTimeMillis); + } else { + this.lastAccessTime = null; + } } public long getRequestTimeMillis() { @@ -309,11 +310,7 @@ public class WebURIStatValue { } public Date getLastAccessTime() { - if (lastAccessTimeMillis < 0L) { - return null; - } - - return new Date(lastAccessTimeMillis); + return lastAccessTime; } public long getJdbcExecuteTimeMillis() { diff --git a/src/main/java/com/alibaba/druid/support/monitor/MField.java b/src/main/java/com/alibaba/druid/support/monitor/annotation/MField.java similarity index 85% rename from src/main/java/com/alibaba/druid/support/monitor/MField.java rename to src/main/java/com/alibaba/druid/support/monitor/annotation/MField.java index 60391280cc..c0cf7ebe55 100644 --- a/src/main/java/com/alibaba/druid/support/monitor/MField.java +++ b/src/main/java/com/alibaba/druid/support/monitor/annotation/MField.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.druid.support.monitor; +package com.alibaba.druid.support.monitor.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -25,6 +25,12 @@ import java.lang.annotation.Target; public @interface MField { String name() default ""; - + boolean groupBy() default false; + + AggregateType aggregate(); + + String hashFor() default ""; + + String hashForType() default ""; } diff --git a/src/main/java/com/alibaba/druid/support/monitor/MTable.java b/src/main/java/com/alibaba/druid/support/monitor/annotation/MTable.java similarity index 94% rename from src/main/java/com/alibaba/druid/support/monitor/MTable.java rename to src/main/java/com/alibaba/druid/support/monitor/annotation/MTable.java index 398389c8aa..cc4965401c 100644 --- a/src/main/java/com/alibaba/druid/support/monitor/MTable.java +++ b/src/main/java/com/alibaba/druid/support/monitor/annotation/MTable.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.druid.support.monitor; +package com.alibaba.druid.support.monitor.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/src/main/java/com/alibaba/druid/support/monitor/dao/MonitorDaoJdbcImpl.java b/src/main/java/com/alibaba/druid/support/monitor/dao/MonitorDaoJdbcImpl.java index 0d0c654369..f2a4159cc6 100644 --- a/src/main/java/com/alibaba/druid/support/monitor/dao/MonitorDaoJdbcImpl.java +++ b/src/main/java/com/alibaba/druid/support/monitor/dao/MonitorDaoJdbcImpl.java @@ -28,6 +28,8 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import javax.sql.DataSource; @@ -38,9 +40,10 @@ import com.alibaba.druid.support.http.stat.WebAppStatValue; import com.alibaba.druid.support.http.stat.WebURIStatValue; import com.alibaba.druid.support.logging.Log; import com.alibaba.druid.support.logging.LogFactory; -import com.alibaba.druid.support.monitor.MField; -import com.alibaba.druid.support.monitor.MTable; import com.alibaba.druid.support.monitor.MonitorContext; +import com.alibaba.druid.support.monitor.annotation.AggregateType; +import com.alibaba.druid.support.monitor.annotation.MField; +import com.alibaba.druid.support.monitor.annotation.MTable; import com.alibaba.druid.support.spring.stat.SpringMethodStatValue; import com.alibaba.druid.util.JdbcUtils; import com.alibaba.druid.util.StringUtils; @@ -58,11 +61,31 @@ public class MonitorDaoJdbcImpl implements MonitorDao { private BeanInfo webURIStatBeanInfo = new BeanInfo(WebURIStatValue.class); private BeanInfo webAppStatBeanInfo = new BeanInfo(WebAppStatValue.class); - // - public MonitorDaoJdbcImpl(){ } + public void createTables(String dbType) { + String[] resources = new String[] { "const.sql", // + "datasource.sql", // + "springmethod.sql", // + "sql.sql", // + "webapp.sql", // + "weburi.sql" }; + + for (String item : resources) { + String path = "/support/monitor/" + dbType + "/" + item; + try { + String text = Utils.readFromResource(path); + String[] sqls = text.split(";"); + for (String sql : sqls) { + JdbcUtils.execute(dataSource, sql); + } + } catch (Exception ex) { + LOG.error("create table error", ex); + } + } + } + public DataSource getDataSource() { return dataSource; } @@ -166,11 +189,30 @@ public class MonitorDaoJdbcImpl implements MonitorDao { List fields = beanInfo.getFields(); for (int i = 0; i < fields.size(); ++i) { - FieldInfo field = fields.get(i); + FieldInfo fieldInfo = fields.get(i); if (i != 0) { buf.append(", "); } - buf.append(field.getColumnName()); + + AggregateType aggregateType = fieldInfo.getField().getAnnotation(MField.class).aggregate(); + + switch (aggregateType) { + case Sum: + buf.append("SUM("); + buf.append(fieldInfo.getColumnName()); + buf.append(")"); + break; + case Max: + buf.append("MAX("); + buf.append(fieldInfo.getColumnName()); + buf.append(")"); + break; + case None: + case Last: + default: + buf.append(fieldInfo.getColumnName()); + break; + } } buf.append("\nFROM "); @@ -212,6 +254,17 @@ public class MonitorDaoJdbcImpl implements MonitorDao { buf.append("\nAND pid = ?"); } + List groupByFields = beanInfo.getGroupByFields(); + for (int i = 0; i < groupByFields.size(); ++i) { + if (i == 0) { + buf.append("\nGROUP BY "); + } else { + buf.append(", "); + } + FieldInfo fieldInfo = groupByFields.get(i); + buf.append(fieldInfo.getColumnName()); + } + { Integer offset = (Integer) filters.get("offset"); Integer limit = (Integer) filters.get("limit"); @@ -275,6 +328,10 @@ public class MonitorDaoJdbcImpl implements MonitorDao { JdbcUtils.close(conn); } + for (FieldInfo hashField : beanInfo.getHashFields()) { + loadHashValue(hashField, list, filters); + } + return list; } @@ -304,7 +361,73 @@ public class MonitorDaoJdbcImpl implements MonitorDao { } } + private void loadHashValue(FieldInfo hashField, List list, Map filters) { + String domain = (String) filters.get("domain"); + if (StringUtils.isEmpty(domain)) { + domain = "defaultDomain"; + } + String app = (String) filters.get("app"); + if (StringUtils.isEmpty(app)) { + app = "defaultApp"; + } + + for (Object statValue : list) { + try { + String sql = "select hash, value from druid_const where domain = ? AND app = ? and type = ? and value = ?"; + Long hash = (Long) hashField.field.get(statValue); + } catch (IllegalArgumentException e) { + throw new DruidRuntimeException("set field error" + hashField.getField(), e); + } catch (IllegalAccessException e) { + throw new DruidRuntimeException("set field error" + hashField.getField(), e); + } + } + } + + private void saveHash(FieldInfo hashField, MonitorContext ctx, List list) { + final String hashType = hashField.getHashForType(); + + for (Object statValue : list) { + try { + Long hash = (Long) hashField.field.get(statValue); + if (!hashField.hashCacheContains(hash)) { + String value = (String) hashField.getHashFor().get(statValue); + final String sql = "insert into druid_const (domain, app, type, hash, value) values (?, ?, ?, ?, ?)"; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = dataSource.getConnection(); + stmt = conn.prepareStatement(sql); + + stmt.setString(1, ctx.getDomainName()); + stmt.setString(2, ctx.getAppName()); + stmt.setString(3, hashType); + stmt.setLong(4, hash); + stmt.setString(5, value); + + stmt.execute(); + stmt.close(); + + } catch (SQLException ex) { + LOG.error("save const error error", ex); + } finally { + JdbcUtils.close(stmt); + JdbcUtils.close(conn); + } + hashField.hashCacheAdd(hash, value); + } + } catch (IllegalArgumentException e) { + throw new DruidRuntimeException("set field error" + hashField.getField(), e); + } catch (IllegalAccessException e) { + throw new DruidRuntimeException("set field error" + hashField.getField(), e); + } + } + } + private void save(BeanInfo beanInfo, MonitorContext ctx, List list) { + for (FieldInfo hashField : beanInfo.getHashFields()) { + saveHash(hashField, ctx, list); + } + String sql = buildInsertSql(beanInfo); Connection conn = null; PreparedStatement stmt = null; @@ -453,8 +576,10 @@ public class MonitorDaoJdbcImpl implements MonitorDao { private final Class clazz; private final List fields = new ArrayList(); private final List groupByFields = new ArrayList(); + private final List hashFields = new ArrayList(); + private final String tableName; + private String insertSql; - private String tableName; public BeanInfo(Class clazz){ this.clazz = clazz; @@ -479,11 +604,27 @@ public class MonitorDaoJdbcImpl implements MonitorDao { columnName = field.getName(); } - FieldInfo fieldInfo = new FieldInfo(field, columnName); + Field hashFor = null; + String hashForType = null; + if (!StringUtils.isEmpty(annotation.hashFor())) { + try { + hashFor = clazz.getDeclaredField(annotation.hashFor()); + hashForType = annotation.hashForType(); + } catch (Exception e) { + throw new IllegalStateException("hashFor error", e); + } + } + + FieldInfo fieldInfo = new FieldInfo(field, columnName, hashFor, hashForType); fields.add(fieldInfo); + if (annotation.groupBy()) { groupByFields.add(fieldInfo); } + + if (hashFor != null) { + hashFields.add(fieldInfo); + } } } @@ -511,24 +652,45 @@ public class MonitorDaoJdbcImpl implements MonitorDao { return groupByFields; } + public List getHashFields() { + return hashFields; + } + } public static class FieldInfo { - private final Field field; - private final String columnName; + private final Field field; + private final String columnName; + private final Field hashFor; + private final String hashForType; - public FieldInfo(Field field, String columnName){ + private ConcurrentMap hashCache = new ConcurrentHashMap(16, 0.75f, 1); + + public FieldInfo(Field field, String columnName, Field hashFor, String hashForType){ this.field = field; this.columnName = columnName; + this.hashFor = hashFor; + this.hashForType = hashForType; field.setAccessible(true); + if (hashFor != null) { + hashFor.setAccessible(true); + } + } + + public String getHashForType() { + return hashForType; } public Field getField() { return field; } + public Field getHashFor() { + return hashFor; + } + public String getColumnName() { return columnName; } @@ -536,6 +698,18 @@ public class MonitorDaoJdbcImpl implements MonitorDao { public Class getFieldType() { return field.getType(); } + + public void hashCacheAdd(long hash, String value) { + if (hashCache.size() > 1000) { + return; + } + + hashCache.put(hash, value); + } + + public boolean hashCacheContains(long hash) { + return hashCache.containsKey(hash); + } } @Override diff --git a/src/main/java/com/alibaba/druid/support/spring/stat/SpringMethodStatValue.java b/src/main/java/com/alibaba/druid/support/spring/stat/SpringMethodStatValue.java index cfddf3410c..46c9774ae1 100644 --- a/src/main/java/com/alibaba/druid/support/spring/stat/SpringMethodStatValue.java +++ b/src/main/java/com/alibaba/druid/support/spring/stat/SpringMethodStatValue.java @@ -19,101 +19,102 @@ import java.util.Date; import java.util.LinkedHashMap; import java.util.Map; -import com.alibaba.druid.support.monitor.MField; -import com.alibaba.druid.support.monitor.MTable; +import com.alibaba.druid.support.monitor.annotation.AggregateType; +import com.alibaba.druid.support.monitor.annotation.MField; +import com.alibaba.druid.support.monitor.annotation.MTable; import com.alibaba.druid.util.Utils; @MTable(name = "druid_springmethod") public class SpringMethodStatValue { - @MField(groupBy = true) + @MField(groupBy = true, aggregate=AggregateType.None) private String className; - @MField(groupBy = true) + @MField(groupBy = true, aggregate=AggregateType.None) private String signature; - @MField + @MField(aggregate = AggregateType.Last) private int runningCount; - @MField + @MField(aggregate = AggregateType.Max) private int concurrentMax; - @MField + @MField(aggregate = AggregateType.Sum) private long executeCount; - @MField + @MField(aggregate = AggregateType.Sum) private long executeErrorCount; - @MField + @MField(aggregate = AggregateType.Sum) private long executeTimeNano; - @MField + @MField(aggregate = AggregateType.Sum) private long jdbcFetchRowCount; - @MField + @MField(aggregate = AggregateType.Sum) private long jdbcUpdateCount; - @MField + @MField(aggregate = AggregateType.Sum) private long jdbcExecuteCount; - @MField + @MField(aggregate = AggregateType.Sum) private long jdbcExecuteErrorCount; - @MField + @MField(aggregate = AggregateType.Sum) private long jdbcExecuteTimeNano; - @MField + @MField(aggregate = AggregateType.Sum) private long jdbcCommitCount; - @MField + @MField(aggregate = AggregateType.Sum) private long jdbcRollbackCount; - @MField + @MField(aggregate = AggregateType.Sum) private long jdbcPoolConnectionOpenCount; - @MField + @MField(aggregate = AggregateType.Sum) private long jdbcPoolConnectionCloseCount; - @MField + @MField(aggregate = AggregateType.Sum) private long jdbcResultSetOpenCount; - @MField + @MField(aggregate = AggregateType.Sum) private long jdbcResultSetCloseCount; - @MField + @MField(aggregate = AggregateType.Last) private String lastErrorClass; - @MField + @MField(aggregate = AggregateType.Last) private String lastErrorMessage; - @MField + @MField(aggregate = AggregateType.Last) private String lastErrorStackTrace; - @MField + @MField(aggregate = AggregateType.Last) private long lastErrorTimeMillis; - @MField(name = "h1") + @MField(name = "h1", aggregate=AggregateType.Sum) long histogram_0_1; - @MField(name = "h10") + @MField(name = "h10", aggregate=AggregateType.Sum) long histogram_1_10; - @MField(name = "h100") + @MField(name = "h100", aggregate=AggregateType.Sum) long histogram_10_100; - @MField(name = "h1000") + @MField(name = "h1000", aggregate=AggregateType.Sum) long histogram_100_1000; - @MField(name = "h10000") + @MField(name = "h10000", aggregate=AggregateType.Sum) int histogram_1000_10000; - @MField(name = "h100000") + @MField(name = "h100000", aggregate=AggregateType.Sum) int histogram_10000_100000; - @MField(name = "h1000000") + @MField(name = "h1000000", aggregate=AggregateType.Sum) int histogram_100000_1000000; - @MField(name = "hmore") + @MField(name = "hmore", aggregate=AggregateType.Sum) int histogram_1000000_more; public String getClassName() { diff --git a/src/main/java/com/alibaba/druid/wall/WallFunctionStatValue.java b/src/main/java/com/alibaba/druid/wall/WallFunctionStatValue.java index deab0ffde8..05e569e41c 100644 --- a/src/main/java/com/alibaba/druid/wall/WallFunctionStatValue.java +++ b/src/main/java/com/alibaba/druid/wall/WallFunctionStatValue.java @@ -18,16 +18,17 @@ package com.alibaba.druid.wall; import java.util.LinkedHashMap; import java.util.Map; -import com.alibaba.druid.support.monitor.MField; -import com.alibaba.druid.support.monitor.MTable; +import com.alibaba.druid.support.monitor.annotation.AggregateType; +import com.alibaba.druid.support.monitor.annotation.MField; +import com.alibaba.druid.support.monitor.annotation.MTable; @MTable(name = "druid_wall_function") public class WallFunctionStatValue { - @MField + @MField(groupBy=true, aggregate=AggregateType.None) private String name; - @MField + @MField(aggregate=AggregateType.Sum) private long invokeCount; public WallFunctionStatValue(){ diff --git a/src/main/java/com/alibaba/druid/wall/WallProviderStatValue.java b/src/main/java/com/alibaba/druid/wall/WallProviderStatValue.java index 7e163aa4ad..19972d293f 100644 --- a/src/main/java/com/alibaba/druid/wall/WallProviderStatValue.java +++ b/src/main/java/com/alibaba/druid/wall/WallProviderStatValue.java @@ -20,28 +20,29 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import com.alibaba.druid.support.monitor.MField; -import com.alibaba.druid.support.monitor.MTable; +import com.alibaba.druid.support.monitor.annotation.AggregateType; +import com.alibaba.druid.support.monitor.annotation.MField; +import com.alibaba.druid.support.monitor.annotation.MTable; @MTable(name = "druid_wall") public class WallProviderStatValue { - @MField + @MField(aggregate = AggregateType.None) private String name; - @MField + @MField(aggregate = AggregateType.Sum) private long checkCount; - @MField + @MField(aggregate = AggregateType.Sum) private long hardCheckCount; - @MField + @MField(aggregate = AggregateType.Sum) private long violationCount; - @MField + @MField(aggregate = AggregateType.Sum) private long whiteListHitCount; - @MField + @MField(aggregate = AggregateType.Sum) private long blackListHitCount; - @MField + @MField(aggregate = AggregateType.Sum) private long syntaxErrorCount; - @MField + @MField(aggregate = AggregateType.Sum) private long violationEffectRowCount; private final List tables = new ArrayList(); diff --git a/src/main/java/com/alibaba/druid/wall/WallSqlStatValue.java b/src/main/java/com/alibaba/druid/wall/WallSqlStatValue.java index 0522400ba2..575f9d5af4 100644 --- a/src/main/java/com/alibaba/druid/wall/WallSqlStatValue.java +++ b/src/main/java/com/alibaba/druid/wall/WallSqlStatValue.java @@ -18,31 +18,32 @@ package com.alibaba.druid.wall; import java.util.LinkedHashMap; import java.util.Map; -import com.alibaba.druid.support.monitor.MField; -import com.alibaba.druid.support.monitor.MTable; +import com.alibaba.druid.support.monitor.annotation.AggregateType; +import com.alibaba.druid.support.monitor.annotation.MField; +import com.alibaba.druid.support.monitor.annotation.MTable; @MTable(name = "druid_wall_sql") public class WallSqlStatValue { - @MField + @MField(aggregate = AggregateType.None) private String sql; - @MField + @MField(aggregate = AggregateType.None) private String sqlSample; - @MField + @MField(aggregate = AggregateType.Sum) private long executeCount; - @MField + @MField(aggregate = AggregateType.Sum) private long fetchRowCount; - @MField + @MField(aggregate = AggregateType.Sum) private long updateCount; - @MField + @MField(aggregate = AggregateType.Last) private boolean syntaxError; - @MField + @MField(aggregate = AggregateType.Last) private String violationMessage; public WallSqlStatValue(){ diff --git a/src/main/java/com/alibaba/druid/wall/WallTableStatValue.java b/src/main/java/com/alibaba/druid/wall/WallTableStatValue.java index fa734b538a..2370320f27 100644 --- a/src/main/java/com/alibaba/druid/wall/WallTableStatValue.java +++ b/src/main/java/com/alibaba/druid/wall/WallTableStatValue.java @@ -18,55 +18,56 @@ package com.alibaba.druid.wall; import java.util.LinkedHashMap; import java.util.Map; -import com.alibaba.druid.support.monitor.MField; -import com.alibaba.druid.support.monitor.MTable; +import com.alibaba.druid.support.monitor.annotation.AggregateType; +import com.alibaba.druid.support.monitor.annotation.MField; +import com.alibaba.druid.support.monitor.annotation.MTable; @MTable(name = "druid_wall_table") public class WallTableStatValue { - @MField + @MField(aggregate = AggregateType.None) private String name; - @MField + @MField(aggregate = AggregateType.Sum) private long selectCount; - @MField + @MField(aggregate = AggregateType.Sum) private long selectIntoCount; - @MField + @MField(aggregate = AggregateType.Sum) private long insertCount; - @MField + @MField(aggregate = AggregateType.Sum) private long updateCount; - @MField + @MField(aggregate = AggregateType.Sum) private long deleteCount; - @MField + @MField(aggregate = AggregateType.Sum) private long truncateCount; - @MField + @MField(aggregate = AggregateType.Sum) private long createCount; - @MField + @MField(aggregate = AggregateType.Sum) private long alterCount; - @MField + @MField(aggregate = AggregateType.Sum) private long dropCount; - @MField + @MField(aggregate = AggregateType.Sum) private long replaceCount; - @MField + @MField(aggregate = AggregateType.Sum) private long deleteDataCount; - @MField + @MField(aggregate = AggregateType.Sum) private long updateDataCount; - @MField + @MField(aggregate = AggregateType.Sum) private long insertDataCount; - @MField + @MField(aggregate = AggregateType.Sum) private long fetchRowCount; public WallTableStatValue(){ diff --git a/doc/monitor/mysql/dataSource.sql b/src/main/resources/support/monitor/mysql/datasource.sql similarity index 76% rename from doc/monitor/mysql/dataSource.sql rename to src/main/resources/support/monitor/mysql/datasource.sql index b06049de84..1891094ce7 100644 --- a/doc/monitor/mysql/dataSource.sql +++ b/src/main/resources/support/monitor/mysql/datasource.sql @@ -9,7 +9,6 @@ CREATE TABLE druid_datasource ( name varchar(256), dbType varchar(256), driverClassName varchar(256), - url varchar(256), activeCount int(10), activePeak int(10), activePeakTime bigint(20), @@ -32,8 +31,17 @@ CREATE TABLE druid_datasource ( pstmtCacheHitCount bigint(20), pstmtCacheMissCount bigint(20), startTransactionCount bigint(20), + txn_0_1 bigint(20), + txn_1_10 bigint(20), + txn_10_100 bigint(20), + txn_100_1000 bigint(20), + txn_1000_10000 bigint(20), + txn_10000_100000 bigint(20), + txn_more bigint(20), clobOpenCount bigint(20), blobOpenCount bigint(20), - PRIMARY KEY (id), - KEY(collectTime, domain, app) -) \ No newline at end of file + sqlSkipCount bigint(20), + PRIMARY KEY (id) +); + +CREATE INDEX druid_datasource_index ON druid_datasource (collectTime, domain, app); \ No newline at end of file diff --git a/doc/monitor/mysql/springmethod.sql b/src/main/resources/support/monitor/mysql/springmethod.sql similarity index 90% rename from doc/monitor/mysql/springmethod.sql rename to src/main/resources/support/monitor/mysql/springmethod.sql index eeda7098a1..efe5ebc748 100644 --- a/doc/monitor/mysql/springmethod.sql +++ b/src/main/resources/support/monitor/mysql/springmethod.sql @@ -36,6 +36,7 @@ CREATE TABLE druid_springmethod ( h100000 int(10), h1000000 int(10), hmore int(10), - PRIMARY KEY (id), - KEY(collectTime, domain, app) -) \ No newline at end of file + PRIMARY KEY (id) +); + +CREATE INDEX druid_springmethod_index ON druid_springmethod (collectTime, domain, app); \ No newline at end of file diff --git a/doc/monitor/mysql/sql.sql b/src/main/resources/support/monitor/mysql/sql.sql similarity index 94% rename from doc/monitor/mysql/sql.sql rename to src/main/resources/support/monitor/mysql/sql.sql index 4fb6460790..2448d42f25 100644 --- a/doc/monitor/mysql/sql.sql +++ b/src/main/resources/support/monitor/mysql/sql.sql @@ -67,7 +67,7 @@ CREATE TABLE druid_sql ( u1000 int(10), u10000 int(10), umore int(10), - PRIMARY KEY (id), - KEY (collectTime, domain, app), - KEY(collectTime, domain, app) -) \ No newline at end of file + PRIMARY KEY (id) +); + +CREATE INDEX druid_sql_index ON druid_sql (collectTime, domain, app); \ No newline at end of file diff --git a/doc/monitor/mysql/webapp.sql b/src/main/resources/support/monitor/mysql/webapp.sql similarity index 95% rename from doc/monitor/mysql/webapp.sql rename to src/main/resources/support/monitor/mysql/webapp.sql index dd4ba7ca0c..513909bee9 100644 --- a/doc/monitor/mysql/webapp.sql +++ b/src/main/resources/support/monitor/mysql/webapp.sql @@ -69,6 +69,7 @@ CREATE TABLE druid_webapp ( botSosoCount bigint(20), botSogouCount bigint(20), botYahooCount bigint(20), - PRIMARY KEY (id), - KEY(collectTime, domain, app) -) \ No newline at end of file + PRIMARY KEY (id) +); + +CREATE INDEX druid_webapp_index ON druid_webapp (collectTime, domain, app); \ No newline at end of file diff --git a/doc/monitor/mysql/webrui.sql b/src/main/resources/support/monitor/mysql/weburi.sql similarity index 89% rename from doc/monitor/mysql/webrui.sql rename to src/main/resources/support/monitor/mysql/weburi.sql index 4e8a3b9d41..8715a9ba53 100644 --- a/doc/monitor/mysql/webrui.sql +++ b/src/main/resources/support/monitor/mysql/weburi.sql @@ -26,7 +26,7 @@ CREATE TABLE druid_weburi ( jdbcResultSetOpenCount bigint(20), jdbcResultSetCloseCount bigint(20), errorCount bigint(20), - lastAccessTimeMillis bigint(20), + lastAccessTime datetime, h1 bigint(20), h10 bigint(20), h100 bigint(20), @@ -35,6 +35,7 @@ CREATE TABLE druid_weburi ( h100000 int(10), h1000000 int(10), hmore int(10), - PRIMARY KEY (id), - KEY(collectTime, domain, app) -) \ No newline at end of file + PRIMARY KEY (id) +); + +CREATE INDEX druid_weburi_index ON druid_weburi (collectTime, domain, app); \ No newline at end of file diff --git a/src/test/java/com/alibaba/druid/bvt/support/monitor/MonitorDaoJdbcImplTest.java b/src/test/java/com/alibaba/druid/bvt/support/monitor/MonitorDaoJdbcImplTest.java index fb84be7709..8ebd0436b9 100644 --- a/src/test/java/com/alibaba/druid/bvt/support/monitor/MonitorDaoJdbcImplTest.java +++ b/src/test/java/com/alibaba/druid/bvt/support/monitor/MonitorDaoJdbcImplTest.java @@ -9,17 +9,11 @@ import junit.framework.TestCase; import org.junit.Assert; import com.alibaba.druid.pool.DruidDataSource; -import com.alibaba.druid.pool.DruidDataSourceStatValue; -import com.alibaba.druid.sql.SQLUtils; import com.alibaba.druid.stat.JdbcSqlStatValue; -import com.alibaba.druid.support.http.stat.WebAppStatValue; -import com.alibaba.druid.support.http.stat.WebURIStatValue; import com.alibaba.druid.support.monitor.MonitorClient; import com.alibaba.druid.support.monitor.dao.MonitorDaoJdbcImpl; import com.alibaba.druid.support.monitor.dao.MonitorDaoJdbcImpl.BeanInfo; import com.alibaba.druid.support.monitor.dao.MonitorDaoJdbcImpl.FieldInfo; -import com.alibaba.druid.support.spring.stat.SpringMethodStatValue; -import com.alibaba.druid.util.JdbcConstants; import com.alibaba.druid.util.JdbcUtils; public class MonitorDaoJdbcImplTest extends TestCase { @@ -32,7 +26,7 @@ public class MonitorDaoJdbcImplTest extends TestCase { dataSource.setUrl("jdbc:h2:mem:test"); dataSource.setInitialSize(2); dataSource.setMinIdle(2); - dataSource.setFilters("stat,log4j"); + dataSource.setFilters("wall,stat,log4j"); dataSource.init(); } @@ -44,40 +38,12 @@ public class MonitorDaoJdbcImplTest extends TestCase { public void testBuildSql() throws Exception { MonitorDaoJdbcImpl dao = new MonitorDaoJdbcImpl(); dao.setDataSource(dataSource); + + dao.createTables("mysql"); MonitorClient client = new MonitorClient(); client.setDao(dao); - { - String sql = buildCreateSql(dao, new BeanInfo(JdbcSqlStatValue.class)); - System.out.println(SQLUtils.format(sql, JdbcConstants.MYSQL)); - JdbcUtils.execute(dataSource, sql, Collections.emptyList()); - } - - { - String sql = buildCreateSql(dao, new BeanInfo(DruidDataSourceStatValue.class)); - System.out.println(SQLUtils.format(sql, JdbcConstants.MYSQL)); - JdbcUtils.execute(dataSource, sql, Collections.emptyList()); - } - - { - String sql = buildCreateSql(dao, new BeanInfo(WebURIStatValue.class)); - System.out.println(SQLUtils.format(sql, JdbcConstants.MYSQL)); - JdbcUtils.execute(dataSource, sql, Collections.emptyList()); - } - - - { - String sql = buildCreateSql(dao, new BeanInfo(WebAppStatValue.class)); - System.out.println(SQLUtils.format(sql, JdbcConstants.MYSQL)); - JdbcUtils.execute(dataSource, sql, Collections.emptyList()); - } - - { - String sql = buildCreateSql(dao, new BeanInfo(SpringMethodStatValue.class)); - System.out.println(SQLUtils.format(sql, JdbcConstants.MYSQL)); - JdbcUtils.execute(dataSource, sql, Collections.emptyList()); - } client.collectSql(); { @@ -85,7 +51,7 @@ public class MonitorDaoJdbcImplTest extends TestCase { for (JdbcSqlStatValue sqlStatValue : sqlList) { System.out.println(sqlStatValue.getData()); } - Assert.assertEquals(5, sqlList.size()); + Assert.assertEquals(11, sqlList.size()); } client.collectSql(); @@ -95,7 +61,7 @@ public class MonitorDaoJdbcImplTest extends TestCase { for (JdbcSqlStatValue sqlStatValue : sqlList) { System.out.println(sqlStatValue.getData()); } - Assert.assertEquals(7, sqlList.size()); + Assert.assertEquals(14, sqlList.size()); } } @@ -128,9 +94,13 @@ public class MonitorDaoJdbcImplTest extends TestCase { } } buf.append(", PRIMARY KEY(id)"); -// buf.append(", KEY(collectTime, domain, app)"); - buf.append(")"); - + buf.append(");"); + + buf.append("\n\nCREATE INDEX "); + buf.append(dao.getTableName(beanInfo)); + buf.append("_index ON "); + buf.append(dao.getTableName(beanInfo)); + buf.append(" (collectTime, domain, app);"); return buf.toString(); } }