2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.logging.aspects;
23 import org.aspectj.lang.ProceedingJoinPoint;
24 import org.aspectj.lang.annotation.Around;
25 import org.aspectj.lang.annotation.Aspect;
26 import org.openecomp.sdc.logging.api.Logger;
27 import org.openecomp.sdc.logging.api.LoggerFactory;
28 import org.openecomp.sdc.logging.api.annotations.Metrics;
31 * <p>Wraps around any method annotated with {@link Metrics} to measure and log its execution time
32 * in milliseconds.</p>
33 * <p>In order for the aspect to be used, AspectJ annotation processing must be tuned on and this
34 * particular aspect enabled. Conversely, it can be disabled completely if the application does not
35 * need to log metrics.</p>
36 * <p>See, for example, <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html">
37 * Aspect Oriented Programming with Spring</a>.</p>
44 public class MetricsAspect {
46 private static final String MESSAGE_TEMPLATE = "'{}' took {} milliseconds";
48 @Around("@annotation(org.openecomp.sdc.logging.api.annotations.Metrics)")
49 public Object logExecutionTime(ProceedingJoinPoint pjp) throws Throwable {
51 final Logger logger = LoggerFactory.getLogger(pjp.getSignature().getDeclaringTypeName());
52 // measure and log only if the logger for this class is enabled
53 if (logger.isMetricsEnabled()) {
55 final String method = pjp.getSignature().getName();
56 final long start = System.currentTimeMillis();
61 logger.metrics(MESSAGE_TEMPLATE, method, System.currentTimeMillis() - start);