mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add stat service
This commit is contained in:
@@ -27,6 +27,14 @@ public class DruidStatService {
|
||||
|
||||
private DruidStatStore store;
|
||||
|
||||
public int getCollectPeriodSeconds() {
|
||||
return collectPeriodSeconds;
|
||||
}
|
||||
|
||||
public void setCollectPeriodSeconds(int collectPeriodSeconds) {
|
||||
this.collectPeriodSeconds = collectPeriodSeconds;
|
||||
}
|
||||
|
||||
public synchronized void start() {
|
||||
if (store != null) {
|
||||
store = new DruidStatMemoryStore();
|
||||
@@ -47,12 +55,12 @@ public class DruidStatService {
|
||||
}
|
||||
|
||||
public void collect() {
|
||||
final long timeMillis = System.currentTimeMillis();
|
||||
|
||||
List<DataSourceInfo> dataSourceStatList = new ArrayList<DataSourceInfo>();
|
||||
for (DruidDataSource dataSource : DruidDataSourceStatManager.getDruidDataSourceInstances()) {
|
||||
DataSourceInfo dataSourceStat = new DataSourceInfo();
|
||||
|
||||
dataSourceStat.setCollectTimeMillis(System.currentTimeMillis());
|
||||
|
||||
dataSourceStat.setId(dataSource.getID());
|
||||
dataSourceStat.setUrl(dataSource.getUrl());
|
||||
dataSourceStat.setDbType(dataSource.getDbType());
|
||||
@@ -67,6 +75,8 @@ public class DruidStatService {
|
||||
dataSourceStat.setDestoryCount((int) dataSource.getDestroyCount());
|
||||
dataSourceStat.setExecuteCount((int) dataSource.getExecuteCount());
|
||||
dataSourceStat.setPoolingCount(dataSource.getPoolingCount());
|
||||
dataSourceStat.setConnectionHoldHistogram(dataSource.getDataSourceStat().getConnectionHistogramValues());
|
||||
dataSourceStat.setTransactionHistogram(dataSource.getTransactionHistogramValues());
|
||||
|
||||
Collection<JdbcSqlStat> sqlStats = dataSource.getDataSourceStat().getSqlStatMap().values();
|
||||
|
||||
@@ -76,9 +86,9 @@ public class DruidStatService {
|
||||
if (sqlStat.getExecuteCount() == 0 && sqlStat.getRunningCount() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
SqlInfo sqlStatInfo = DruidStatServiceUtils.createSqlInfo(sqlStat);
|
||||
|
||||
|
||||
SqlInfo oldsqlStatInfo = sqlStatInfoMap.get(sqlStatInfo.getSql());
|
||||
if (oldsqlStatInfo != null) {
|
||||
oldsqlStatInfo.merge(sqlStatInfo);
|
||||
@@ -92,7 +102,7 @@ public class DruidStatService {
|
||||
dataSourceStatList.add(dataSourceStat);
|
||||
}
|
||||
|
||||
store.saveDataSource(timeMillis, dataSourceStatList);
|
||||
store.saveDataSource(dataSourceStatList);
|
||||
}
|
||||
|
||||
private final class CollectTask implements Runnable {
|
||||
|
||||
@@ -6,5 +6,5 @@ import com.alibaba.druid.stat.service.dto.DataSourceInfo;
|
||||
|
||||
public interface DruidStatStore {
|
||||
|
||||
void saveDataSource(long timeMillis, List<DataSourceInfo> dataSourceList);
|
||||
void saveDataSource(List<DataSourceInfo> dataSourceList);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,12 @@ public class DataSourceInfo {
|
||||
private int closeCount;
|
||||
private int destoryCount;
|
||||
|
||||
private long collectTimeMillis;
|
||||
|
||||
private int[] transactionHistogram;
|
||||
|
||||
private int[] connectionHoldHistogram;
|
||||
|
||||
private List<SqlInfo> sqlList;
|
||||
|
||||
public String getDbType() {
|
||||
@@ -58,6 +64,36 @@ public class DataSourceInfo {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int[] getTransactionHistogram() {
|
||||
return transactionHistogram;
|
||||
}
|
||||
|
||||
public void setTransactionHistogram(long[] values) {
|
||||
this.transactionHistogram = new int[values.length];
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
transactionHistogram[i] = (int) values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public void setTransactionHistogram(int[] values) {
|
||||
this.transactionHistogram = values;
|
||||
}
|
||||
|
||||
public int[] getConnectionHoldHistogram() {
|
||||
return connectionHoldHistogram;
|
||||
}
|
||||
|
||||
public void setConnectionHoldHistogram(int[] values) {
|
||||
this.connectionHoldHistogram = values;
|
||||
}
|
||||
|
||||
public void setConnectionHoldHistogram(long[] values) {
|
||||
this.connectionHoldHistogram = new int[values.length];
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
connectionHoldHistogram[i] = (int) values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int getActiveCount() {
|
||||
return activeCount;
|
||||
}
|
||||
@@ -138,4 +174,12 @@ public class DataSourceInfo {
|
||||
this.destoryCount = destoryCount;
|
||||
}
|
||||
|
||||
public long getCollectTimeMillis() {
|
||||
return collectTimeMillis;
|
||||
}
|
||||
|
||||
public void setCollectTimeMillis(long collectTimeMillis) {
|
||||
this.collectTimeMillis = collectTimeMillis;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.alibaba.druid.stat.service.impl;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
@@ -12,25 +14,76 @@ public class DruidStatMemoryStore implements DruidStatStore {
|
||||
private ConcurrentHashMap<Long, DataSourceData> dataSourceDataMap = new ConcurrentHashMap<Long, DataSourceData>();
|
||||
|
||||
@Override
|
||||
public void saveDataSource(long timeMillis, List<DataSourceInfo> dataSourceList) {
|
||||
public void saveDataSource(List<DataSourceInfo> dataSourceList) {
|
||||
for (DataSourceInfo dataSourceInfo : dataSourceList) {
|
||||
DataSourceData data = dataSourceDataMap.get(dataSourceInfo.getId());
|
||||
|
||||
if (data == null) {
|
||||
dataSourceDataMap.putIfAbsent(dataSourceInfo.getId(), new DataSourceData());
|
||||
dataSourceDataMap.putIfAbsent(dataSourceInfo.getId(), new DataSourceData(dataSourceInfo.getId(),
|
||||
dataSourceInfo.getUrl(),
|
||||
dataSourceInfo.getDbType()));
|
||||
data = dataSourceDataMap.get(dataSourceInfo.getId());
|
||||
}
|
||||
|
||||
data.add(timeMillis, dataSourceInfo);
|
||||
|
||||
data.add(dataSourceInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<DataSourceInfo> scan(long dataSourceId, Long startTimeMillis, Long endTimeMillis) {
|
||||
DataSourceData data = dataSourceDataMap.get(dataSourceId);
|
||||
|
||||
if (data == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return data.scan(startTimeMillis, endTimeMillis);
|
||||
}
|
||||
|
||||
public static class DataSourceData {
|
||||
|
||||
private final Long id;
|
||||
private final String url;
|
||||
private final String dbType;
|
||||
|
||||
private ConcurrentSkipListMap<Long, DataSourceInfo> data = new ConcurrentSkipListMap<Long, DataSourceInfo>();
|
||||
|
||||
public void add(long timeMillis, DataSourceInfo dataSourceInfo) {
|
||||
data.put(timeMillis, dataSourceInfo);
|
||||
public DataSourceData(Long id, String url, String dbType){
|
||||
super();
|
||||
this.id = id;
|
||||
this.url = url;
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void add(DataSourceInfo dataSourceInfo) {
|
||||
data.put(dataSourceInfo.getCollectTimeMillis(), dataSourceInfo);
|
||||
}
|
||||
|
||||
public Collection<DataSourceInfo> scan(Long startTimeMillis, Long endTimeMillis) {
|
||||
if (data.size() == 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
if (startTimeMillis == null) {
|
||||
startTimeMillis = data.keySet().iterator().next();
|
||||
}
|
||||
|
||||
if (endTimeMillis == null) {
|
||||
endTimeMillis = data.descendingKeySet().iterator().next();
|
||||
}
|
||||
|
||||
return data.subMap(startTimeMillis, endTimeMillis).values();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user