Skip to content

Commit

Permalink
Inception agent compatibility workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
rshivane committed Jan 4, 2022
1 parent 9673a53 commit 02848db
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.codahale.metrics.Clock;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
Expand All @@ -30,6 +32,7 @@
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jboss.byteman.rule.Rule;
import org.jboss.byteman.rule.helper.Helper;
Expand All @@ -43,11 +46,40 @@ public class RuleHelper extends Helper {

private static final Map<Object, Map<String, Object>> objectProperties = Collections
.synchronizedMap(new WeakHashMap<>());
private static Method GET_MODULE_METHOD;
private static Method MODULE_GET_NAME_METHOD;
static {
try {
GET_MODULE_METHOD = Class.class.getMethod("getModule");
} catch (NoSuchMethodException e) {
LOGGER.log(Level.FINE, "Can't find Class.getModule method.", e);
}
try {
Class<?> clazz = Class.forName("java.lang.Module");
MODULE_GET_NAME_METHOD = clazz.getMethod("getName");
} catch (NoSuchMethodException e) {
LOGGER.log(Level.FINE, "Can't find Class.getModule method.", e);
} catch (ClassNotFoundException e) {
LOGGER.log(Level.FINE, "Can't find java.lang.Module class.", e);
}
}

public RuleHelper(Rule rule) {
super(rule);
}

public boolean canAccess(Object o) throws InvocationTargetException, IllegalAccessException {
if (GET_MODULE_METHOD ==null)
return true;

Class<?> clazz = o.getClass();
Object module = GET_MODULE_METHOD.invoke(clazz);
Object moduleName = MODULE_GET_NAME_METHOD.invoke(module);
if (moduleName!=null && moduleName.equals("inception.agent"))
return false;
return true;
}

public String setObjectProperty(Object o, String propertyName, String propertyValue) {
return setObjectProperty0(o, propertyName, propertyValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ CLASS org.apache.http.protocol.HttpRequestExecutor
METHOD execute
HELPER ai.apptuit.metrics.jinsight.modules.httpclient.HttpClientRuleHelper
AT ENTRY
IF TRUE
IF canAccess($1)
DO onExecuteStart($1)
ENDRULE

Expand All @@ -29,6 +29,6 @@ CLASS org.apache.http.protocol.HttpRequestExecutor
METHOD execute
HELPER ai.apptuit.metrics.jinsight.modules.httpclient.HttpClientRuleHelper
AT EXIT
IF TRUE
IF canAccess($1)
DO onExecuteEnd($1, $!)
ENDRULE
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ CLASS org.apache.log4j.Category
METHOD callAppenders
HELPER ai.apptuit.metrics.jinsight.modules.log4j.Log4JRuleHelper
AT ENTRY
IF TRUE
IF canAccess($1)
DO appendersCalled($1)
ENDRULE

Expand All @@ -28,6 +28,6 @@ CLASS org.apache.log4j.helpers.PatternParser$BasicPatternConverter
METHOD convert
HELPER ai.apptuit.metrics.jinsight.modules.log4j.Log4JRuleHelper
AT EXIT
IF $0.type == org.apache.log4j.helpers.PatternParser.MESSAGE_CONVERTER
IF canAccess($1) && $0.type == org.apache.log4j.helpers.PatternParser.MESSAGE_CONVERTER
DO return convertMessage($1, $!)
ENDRULE

0 comments on commit 02848db

Please sign in to comment.