Guest User

Untitled

a guest
Mar 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import lombok.extern.slf4j.Slf4j;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.Around;
  4. import org.aspectj.lang.annotation.Aspect;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.actuate.metrics.CounterService;
  7. import org.springframework.boot.actuate.metrics.GaugeService;
  8. import org.springframework.stereotype.Component;
  9.  
  10. import static java.lang.String.format;
  11. import static java.lang.System.currentTimeMillis;
  12.  
  13. @Slf4j
  14. @Aspect
  15. @Component
  16. public class CustomMetricsCollector {
  17.  
  18. private static final String COUNTER_TEMPLATE = "counter.%s.%s";
  19. private static final String GAUGE_TEMPLATE = "gauge.%s.%s";
  20. @Autowired
  21. private CounterService counterService;
  22. @Autowired
  23. private GaugeService gaugeService;
  24.  
  25. @Around("@annotation(com.fxclub.metrics.CollectMetrics)")
  26. public Object collectMetrics(ProceedingJoinPoint pjp) throws Throwable {
  27. String typeName = pjp.getSignature().getDeclaringTypeName();
  28. String methodName = pjp.getSignature().getName();
  29.  
  30. counterService.increment(format(COUNTER_TEMPLATE, typeName, methodName));
  31.  
  32. long startTime = currentTimeMillis();
  33. try {
  34. return pjp.proceed();
  35. } finally {
  36. long elapsedTime = currentTimeMillis() - startTime;
  37. gaugeService.submit(format(GAUGE_TEMPLATE, typeName, methodName), elapsedTime);
  38. }
  39. }
  40. }
Add Comment
Please, Sign In to add comment