Implement the Interceptor Logic
The Java code to be run when the custom interceptor is hit is provided in the
onMethodBegin
and/or onMethodEnd
methods of the interceptor
object.
A code example of a simple interceptor that leverages the Agent API is shown below.
public class SampleGenericInterceptor extends AGenericInterceptor {
public SampleGenericInterceptor() {
super();
}
@Override
public List<Rule> initializeRules() {
List<Rule> rules = new ArrayList<Rule>();
rules.add(new Rule.Builder("Main").methodMatchString("genericInterceptorTarget").build());
return rules;
}
public Object onMethodBegin(Object invokedObject, String className, String methodName, Object[] paramValues) {
System.out.print("In begin");
AppdynamicsAgent.startTransaction("SampleGenericInterceptorBT", null, EntryTypes.POJO, false);
Map<String, String> keyVsValue = new HashMap<String, String>();
keyVsValue.put("foo", "bar");
AppdynamicsAgent.getEventPublisher().publishEvent("an event happened", "INFO", "AGENT_STATUS", keyVsValue);
return null;
}
public void onMethodEnd(Object state, Object invokedObject, String className, String methodName,
Object[] paramValues, Throwable thrownException, Object returnValue) {
System.out.print("In end");
Transaction currentTransaction = AppdynamicsAgent.getTransaction();
Set<DataScope> dataScopes = new HashSet<DataScope>();
dataScopes.add(DataScope.SNAPSHOTS); // collect data for BT snapshot.
currentTransaction.collectData("key", "value", dataScopes);
currentTransaction.end();
}
}