Improve logging in NCMP 82/126682/6
authorsourabh_sourabh <sourabh.sourabh@est.tech>
Thu, 20 Jan 2022 20:16:12 +0000 (01:46 +0530)
committersourabh_sourabh <sourabh.sourabh@est.tech>
Tue, 25 Jan 2022 17:38:56 +0000 (23:08 +0530)
Issue-ID: CPS-855

Signed-off-by: sourabh_sourabh <sourabh.sourabh@est.tech>
Change-Id: I5d8a6a711d1c6b13d5d8eeba52a1dcd689dbdae2

cps-application/src/main/resources/application.yml
cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandler.java
cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java
cps-service/pom.xml
cps-service/src/main/java/org/onap/cps/aop/CpsLoggingAspectService.java [new file with mode: 0644]
cps-service/src/test/groovy/org/onap/cps/aop/CpsLoggingAspectServiceSpec.groovy [new file with mode: 0644]

index 1411f31..d615e99 100644 (file)
@@ -1,7 +1,7 @@
 #  ============LICENSE_START=======================================================\r
 #  Copyright (C) 2021 Pantheon.tech\r
 #  Modifications Copyright (C) 2021 Bell Canada\r
-#  Modifications Copyright (C) 2021 Nordix Foundation\r
+#  Modifications Copyright (C) 2021-2022 Nordix Foundation\r
 #  ================================================================================\r
 #  Licensed under the Apache License, Version 2.0 (the "License");\r
 #  you may not use this file except in compliance with the License.\r
@@ -30,6 +30,8 @@ rest:
 spring:\r
     main:\r
         banner-mode: "off"\r
+    application:\r
+        name: "cps-application"\r
     jpa:\r
         ddl-auto: create\r
         open-in-view: false\r
@@ -125,6 +127,8 @@ logging:
     level:\r
         org:\r
             springframework: INFO\r
+            onap:\r
+                cps: INFO\r
 \r
 dmi:\r
     auth:\r
index 6729329..ce9dd5a 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2021 Pantheon.tech
- *  Modifications Copyright (C) 2021 Nordix Foundation
+ *  Modifications Copyright (C) 2021-2022 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -20,8 +20,6 @@
 
 package org.onap.cps.ncmp.rest.exceptions;
 
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.onap.cps.ncmp.api.impl.exception.NcmpException;
 import org.onap.cps.ncmp.rest.controller.NetworkCmProxyController;
@@ -36,7 +34,6 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
  * Exception handler with error message return.
  */
 @Slf4j
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
 @RestControllerAdvice(assignableTypes = {NetworkCmProxyController.class})
 public class NetworkCmProxyRestExceptionHandler {
 
index d790e08..52af81c 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2020 Pantheon.tech
- *  Modifications Copyright (C) 2021 Nordix Foundation
+ *  Modifications Copyright (C) 2021-2022 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -48,9 +48,6 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
     QueryRestController.class})
 public class CpsRestExceptionHandler {
 
-    private CpsRestExceptionHandler() {
-    }
-
     /**
      * Default exception handler.
      *
index 6cef985..b875bbb 100644 (file)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>\r
 <!--\r
   ============LICENSE_START=======================================================\r
-  Copyright (C) 2021 Nordix Foundation\r
+  Copyright (C) 2021-2022 Nordix Foundation\r
   Modifications Copyright (C) 2021 Bell Canada.\r
   Modifications Copyright (C) 2021 Pantheon.tech\r
   ================================================================================\r
       <groupId>com.google.code.gson</groupId>\r
       <artifactId>gson</artifactId>\r
     </dependency>\r
+    <dependency>\r
+      <groupId>org.springframework.boot</groupId>\r
+      <artifactId>spring-boot-starter-aop</artifactId>\r
+    </dependency>\r
     <!-- T E S T   D E P E N D E N C I E S -->\r
     <dependency>\r
       <groupId>org.codehaus.groovy</groupId>\r
       <artifactId>spring-kafka-test</artifactId>\r
       <scope>test</scope>\r
     </dependency>\r
+    <dependency>\r
+      <groupId>org.aspectj</groupId>\r
+      <artifactId>aspectjrt</artifactId>\r
+      <scope>test</scope>\r
+    </dependency>\r
   </dependencies>\r
 </project>\r
diff --git a/cps-service/src/main/java/org/onap/cps/aop/CpsLoggingAspectService.java b/cps-service/src/main/java/org/onap/cps/aop/CpsLoggingAspectService.java
new file mode 100644 (file)
index 0000000..20c6af6
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2022 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.aop;
+
+import java.util.Arrays;
+import java.util.Optional;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
+import org.springframework.stereotype.Component;
+import org.springframework.util.StopWatch;
+
+@Aspect
+@Component
+@Slf4j
+@ConditionalOnExpression(
+        "'${logging.level.org.onap.cps}'.equalsIgnoreCase('DEBUG')"
+)
+public class CpsLoggingAspectService {
+
+    private static final String ALL_CPS_METHODS = "execution(* org.onap.cps..*(..)))";
+
+    /**
+     * To measure method execution time as a logging.
+     * @param proceedingJoinPoint exposes the proceed(..) method in order to support around advice.
+     * @return empty in case of void otherwise an object of return type
+     */
+    @Around(ALL_CPS_METHODS)
+    @SneakyThrows
+    public Object logMethodExecutionTime(final ProceedingJoinPoint proceedingJoinPoint) {
+        final StopWatch stopWatch = new StopWatch();
+
+        //Calculate method execution time
+        stopWatch.start();
+        final Object logObject = Optional.ofNullable(proceedingJoinPoint.proceed()).orElse("");
+        stopWatch.stop();
+        final MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
+        //Log method execution time
+        log.debug("Execution time of : {}.{}() with argument[s] = {} having result = {} :: {} ms",
+                methodSignature.getDeclaringType().getSimpleName(),
+                methodSignature.getName(), Arrays.toString(proceedingJoinPoint.getArgs()), logObject,
+                stopWatch.getTotalTimeMillis());
+
+        return logObject;
+    }
+
+}
diff --git a/cps-service/src/test/groovy/org/onap/cps/aop/CpsLoggingAspectServiceSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/aop/CpsLoggingAspectServiceSpec.groovy
new file mode 100644 (file)
index 0000000..b15af49
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2022 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.aop
+
+import org.aspectj.lang.ProceedingJoinPoint
+import org.aspectj.lang.reflect.MethodSignature
+import org.onap.cps.spi.exceptions.DataValidationException
+import spock.lang.Specification
+
+class CpsLoggingAspectServiceSpec extends Specification {
+
+    def mockProceedingJoinPoint = Mock(ProceedingJoinPoint)
+    def mockMethodSignature = Mock(MethodSignature);
+    def objectUnderTest = new CpsLoggingAspectService()
+
+    def setup() {
+        mockMethodSignature.getDeclaringType() >> this.getClass()
+        mockMethodSignature.getDeclaringType().getSimpleName() >> 'CpsLoggingAspectServiceSpec'
+        mockMethodSignature.getName() >> 'logMethodExecutionTime'
+        mockProceedingJoinPoint.getSignature() >> mockMethodSignature
+    }
+
+    def 'Log method execution time.'() {
+        given: 'mock valid arguments'
+            mockProceedingJoinPoint.getArgs() >> 'dataspace-name'
+        when: 'aop intercepts cps method and start calculation of time'
+            objectUnderTest.logMethodExecutionTime(mockProceedingJoinPoint)
+        then: 'process successfully and log details of executed method'
+            1 * mockProceedingJoinPoint.proceed()
+    }
+
+    def 'Creating a data validation exception for invalid args.'() {
+        given: 'a data validation exception is created'
+            mockProceedingJoinPoint.getArgs() >> {
+                throw new DataValidationException('invalid args',
+                        'invalid method arg(s) is passed', new Throwable())
+            }
+        when: 'aop intercepts cps method and start calculation of time'
+            objectUnderTest.logMethodExecutionTime(mockProceedingJoinPoint)
+        then: 'data validation exception is thrown'
+            thrown(DataValidationException.class)
+    }
+}