[강운덕] [LUCYSUS-1744] 추적 대상 데이터를 호출되는 api 기반으로 변경함.

git-svn-id: http://svn.bds.nhncorp.com/pe/hippo-tomcat-profiler/trunk@881 84d0f5b1-2673-498c-a247-62c4ff18d310
This commit is contained in:
Woonduk Kang
2012-11-20 09:34:18 +00:00
parent 188ef0a047
commit cbdb866f41
8 changed files with 105 additions and 63 deletions
@@ -226,10 +226,24 @@ public final class Trace {
}
}
public void recordApi(MethodDescriptor methodDescriptor) {
String method = methodDescriptor.getClassName() + "." + methodDescriptor.getMethodName() + methodDescriptor.getSimpleParameterDescriptor() + ":" + methodDescriptor.getLineNumber();
recordAttribute("API", method);
}
public void recordApi(MethodDescriptor methodDescriptor, Object[] args) {
// API 저장 방법의 개선 필요.
String method = methodDescriptor.getClassName() + "." + methodDescriptor.getMethodName() + methodDescriptor.getSimpleParameterDescriptor() + ":" + methodDescriptor.getLineNumber();
recordAttribute("API", method);
recocordArgs(args);
}
private void recocordArgs(Object[] args) {
if (args != null) {
for (int i = 0; i < args.length; i++) {
recordAttribute("args[" + i + "]", args[i]);
}
}
}
public void recordAttribute(final String key, final String value) {
@@ -73,7 +73,7 @@ public class ExecuteMethodInterceptor implements StaticAroundInterceptor, ByteCo
if (trace == null) {
return;
}
trace.recordApi(descriptor, args);
trace.recordApi(descriptor);
trace.recordException(result);
trace.markAfterTime();
@@ -73,15 +73,9 @@ public class DriverConnectInterceptor implements StaticAroundInterceptor, ByteCo
}
trace.recordRpcName(databaseInfo.getType() + "/" + databaseInfo.getDatabaseId(), databaseInfo.getUrl());
trace.recordTerminalEndPoint(databaseInfo.getUrl());
trace.recordAttribute("JDBCConnection", "create");
if (success) {
trace.recordAttribute("Success", "true");
} else {
Throwable th = (Throwable) result;
trace.recordAttribute("Success", "false");
trace.recordAttribute("Exception", th.getMessage());
}
trace.recordApi(descriptor, args);
trace.recordApi(descriptor, new Object[]{args[0]});
trace.recordException(result);
trace.markAfterTime();
trace.traceBlockEnd();
@@ -74,7 +74,7 @@ public class PreparedStatementCreateInterceptor implements StaticAroundIntercept
this.setSql.invoke(result, sql);
trace.recordException(result);
trace.recordAttribute("PreparedStatement", sql);
// trace.recordAttribute("PreparedStatement", sql);
}
trace.recordApi(descriptor, args);
@@ -3,6 +3,8 @@ package com.profiler.modifier.db.interceptor;
import com.profiler.context.Annotation;
import com.profiler.context.Trace;
import com.profiler.context.TraceContext;
import com.profiler.interceptor.ByteCodeMethodDescriptorSupport;
import com.profiler.interceptor.MethodDescriptor;
import com.profiler.interceptor.StaticAroundInterceptor;
import com.profiler.modifier.db.util.DatabaseInfo;
import com.profiler.util.InterceptorUtils;
@@ -16,11 +18,12 @@ import java.util.logging.Logger;
/**
* @author netspider
*/
public class StatementExecuteQueryInterceptor implements StaticAroundInterceptor {
public class StatementExecuteQueryInterceptor implements StaticAroundInterceptor, ByteCodeMethodDescriptorSupport {
private final Logger logger = Logger.getLogger(StatementExecuteQueryInterceptor.class.getName());
private final MetaObject<Object> getUrl = new MetaObject<Object>("__getUrl");
private MethodDescriptor descriptor;
@Override
public void before(Object target, String className, String methodName, String parameterDescription, Object[] args) {
@@ -46,9 +49,9 @@ public class StatementExecuteQueryInterceptor implements StaticAroundInterceptor
DatabaseInfo databaseInfo = (DatabaseInfo) this.getUrl.invoke(target);
trace.recordRpcName(databaseInfo.getType() + "/" + databaseInfo.getDatabaseId(), databaseInfo.getUrl());
trace.recordTerminalEndPoint(databaseInfo.getUrl());
if (args.length > 0) {
trace.recordAttribute("Statement", args[0]);
}
// if (args.length > 0) {
// trace.recordAttribute("Statement", args[0]);
// }
} catch (Exception e) {
if (logger.isLoggable(Level.WARNING)) {
@@ -71,10 +74,15 @@ public class StatementExecuteQueryInterceptor implements StaticAroundInterceptor
if (trace == null) {
return;
}
trace.recordApi(descriptor, args);
trace.recordException(result);
trace.markAfterTime();
trace.traceBlockEnd();
}
@Override
public void setMethodDescriptor(MethodDescriptor descriptor) {
this.descriptor = descriptor;
}
}
@@ -5,20 +5,21 @@ import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.profiler.context.Annotation;
import com.profiler.context.Trace;
import com.profiler.context.TraceContext;
import com.profiler.interceptor.ByteCodeMethodDescriptorSupport;
import com.profiler.interceptor.MethodDescriptor;
import com.profiler.interceptor.StaticAroundInterceptor;
import com.profiler.modifier.db.util.DatabaseInfo;
import com.profiler.util.InterceptorUtils;
import com.profiler.util.MetaObject;
import com.profiler.util.StringUtils;
public class TransactionInterceptor implements StaticAroundInterceptor {
public class TransactionInterceptor implements StaticAroundInterceptor, ByteCodeMethodDescriptorSupport {
private final Logger logger = Logger.getLogger(TransactionInterceptor.class.getName());
private final MetaObject<Object> getUrl = new MetaObject<Object>("__getUrl");
private MethodDescriptor descriptor;
@Override
public void before(Object target, String className, String methodName, String parameterDescription, Object[] args) {
@@ -62,7 +63,7 @@ public class TransactionInterceptor implements StaticAroundInterceptor {
if (target instanceof Connection) {
Connection con = (Connection) target;
if ("setAutoCommit".equals(methodName)) {
afterStartTransaction(trace, con, args[0], result);
afterStartTransaction(trace, con, args, result);
} else if ("commit".equals(methodName)) {
afterCommit(trace, con, result);
} else if ("rollback".equals(methodName)) {
@@ -79,37 +80,39 @@ public class TransactionInterceptor implements StaticAroundInterceptor {
DatabaseInfo databaseInfo = (DatabaseInfo) this.getUrl.invoke(target);
trace.recordRpcName(getRpcName(databaseInfo), databaseInfo.getUrl());
trace.recordTerminalEndPoint(databaseInfo.getUrl());
}
private String getRpcName(DatabaseInfo databaseInfo) {
return databaseInfo.getType() + "/" + databaseInfo.getDatabaseId();
}
private void afterStartTransaction(Trace trace, Connection target, Object arg, Object result) {
private void afterStartTransaction(Trace trace, Connection target, Object[] arg, Object result) {
try {
Boolean autocommit = (Boolean) arg;
boolean success = InterceptorUtils.isSuccess(result);
if (!autocommit) {
// transaction start;
if (success) {
trace.recordAttribute("Transaction", "begin");
} else {
trace.recordAttribute("Transaction", "begin fail");
Throwable th = (Throwable) result;
trace.recordAttribute("Exception", th.getMessage());
}
} else {
if (success) {
trace.recordAttribute("Transaction", "autoCommit:false");
} else {
trace.recordAttribute("Transaction", "autoCommit:false fail");
Throwable th = (Throwable) result;
trace.recordAttribute("Exception", th.getMessage());
}
}
trace.recordApi(descriptor, arg);
trace.recordException(result);
// Boolean autocommit = (Boolean) arg;
// boolean success = InterceptorUtils.isSuccess(result);
// if (!autocommit) {
// // transaction start;
// if (success) {
// trace.recordAttribute("Transaction", "begin");
// trace.recordApi(descriptor, null);
// } else {
// trace.recordAttribute("Transaction", "begin fail");
// Throwable th = (Throwable) result;
// trace.recordAttribute("Exception", th.getMessage());
// }
//
// } else {
// if (success) {
// trace.recordAttribute("Transaction", "autoCommit:false");
// } else {
// trace.recordAttribute("Transaction", "autoCommit:false fail");
// Throwable th = (Throwable) result;
// trace.recordAttribute("Exception", th.getMessage());
// }
//
// }
} catch (Exception e) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, e.getMessage(), e);
@@ -137,14 +140,17 @@ public class TransactionInterceptor implements StaticAroundInterceptor {
trace.recordRpcName(getRpcName(databaseInfo), databaseInfo.getUrl());
trace.recordTerminalEndPoint(databaseInfo.getUrl());
boolean success = InterceptorUtils.isSuccess(result);
if (success) {
trace.recordAttribute("Transaction", "commit");
} else {
trace.recordAttribute("Transaction", "commit fail");
Throwable th = (Throwable) result;
trace.recordAttribute("Exception", th.getMessage());
}
trace.recordApi(descriptor);
trace.recordException(result);
// boolean success = InterceptorUtils.isSuccess(result);
// if (success) {
// trace.recordAttribute("Transaction", "commit");
// } else {
// trace.recordAttribute("Transaction", "commit fail");
// Throwable th = (Throwable) result;
// trace.recordAttribute("Exception", th.getMessage());
// }
} catch (Exception e) {
if (logger.isLoggable(Level.WARNING)) {
@@ -173,12 +179,14 @@ public class TransactionInterceptor implements StaticAroundInterceptor {
trace.recordRpcName(getRpcName(databaseInfo), databaseInfo.getUrl());
trace.recordTerminalEndPoint(databaseInfo.getUrl());
boolean success = InterceptorUtils.isSuccess(result);
if (success) {
trace.recordAttribute("Transaction", "rollback");
} else {
trace.recordAttribute("Transaction", "rollback fail");
}
trace.recordApi(descriptor);
trace.recordException(result);
// boolean success = InterceptorUtils.isSuccess(result);
// if (success) {
// trace.recordAttribute("Transaction", "rollback");
// } else {
// trace.recordAttribute("Transaction", "rollback fail");
// }
trace.recordException(result);
} catch (Exception e) {
if (logger.isLoggable(Level.WARNING)) {
@@ -190,4 +198,8 @@ public class TransactionInterceptor implements StaticAroundInterceptor {
}
}
@Override
public void setMethodDescriptor(MethodDescriptor descriptor) {
this.descriptor = descriptor;
}
}
@@ -54,10 +54,12 @@ public class MySQLConnectionImplModifier extends AbstractModifier {
mysqlConnection.addInterceptor("prepareStatement", new String[]{"java.lang.String"}, preparedStatement);
Interceptor transaction = new TransactionInterceptor();
int interceptorId = mysqlConnection.addInterceptor("setAutoCommit", new String[]{"boolean"}, transaction);
mysqlConnection.reuseInterceptor("commit", null, interceptorId);
mysqlConnection.reuseInterceptor("rollback", null, interceptorId);
Interceptor setAutocommit = new TransactionInterceptor();
mysqlConnection.addInterceptor("setAutoCommit", new String[]{"boolean"}, setAutocommit);
Interceptor commit = new TransactionInterceptor();
mysqlConnection.addInterceptor("commit", null, commit);
Interceptor rollback = new TransactionInterceptor();
mysqlConnection.addInterceptor("rollback", null, rollback);
printClassConvertComplete(javassistClassName);
@@ -163,6 +163,9 @@ public class JavaAssistUtils {
public static String[] getParameterVariableName(CtBehavior method) throws NotFoundException {
LocalVariableAttribute localVariableAttribute = lookupLocalVariableAttribute(method);
if (localVariableAttribute == null) {
return getParameterDefaultVariableName(method);
}
return getParameterVariableName(method, localVariableAttribute);
}
@@ -185,7 +188,7 @@ public class JavaAssistUtils {
// 이거 참고함.
if (localVariableAttribute == null) {
// null이라는건 debug모드로 컴파일 되지 않았다는 의미이다.
// parameter class명을 default로 하자.
// parameter class명을 default로 넘기는 건 아래 메소드가 함. getParameterDefaultVariableName.
return null;
}
CtClass[] parameterTypes = method.getParameterTypes();
@@ -209,4 +212,13 @@ public class JavaAssistUtils {
}
return parameterVariableNames;
}
public static String[] getParameterDefaultVariableName(CtBehavior method) throws NotFoundException {
CtClass[] parameterTypes = method.getParameterTypes();
String[] variableName = new String[parameterTypes.length];
for (int i = 0; i < variableName.length; i++) {
variableName[i] = parameterTypes[i].getSimpleName().toLowerCase();
}
return variableName;
}
}