2015-04-22 68 views

回答

1

我搜索周圍相當多,又沒有找到任何東西,所以我創造了我的機制來跟蹤時間和標準來蒙戈。

鑑於我們使用ELK來收集日誌和一些指標數據,我添加了一個註釋和一個方面來跟蹤時間。我把它放在我想要的與我們的mongo方法連接的度量上。它收集數據並將它們放入日誌中,通過Kibana,我可以在每種訪問類型中看到負載下的訪問Mongo。

這是註釋

@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.METHOD, ElementType.TYPE}) 
public @interface TimedMethod { 

} 

這是方面:

@Component() 
@Slf4j(topic="com.cisco.services.common.rpil.metrics") 
@Aspect 
public class TimedMethodAspect { 

    @Around("@annotation(com.cisco.services.common.rpil.metrics.TimedMethod) && execution(public * *(..))") 
    public Object time(ProceedingJoinPoint pjp) throws Throwable { 

     long start = System.nanoTime(); 
     String throwableName = null; 

     try { 

      return pjp.proceed(); 

     } catch(Throwable t) { 
      throwableName = t.getClass().getName(); 
      throw t; 
     } finally { 
      long duration = System.nanoTime() - start; 
      if (throwableName != null) { 
       log.info("Timed [{}]: {} nsecs, with exception [{}]", pjp.getSignature().toString(), duration, throwableName); 
      } else { 
       log.info("Timed [{}]: {} nsecs", pjp.getSignature().toString(), duration); 
      } 
     } 
    } 
} 

基本上它是這樣工作的:

@TimedMethod 
public Object measureMe() { 
... 
}