Commit f907a3e0 authored by Hema Vishnu Pola [Microsoft]'s avatar Hema Vishnu Pola [Microsoft]
Browse files

Merge branch 'kibattul/mdc-filter' into 'master'

Added mdc context on start of request

See merge request !18
parents f0fa8b37 9795bf2d
Pipeline #6423 passed with stages
in 2 minutes and 15 seconds
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
<groupId>org.opengroup.osdu</groupId> <groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId> <artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>0.0.18</version> <version>0.0.19</version>
<name>core-lib-azure</name> <name>core-lib-azure</name>
<properties> <properties>
......
package org.opengroup.osdu.azure.filters;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* MDC Filter for logging.
*/
@Component
@ConditionalOnProperty(value = "logging.mdccontext.enabled", havingValue = "true", matchIfMissing = false)
public class Slf4jMDCFilter implements Filter {
@Autowired
private DpsHeaders dpsHeaders;
/**
* Filter logic.
* @param servletRequest Request object.
* @param servletResponse Response object.
* @param filterChain Filter Chain object.
* @throws IOException
* @throws ServletException
*/
@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
final FilterChain filterChain) throws IOException, ServletException {
MDC.setContextMap(getContextMap());
filterChain.doFilter(servletRequest, servletResponse);
MDC.clear();
}
/**
* Method to create context map for mdc.
* @return Context map.
*/
private Map<String, String> getContextMap() {
final Map<String, String> contextMap = new HashMap<>();
contextMap.put(DpsHeaders.CORRELATION_ID, dpsHeaders.getCorrelationId());
contextMap.put(DpsHeaders.DATA_PARTITION_ID, dpsHeaders.getPartitionId());
return contextMap;
}
}
...@@ -18,12 +18,12 @@ ...@@ -18,12 +18,12 @@
<Configuration packages="com.microsoft.applicationinsights.log4j.v2"> <Configuration packages="com.microsoft.applicationinsights.log4j.v2">
<Properties> <Properties>
<Property name="LOG_PATTERN"> <Property name="LOG_PATTERN">
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} correlation-id=%X{correlation-id} data-partition-id=%X{data-partition-id}: %m%n%ex
</Property> </Property>
</Properties> </Properties>
<Appenders> <Appenders>
<Console name="Console" target="SYSTEM_OUT"> <Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex"/> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} correlation-id=%X{correlation-id} data-partition-id=%X{data-partition-id}: %m%n%ex"/>
</Console> </Console>
<ApplicationInsightsAppender name="aiAppender"> <ApplicationInsightsAppender name="aiAppender">
</ApplicationInsightsAppender> </ApplicationInsightsAppender>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment