1 package cn.home1.oss.environment.admin;
2
3 import org.aspectj.lang.JoinPoint;
4 import org.aspectj.lang.ProceedingJoinPoint;
5 import org.aspectj.lang.annotation.Around;
6 import org.aspectj.lang.annotation.Aspect;
7 import org.aspectj.lang.annotation.Before;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.boot.actuate.metrics.CounterService;
10 import org.springframework.boot.actuate.metrics.GaugeService;
11 import org.springframework.stereotype.Component;
12
13
14
15
16
17 @Aspect
18 @Component
19 public class ServiceMonitor {
20
21 private final CounterService counterService;
22
23 private final GaugeService gaugeService;
24
25 public ServiceMonitor(final CounterService counterService, final GaugeService gaugeService) {
26 this.counterService = counterService;
27 this.gaugeService = gaugeService;
28 }
29
30 @Autowired
31 public ServiceMonitor serviceMonitor(final CounterService counterService, final GaugeService gaugeService) {
32 return new ServiceMonitor(counterService, gaugeService);
33 }
34
35 @Before("execution(* cn.home1.oss.environment.admin.controller.*.*(..))")
36 public void countServiceInvoke(final JoinPoint joinPoint) {
37 this.counterService.increment("meter." + joinPoint.getSignature() + "-invokeNum");
38 }
39
40 @Around("execution(* cn.home1.oss.environment.admin.controller.*.*(..))")
41 public Object latencyService(final ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
42 long start = System.currentTimeMillis();
43 final Object proceed = proceedingJoinPoint.proceed();
44 long end = System.currentTimeMillis();
45 this.gaugeService.submit(proceedingJoinPoint.getSignature().toString() + "-invokeTime", (double) end - start);
46 return proceed;
47 }
48 }