changes in metrics logs 39/112839/2
authorKishore Reddy Dwaram <kishore.reddy.dwaram@att.com>
Thu, 17 Sep 2020 15:22:40 +0000 (11:22 -0400)
committerKishore Reddy Dwaram <kishore.reddy.dwaram@att.com>
Thu, 17 Sep 2020 21:00:15 +0000 (17:00 -0400)
Issue-ID: PORTAL-1008
Change-Id: I007420af6bf5f89862b55b7625f0f2a76d73802f
Signed-off-by: Kishore Reddy Dwaram <kishore.reddy.dwaram@att.com>
ecomp-sdk/epsdk-core/src/main/java/org/onap/portalsdk/core/logging/aspect/EELFLoggerAdvice.java
ecomp-sdk/epsdk-core/src/main/java/org/onap/portalsdk/core/logging/aspect/EELFLoggerAspect.java
ecomp-sdk/epsdk-core/src/main/java/org/onap/portalsdk/core/util/SystemProperties.java
ecomp-sdk/epsdk-core/src/test/java/org/onap/portalsdk/core/logging/aspect/EELFLoggerAspectTest.java
ecomp-sdk/epsdk-core/src/test/java/org/onap/portalsdk/core/logging/logic/EELFLoggerDelegateTest.java
ecomp-sdk/epsdk-logger/src/main/java/org/onap/portalsdk/core/logging/logic/EELFLoggerDelegate.java

index 55a2783..6a5fe46 100644 (file)
  */
 package org.onap.portalsdk.core.logging.aspect;
 
+import static com.att.eelf.configuration.Configuration.MDC_INSTANCE_UUID;
+import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN;
+import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS;
+import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
+
+import java.net.InetAddress;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.UUID;
 
 import javax.servlet.http.HttpServletRequest;
 
 import org.onap.portalsdk.core.domain.User;
+import org.onap.portalsdk.core.exception.SessionExpiredException;
+import org.onap.portalsdk.core.logging.format.AlarmSeverityEnum;
 import org.onap.portalsdk.core.logging.format.AuditLogFormatter;
 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
+import org.onap.portalsdk.core.logging.logic.LoggerProperties;
 import org.onap.portalsdk.core.service.AppService;
 import org.onap.portalsdk.core.util.SystemProperties;
 import org.onap.portalsdk.core.util.SystemProperties.SecurityEventTypeEnum;
@@ -94,21 +104,39 @@ public class EELFLoggerAdvice {
                                methodName = passOnArgs[1].toString();
                        }
 
-                       String appName = appService.getDefaultAppName();
+                       String appName ="";
                        if (appName == null || appName == "") {
                                appName = SystemProperties.SDK_NAME;
                        }
-
-                       EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(className);
+                       else {
+                               appName = appService.getDefaultAppName();
+                       }
 
                        // Initialize Request defaults only for controller methods.
+                       MDC.put(MDC_SERVICE_NAME, SystemProperties.getProperty(SystemProperties.APPLICATION_NAME));
+                       MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
+                       MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
+                       MDC.put(MDC_INSTANCE_UUID, LoggerProperties.getProperty(LoggerProperties.INSTANCE_UUID));
                        MDC.put(className + methodName + SystemProperties.METRICSLOG_BEGIN_TIMESTAMP, getCurrentDateTimeUTC());
+                       MDC.put(SystemProperties.METRICSLOG_BEGIN_TIMESTAMP, getCurrentDateTimeUTC());
+                       MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, getCurrentDateTimeUTC());
                        MDC.put(SystemProperties.TARGET_ENTITY, appName + "_BE");
                        MDC.put(SystemProperties.TARGET_SERVICE_NAME, methodName);
+                       MDC.put(SystemProperties.STATUS_CODE, "INPROGRESS");
+                       if (MDC.get(Configuration.MDC_KEY_REQUEST_ID) == null||MDC.get(Configuration.MDC_KEY_REQUEST_ID).isEmpty()){
+                               String requestId = UUID.randomUUID().toString();
+                               MDC.put(Configuration.MDC_KEY_REQUEST_ID, requestId);
+                       }
+                       MDC.put(SystemProperties.PARTNER_NAME, "Unknown");
+                       
+                       EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(className);
+                       
                        if (securityEventType != null) {
                                MDC.put(className + methodName + SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, getCurrentDateTimeUTC());
+                               MDC.put(SystemProperties.CUSTOM_FIELD1, "ENTRY");
+                               HttpServletRequest req = null;
                                if (args[0] != null && args[0] instanceof HttpServletRequest) {
-                                       HttpServletRequest req = (HttpServletRequest) args[0];
+                                       req = (HttpServletRequest) args[0];
                                        String requestId = UserUtils.getRequestId(req);
                                        String loginId = "";
                                        User user = UserUtils.getUserSession(req);
@@ -117,7 +145,17 @@ public class EELFLoggerAdvice {
                                        }
                                        logger.setRequestBasedDefaultsIntoGlobalLoggingContext(req, appName, requestId, loginId);
                                }
+                               MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, getCurrentDateTimeUTC());
+                               this.calculateDateTimeDifference(MDC.get(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
+                                               MDC.get(SystemProperties.AUDITLOG_END_TIMESTAMP));
+                               
+                       }else {
+                               MDC.put(SystemProperties.METRICSLOG_END_TIMESTAMP, getCurrentDateTimeUTC());
+                               this.calculateDateTimeDifference(MDC.get(SystemProperties.METRICSLOG_BEGIN_TIMESTAMP),MDC.get(SystemProperties.METRICSLOG_END_TIMESTAMP));
+                               MDC.put(SystemProperties.CUSTOM_FIELD1, "INVOKE");
                        }
+                       MDC.put(SystemProperties.CUSTOM_FIELD2, " InvocationID="+MDC.get(Configuration.MDC_KEY_REQUEST_ID));
+                       logger.info(EELFLoggerDelegate.metricsLogger, methodName + " operation is started.");
                        logger.debug(EELFLoggerDelegate.debugLogger, "{} was invoked.", methodName);
                } catch (Exception e) {
                        adviceLogger.error(EELFLoggerDelegate.errorLogger, "before failed", e);
@@ -147,8 +185,6 @@ public class EELFLoggerAdvice {
                                methodName = passOnArgs[1].toString();
                        }
 
-                       EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(className);
-
                        String appName = appService.getDefaultAppName();
                        if (appName == null || appName == "") {
                                appName = SystemProperties.SDK_NAME;
@@ -163,6 +199,15 @@ public class EELFLoggerAdvice {
                                MDC.put(SystemProperties.TARGET_ENTITY, appName + "_BE");
                        }
                        
+                       if (MDC.get(Configuration.MDC_KEY_REQUEST_ID) == null||MDC.get(Configuration.MDC_KEY_REQUEST_ID).isEmpty()){
+                               String requestId = UUID.randomUUID().toString();
+                               MDC.put(Configuration.MDC_KEY_REQUEST_ID, requestId);
+                       }
+                       
+                       if (MDC.get(SystemProperties.PARTNER_NAME) == null|| MDC.get(SystemProperties.PARTNER_NAME).isEmpty()){
+                               MDC.put(SystemProperties.PARTNER_NAME, "Unknown");
+                       }
+                       MDC.put(MDC_SERVICE_NAME, SystemProperties.getProperty(SystemProperties.APPLICATION_NAME));
                        MDC.put(SystemProperties.STATUS_CODE, result);
 
                        
@@ -172,9 +217,8 @@ public class EELFLoggerAdvice {
 
                        this.calculateDateTimeDifference(MDC.get(SystemProperties.METRICSLOG_BEGIN_TIMESTAMP),
                                        MDC.get(SystemProperties.METRICSLOG_END_TIMESTAMP));
-
-                       logger.info(EELFLoggerDelegate.metricsLogger, methodName + " operation is completed.");
-                       logger.debug(EELFLoggerDelegate.debugLogger, "Finished executing " + methodName + ".");
+                       MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, getCurrentDateTimeUTC());
+                       EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(className);
 
                        if (securityEventType != null) {
 
@@ -183,7 +227,8 @@ public class EELFLoggerAdvice {
                                MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, getCurrentDateTimeUTC());
                                this.calculateDateTimeDifference(MDC.get(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
                                                MDC.get(SystemProperties.AUDITLOG_END_TIMESTAMP));
-
+                               MDC.put(SystemProperties.CUSTOM_FIELD1, "EXIT");
+                               
                                this.logSecurityMessage(logger, securityEventType, result, methodName);
 
                                // clear when finishes audit logging
@@ -199,8 +244,12 @@ public class EELFLoggerAdvice {
                                MDC.remove(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
                                MDC.remove(SystemProperties.AUDITLOG_END_TIMESTAMP);
                        }else{
-                               MDC.put(SystemProperties.STATUS_CODE, "COMPLETE");
+                               MDC.put(SystemProperties.CUSTOM_FIELD1, "INVOKE-RETURN");
                        }
+                       MDC.put(SystemProperties.STATUS_CODE, "COMPLETE");
+                       MDC.put(SystemProperties.CUSTOM_FIELD2, " InvocationID="+MDC.get(Configuration.MDC_KEY_REQUEST_ID));
+                       logger.info(EELFLoggerDelegate.metricsLogger, methodName + " operation is completed.");
+                       logger.debug(EELFLoggerDelegate.debugLogger, "Finished executing " + methodName + ".");
 
                        MDC.remove(className + methodName + SystemProperties.METRICSLOG_BEGIN_TIMESTAMP);
                        MDC.remove(SystemProperties.METRICSLOG_BEGIN_TIMESTAMP);
@@ -209,6 +258,9 @@ public class EELFLoggerAdvice {
                        MDC.remove(SystemProperties.TARGET_ENTITY);
                        MDC.remove(SystemProperties.TARGET_SERVICE_NAME);
                        MDC.remove(SystemProperties.STATUS_CODE);
+                       MDC.remove(MDC_SERVICE_NAME);
+                       MDC.remove(SystemProperties.CUSTOM_FIELD1);
+                       MDC.remove(SystemProperties.CUSTOM_FIELD2);
 
                } catch (Exception e) {
                        adviceLogger.error(EELFLoggerDelegate.errorLogger, "after failed", e);
index 0963ef0..b0a2e09 100644 (file)
@@ -69,24 +69,6 @@ public class EELFLoggerAspect {
                return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.INCOMING_REST_MESSAGE);
        }
 
-       /*
-        * Point cut expression to capture metrics logging
-        */
-       @Pointcut("execution(public * *(..))")
-       public void publicMethod() {
-               // By default do nothing
-       }
-
-       @Around("publicMethod() && @within(metricsLog)")
-       public Object logMetricsClassAround(ProceedingJoinPoint joinPoint, MetricsLog metricsLog) throws Throwable {
-               return this.logAroundMethod(joinPoint, null);
-       }
-
-       @Around("publicMethod() && @annotation(metricsLog)")
-       public Object logMetricsMethodAround(ProceedingJoinPoint joinPoint, MetricsLog metricsLog) throws Throwable {
-               return this.logAroundMethod(joinPoint, null);
-       }
-
        private Object logAroundMethod(ProceedingJoinPoint joinPoint, SecurityEventTypeEnum securityEventType)
                        throws Throwable {
                // Before
index ad2e7b8..0ce15b7 100644 (file)
@@ -229,11 +229,14 @@ public class SystemProperties {
        public static final String CLIENT_IP_ADDRESS = "ClientIPAddress";
        public static final String STATUS_CODE = "StatusCode";
        public static final String RESPONSE_CODE = "ResponseCode";
+       public static final String CUSTOM_FIELD1 = "CustomField1";
+       public static final String CUSTOM_FIELD2 = "CustomField2";
+       public static final String EXTERNAL_API_RESPONSE_CODE = "External_API_ResponseCode";
        // Component or sub component name
        public static final String TARGET_ENTITY = "TargetEntity";
        // API or operation name
        public static final String TARGET_SERVICE_NAME = "TargetServiceName";
-
+       public static final String SERVICE_NAME = "ServiceName";
        // Logging Compliance
        public static final String DOUBLE_WHITESPACE_SEPARATOR = "  ";
        public static final String SINGLE_WHITESPACE_SEPARATOR = " ";
@@ -251,6 +254,8 @@ public class SystemProperties {
        public static final String ADDITIONAL_INFO = "ADDITIONAL_INFO";
        public static final String INTERFACE_NAME = "INTERFACE_NAME";
        public static final String USERAGENT_NAME = "user-agent";
+       public static final String AUTHORIZATION = "Authorization";
+       public static final String USERNAME     = "username";
 
        // Protocols
        public static final String HTTP = "HTTP";
@@ -342,7 +347,7 @@ public class SystemProperties {
         * 
         * @return Application name
         */
-       public String getApplicationName() {
+       public static String getApplicationName() {
                return getProperty(APPLICATION_NAME);
        }
 
index fb3dc07..a81de61 100644 (file)
@@ -92,40 +92,6 @@ public class EELFLoggerAspectTest {
                Assert.assertTrue(true);
        }
        
-       @Test
-       public void publicMethodTst(){
-               eelFLoggerAspect.publicMethod();
-               Assert.assertTrue(true);
-       }
-       
-       @Test
-       public void logMetricsClassAroundTest() throws Throwable {
-               ProceedingJoinPoint joinPoint = Mockito.mock(ProceedingJoinPoint.class);
-               MetricsLog mockLog = Mockito.mock(MetricsLog.class);
-               Signature sign = Mockito.mock(Signature.class);
-               
-               Mockito.when(joinPoint.getSignature()).thenReturn(sign);
-               Mockito.when(sign.getDeclaringType()).thenReturn(getClass());
-               Mockito.when(sign.getName()).thenReturn("MethodName");
-                               
-               eelFLoggerAspect.logMetricsClassAround(joinPoint, mockLog);
-               Assert.assertTrue(true);
-       }
-       
-       @Test
-       public void logMetricsMethodAroundTest() throws Throwable {
-               ProceedingJoinPoint joinPoint = Mockito.mock(ProceedingJoinPoint.class);
-               MetricsLog mockLog = Mockito.mock(MetricsLog.class);
-               Signature sign = Mockito.mock(Signature.class);
-               
-               Mockito.when(joinPoint.getSignature()).thenReturn(sign);
-               Mockito.when(sign.getDeclaringType()).thenReturn(getClass());
-               Mockito.when(sign.getName()).thenReturn("MethodName");
-                               
-               eelFLoggerAspect.logMetricsMethodAround(joinPoint, mockLog);
-               Assert.assertTrue(true);
-       }
-       
        @Test
        public void performMetricsLoggingTest() {
                eelFLoggerAspect.performMetricsLogging();
index 322e3c6..2d1187f 100644 (file)
@@ -241,7 +241,8 @@ public class EELFLoggerDelegateTest {
        @Test
        public void initTest(){
                EELFLoggerDelegate eelFLoggerDelegate = new EELFLoggerDelegate(getClass().getName());
-               eelFLoggerDelegate.init(SystemProperties.getProperty(SystemProperties.APPLICATION_NAME));
+               String appName = "PortalSDK";
+               eelFLoggerDelegate.init(appName);
                Assert.assertTrue(true);
        }
        
index 2bb1b52..adad5ca 100644 (file)
@@ -49,8 +49,7 @@ import java.net.InetAddress;
 import java.text.MessageFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.UUID;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
@@ -61,6 +60,7 @@ import org.onap.portalsdk.core.logging.format.AppMessagesEnum;
 import org.onap.portalsdk.core.logging.format.ErrorSeverityEnum;
 import org.slf4j.MDC;
 
+import com.att.eelf.configuration.Configuration;
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
 import com.att.eelf.configuration.SLF4jWrapper;
@@ -112,12 +112,20 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger {
                return delegate;
        }
 
-       public static void logWrite(EELFLogger logger, Level level, String msg, Throwable th, Object... arguments) {
-
+       public void logWrite(EELFLogger logger, Level level, String msg, Throwable th, Object... arguments) {
+               MDC.put(LoggerProperties.MDC_CLASS_NAME, className);
+               setGlobalLoggingContext();
                if ( logger == errorLogger) {
+                       MDC.put(LoggerProperties.MDC_CLASS_NAME, className);
                        MDC.put("ErrorCategory", ErrorSeverityEnum.ERROR.name());
                        MDC.put("ErrorCode", "900");
                        MDC.put("ErrorDescription", "Exception");
+
+               }
+               
+               if (MDC.get(Configuration.MDC_KEY_REQUEST_ID) == null||MDC.get(Configuration.MDC_KEY_REQUEST_ID).isEmpty()){
+                       String requestId = UUID.randomUUID().toString();
+                       MDC.put(Configuration.MDC_KEY_REQUEST_ID, requestId);
                }
                
                if (logger.isErrorEnabled()) {
@@ -127,6 +135,8 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger {
                }
                
                if ( logger == errorLogger) {
+                       MDC.remove(MDC_SERVICE_NAME);
+                       MDC.remove(LoggerProperties.MDC_CLASS_NAME);
                        MDC.remove("ErrorCode");
                        MDC.remove("ErrorDescription");
                        MDC.remove("ErrorCategory");
@@ -205,9 +215,7 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger {
         * @param msg
         */
        public void info(EELFLogger logger, String msg) {
-               MDC.put(LoggerProperties.MDC_CLASS_NAME, className);
                logWrite(logger, Level.INFO, msg, null);
-               MDC.remove(LoggerProperties.MDC_CLASS_NAME);
        }
 
        /**
@@ -218,9 +226,7 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger {
         * @param arguments
         */
        public void info(EELFLogger logger, String msg, Object... arguments) {
-               MDC.put(LoggerProperties.MDC_CLASS_NAME, className);
                logWrite(logger, Level.INFO, msg, null, arguments);
-               MDC.remove(LoggerProperties.MDC_CLASS_NAME);
 
        }
 
@@ -232,9 +238,7 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger {
         * @param th
         */
        public void info(EELFLogger logger, String msg, Throwable th) {
-               MDC.put(LoggerProperties.MDC_CLASS_NAME, className);
                logWrite(logger, Level.INFO, msg, null, th);
-               MDC.remove(LoggerProperties.MDC_CLASS_NAME);
 
        }
 
@@ -245,9 +249,7 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger {
         * @param msg
         */
        public void warn(EELFLogger logger, String msg) {
-               MDC.put(LoggerProperties.MDC_CLASS_NAME, className);
                logWrite(logger, Level.WARN, msg, null);
-               MDC.remove(LoggerProperties.MDC_CLASS_NAME);
        }
 
        /**
@@ -258,9 +260,7 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger {
         * @param arguments
         */
        public void warn(EELFLogger logger, String msg, Object... arguments) {
-               MDC.put(LoggerProperties.MDC_CLASS_NAME, className);
                logWrite(logger, Level.WARN, msg, null, arguments);
-               MDC.remove(LoggerProperties.MDC_CLASS_NAME);
        }
 
        /**
@@ -271,9 +271,7 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger {
         * @param th
         */
        public void warn(EELFLogger logger, String msg, Throwable th) {
-               MDC.put(LoggerProperties.MDC_CLASS_NAME, className);
                logWrite(logger, Level.WARN, msg, null, th);
-               MDC.remove(LoggerProperties.MDC_CLASS_NAME);
        }
 
        /**
@@ -283,9 +281,7 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger {
         * @param msg
         */
        public void error(EELFLogger logger, String msg) {
-               MDC.put(LoggerProperties.MDC_CLASS_NAME, className);
                logWrite(logger, Level.ERROR, msg, null);
-               MDC.remove(LoggerProperties.MDC_CLASS_NAME);
        }
 
        /**
@@ -296,9 +292,7 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger {
         * @param arguments
         */
        public void error(EELFLogger logger, String msg, Object... arguments) {
-               MDC.put(LoggerProperties.MDC_CLASS_NAME, className);
                logWrite(logger, Level.ERROR, msg, null, arguments);
-               MDC.remove(LoggerProperties.MDC_CLASS_NAME);
        }
 
        /**
@@ -309,9 +303,7 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger {
         * @param th
         */
        public void error(EELFLogger logger, String msg, Throwable th) {
-               MDC.put(LoggerProperties.MDC_CLASS_NAME, className);
                logWrite(logger, Level.ERROR, msg, null);
-               MDC.remove(LoggerProperties.MDC_CLASS_NAME);
        }
 
        /**
@@ -323,9 +315,7 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger {
         */
        public void error(EELFLogger logger, String msg, AlarmSeverityEnum severtiy) {
                MDC.put(MDC_ALERT_SEVERITY, severtiy.name());
-               MDC.put(LoggerProperties.MDC_CLASS_NAME, className);
                logWrite(logger, Level.ERROR, msg, null);
-               MDC.remove(LoggerProperties.MDC_CLASS_NAME);
                MDC.remove(MDC_ALERT_SEVERITY);
        }