mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
sql merge stat filter
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
package com.alibaba.druid.filter.stat;
|
||||
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
|
||||
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
|
||||
import com.alibaba.druid.stat.JdbcSqlStat;
|
||||
|
||||
public class MergeSqlStatFilter extends StatFilter {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(MergeSqlStatFilter.class);
|
||||
|
||||
private String dbType;
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(DataSourceProxy dataSource) {
|
||||
super.init(dataSource);
|
||||
|
||||
if (this.dbType == null || this.dbType.trim().length() == 0) {
|
||||
this.dbType = dataSource.getDbType();
|
||||
}
|
||||
}
|
||||
|
||||
public JdbcSqlStat getSqlStat(String sql) {
|
||||
try {
|
||||
sql = ParameterizedOutputVisitorUtils.parameterize(sql, sql);
|
||||
} catch (Exception e) {
|
||||
LOG.error("merge sql error", e);
|
||||
}
|
||||
return dataSourceStat.getSqlStat(sql);
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,7 @@ import com.alibaba.druid.proxy.jdbc.JdbcParameter;
|
||||
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.ResultSetProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.StatementProxy;
|
||||
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
|
||||
import com.alibaba.druid.stat.JdbcConnectionStat;
|
||||
import com.alibaba.druid.stat.JdbcDataSourceStat;
|
||||
import com.alibaba.druid.stat.JdbcResultSetStat;
|
||||
@@ -77,9 +78,21 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
// 3 seconds is slow sql
|
||||
protected long slowSqlMillis = 3 * 1000;
|
||||
|
||||
private String dbType;
|
||||
|
||||
private boolean mergeSql = false;
|
||||
|
||||
public StatFilter(){
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
public boolean isConnectionStackTraceEnable() {
|
||||
return connectionStackTraceEnable;
|
||||
}
|
||||
@@ -102,11 +115,67 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
return resetCount.get();
|
||||
}
|
||||
|
||||
public boolean isMergeSql() {
|
||||
return mergeSql;
|
||||
}
|
||||
|
||||
public void setMergeSql(boolean mergeSql) {
|
||||
this.mergeSql = mergeSql;
|
||||
}
|
||||
|
||||
public String mergeSql(String sql) {
|
||||
if (!mergeSql) {
|
||||
return sql;
|
||||
}
|
||||
|
||||
try {
|
||||
sql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
|
||||
} catch (Exception e) {
|
||||
LOG.error("merge sql error", e);
|
||||
}
|
||||
|
||||
return sql;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void init(DataSourceProxy dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
|
||||
this.dataSourceStat = dataSource.getDataSourceStat();
|
||||
|
||||
if (this.dbType == null || this.dbType.trim().length() == 0) {
|
||||
this.dbType = dataSource.getDbType();
|
||||
}
|
||||
|
||||
initFromProperties(dataSource.getConnectProperties());
|
||||
initFromProperties(System.getProperties());
|
||||
}
|
||||
|
||||
private void initFromProperties(Properties properties) {
|
||||
if (properties == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
String property = properties.getProperty("druid.stat.mergeSql");
|
||||
if ("true".equals(property)) {
|
||||
this.mergeSql = true;
|
||||
} else if ("false".equals(property)) {
|
||||
this.mergeSql = false;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
String property = properties.getProperty("druid.stat.slowSqlMillis");
|
||||
if (property != null && property.trim().length() > 0) {
|
||||
property = property.trim();
|
||||
try {
|
||||
this.slowSqlMillis = Long.parseLong(property);
|
||||
} catch (Exception e) {
|
||||
LOG.error("property 'druid.stat.slowSqlMillis' format error");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -86,6 +86,8 @@ public class MultiDataSource extends DataSourceAdapter implements MultiDataSourc
|
||||
|
||||
private JdbcDataSourceStat dataSourceStat;
|
||||
|
||||
protected Properties connectionProperties = new Properties();
|
||||
|
||||
public long getMaxWaitMillis() {
|
||||
return maxWaitMillis;
|
||||
}
|
||||
@@ -171,7 +173,7 @@ public class MultiDataSource extends DataSourceAdapter implements MultiDataSourc
|
||||
if (inited) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
dataSourceStat = new JdbcDataSourceStat(name, null);
|
||||
|
||||
this.balancer.init(this);
|
||||
@@ -501,4 +503,10 @@ public class MultiDataSource extends DataSourceAdapter implements MultiDataSourc
|
||||
public JdbcDataSourceStat getDataSourceStat() {
|
||||
return dataSourceStat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Properties getConnectProperties() {
|
||||
return connectionProperties;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.alibaba.druid.proxy.jdbc;
|
||||
|
||||
import java.sql.Driver;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.alibaba.druid.filter.Filter;
|
||||
import com.alibaba.druid.stat.JdbcDataSourceStat;
|
||||
@@ -25,6 +26,7 @@ import com.alibaba.druid.stat.JdbcDataSourceStat;
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public interface DataSourceProxy {
|
||||
|
||||
JdbcDataSourceStat getDataSourceStat();
|
||||
|
||||
String getName();
|
||||
@@ -46,4 +48,6 @@ public interface DataSourceProxy {
|
||||
long createResultSetId();
|
||||
|
||||
long createTransactionId();
|
||||
|
||||
Properties getConnectProperties();
|
||||
}
|
||||
|
||||
@@ -183,6 +183,10 @@ public class DataSourceProxyImpl implements DataSourceProxy, DataSourceProxyImpl
|
||||
return properties.toString();
|
||||
}
|
||||
|
||||
public Properties getConnectProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
public CompositeDataSupport getCompositeData() throws JMException {
|
||||
StatFilter statFilter = null;
|
||||
JdbcDataSourceStat stat = null;
|
||||
|
||||
Reference in New Issue
Block a user