FINERACT-1656 Correlation ID propagation and configuration
This commit is contained in:
committed by
Arnold Gálovics
parent
1db8ef46fc
commit
8a55d5807a
+50
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -34,6 +34,8 @@ public class FineractProperties {
|
||||
|
||||
private FineractModeProperties mode;
|
||||
|
||||
private FineractCorrelationProperties correlation;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public static class FineractTenantProperties {
|
||||
@@ -62,4 +64,12 @@ public class FineractProperties {
|
||||
return readEnabled && !writeEnabled && !batchWorkerEnabled && !batchManagerEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public static class FineractCorrelationProperties {
|
||||
|
||||
private boolean enabled;
|
||||
private String headerName;
|
||||
}
|
||||
}
|
||||
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* 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.filters;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.web.filter.OncePerRequestFilter;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class CorrelationHeaderFilter extends OncePerRequestFilter {
|
||||
|
||||
private String correlationIdHeader;
|
||||
|
||||
public static final String correlationIdKey = "correlationId";
|
||||
|
||||
@Autowired
|
||||
public CorrelationHeaderFilter(Environment env) {
|
||||
correlationIdHeader = env.getRequiredProperty("fineract.logging.http.correlation-id.header-name");
|
||||
}
|
||||
|
||||
@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);
|
||||
filterChain.doFilter(request, response);
|
||||
} finally {
|
||||
MDC.remove(correlationIdKey);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getCurrentValue() {
|
||||
return MDC.get(correlationIdKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isAsyncDispatch(final HttpServletRequest request) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldNotFilterErrorDispatch() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* 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.service;
|
||||
|
||||
import java.util.Map;
|
||||
import org.slf4j.MDC;
|
||||
import org.slf4j.spi.MDCAdapter;
|
||||
|
||||
public class MdcAdapter implements MDCAdapter {
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
+4
@@ -25,4 +25,8 @@ public final class LogParameterEscapeUtil {
|
||||
public static String escapeLogParameter(String logParameter) {
|
||||
return logParameter.replaceAll("[\n\r\t]", "_");
|
||||
}
|
||||
|
||||
public static String escapeLogMDCParameter(String logParameter) {
|
||||
return logParameter.replaceAll("[\r\n]", "");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user