use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.
the class GsonPlugin method setup.
@Override
public void setup(ProfilerPluginSetupContext context) {
GsonConfig config = new GsonConfig(context.getConfig());
logger.debug("[Gson] Initialized config={}", config);
if (config.isProfile()) {
transformTemplate.transform("com.google.gson.Gson", new TransformCallback() {
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
for (InstrumentMethod m : target.getDeclaredMethods(MethodFilters.name("fromJson"))) {
m.addScopedInterceptor("com.navercorp.pinpoint.plugin.gson.interceptor.FromJsonInterceptor", GSON_SCOPE);
}
for (InstrumentMethod m : target.getDeclaredMethods(MethodFilters.name("toJson"))) {
m.addScopedInterceptor("com.navercorp.pinpoint.plugin.gson.interceptor.ToJsonInterceptor", GSON_SCOPE);
}
return target.toBytecode();
}
});
}
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.
the class DedicatedMethodTransformer method edit.
@Override
public void edit(ClassLoader classLoader, InstrumentClass target) throws Throwable {
InstrumentMethod targetMethod = target.getDeclaredMethod(targetMethodName, targetMethodParameterTypes);
if (targetMethod == null) {
if (ignoreIfNotExist) {
return;
} else {
Exception e = new NoSuchMethodException("No such method: " + targetMethodName + "(" + Arrays.deepToString(targetMethodParameterTypes) + ")");
if (exceptionHandler != null) {
exceptionHandler.handle(target.getName(), targetMethodName, targetMethodParameterTypes, e);
logger.info("Cannot find target method" + targetMethodName + "(" + Arrays.deepToString(targetMethodParameterTypes) + ") but MethodTransformerExceptionHandler handled it.");
return;
} else {
throw new InstrumentException("Fail to edit method", e);
}
}
}
for (MethodRecipe recipe : recipes) {
try {
recipe.edit(classLoader, target, targetMethod);
} catch (Throwable t) {
if (exceptionHandler != null) {
exceptionHandler.handle(target.getName(), targetMethodName, targetMethodParameterTypes, t);
logger.info("Exception thrown while editing" + targetMethod.getDescriptor().getApiDescriptor() + " but MethodTransformerExceptionHandler handled it.", t);
} else {
throw new InstrumentException("Fail to edit method " + targetMethod.getDescriptor().getApiDescriptor(), t);
}
}
}
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.
the class ASMMethodNodeTest method addInterceptor.
@Test
public void addInterceptor() throws Exception {
final int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor().addInterceptor(new ArgsArrayInterceptor());
final String targetClassName = "com.navercorp.pinpoint.profiler.instrument.mock.NormalClass";
final ASMClass declaringClass = mock(ASMClass.class);
when(declaringClass.getName()).thenReturn(targetClassName);
final ObjectBinderFactory objectBinderFactory = mock(ObjectBinderFactory.class);
ASMClassNodeLoader.TestClassLoader classLoader = ASMClassNodeLoader.getClassLoader();
classLoader.setTrace(false);
classLoader.setVerify(false);
classLoader.setTargetClassName(targetClassName);
classLoader.setCallbackHandler(new ASMClassNodeLoader.CallbackHandler() {
@Override
public void handle(ClassNode classNode) {
List<MethodNode> methodNodes = classNode.methods;
for (MethodNode methodNode : methodNodes) {
ASMMethod method = new ASMMethod(objectBinderFactory, pluginContext, apiMetaDataService, interceptorRegistryBinder, declaringClass, methodNode);
try {
method.addInterceptor(interceptorId);
} catch (InstrumentException e) {
e.printStackTrace();
}
}
}
});
Class<?> clazz = classLoader.loadClass(targetClassName);
Method method = clazz.getDeclaredMethod("sum", int.class);
assertEquals(55, method.invoke(clazz.newInstance(), 10));
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.
the class MyBatisPlugin method addInterceptorsForSqlSession.
// SqlSession implementations
private void addInterceptorsForSqlSession() {
final MethodFilter methodFilter = MethodFilters.name("selectOne", "selectList", "selectMap", "select", "insert", "update", "delete");
final String[] sqlSessionImpls = { "org.apache.ibatis.session.defaults.DefaultSqlSession", "org.mybatis.spring.SqlSessionTemplate" };
for (final String sqlSession : sqlSessionImpls) {
transformTemplate.transform(sqlSession, new TransformCallback() {
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
final InstrumentClass target = instrumentor.getInstrumentClass(loader, sqlSession, classfileBuffer);
final List<InstrumentMethod> methodsToTrace = target.getDeclaredMethods(methodFilter);
for (InstrumentMethod methodToTrace : methodsToTrace) {
String sqlSessionOperationInterceptor = "com.navercorp.pinpoint.plugin.mybatis.interceptor.SqlSessionOperationInterceptor";
methodToTrace.addScopedInterceptor(sqlSessionOperationInterceptor, MYBATIS_SCOPE, ExecutionPolicy.BOUNDARY);
}
return target.toBytecode();
}
});
}
}
use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.
the class Log4jPlugin method setup.
@Override
public void setup(ProfilerPluginSetupContext context) {
final Log4jConfig config = new Log4jConfig(context.getConfig());
if (logger.isInfoEnabled()) {
logger.info("Log4jPlugin config:{}", config);
}
if (!config.isLog4jLoggingTransactionInfo()) {
logger.info("Log4j plugin is not executed because log4j transform enable config value is false.");
return;
}
transformTemplate.transform("org.apache.log4j.spi.LoggingEvent", new TransformCallback() {
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass mdcClass = instrumentor.getInstrumentClass(loader, "org.apache.log4j.MDC", null);
if (mdcClass == null) {
logger.warn("Can not modify. Because org.apache.log4j.MDC does not exist.");
return null;
}
if (!mdcClass.hasMethod("put", "java.lang.String", "java.lang.Object")) {
logger.warn("Can not modify. Because put method does not exist at org.apache.log4j.MDC class.");
return null;
}
if (!mdcClass.hasMethod("remove", "java.lang.String")) {
logger.warn("Can not modify. Because remove method does not exist at org.apache.log4j.MDC class.");
return null;
}
InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
if (!target.hasConstructor("java.lang.String", "org.apache.log4j.Category", "org.apache.log4j.Priority", "java.lang.Object", "java.lang.Throwable")) {
logger.warn("Can not modify. Because constructor to modify not exist at org.apache.log4j.MDC class." + "\nThere is a strong presumption that your application use under version 1.2.14 log4j." + "\nconstructor prototype : LoggingEvent(String fqnOfCategoryClass, Category logger, Priority level, Object message, Throwable throwable);");
return null;
}
if (!target.hasConstructor("java.lang.String", "org.apache.log4j.Category", "long", "org.apache.log4j.Priority", "java.lang.Object", "java.lang.Throwable")) {
logger.warn("Can not modify. Because constructor to modify not exist at org.apache.log4j.MDC class." + "\nThere is a strong presumption that your application use under version 1.2.14 log4j." + "\nconstructor prototype : LoggingEvent(String fqnOfCategoryClass, Category logger, long timeStamp, Priority level, Object message, Throwable throwable);");
return null;
}
if (!target.hasConstructor("java.lang.String", "org.apache.log4j.Category", "long", "org.apache.log4j.Level", "java.lang.Object", "java.lang.String", "org.apache.log4j.spi.ThrowableInformation", "java.lang.String", "org.apache.log4j.spi.LocationInfo", "java.util.Map")) {
logger.warn("Can not modify. Because constructor to modify not exist at org.apache.log4j.MDC class. " + "\nThere is a strong presumption that your application use under version 1.2.14 log4j." + "\nconstructor prototype : LoggingEvent(final String fqnOfCategoryClass, final Category logger, final long timeStamp, final Level level, final Object message, final String threadName, final ThrowableInformation throwable, final String ndc, final LocationInfo info, final java.util.Map properties);");
return null;
}
target.addInterceptor("com.navercorp.pinpoint.plugin.log4j.interceptor.LoggingEventOfLog4jInterceptor");
return target.toBytecode();
}
});
}
Aggregations