metric log updates and util fix
[logging-analytics.git] / reference / logging-filter / logging-filter-base / src / main / java / org / onap / logging / filter / base / MDCSetup.java
index 75fde49..03dc055 100644 (file)
@@ -54,7 +54,7 @@ public class MDCSetup {
             serverFQDN = addr.getCanonicalHostName();
             MDC.put(ONAPLogConstants.MDCs.SERVER_IP_ADDRESS, addr.getHostAddress());
         } catch (UnknownHostException e) {
-            logger.warn("Cannot Resolve Host Name");
+            logger.trace("Cannot Resolve Host Name");
             serverFQDN = "";
         }
         MDC.put(ONAPLogConstants.MDCs.SERVER_FQDN, serverFQDN);
@@ -113,13 +113,7 @@ public class MDCSetup {
         String invocationId = headers.get(ONAPLogConstants.Headers.INVOCATION_ID);
         if (invocationId == null || invocationId.isEmpty())
             invocationId = UUID.randomUUID().toString();
-        MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
-    }
-
-    public void setInvocationIdFromMDC() {
-        String invocationId = MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID);
-        if (invocationId == null || invocationId.isEmpty())
-            invocationId = UUID.randomUUID().toString();
+        MDC.put(ONAPLogConstants.MDCs.SERVER_INVOCATION_ID, invocationId);
         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
     }
 
@@ -149,12 +143,6 @@ public class MDCSetup {
             return partnerName;
         }
 
-        logger.trace(checkHeaderLogPattern, Constants.HttpHeaders.CLIENT_ID, ONAPLogConstants.MDCs.PARTNER_NAME);
-        partnerName = headers.get(Constants.HttpHeaders.CLIENT_ID);
-        if (partnerName != null && !partnerName.isEmpty()) {
-            return partnerName;
-        }
-
         logger.trace("{} value could not be determined, defaulting partnerName to {}.",
                 ONAPLogConstants.MDCs.PARTNER_NAME, Constants.DefaultValues.UNKNOWN);
         return Constants.DefaultValues.UNKNOWN;
@@ -166,23 +154,33 @@ public class MDCSetup {
     }
 
     public void setElapsedTime() {
-        DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
-        ZonedDateTime entryTimestamp =
-                ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP), timeFormatter);
-        ZonedDateTime endTimestamp = ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP), timeFormatter);
-
-        MDC.put(ONAPLogConstants.MDCs.ELAPSED_TIME,
-                Long.toString(ChronoUnit.MILLIS.between(entryTimestamp, endTimestamp)));
+        try {
+            DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
+            ZonedDateTime entryTimestamp =
+                    ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP), timeFormatter);
+            ZonedDateTime endTimestamp =
+                    ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP), timeFormatter);
+
+            MDC.put(ONAPLogConstants.MDCs.ELAPSED_TIME,
+                    Long.toString(ChronoUnit.MILLIS.between(entryTimestamp, endTimestamp)));
+        } catch (Exception e) {
+            logger.trace("Unable to calculate elapsed time due to error: {}", e.getMessage());
+        }
     }
 
     public void setElapsedTimeInvokeTimestamp() {
-        DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
-        ZonedDateTime entryTimestamp =
-                ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP), timeFormatter);
-        ZonedDateTime endTimestamp = ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP), timeFormatter);
-
-        MDC.put(ONAPLogConstants.MDCs.ELAPSED_TIME,
-                Long.toString(ChronoUnit.MILLIS.between(entryTimestamp, endTimestamp)));
+        try {
+            DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
+            ZonedDateTime entryTimestamp =
+                    ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP), timeFormatter);
+            ZonedDateTime endTimestamp =
+                    ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP), timeFormatter);
+
+            MDC.put(ONAPLogConstants.MDCs.ELAPSED_TIME,
+                    Long.toString(ChronoUnit.MILLIS.between(entryTimestamp, endTimestamp)));
+        } catch (Exception e) {
+            logger.trace("Unable to calculate elapsed time due to error: {}", e.getMessage());
+        }
     }
 
     public void setResponseStatusCode(int code) {
@@ -202,7 +200,7 @@ public class MDCSetup {
     }
 
     public void clearClientMDCs() {
-        MDC.remove(ONAPLogConstants.MDCs.INVOCATION_ID);
+        MDC.remove(ONAPLogConstants.MDCs.CLIENT_INVOCATION_ID);
         MDC.remove(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION);
         MDC.remove(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
         MDC.remove(ONAPLogConstants.MDCs.RESPONSE_CODE);
@@ -226,14 +224,10 @@ public class MDCSetup {
     }
 
     public String getProperty(String property) {
-        logger.info("Checking for system property [{}]", property);
         String propertyValue = System.getProperty(property);
         if (propertyValue == null || propertyValue.isEmpty()) {
-            logger.info("System property was null or empty. Checking environment variable for: {}", property);
             propertyValue = System.getenv(property);
             if (propertyValue == null || propertyValue.isEmpty()) {
-                logger.info("Environment variable: {} was null or empty. Returning value: {}", property,
-                        Constants.DefaultValues.UNKNOWN);
                 propertyValue = Constants.DefaultValues.UNKNOWN;
             }
         }
@@ -242,7 +236,7 @@ public class MDCSetup {
 
     protected String getBasicAuthUserName(SimpleMap headers) {
         String encodedAuthorizationValue = headers.get(HttpHeaders.AUTHORIZATION);
-        if (encodedAuthorizationValue != null) {
+        if (encodedAuthorizationValue != null && encodedAuthorizationValue.startsWith("Basic")) {
             try {
                 // This will strip the word Basic and single space
                 encodedAuthorizationValue = encodedAuthorizationValue.substring(6);