From 7a4909432c4668e5b4e0e1d0452d217d00d1b48e Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Mon, 14 Sep 2020 14:01:16 -0500 Subject: add ability to specify instant precision it is now easier to configure timestamp output Issue-ID: LOG-1236 Signed-off-by: Smokowski, Kevin (ks6305) Change-Id: Ie31b3faf706fe28f73d6f8e230bf7a7e2b3009cf --- .../filter/base/AbstractBaseMetricLogFilter.java | 3 +-- .../org/onap/logging/filter/base/MDCSetup.java | 26 ++++++++++++++++++---- .../org/onap/logging/filter/base/MDCSetupTest.java | 13 +++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) (limited to 'reference/logging-filter') diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractBaseMetricLogFilter.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractBaseMetricLogFilter.java index 85a21ea..4dd1b49 100644 --- a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractBaseMetricLogFilter.java +++ b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractBaseMetricLogFilter.java @@ -72,8 +72,7 @@ public abstract class AbstractBaseMetricLogFilter 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()); diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/MDCSetup.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/MDCSetup.java index 7141f9f..1bcc6e3 100644 --- a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/MDCSetup.java +++ b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/MDCSetup.java @@ -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() { diff --git a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MDCSetupTest.java b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MDCSetupTest.java index 8852f8d..fb6ca71 100644 --- a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MDCSetupTest.java +++ b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MDCSetupTest.java @@ -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()); + } + } -- cgit 1.2.3-korg