support monitor

This commit is contained in:
wenshao
2013-08-14 17:24:40 +08:00
parent 5aa3d51128
commit a72da37909
41 changed files with 701 additions and 284 deletions
@@ -37,7 +37,7 @@ import com.alibaba.druid.support.http.util.IPAddress;
import com.alibaba.druid.support.http.util.IPRange;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.IOUtils;
import com.alibaba.druid.util.Utils;
import com.alibaba.druid.util.StringUtils;
/**
@@ -89,7 +89,7 @@ public class StatViewServlet extends HttpServlet {
initAuthEnv();
try {
templatePage = IOUtils.readFromResource(TEMPLATE_PAGE_RESOURCE_PATH);
templatePage = Utils.readFromResource(TEMPLATE_PAGE_RESOURCE_PATH);
} catch (IOException e) {
throw new ServletException("error read templatePage:" + TEMPLATE_PAGE_RESOURCE_PATH, e);
@@ -392,7 +392,7 @@ public class StatViewServlet extends HttpServlet {
private void returnResourceFile(String fileName, String uri, HttpServletResponse response) throws ServletException,
IOException {
if (fileName.endsWith(".jpg")) {
byte[] bytes = IOUtils.readByteArrayFromResource(RESOURCE_PATH + fileName);
byte[] bytes = Utils.readByteArrayFromResource(RESOURCE_PATH + fileName);
if (bytes != null) {
response.getOutputStream().write(bytes);
}
@@ -400,7 +400,7 @@ public class StatViewServlet extends HttpServlet {
return;
}
String text = IOUtils.readFromResource(RESOURCE_PATH + fileName);
String text = Utils.readFromResource(RESOURCE_PATH + fileName);
if (text == null) {
response.sendRedirect(uri + "/index.html");
return;
@@ -60,8 +60,8 @@ public class WebURIStatValue {
protected long histogram_0_1;
protected long histogram_1_10;
protected long histogram_10_100;
protected long histogram_100_1000;
protected long histogram_10_100;
protected long histogram_100_1000;
protected int histogram_1000_10000;
protected int histogram_10000_100000;
protected int histogram_100000_1000000;
@@ -23,7 +23,7 @@ import java.util.Map;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.TabularData;
import com.alibaba.druid.util.IOUtils;
import com.alibaba.druid.util.Utils;
public class JSONWriter {
@@ -152,7 +152,7 @@ public class JSONWriter {
write(",\"Message\":");
writeString(error.getMessage());
write(",\"StackTrace\":");
writeString(IOUtils.getStackTrace(error));
writeString(Utils.getStackTrace(error));
write('}');
}
@@ -0,0 +1,27 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.support.monitor;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
public @interface MField {
String name() default "";
}
@@ -34,9 +34,9 @@ import com.alibaba.druid.support.monitor.dao.MonitorDao;
import com.alibaba.druid.support.spring.stat.SpringMethodStatValue;
import com.alibaba.druid.support.spring.stat.SpringStat;
import com.alibaba.druid.support.spring.stat.SpringStatManager;
import com.alibaba.druid.util.IOUtils;
import com.alibaba.druid.util.Utils;
public class MonitorService {
public class MonitorClient {
private final static long DEFAULT_TIME_BETWEEN_COLLECT = 60 * 5;
@@ -111,8 +111,8 @@ public class MonitorService {
private MonitorContext createContext() {
MonitorContext ctx = new MonitorContext();
ctx.setCollectTime(new Date());
ctx.setPID(IOUtils.getPID());
ctx.setCollectTime(IOUtils.getStartTime());
ctx.setPID(Utils.getPID());
ctx.setCollectTime(Utils.getStartTime());
return ctx;
}
@@ -15,19 +15,152 @@
*/
package com.alibaba.druid.support.monitor.dao;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.sql.DataSource;
import com.alibaba.druid.DruidRuntimeException;
import com.alibaba.druid.pool.DruidDataSourceStatValue;
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.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.support.monitor.MField;
import com.alibaba.druid.support.monitor.MonitorContext;
import com.alibaba.druid.support.spring.stat.SpringMethodStatValue;
import com.alibaba.druid.util.JdbcUtils;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.druid.util.Utils;
public class MonitorDaoJdbcImpl implements MonitorDao {
private final static Log LOG = LogFactory.getLog(MonitorDaoJdbcImpl.class);
private DataSource dataSource;
private BeanInfo sqlStatBeanInfo = new BeanInfo(JdbcSqlStatValue.class);
public MonitorDaoJdbcImpl(){
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void saveSql(MonitorContext ctx, List<DruidDataSourceStatValue> dataSourceList) {
for (DruidDataSourceStatValue dataSourceStatValue : dataSourceList) {
List<JdbcSqlStatValue> sqlList = dataSourceStatValue.getSqlList();
String sql = buildInsertSql(sqlStatBeanInfo);
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = dataSource.getConnection();
stmt = conn.prepareStatement(sql);
for (JdbcSqlStatValue statValue : sqlList) {
setParameterForSqlStat(sqlStatBeanInfo, stmt, statValue);
stmt.addBatch();
}
stmt.executeBatch();
stmt.close();
} catch (SQLException ex) {
LOG.error("save sql error", ex);
} finally {
JdbcUtils.close(stmt);
JdbcUtils.close(conn);
}
}
}
protected void setParameterForSqlStat(BeanInfo beanInfo, PreparedStatement stmt, JdbcSqlStatValue sqlStat)
throws SQLException {
int paramIndex = 1;
try {
List<FieldInfo> fields = beanInfo.getFields();
for (int i = 0; i < fields.size(); ++i) {
FieldInfo field = fields.get(i);
Class<?> fieldType = field.getFieldType();
Object value = field.getField().get(sqlStat);
if (fieldType.equals(int.class) || fieldType.equals(Integer.class)) {
setParam(stmt, paramIndex, (Integer) value);
} else if (fieldType.equals(long.class) || fieldType.equals(Long.class)) {
setParam(stmt, paramIndex, (Long) value);
} else if (fieldType.equals(String.class)) {
setParam(stmt, paramIndex, (String) value);
} else if (fieldType.equals(Date.class)) {
setParam(stmt, paramIndex, (Date) value);
} else {
throw new UnsupportedOperationException();
}
paramIndex++;
}
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new DruidRuntimeException("setParam error", ex);
}
}
public String buildInsertSql(BeanInfo beanInfo) {
String sql = beanInfo.insertSql;
if (sql != null) {
return sql;
}
StringBuilder buf = new StringBuilder();
buf.append("INSERT INTO ") //
.append(getTableName(beanInfo));
buf.append(" (");
List<FieldInfo> fields = beanInfo.getFields();
for (int i = 0; i < fields.size(); ++i) {
FieldInfo field = fields.get(i);
if (i != 0) {
buf.append(", ");
}
buf.append(field.getColumnName());
}
buf.append(")\nVALUES (");
for (int i = 0; i < fields.size(); ++i) {
if (i != 0) {
buf.append(", ?");
} else {
buf.append("?");
}
}
buf.append(")");
sql = buf.toString();
beanInfo.setInsertSql(sql);
return sql;
}
protected String getTableName(BeanInfo beanInfo) {
return "druid_sql";
}
protected long getSqlHash(String sql) {
return Utils.murmurhash2_64(sql);
}
@Override
@@ -42,7 +175,104 @@ public class MonitorDaoJdbcImpl implements MonitorDao {
@Override
public void saveWebApp(MonitorContext ctx, List<WebAppStatValue> uriList) {
}
static void setParam(PreparedStatement stmt, int paramIndex, String value) throws SQLException {
if (value == null) {
stmt.setNull(paramIndex, Types.VARCHAR);
} else {
stmt.setString(paramIndex, value);
}
}
static void setParam(PreparedStatement stmt, int paramIndex, Long value) throws SQLException {
if (value == null || value == 0) {
stmt.setNull(paramIndex, Types.BIGINT);
} else {
stmt.setLong(paramIndex, value);
}
}
static void setParam(PreparedStatement stmt, int paramIndex, Integer value) throws SQLException {
if (value == null || value == 0) {
stmt.setNull(paramIndex, Types.INTEGER);
} else {
stmt.setInt(paramIndex, value);
}
}
static void setParam(PreparedStatement stmt, int paramIndex, Date value) throws SQLException {
if (value == null) {
stmt.setNull(paramIndex, Types.TIMESTAMP);
} else {
stmt.setTimestamp(paramIndex, new Timestamp(value.getTime()));
}
}
public static class BeanInfo {
private final Class<?> clazz;
private final List<FieldInfo> fields = new ArrayList<FieldInfo>();
private String insertSql;
public BeanInfo(Class<?> clazz){
this.clazz = clazz;
for (Field field : JdbcSqlStatValue.class.getDeclaredFields()) {
MField annotation = field.getAnnotation(MField.class);
if (annotation == null) {
continue;
}
String columnName = annotation.name();
if (StringUtils.isEmpty(columnName)) {
columnName = field.getName();
}
fields.add(new FieldInfo(field, columnName));
}
}
public Class<?> getClazz() {
return clazz;
}
public String getInsertSql() {
return insertSql;
}
public void setInsertSql(String insertSql) {
this.insertSql = insertSql;
}
public List<FieldInfo> getFields() {
return fields;
}
}
public static class FieldInfo {
private final Field field;
private final String columnName;
public FieldInfo(Field field, String columnName){
this.field = field;
this.columnName = columnName;
field.setAccessible(true);
}
public Field getField() {
return field;
}
public String getColumnName() {
return columnName;
}
public Class<?> getFieldType() {
return field.getType();
}
}
}