项目中如何实现 SQL 耗时监控?
2011 年 9 月 13 日
import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Properties; import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.ParameterMapping; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.type.TypeHandlerRegistry; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }), @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }) }) public class MybatisSqlLoggingInterceptor implements Interceptor {
private static Logger logger = LoggerFactory.getLogger("db_file");
private static final Pattern WHITE_SPACE_BLOCK_PATTERN = Pattern .compile("([\\s]{2,}|[\\t\\r\\n])");
public Object intercept(Invocation invocation) throws Throwable { MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; Object parameter = null; if (invocation.getArgs().length > 1) { parameter = invocation.getArgs()[1]; } String sqlId = mappedStatement.getId(); BoundSql boundSql = mappedStatement.getBoundSql(parameter); Configuration configuration = mappedStatement.getConfiguration(); Object returnValue = null; long start = System.currentTimeMillis(); // 执行目标方法 returnValue = invocation.proceed(); // 统计 SQL 耗时 long end = System.currentTimeMillis(); long time = end - start; // 打印日志 logSqlInfo(configuration, boundSql, sqlId, time);
return returnValue; }
public static void logSqlInfo(Configuration configuration, BoundSql boundSql, String sqlId, long time) {
String sqlAndParams = showSqlAndParams(configuration, boundSql);
StringBuilder str = new StringBuilder(100); str.append(sqlId); str.append(":"); str.append(sqlAndParams); str.append(":"); str.append(time); str.append("ms");
String line = str.toString();
logger.info(line); }
private static String getParameterValue(Object obj) { String value = null; if (obj instanceof String) { value = "'" + obj.toString() + "'"; } else if (obj instanceof Date) { value = "'" + DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(new Date()) + "'"; } else { if (obj != null) { value = obj.toString(); } else { value = ""; }
} return value; }
public static String showSqlAndParams(Configuration configuration, BoundSql boundSql) { Object parameterObject = boundSql.getParameterObject(); List parameterMappings = boundSql.getParameterMappings();
String sql = WHITE_SPACE_BLOCK_PATTERN.matcher(boundSql.getSql()).replaceAll(" ");
List<String> params = new ArrayList();
if (parameterMappings.size() > 0 && parameterObject != null) { TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { params.add(getParameterValue(parameterObject)); } else { MetaObject metaObject = configuration.newMetaObject(parameterObject); for (ParameterMapping parameterMapping : parameterMappings) { String propertyName = parameterMapping.getProperty(); if (metaObject.hasGetter(propertyName)) { Object obj = metaObject.getValue(propertyName); params.add(getParameterValue(obj)); } else if (boundSql.hasAdditionalParameter(propertyName)) { Object obj = boundSql.getAdditionalParameter(propertyName); params.add(getParameterValue(obj)); } } } }
return sql + ":[" + StringUtils.join(params, ',') + "]"; }
public Object plugin(Object target) { return Plugin.wrap(target, this); }
@Override public void setProperties(Properties arg0) { // just ignore }}