diff options
Diffstat (limited to 'common/src/main')
-rw-r--r-- | common/src/main/java/org/onap/so/logger/MsoLogger.java | 34 | ||||
-rw-r--r-- | common/src/main/java/org/onap/so/logging/jaxrs/filter/jersey/JaxRsFilterLogging.java | 261 |
2 files changed, 163 insertions, 132 deletions
diff --git a/common/src/main/java/org/onap/so/logger/MsoLogger.java b/common/src/main/java/org/onap/so/logger/MsoLogger.java index 10f572e772..e4cac067ba 100644 --- a/common/src/main/java/org/onap/so/logger/MsoLogger.java +++ b/common/src/main/java/org/onap/so/logger/MsoLogger.java @@ -35,6 +35,8 @@ import org.onap.so.entity.MsoRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; /** @@ -49,15 +51,23 @@ import org.slf4j.MDC; */ public class MsoLogger { - // MDC parameters - public static final String REQUEST_ID = "RequestId"; - public static final String SERVICE_INSTANCE_ID = "ServiceInstanceId"; + // Required MDC parameters + public static final String REQUEST_ID = "RequestID"; + public static final String INVOCATION_ID = "InvocationID"; + public static final String INSTANCE_UUID = "InstanceUUID"; public static final String SERVICE_NAME = "ServiceName"; + public static final String STATUSCODE = "StatusCode"; + public static final String RESPONSECODE = "ResponseCode"; + public static final String RESPONSEDESC = "ResponseDesc"; + public static final String FQDN = "ServerFQDN"; + + + public static final String SERVICE_INSTANCE_ID = "ServiceInstanceId"; + public static final String SERVICE_NAME_IS_METHOD_NAME = "ServiceNameIsMethodName"; - public static final String INSTANCE_UUID = "InstanceUUID"; public static final String SERVER_IP = "ServerIPAddress"; - public static final String FQDN = "ServerFQDN"; + public static final String REMOTE_HOST = "RemoteHost"; public static final String ALERT_SEVERITY = "AlertSeverity"; public static final String TIMER = "Timer"; @@ -73,16 +83,17 @@ public class MsoLogger { public static final String HEADER_REQUEST_ID = "X-RequestId"; public static final String TRANSACTION_ID = "X-TransactionID"; public static final String ECOMP_REQUEST_ID = "X-ECOMP-RequestID"; + public static final String ONAP_REQUEST_ID = "X-ONAP-RequestID"; + public static final String CLIENT_ID = "X-ClientID"; + public static final String INVOCATION_ID_HEADER = "X-InvocationID"; // Audit/Metric log specific public static final String BEGINTIME = "BeginTimestamp"; public static final String STARTTIME = "StartTimeMilis"; public static final String ENDTIME = "EndTimestamp"; public static final String PARTNERNAME = "PartnerName"; - public static final String STATUSCODE = "StatusCode"; - public static final String RESPONSECODE = "ResponseCode"; - public static final String RESPONSEDESC = "ResponseDesc"; + // Metric log specific @@ -103,8 +114,10 @@ public class MsoLogger { public static final String ERRORCODE = "ErrorCode"; public static final String ERRORDESC = "ErrorDesc"; + //Status Codes public static final String COMPLETE = "COMPLETE"; - + public static final String INPROGRESS = "INPROGRESS"; + public enum Catalog { APIH, BPEL, RA, ASDC, GENERAL } @@ -157,6 +170,9 @@ public class MsoLogger { this.value = value; } } + + public static final Marker ENTRY = MarkerFactory.getMarker("ENTRY"); + public static final Marker EXIT = MarkerFactory.getMarker("EXIT"); private Logger logger; private Logger metricsLogger; diff --git a/common/src/main/java/org/onap/so/logging/jaxrs/filter/jersey/JaxRsFilterLogging.java b/common/src/main/java/org/onap/so/logging/jaxrs/filter/jersey/JaxRsFilterLogging.java index 9fab4ff0df..d278a5f761 100644 --- a/common/src/main/java/org/onap/so/logging/jaxrs/filter/jersey/JaxRsFilterLogging.java +++ b/common/src/main/java/org/onap/so/logging/jaxrs/filter/jersey/JaxRsFilterLogging.java @@ -44,137 +44,152 @@ import javax.ws.rs.core.Response; import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.Providers; - import org.onap.so.logger.MsoLogger; +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 com.fasterxml.jackson.databind.ObjectMapper; @Priority(1) @Provider @Component public class JaxRsFilterLogging implements ContainerRequestFilter,ContainerResponseFilter { - - private static MsoLogger logger = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA,JaxRsFilterLogging.class); - - @Context - private HttpServletRequest httpServletRequest; - - @Context - private Providers providers; - - @Autowired - ObjectMapper objectMapper; - - @Override - public void filter(ContainerRequestContext containerRequest) { - - try { - String clientID = null; - //check headers for request id - MultivaluedMap<String, String> headers = containerRequest.getHeaders(); - String requestId = (String) headers.getFirst(MsoLogger.HEADER_REQUEST_ID ); - if(requestId == null || requestId.isEmpty()){ - if(headers.containsKey(MsoLogger.TRANSACTION_ID)){ - requestId = headers.getFirst(MsoLogger.TRANSACTION_ID); - }else if(headers.containsKey(MsoLogger.ECOMP_REQUEST_ID)){ - requestId = headers.getFirst(MsoLogger.ECOMP_REQUEST_ID); - }else{ - requestId = UUID.randomUUID().toString(); - logger.warnSimple(containerRequest.getUriInfo().getPath(),"Generating RequestId for Request"); - } - } - containerRequest.setProperty("requestId", requestId); - if(headers.containsKey(MsoLogger.CLIENT_ID)){ - clientID = headers.getFirst(MsoLogger.CLIENT_ID); - }else{ - clientID = "UNKNOWN"; - headers.add(MsoLogger.CLIENT_ID, clientID); - } - String remoteIpAddress = ""; - if (httpServletRequest != null) { - remoteIpAddress = httpServletRequest.getRemoteAddr(); - } - Instant instant = Instant.now(); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX" ) - .withLocale( Locale.US ) - .withZone( ZoneId.systemDefault() ); - - String partnerName = headers.getFirst(MsoLogger.HEADER_FROM_APP_ID ); - if(partnerName == null || partnerName.isEmpty()) - partnerName="UNKNOWN"; - - MDC.put(MsoLogger.FROM_APP_ID,partnerName); - MDC.put(MsoLogger.SERVICE_NAME, containerRequest.getUriInfo().getPath()); - MDC.put(MsoLogger.BEGINTIME, formatter.format(instant)); - MDC.put(MsoLogger.REQUEST_ID,requestId); - MDC.put(MsoLogger.PARTNERNAME,partnerName); - MDC.put(MsoLogger.REMOTE_HOST, String.valueOf(remoteIpAddress)); - MDC.put(MsoLogger.STARTTIME, String.valueOf(System.currentTimeMillis())); - MDC.put(MsoLogger.CLIENT_ID, clientID); - } catch (Exception e) { - logger.warnSimple("Error in incoming JAX-RS Inteceptor", e); - } - } - - - @Override - public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) - throws IOException { - try { - Instant instant = Instant.now(); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX" ) - .withLocale( Locale.US ) - .withZone( ZoneId.systemDefault() ); - String startTime= MDC.get(MsoLogger.STARTTIME); - long elapsedTime; - try { - elapsedTime = System.currentTimeMillis() - Long.parseLong(startTime); - }catch(NumberFormatException e){ - elapsedTime = 0; - } - String statusCode; - if(Response.Status.Family.familyOf(responseContext.getStatus()).equals(Response.Status.Family.SUCCESSFUL)){ - statusCode=MsoLogger.COMPLETE; - }else{ - statusCode= MsoLogger.StatusCode.ERROR.toString(); - } - - MDC.put(MsoLogger.RESPONSEDESC,payloadMessage(responseContext)); - MDC.put(MsoLogger.STATUSCODE, statusCode); - MDC.put(MsoLogger.RESPONSECODE,String.valueOf(responseContext.getStatus())); - MDC.put(MsoLogger.TIMER, String.valueOf(elapsedTime)); - MDC.put(MsoLogger.ENDTIME,formatter.format(instant)); - logger.recordAuditEvent(); - } catch ( Exception e) { - logger.warnSimple("Error in outgoing JAX-RS Inteceptor", e); - } - } - - private String payloadMessage(ContainerResponseContext responseContext) throws IOException { - String message = new String(); - if (responseContext.hasEntity()) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Class<?> entityClass = responseContext.getEntityClass(); - Type entityType = responseContext.getEntityType(); - Annotation[] entityAnnotations = responseContext.getEntityAnnotations(); - MediaType mediaType = responseContext.getMediaType(); - @SuppressWarnings("unchecked") - MessageBodyWriter<Object> bodyWriter = (MessageBodyWriter<Object>) providers.getMessageBodyWriter(entityClass, - entityType, - entityAnnotations, - mediaType); - bodyWriter.writeTo(responseContext.getEntity(), - entityClass, - entityType, - entityAnnotations, - mediaType, - responseContext.getHeaders(), - baos); - message = message.concat(new String(baos.toByteArray())); - } - return message; - } + + protected static Logger logger = LoggerFactory.getLogger(JaxRsFilterLogging.class); + + @Context + private HttpServletRequest httpServletRequest; + + @Context + private Providers providers; + + @Autowired + ObjectMapper objectMapper; + + @Override + public void filter(ContainerRequestContext containerRequest) { + + try { + String clientID = null; + //check headers for request id + MultivaluedMap<String, String> headers = containerRequest.getHeaders(); + String requestId = findRequestId(headers); + containerRequest.setProperty("requestId", requestId); + if(headers.containsKey(MsoLogger.CLIENT_ID)){ + clientID = headers.getFirst(MsoLogger.CLIENT_ID); + }else{ + clientID = "UNKNOWN"; + headers.add(MsoLogger.CLIENT_ID, clientID); + } + + String remoteIpAddress = ""; + if (httpServletRequest != null) { + remoteIpAddress = httpServletRequest.getRemoteAddr(); + } + Instant instant = Instant.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX" ) + .withLocale( Locale.US ) + .withZone( ZoneId.systemDefault() ); + + String partnerName = headers.getFirst(MsoLogger.HEADER_FROM_APP_ID ); + if(partnerName == null || partnerName.isEmpty()) + partnerName="UNKNOWN"; + + MDC.put(MsoLogger.REQUEST_ID,requestId); + MDC.put(MsoLogger.INVOCATION_ID,requestId); + MDC.put(MsoLogger.FROM_APP_ID,partnerName); + MDC.put(MsoLogger.SERVICE_NAME, containerRequest.getUriInfo().getPath()); + MDC.put(MsoLogger.INVOCATION_ID, findInvocationId(headers)); + MDC.put(MsoLogger.STATUSCODE, MsoLogger.INPROGRESS); + MDC.put(MsoLogger.BEGINTIME, formatter.format(instant)); + MDC.put(MsoLogger.PARTNERNAME,partnerName); + MDC.put(MsoLogger.REMOTE_HOST, String.valueOf(remoteIpAddress)); + MDC.put(MsoLogger.STARTTIME, String.valueOf(System.currentTimeMillis())); + logger.debug(MsoLogger.ENTRY, "Entering."); + } catch (Exception e) { + logger.warn("Error in incoming JAX-RS Inteceptor", e); + } + } + + + private String findRequestId(MultivaluedMap<String, String> headers) { + String requestId = (String) headers.getFirst(MsoLogger.HEADER_REQUEST_ID ); + if(requestId == null || requestId.isEmpty()){ + if(headers.containsKey(MsoLogger.ONAP_REQUEST_ID)){ + requestId = headers.getFirst(MsoLogger.ONAP_REQUEST_ID); + }else if(headers.containsKey(MsoLogger.ECOMP_REQUEST_ID)){ + requestId = headers.getFirst(MsoLogger.ECOMP_REQUEST_ID); + }else{ + requestId = UUID.randomUUID().toString(); + } + } + return requestId; + } + + private String findInvocationId(MultivaluedMap<String, String> headers) { + String invocationId = (String) headers.getFirst(MsoLogger.INVOCATION_ID_HEADER ); + if(invocationId == null || invocationId.isEmpty()) + invocationId =UUID.randomUUID().toString(); + return invocationId; + } + + @Override + public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) + throws IOException { + try { + Instant instant = Instant.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX" ) + .withLocale( Locale.US ) + .withZone( ZoneId.systemDefault() ); + String startTime= MDC.get(MsoLogger.STARTTIME); + long elapsedTime; + try { + elapsedTime = System.currentTimeMillis() - Long.parseLong(startTime); + }catch(NumberFormatException e){ + elapsedTime = 0; + } + String statusCode; + if(Response.Status.Family.familyOf(responseContext.getStatus()).equals(Response.Status.Family.SUCCESSFUL)){ + statusCode=MsoLogger.COMPLETE; + }else{ + statusCode= MsoLogger.StatusCode.ERROR.toString(); + } + + MDC.put(MsoLogger.RESPONSEDESC,payloadMessage(responseContext)); + MDC.put(MsoLogger.STATUSCODE, statusCode); + MDC.put(MsoLogger.RESPONSECODE,String.valueOf(responseContext.getStatus())); + MDC.put(MsoLogger.TIMER, String.valueOf(elapsedTime)); + MDC.put(MsoLogger.ENDTIME,formatter.format(instant)); + logger.debug(MsoLogger.EXIT, "Exiting."); + } catch ( Exception e) { + logger.warn("Error in outgoing JAX-RS Inteceptor", e); + } + } + + private String payloadMessage(ContainerResponseContext responseContext) throws IOException { + String message = new String(); + if (responseContext.hasEntity()) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + Class<?> entityClass = responseContext.getEntityClass(); + Type entityType = responseContext.getEntityType(); + Annotation[] entityAnnotations = responseContext.getEntityAnnotations(); + MediaType mediaType = responseContext.getMediaType(); + @SuppressWarnings("unchecked") + MessageBodyWriter<Object> bodyWriter = (MessageBodyWriter<Object>) providers.getMessageBodyWriter(entityClass, + entityType, + entityAnnotations, + mediaType); + bodyWriter.writeTo(responseContext.getEntity(), + entityClass, + entityType, + entityAnnotations, + mediaType, + responseContext.getHeaders(), + baos); + message = message.concat(new String(baos.toByteArray())); + } + return message; + } } |