Added catching error when calculating elapsedTime.
[logging-analytics.git] / reference / logging-filter / logging-filter-base / src / main / java / org / onap / logging / filter / base / MDCSetup.java
index 16fa210..1a0c3b4 100644 (file)
@@ -79,10 +79,6 @@ public class MDCSetup {
                 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
     }
 
-    public void setServiceName(HttpServletRequest request) {
-        MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, request.getRequestURI());
-    }
-
     public String getRequestId(SimpleMap headers) {
         logger.trace("Checking X-ONAP-RequestID header for requestId.");
         String requestId = headers.get(ONAPLogConstants.Headers.REQUEST_ID);
@@ -150,23 +146,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) {
@@ -209,4 +215,18 @@ public class MDCSetup {
         MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, Response.Status.fromStatusCode(statusCode).toString());
     }
 
+    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;
+            }
+        }
+        return propertyValue;
+    }
 }