Set InvocationID value from server and client invocationIds
[logging-analytics.git] / reference / logging-filter / logging-filter-base / src / main / java / org / onap / logging / filter / base / MDCSetup.java
index 75fde49..d600a2c 100644 (file)
@@ -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);
     }
 
@@ -166,23 +160,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.warn("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.warn("Unable to calculate elapsed time due to error: {}", e.getMessage());
+        }
     }
 
     public void setResponseStatusCode(int code) {
@@ -202,7 +206,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 +230,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 +242,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);