Sunday, April 20, 2014

[add-on] monitoring lite with deltaspike

there are quite a lot of different use-cases for monitoring and auditing. for more advanced use-cases projects like apache sirona are reasonable. however, in several cases it's enough to have a simple interceptor which collects information (which can get processed at the end of a request). since a lot of projects implemented it on their own and end up with similar solutions, i've created an add-on for apache deltaspike which supports several common use-cases like exception monitoring, performance monitoring (via execution time) and auditing (with or without method-parameter values).

after adding the add-on to a project based on deltaspike the usage is simple, just annotate a method or the whole class with @InvocationMonitored and create a cdi-observer for MonitoredMethodInvocationsEvent. the add-on will collect information about the monitored method-invocations during a request and you can process the entries based on your requirements. the following example illustrates a sample usage. it's up to you which information you use and how you process it (e.g. logging it, storing it in a persistent store,...).


public class MonitoredMethodInvocationProcessor
{
//...
public void onMonitoredMethodInvocations(@Observes MonitoredMethodInvocationsEvent methodInvocationsEvent)
{
String userId = this.userHolder.getCurrentUserId();
for (MethodInvocationDescriptor methodInvocation : methodInvocationsEvent.getMethodInvocationDescriptors())
{
if (methodInvocation.getException() != null)
{
error(userId + "@" + methodInvocation + ":L" + methodInvocation.getException().getStackTrace()[0].getLineNumber());
}
else
{
trace(userId + "@" + methodInvocation);
}
if (methodInvocation.getExecutionTime() > this.maxThreshold)
{
warn("slow method-invocation detected: " + methodInvocation.getMethodDetails());
}
}
}
//...
}
asynchronous processing is also supported via an @Asynchronous observer in an ejb.

the optional jsf module allows to record the current view-id per entry and the processing after the rendering-phase.

the add-on is available here.