-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- timestamp field now ts in compliant format - request/response logs now contain an explicit path to enable filtering - includes request_id field - uses incoming x-request-id field if present else generates transaction count Addresses: #12 and part of epimorphics/hmlr-ansible-deployment#76
- Loading branch information
Showing
6 changed files
with
111 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/com/epimorphics/standardReports/webapi/LogRequestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.epimorphics.standardReports.webapi; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.ws.rs.core.Response.Status; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.epimorphics.appbase.webapi.WebApiException; | ||
import com.epimorphics.util.NameUtils; | ||
import org.slf4j.MDC; | ||
|
||
/** | ||
* A Filter that can be added to filter chain to log all incoming requests and | ||
* the corresponding response (with response code and execution time). Assigns a | ||
* simple request number to each request and includes that in the response headers | ||
* for diagnosis. Not robust against restarts but easier to work with than UUIDs. | ||
*/ | ||
public class LogRequestFilter implements Filter { | ||
public static final String TRANSACTION_ATTRIBUTE = "transaction"; | ||
public static final String START_TIME_ATTRIBUTE = "startTime"; | ||
public static final String RESPONSE_ID_HEADER = "X-Response-ID"; | ||
public static final String REQUEST_ID_HEADER = "X-Request-ID"; | ||
|
||
static final Logger log = LoggerFactory.getLogger( LogRequestFilter.class ); | ||
|
||
protected static AtomicLong transactionCount = new AtomicLong(0); | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) throws ServletException { | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, | ||
FilterChain chain) throws IOException, ServletException { | ||
HttpServletRequest httpRequest = (HttpServletRequest)request; | ||
HttpServletResponse httpResponse = (HttpServletResponse)response; | ||
String path = httpRequest.getRequestURI(); | ||
String query = httpRequest.getQueryString(); | ||
String requestID = httpRequest.getHeader(REQUEST_ID_HEADER); | ||
if (requestID == null || requestID.isEmpty()) { | ||
requestID = Long.toString(transactionCount.incrementAndGet()); | ||
} | ||
MDC.put("request_id", requestID); | ||
long start = System.currentTimeMillis(); | ||
|
||
String fullpath = path + (query == null ? "" : ("?" + query)); | ||
MDC.put("path", fullpath); | ||
log.info( String.format("Request [%s] : %s", requestID, fullpath) ); | ||
httpResponse.addHeader(RESPONSE_ID_HEADER, requestID); | ||
chain.doFilter(request, response); | ||
log.info( String.format("Response [%s] : %d (%s)", requestID, httpResponse.getStatus(), | ||
NameUtils.formatDuration(System.currentTimeMillis() - start)) ); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
} | ||
|
||
public static WebApiException badRequestException(String message) { | ||
log.warn("Bad request: " + message); | ||
return new WebApiException(Status.BAD_REQUEST, message); | ||
} | ||
|
||
public static WebApiException serverErrorException(String message) { | ||
log.warn("Server error: " + message); | ||
return new WebApiException(Status.INTERNAL_SERVER_ERROR, message); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> | ||
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout"> | ||
<jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter"/> | ||
<appendLineSeparator>true</appendLineSeparator> | ||
<timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSZ</timestampFormat> | ||
</layout> | ||
<appender name="jsonstdout" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder class="net.logstash.logback.encoder.LogstashEncoder"> | ||
<excludeMdcKeyName>X-B3-SpanId</excludeMdcKeyName> | ||
<excludeMdcKeyName>X-B3-TraceId</excludeMdcKeyName> | ||
<excludeMdcKeyName>X-Span-Export</excludeMdcKeyName> | ||
<excludeMdcKeyName>spanExportable</excludeMdcKeyName> | ||
<timeZone>UTC</timeZone> | ||
<fieldNames> | ||
<timestamp>ts</timestamp> | ||
<version>version</version> | ||
</fieldNames> | ||
<throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter"> | ||
<maxDepthPerThrowable>30</maxDepthPerThrowable> | ||
<maxLength>2048</maxLength> | ||
<shortenedClassNameLength>20</shortenedClassNameLength> | ||
<rootCauseFirst>true</rootCauseFirst> | ||
</throwableConverter> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="info"> | ||
<appender-ref ref="STDOUT" /> | ||
<appender-ref ref="jsonstdout" /> | ||
</root> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters