Skip to content

Instantly share code, notes, and snippets.

@huljas
Created January 12, 2012 14:19
Show Gist options
  • Save huljas/1600772 to your computer and use it in GitHub Desktop.
Save huljas/1600772 to your computer and use it in GitHub Desktop.
Aspect for monitoring all @controller spring controllers
import com.jamonapi.Monitor;
import com.jamonapi.MonitorFactory;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.util.regex.Pattern;
/**
* @author huljas
*/
@Component
@Aspect
public class MonitoredSpringBeanAspect {
@Around(value = "@within(org.springframework.stereotype.Controller)")
public Object monitorClass(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
return doMonitor(proceedingJoinPoint, "Controller");
}
protected Object doMonitor(ProceedingJoinPoint proceedingJoinPoint, String label) throws Throwable {
MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
// In case of proxies we split the ugly $$ part away
String className = proceedingJoinPoint.getTarget().getClass().getSimpleName().split(Pattern.quote("$$"))[0];
String methodName = signature.getMethod().getName();
Monitor monitor = MonitorFactory.startPrimary(label + ": " + className + "." + methodName);
try {
return proceedingJoinPoint.proceed();
} finally {
monitor.stop();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment