add ability to specify instant precision 34/112634/2
authorSmokowski, Kevin (ks6305) <kevin.smokowski@att.com>
Mon, 14 Sep 2020 19:01:16 +0000 (14:01 -0500)
committerKevin Smokowski <kevin.smokowski@att.com>
Tue, 15 Sep 2020 13:19:46 +0000 (13:19 +0000)
it is now easier to configure timestamp output

Issue-ID: LOG-1236
Signed-off-by: Smokowski, Kevin (ks6305) <kevin.smokowski@att.com>
Change-Id: Ie31b3faf706fe28f73d6f8e230bf7a7e2b3009cf

reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractBaseMetricLogFilter.java
reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/MDCSetup.java
reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MDCSetupTest.java

index 85a21ea..4dd1b49 100644 (file)
@@ -72,8 +72,7 @@ public abstract class AbstractBaseMetricLogFilter<Request, Response> extends MDC
     }
 
     protected void setupMDC(Request request) {
-        MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP,
-                ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
+        MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, getCurrentTimeStamp());
         MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, getTargetServiceName(request));
         MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
 
index 7141f9f..1bcc6e3 100644 (file)
@@ -25,6 +25,7 @@ import java.net.UnknownHostException;
 import java.time.ZoneOffset;
 import java.time.ZonedDateTime;
 import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeFormatterBuilder;
 import java.time.temporal.ChronoUnit;
 import java.util.Base64;
 import java.util.UUID;
@@ -42,11 +43,13 @@ public class MDCSetup {
     private static final String INSTANCE_UUID = UUID.randomUUID().toString();
     protected static final String serverIpAddressOverride = "SERVER_IP_ADDRESS_OVERRIDE";
     protected static final String serverFqdnOverride = "SERVER_FQDN_OVERRIDE";
+    protected static final String INSTANT_PRECISION_OVERRIDE = "INSTANT_PRECISION_OVERRIDE";
     protected static final String checkHeaderLogPattern = "Checking {} header to determine the value of {}";
     protected String serverFqdn;
     protected String serverIpAddress;
     protected String[] prioritizedIdHeadersNames;
     protected String[] prioritizedPartnerHeadersNames;
+    protected DateTimeFormatter iso8601Formatter;
 
     public MDCSetup() {
         this.prioritizedIdHeadersNames =
@@ -55,6 +58,23 @@ public class MDCSetup {
         this.prioritizedPartnerHeadersNames =
                 new String[] {HttpHeaders.AUTHORIZATION, ONAPLogConstants.Headers.PARTNER_NAME, HttpHeaders.USER_AGENT};
         initServerFqdnandIp();
+        this.iso8601Formatter = createFormatter();
+    }
+
+    protected String getCurrentTimeStamp() {
+        return ZonedDateTime.now(ZoneOffset.UTC).format(iso8601Formatter);
+    }
+
+    protected DateTimeFormatter createFormatter() {
+        DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
+        try {
+            Integer instantPrecision = Integer.valueOf(System.getProperty(INSTANT_PRECISION_OVERRIDE, "3"));
+            builder.appendInstant(instantPrecision);
+        } catch (NumberFormatException nfe) {
+            logger.warn("instant precision could not be read and thus won't be set, the default will be used instead."
+                    + nfe.getMessage());
+        }
+        return builder.toFormatter();
     }
 
     public void setInstanceID() {
@@ -102,8 +122,7 @@ public class MDCSetup {
     }
 
     public void setEntryTimeStamp() {
-        MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP,
-                ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
+        MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP, getCurrentTimeStamp());
     }
 
     public String getRequestId(SimpleMap headers) {
@@ -152,8 +171,7 @@ public class MDCSetup {
     }
 
     public void setLogTimestamp() {
-        MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP,
-                ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
+        MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP, getCurrentTimeStamp());
     }
 
     public void setElapsedTime() {
index 8852f8d..fb6ca71 100644 (file)
@@ -24,6 +24,9 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.mockito.Mockito.when;
+
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
 import java.util.HashMap;
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.core.HttpHeaders;
@@ -315,4 +318,14 @@ public class MDCSetupTest extends MDCSetup {
         assertEquals(nodeName, m.serverFqdn);
     }
 
+    @Test
+    public void testPrecision() {
+        System.setProperty(MDCSetup.INSTANT_PRECISION_OVERRIDE, "3");
+        ZonedDateTime zdt = ZonedDateTime.now(ZoneOffset.UTC);
+        zdt = zdt.withNano(333666999);
+        MDCSetup m = new MDCSetup();
+        String currentTimestamp = m.getCurrentTimeStamp();
+        assertEquals(24, currentTimestamp.length());
+    }
+
 }