From b1c2cbe73420cbb62f643488422f174fe3311b1c Mon Sep 17 00:00:00 2001 From: "Smokowski, Steve (ss835w)" Date: Sat, 1 Sep 2018 06:58:20 -0400 Subject: Logging Improvement Add Spring interceptor to log all the SpringDataRest calls Issue-ID: SO-947 Change-Id: Ia1a574cedd90e79bb2fd30a094831dc5cd269ff0 Signed-off-by: Smokowski, Steve (ss835w) --- common/pom.xml | 4 + .../org/onap/so/logging/jaxrs/filter/MDCSetup.java | 39 +++++++++ .../spring/interceptor/LoggingInterceptor.java | 99 ++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 common/src/main/java/org/onap/so/logging/spring/interceptor/LoggingInterceptor.java (limited to 'common') diff --git a/common/pom.xml b/common/pom.xml index ca2c20df7a..490f2c782e 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -71,6 +71,10 @@ org.springframework spring-web + + org.springframework + spring-webmvc + org.onap.aai.aai-common aai-schema diff --git a/common/src/main/java/org/onap/so/logging/jaxrs/filter/MDCSetup.java b/common/src/main/java/org/onap/so/logging/jaxrs/filter/MDCSetup.java index 3c04fa1a8e..efc0aa84ef 100644 --- a/common/src/main/java/org/onap/so/logging/jaxrs/filter/MDCSetup.java +++ b/common/src/main/java/org/onap/so/logging/jaxrs/filter/MDCSetup.java @@ -5,8 +5,11 @@ import java.net.UnknownHostException; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.util.Map; import java.util.UUID; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.Response; import org.onap.logging.ref.slf4j.ONAPLogConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,4 +51,40 @@ public class MDCSetup { public void setEntryTimeStamp() { MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP,ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT)); } + + public void setServiceName(HttpServletRequest request) { + MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, request.getRequestURI()); + } + + public void setRequestId(Map headers) { + String requestId=headers.get(ONAPLogConstants.Headers.REQUEST_ID); + if(requestId == null || requestId.isEmpty()) + requestId = UUID.randomUUID().toString(); + MDC.put(ONAPLogConstants.MDCs.REQUEST_ID,requestId); + } + + public void setInvocationId(Map headers) { + 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 setMDCPartnerName(Map headers) { + String partnerName=headers.get(ONAPLogConstants.Headers.PARTNER_NAME); + if(partnerName == null || partnerName.isEmpty()) + partnerName = ""; + MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME,partnerName); + } + + + public void setResponseStatusCode(HttpServletResponse response) { + String statusCode; + if(Response.Status.Family.familyOf(response.getStatus()).equals(Response.Status.Family.SUCCESSFUL)){ + statusCode=ONAPLogConstants.ResponseStatus.COMPLETED.toString(); + }else{ + statusCode= ONAPLogConstants.ResponseStatus.ERROR.toString(); + } + MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode); + } } \ No newline at end of file diff --git a/common/src/main/java/org/onap/so/logging/spring/interceptor/LoggingInterceptor.java b/common/src/main/java/org/onap/so/logging/spring/interceptor/LoggingInterceptor.java new file mode 100644 index 0000000000..755d18b5ee --- /dev/null +++ b/common/src/main/java/org/onap/so/logging/spring/interceptor/LoggingInterceptor.java @@ -0,0 +1,99 @@ +package org.onap.so.logging.spring.interceptor; + +import java.util.Collections; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.Providers; +import org.onap.logging.ref.slf4j.ONAPLogConstants; +import org.onap.so.logging.jaxrs.filter.MDCSetup; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +@Component +public class LoggingInterceptor extends HandlerInterceptorAdapter { + + Logger logger = LoggerFactory.getLogger(LoggingInterceptor.class); + + @Autowired + MDCSetup mdcSetup; + + @Context + private Providers providers; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) + throws Exception { + Map headers = Collections.list(((HttpServletRequest) request).getHeaderNames()) + .stream() + .collect(Collectors.toMap(h -> h, request::getHeader)); + setRequestId(headers); + setInvocationId(headers); + setServiceName(request); + setMDCPartnerName(headers); + mdcSetup.setClientIPAddress(request); + mdcSetup.setEntryTimeStamp(); + mdcSetup.setInstanceUUID(); + mdcSetup.setServerFQDN(); + MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, "INPROGRESS"); + logger.info(ONAPLogConstants.Markers.ENTRY, "Entering"); + return true; + } + + @Override + public void postHandle( + HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) + throws Exception { + setResponseStatusCode(response); + MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION,""); + MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE,String.valueOf(response.getStatus())); + logger.info(ONAPLogConstants.Markers.EXIT, "Exiting."); + MDC.clear(); + } + + private void setResponseStatusCode(HttpServletResponse response) { + String statusCode; + if(Response.Status.Family.familyOf(response.getStatus()).equals(Response.Status.Family.SUCCESSFUL)){ + statusCode=ONAPLogConstants.ResponseStatus.COMPLETED.toString(); + }else{ + statusCode= ONAPLogConstants.ResponseStatus.ERROR.toString(); + } + MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode); + } + + private void setServiceName(HttpServletRequest request) { + MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, request.getRequestURI()); + } + + private void setRequestId(Map headers) { + String requestId=headers.get(ONAPLogConstants.Headers.REQUEST_ID); + if(requestId == null || requestId.isEmpty()) + requestId = UUID.randomUUID().toString(); + MDC.put(ONAPLogConstants.MDCs.REQUEST_ID,requestId); + } + + private void setInvocationId(Map headers) { + String invocationId = headers.get(ONAPLogConstants.Headers.INVOCATION_ID); + if(invocationId == null || invocationId.isEmpty()) + invocationId =UUID.randomUUID().toString(); + MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId); + } + + private void setMDCPartnerName(Map headers) { + String partnerName=headers.get(ONAPLogConstants.Headers.PARTNER_NAME); + if(partnerName == null || partnerName.isEmpty()) + partnerName = ""; + MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME,partnerName); + } + + +} \ No newline at end of file -- cgit 1.2.3-korg