Skip to content

Instantly share code, notes, and snippets.

@benhubert
Created September 20, 2017 12:05
Show Gist options
  • Save benhubert/a20b5cf24dfd9a8568ef9c67b1d97092 to your computer and use it in GitHub Desktop.
Save benhubert/a20b5cf24dfd9a8568ef9c67b1d97092 to your computer and use it in GitHub Desktop.
Example method profiler using AspectJ for StatsD blogpost at tech.willhaben.at
@Aspect
public class MethodProfiler {
private final StatsDClient statsd;
public MethodProfiler(StatsDClient statsd) {
this.statsd = statsd;
}
@Pointcut("execution(* at.willhaben.examples.statsd.rest.RandomController.*(..))")
public void restServiceMethods() {
}
@Around("restServiceMethods()")
public Object profile(ProceedingJoinPoint pjp) throws Throwable {
// execute the method, record the result and measure the time
Stopwatch stopwatch = Stopwatch.createStarted();
Object output = pjp.proceed();
stopwatch.stop();
// send the recorded time to statsd
String key = String.format("%s.%s", pjp.getSignature().getDeclaringTypeName(), pjp.getSignature().getName());
statsd.recordExecutionTime(key, stopwatch.elapsed(TimeUnit.MILLISECONDS));
// return the recorded result
return output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment