From 92b12efbcf990c5cdd902c5de8c2efab78525d52 Mon Sep 17 00:00:00 2001 From: jackjii79 Date: Thu, 29 Jun 2023 10:01:23 -0700 Subject: [PATCH] add RequestId in MDC logging --- .../h2o/mojos/deploy/local/rest/filter/MdcLoggingFilter.java | 5 ++++- .../mojos/deploy/local/rest/logging/ScorerJsonLayout.java | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/local-rest-scorer/src/main/java/ai/h2o/mojos/deploy/local/rest/filter/MdcLoggingFilter.java b/local-rest-scorer/src/main/java/ai/h2o/mojos/deploy/local/rest/filter/MdcLoggingFilter.java index ea361864..a1e2c410 100644 --- a/local-rest-scorer/src/main/java/ai/h2o/mojos/deploy/local/rest/filter/MdcLoggingFilter.java +++ b/local-rest-scorer/src/main/java/ai/h2o/mojos/deploy/local/rest/filter/MdcLoggingFilter.java @@ -5,6 +5,7 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.util.Optional; +import java.util.UUID; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -33,7 +34,9 @@ protected void doFilterInternal( MDC.MDCCloseable modelId = MDC.putCloseable( ScorerJsonLayout.EXPERIMENT_ID, getFromEnvironment(MODEL_ID)); MDC.MDCCloseable scorerType = - MDC.putCloseable(ScorerJsonLayout.SCORER_TYPE, getScorerType()); ) { + MDC.putCloseable(ScorerJsonLayout.SCORER_TYPE, getScorerType()); + MDC.MDCCloseable requestId = + MDC.putCloseable(ScorerJsonLayout.REQUEST_ID, UUID.randomUUID().toString());) { filterChain.doFilter(request, response); if (HttpServletResponse.SC_OK == response.getStatus()) { try (MDC.MDCCloseable responseCode = MDC.putCloseable( diff --git a/local-rest-scorer/src/main/java/ai/h2o/mojos/deploy/local/rest/logging/ScorerJsonLayout.java b/local-rest-scorer/src/main/java/ai/h2o/mojos/deploy/local/rest/logging/ScorerJsonLayout.java index 1c94c3bb..b31ba24c 100644 --- a/local-rest-scorer/src/main/java/ai/h2o/mojos/deploy/local/rest/logging/ScorerJsonLayout.java +++ b/local-rest-scorer/src/main/java/ai/h2o/mojos/deploy/local/rest/logging/ScorerJsonLayout.java @@ -17,6 +17,7 @@ public class ScorerJsonLayout extends JsonLayout { public static final String SCORER_TYPE = "ScorerType"; public static final String TIMESTAMP = "Timestamp"; public static final String LOG_LEVEL = "LogLevel"; + public static final String REQUEST_ID = "RequestId"; public ScorerJsonLayout() { super(); @@ -36,6 +37,7 @@ protected Map toJsonMap(ILoggingEvent event) { add(EXPERIMENT_ID, containsMdc(event, EXPERIMENT_ID), getFromMdc(event, EXPERIMENT_ID), map); add(NUM_ROWS, containsMdc(event, NUM_ROWS), getFromMdc(event, NUM_ROWS), map); add(RESPONSE_CODE, containsMdc(event, RESPONSE_CODE), getFromMdc(event, RESPONSE_CODE), map); + add(REQUEST_ID, containsMdc(event, REQUEST_ID), getFromMdc(event, REQUEST_ID), map); addCustomDataToJsonMap(map, event); return map; }