Created
September 20, 2017 12:05
-
-
Save benhubert/a20b5cf24dfd9a8568ef9c67b1d97092 to your computer and use it in GitHub Desktop.
Example method profiler using AspectJ for StatsD blogpost at tech.willhaben.at
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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