Correlation ID support implementation

This commit is contained in:
Arnold Galovics
2022-07-13 21:29:55 +02:00
committed by Arnold Gálovics
parent 8a55d5807a
commit 4f832f9481
12 changed files with 165 additions and 193 deletions
@@ -81,7 +81,7 @@ public class CommandHandlerProvider implements ApplicationContextAware {
final String[] commandHandlerBeans = this.applicationContext.getBeanNamesForAnnotation(CommandType.class);
if (ArrayUtils.isNotEmpty(commandHandlerBeans)) {
for (final String commandHandlerName : commandHandlerBeans) {
log.info("Register command handler '{}' ...", commandHandlerName);
log.debug("Register command handler '{}' ...", commandHandlerName);
final CommandType commandType = this.applicationContext.findAnnotationOnBean(commandHandlerName, CommandType.class);
try {
this.registeredHandlers.put(commandType.entity() + "|" + commandType.action(), commandHandlerName);
@@ -1,50 +0,0 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.core.config;
import java.util.Arrays;
import org.apache.fineract.infrastructure.core.filters.CorrelationHeaderFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@Configuration
@ConditionalOnProperty("fineract.logging.http.correlation-id.enabled")
public class CorrelationIdConfig implements EnvironmentAware {
private Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Bean
public FilterRegistrationBean<CorrelationHeaderFilter> correlationHeaderFilter() {
FilterRegistrationBean<CorrelationHeaderFilter> filterRegBean = new FilterRegistrationBean<CorrelationHeaderFilter>();
filterRegBean.setFilter(new CorrelationHeaderFilter(environment));
filterRegBean.setUrlPatterns(Arrays.asList("/*"));
return filterRegBean;
}
}
@@ -26,42 +26,49 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.fineract.infrastructure.core.config.FineractProperties;
import org.apache.fineract.infrastructure.core.service.MDCWrapper;
import org.apache.fineract.infrastructure.security.utils.LogParameterEscapeUtil;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
@RequiredArgsConstructor
@Slf4j
@Component
public class CorrelationHeaderFilter extends OncePerRequestFilter {
private String correlationIdHeader;
public static final String CORRELATION_ID_KEY = "correlationId";
public static final String correlationIdKey = "correlationId";
@Autowired
public CorrelationHeaderFilter(Environment env) {
correlationIdHeader = env.getRequiredProperty("fineract.logging.http.correlation-id.header-name");
}
private final FineractProperties fineractProperties;
private final MDCWrapper mdcWrapper;
@Override
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain)
throws IOException, ServletException {
try {
final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
String currentCorrId = httpServletRequest.getHeader(correlationIdHeader);
log.debug("Found correlationId in Header : {}", LogParameterEscapeUtil.escapeLogMDCParameter(currentCorrId));
MDC.put(correlationIdKey, currentCorrId);
FineractProperties.FineractCorrelationProperties correlationProperties = fineractProperties.getCorrelation();
if (correlationProperties.isEnabled()) {
handleCorrelations(request, response, filterChain, correlationProperties);
} else {
filterChain.doFilter(request, response);
} finally {
MDC.remove(correlationIdKey);
}
}
public static String getCurrentValue() {
return MDC.get(correlationIdKey);
private void handleCorrelations(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain,
FineractProperties.FineractCorrelationProperties correlationProperties) throws IOException, ServletException {
try {
String correlationHeaderName = correlationProperties.getHeaderName();
String correlationId = request.getHeader(correlationHeaderName);
if (StringUtils.isNotBlank(correlationId)) {
String escapedCorrelationId = LogParameterEscapeUtil.escapeLogMDCParameter(correlationId);
log.debug("Found correlationId in header : {}", escapedCorrelationId);
mdcWrapper.put(CORRELATION_ID_KEY, escapedCorrelationId);
}
filterChain.doFilter(request, response);
} finally {
mdcWrapper.remove(CORRELATION_ID_KEY);
}
}
@Override
@@ -19,39 +19,17 @@
package org.apache.fineract.infrastructure.core.service;
import java.util.Map;
import org.slf4j.MDC;
import org.slf4j.spi.MDCAdapter;
import org.springframework.stereotype.Component;
public class MdcAdapter implements MDCAdapter {
@Component
public class MDCWrapper {
@Override
public void put(String key, String val) {
MDC.put(key, val);
}
@Override
public String get(String key) {
return MDC.get(key);
}
@Override
public void remove(String key) {
MDC.remove(key);
}
@Override
public void clear() {
MDC.clear();
}
@Override
public Map<String, String> getCopyOfContextMap() {
return MDC.getCopyOfContextMap();
}
@Override
public void setContextMap(Map<String, String> map) {
MDC.setContextMap(map);
}
}
@@ -29,7 +29,6 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.ext.Provider;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.infrastructure.core.config.FineractProperties;
@@ -38,7 +37,6 @@ import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
@Provider
@Component
@RequiredArgsConstructor
public class FineractInstanceModeApiFilter extends OncePerRequestFilter {
@@ -40,11 +40,11 @@ fineract.mode.write-enabled=${FINERACT_MODE_WRITE_ENABLED:true}
fineract.mode.batch-worker-enabled=${FINERACT_MODE_BATCH_WORKER_ENABLED:true}
fineract.mode.batch-manager-enabled=${FINERACT_MODE_BATCH_MANAGER_ENABLED:true}
fineract.logging.http.correlation-id.enabled=${FINERACT_LOGGING_HTTP_CORRELATION_ID_ENABLED:false}
fineract.logging.http.correlation-id.header-name=${FINERACT_LOGGING_HTTP_CORRELATION_ID_HEADER_NAME:X-Correlation-ID}
fineract.correlation.enabled=${FINERACT_LOGGING_HTTP_CORRELATION_ID_ENABLED:false}
fineract.correlation.header-name=${FINERACT_LOGGING_HTTP_CORRELATION_ID_HEADER_NAME:X-Correlation-ID}
# Logging pattern for the console
logging.pattern.console=${CONSOLE_LOG_PATTERN:%d{yyyy-MM-dd HH\:mm\:ss.SSS} %thread %replace([%X{correlationId}]){'\\[\\]', ''} [%-5level] %class{0} - %msg%n}
logging.pattern.console=${CONSOLE_LOG_PATTERN:%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(%replace([%X{correlationId}]){'\\[\\]', ''}) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}}
management.health.jms.enabled=false