Add transaction id to http request

This commit is contained in:
René Pfeuffer
2018-10-25 12:34:32 +02:00
parent a36768fbb3
commit bda5b41271
5 changed files with 16 additions and 11 deletions

View File

@@ -2,6 +2,7 @@ package sonia.scm.api.v2.resources;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import org.slf4j.MDC;
import sonia.scm.ContextEntry;
import sonia.scm.NotFoundException;
@@ -29,6 +30,6 @@ public class ErrorDto {
}
static ErrorDto from(NotFoundException notFoundException) {
return new ErrorDto("todo", "todo", notFoundException.getContext(), notFoundException.getMessage());
return new ErrorDto(MDC.get("transaction_id"), "todo", notFoundException.getContext(), notFoundException.getMessage());
}
}

View File

@@ -41,6 +41,8 @@ import org.apache.shiro.subject.Subject;
import org.slf4j.MDC;
import sonia.scm.SCMContext;
import sonia.scm.security.DefaultKeyGenerator;
import sonia.scm.security.KeyGenerator;
import sonia.scm.web.filter.HttpFilter;
//~--- JDK imports ------------------------------------------------------------
@@ -61,27 +63,26 @@ import sonia.scm.Priority;
@WebElement(Filters.PATTERN_ALL)
public class MDCFilter extends HttpFilter
{
private static final DefaultKeyGenerator TRANSACTION_KEY_GENERATOR = new DefaultKeyGenerator();
/** Field description */
@VisibleForTesting
static final String MDC_CLIENT_HOST = "client_host";
/** Field description */
@VisibleForTesting
static final String MDC_CLIENT_IP = "client_ip";
/** url of the current request */
@VisibleForTesting
static final String MDC_REQUEST_URI = "request_uri";
/** request method */
@VisibleForTesting
static final String MDC_REQUEST_METHOD = "request_method";
/** Field description */
@VisibleForTesting
static final String MDC_USERNAME = "username";
@VisibleForTesting
static final String MDC_TRANSACTION_ID = "transaction_id";
//~--- methods --------------------------------------------------------------
/**
@@ -105,6 +106,7 @@ public class MDCFilter extends HttpFilter
MDC.put(MDC_CLIENT_HOST, request.getRemoteHost());
MDC.put(MDC_REQUEST_METHOD, request.getMethod());
MDC.put(MDC_REQUEST_URI, request.getRequestURI());
MDC.put(MDC_TRANSACTION_ID, TRANSACTION_KEY_GENERATOR.createKey());
try
{
@@ -117,6 +119,7 @@ public class MDCFilter extends HttpFilter
MDC.remove(MDC_CLIENT_HOST);
MDC.remove(MDC_REQUEST_METHOD);
MDC.remove(MDC_REQUEST_URI);
MDC.remove(MDC_TRANSACTION_ID);
}
}

View File

@@ -46,7 +46,7 @@
<!-- encoders are by default assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%-10X{transaction_id}] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

View File

@@ -61,14 +61,14 @@
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%-10X{transaction_id}] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%-10X{transaction_id}] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

View File

@@ -101,6 +101,7 @@ public class MDCFilterTest extends AbstractTestBase {
assertEquals("127.0.0.1", chain.ctx.get(MDCFilter.MDC_CLIENT_IP));
assertEquals("localhost", chain.ctx.get(MDCFilter.MDC_CLIENT_HOST));
assertEquals("GET", chain.ctx.get(MDCFilter.MDC_REQUEST_METHOD));
assertNotNull(chain.ctx.get(MDCFilter.MDC_TRANSACTION_ID));
}
/**