[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / common / openecomp-logging-lib / openecomp-logging-core / src / main / java / org / openecomp / core / logging / aspects / MetricsAspect.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.core.logging.aspects;
22
23 import org.aspectj.lang.ProceedingJoinPoint;
24 import org.aspectj.lang.annotation.Around;
25 import org.aspectj.lang.annotation.Aspect;
26 import org.openecomp.core.logging.api.Logger;
27 import org.openecomp.core.logging.api.LoggerFactory;
28 import org.openecomp.core.logging.api.annotations.Metrics;
29
30 /**
31  * <p>Wraps around any method annotated with {@link Metrics} to measure and log its execution time
32  * in milliseconds.</p> <p>In order for the aspect to be used, AspectJ annotation processing must be
33  * tuned on and this particular aspect enabled. Conversely, it can be disabled completely if the
34  * application does not need to log metrics.</p> <p>See, for example, <a
35  * href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html"> Aspect
36  * Oriented Programming with Spring</a>.</p>
37  *
38  * @see Metrics
39  */
40 @Aspect
41 public class MetricsAspect {
42
43   private static final String MESSAGE_TEMPLATE = "'{}' took {} milliseconds";
44
45   /**
46    * Log execution time object.
47    *
48    * @param pjp the pjp
49    * @return the object
50    * @throws Throwable the throwable
51    */
52   @Around("@annotation(org.openecomp.core.logging.api.annotations.Metrics)")
53   public Object logExecutionTime(ProceedingJoinPoint pjp) throws Throwable {
54
55     final Logger logger = LoggerFactory.getLogger(pjp.getSignature().getDeclaringTypeName());
56     // measure and log only if the logger for this class is enabled
57     if (logger.isMetricsEnabled()) {
58
59       final String method = pjp.getSignature().getName();
60       final long start = System.currentTimeMillis();
61
62       try {
63         return pjp.proceed();
64       } finally {
65         logger.metrics(MESSAGE_TEMPLATE, method, System.currentTimeMillis() - start);
66       }
67
68     } else {
69       return pjp.proceed();
70     }
71   }
72 }