Skip to content

Commit

Permalink
Merge pull request #41 from SAP/fix-correlation_id
Browse files Browse the repository at this point in the history
Fix correlation-id in cf-java-logging-support-servlet
  • Loading branch information
KarstenSchnitter authored Jul 29, 2019
2 parents c5519ed + bf16332 commit 32bba94
Show file tree
Hide file tree
Showing 19 changed files with 420 additions and 91 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ All in all, you should do the following:
And
4. Adjust your logging configuration accordingly.

Let's say you want to make use of the *servlet filter* feature, then you need to add the following dependency to your POM with property `cf-logging-version` referring to the latest nexus version (currently `3.0.2`):
Let's say you want to make use of the *servlet filter* feature, then you need to add the following dependency to your POM with property `cf-logging-version` referring to the latest nexus version (currently `3.0.3`):

```xml
<properties>
<cf-logging-version>3.0.2</cf-logging-version>
<cf-logging-version>3.0.3</cf-logging-version>
</properties>
```

Expand Down
2 changes: 1 addition & 1 deletion cf-java-logging-support-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<parent>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-parent</artifactId>
<version>3.0.2</version>
<version>3.0.3</version>
<relativePath>../pom.xml</relativePath>
</parent>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void run() {
"\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + //
"\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.generateException(StacktraceGenerator.java:X)\"," + //
"\"\\tat com.sap.hcp.cf.logging.common.converter.DefaultStacktraceConverterTest$X.run(DefaultStacktraceConverterTest.java:X)\"," + //
"\"\\tat java.lang.Thread.run(Thread.java:X)\"]";
"\"\\tat " + getThreadClassName() + ".run(Thread.java:X)\"]";
Assert.assertEquals(expectedString, stringBuilder.toString().replaceAll(":\\d+\\)", ":X)").//
replaceAll("\\$\\d", "\\$X"));
}
Expand All @@ -99,7 +99,7 @@ public void run() {
});
thread.start();
thread.join();
expectedString = "[\"-------- STACK TRACE TRUNCATED --------\"," + //
expectedString = "[\"-------- STACK TRACE TRUNCATED --------\"," + //
"\"java.lang.IllegalArgumentException: too long\"," + //
"\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + //
"\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + //
Expand All @@ -115,9 +115,17 @@ public void run() {
"\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + //
"\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.generateException(StacktraceGenerator.java:X)\"," + //
"\"\\tat com.sap.hcp.cf.logging.common.converter.DefaultStacktraceConverterTest$X.run(DefaultStacktraceConverterTest.java:X)\"," + //
"\"\\tat java.lang.Thread.run(Thread.java:X)\"]";
"\"\\tat " + getThreadClassName() + ".run(Thread.java:X)\"]";
Assert.assertEquals(expectedString, stringBuilder.toString().replaceAll(":\\d+\\)", ":X)").//
replaceAll("\\$\\d", "\\$X"));
}

// This method is required to account for the module path in stacktraces
// after Java 9
private String getThreadClassName() {
String stackTraceElement = Thread.currentThread().getStackTrace()[0].toString();
int i = stackTraceElement.indexOf("Thread");
return stackTraceElement.subSequence(0, i) + Thread.class.getSimpleName();
}

}
159 changes: 92 additions & 67 deletions cf-java-logging-support-jersey/pom.xml
Original file line number Diff line number Diff line change
@@ -1,78 +1,103 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>cf-java-logging-support-jersey</artifactId>
<packaging>jar</packaging>
<artifactId>cf-java-logging-support-jersey</artifactId>
<packaging>jar</packaging>

<parent>
<relativePath>../pom.xml</relativePath>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-parent</artifactId>
<version>3.0.2</version>
</parent>
<parent>
<relativePath>../pom.xml</relativePath>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-parent</artifactId>
<version>3.0.3</version>
</parent>

<name>cf-java-logging-support-jersey</name>
<name>cf-java-logging-support-jersey</name>

<properties>
<jersey.version>2.22.1</jersey.version>
</properties>
<properties>
<jersey.version>2.22.1</jersey.version>
</properties>

<dependencies>
<!-- Jersey - RESTful Web service -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependencies>
<!-- Jersey - RESTful Web service -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-core</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-core</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>

<!-- unit test related -->
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<!-- unit test related -->
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<!-- we need our log4j2 implementation for testing! -->
<dependency>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-log4j2</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>
<!-- we need our log4j2 implementation for testing! -->
<dependency>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-log4j2</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2 changes: 1 addition & 1 deletion cf-java-logging-support-log4j2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<relativePath>../pom.xml</relativePath>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-parent</artifactId>
<version>3.0.2</version>
<version>3.0.3</version>
</parent>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion cf-java-logging-support-logback/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<relativePath>../pom.xml</relativePath>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-parent</artifactId>
<version>3.0.2</version>
<version>3.0.3</version>
</parent>

<dependencies>
Expand Down
50 changes: 43 additions & 7 deletions cf-java-logging-support-servlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,34 @@
<artifactId>cf-java-logging-support-servlet</artifactId>
<packaging>jar</packaging>

<name>cf-java-logging-support-servlet</name>
<parent>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-parent</artifactId>
<version>3.0.2</version>
<relativePath>../pom.xml</relativePath>
</parent>
<name>cf-java-logging-support-servlet</name>
<parent>
<groupId>com.sap.hcp.cf.logging</groupId>
<artifactId>cf-java-logging-support-parent</artifactId>
<version>3.0.3</version>
<relativePath>../pom.xml</relativePath>
</parent>

<properties>
<servlet.api.version>3.0.1</servlet.api.version>
</properties>

<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.8</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<!-- servlet api -->
<dependency>
<groupId>javax.servlet</groupId>
Expand Down Expand Up @@ -54,5 +69,26 @@
<artifactId>jackson-databind</artifactId>
<version>2.9.9.1</version>
</dependency>

<!-- testing -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.19.v20190610</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.4.19.v20190610</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.9</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sap.hcp.cf.logging.common.LogContext;
import com.sap.hcp.cf.logging.common.request.HttpHeader;

public final class HttpHeaderUtilities {
Expand All @@ -25,14 +26,26 @@ public static String getHeaderValue(HttpServletRequest httpRequest, HttpHeader h
return value;
}
}
String contextFieldValue = getLogContextFieldValue(header);
if (contextFieldValue != null) {
return contextFieldValue;
}
return defaultValue;
}


private static String getHeaderValueInternal(HttpServletRequest httpRequest, HttpHeader header) {
String headerName = header.getName();
return httpRequest.getHeader(headerName);
}

private static String getLogContextFieldValue(HttpHeader header) {
if (header.getField() == null) {
return null;
}
return LogContext.get(header.getField());
}

public static String getHeaderValue(HttpServletResponse httpResponse, HttpHeader header) {
return getHeaderValue(httpResponse, header, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ private void doFilterRequest(HttpServletRequest httpRequest, HttpServletResponse

RequestRecord rr = requestRecordFactory.create(httpRequest);
httpRequest.setAttribute(MDC.class.getName(), MDC.getCopyOfContextMap());

if (!httpResponse.isCommitted() && httpResponse.getHeader(HttpHeaders.CORRELATION_ID.getName()) == null) {
httpResponse.setHeader(HttpHeaders.CORRELATION_ID.getName(), LogContext.getCorrelationId());
}

/*
* -- we essentially do three things here: -- a) we create a log
Expand Down Expand Up @@ -133,6 +137,7 @@ private void doFilterRequest(HttpServletRequest httpRequest, HttpServletResponse
if (dynamicLogLevelProcessor != null) {
dynamicLogLevelProcessor.removeDynamicLogLevelFromMDC();
}
LogContext.resetContextFields();
}
}

Expand Down
Loading

0 comments on commit 32bba94

Please sign in to comment.