[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-logging-lib / openecomp-sdc-logging-core / src / main / java / org / openecomp / sdc / 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.sdc.logging.aspects;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.aspectj.lang.ProceedingJoinPoint;
26 import org.aspectj.lang.annotation.Around;
27 import org.aspectj.lang.annotation.Aspect;
28 import org.slf4j.Marker;
29 import org.slf4j.MarkerFactory;
30
31 /**
32  * <p>Wraps around any method annotated with {@link Metrics} to measure and log its execution time
33  * in milliseconds.</p>
34  * <p>In order for the aspect to be used, AspectJ annotation processing must be tuned on and this
35  * particular aspect enabled. Conversely, it can be disabled completely if the application does not
36  * need to log metrics.</p>
37  * <p>See, for example, <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html">
38  * Aspect Oriented Programming with Spring</a>.</p>
39  *
40  * @author evitaliy
41  * @see Metrics
42  * @since 27/07/2016.
43  */
44 @Aspect
45 public class MetricsAspect {
46
47   private static final String MESSAGE_TEMPLATE = "'{}' took {} milliseconds";
48   private static final Marker METRICS = MarkerFactory.getMarker("METRICS");
49
50   @Around("@annotation(org.openecomp.sdc.logging.api.annotations.Metrics)")
51   public Object logExecutionTime(ProceedingJoinPoint pjp) throws Throwable {
52
53     final Logger logger = LoggerFactory.getLogger(pjp.getSignature().getDeclaringTypeName());
54     // measure and log only if the logger for this class is enabled
55     if (logger.isInfoEnabled()) {
56
57       final String method = pjp.getSignature().getName();
58       final long start = System.currentTimeMillis();
59
60       try {
61         return pjp.proceed();
62       } finally {
63         logger.info(METRICS, MESSAGE_TEMPLATE, method, System.currentTimeMillis() - start);
64       }
65
66     } else {
67       return pjp.proceed();
68     }
69   }
70 }