Skip to content

Commit

Permalink
Merge e44c04e into 19df38b
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed May 5, 2019
2 parents 19df38b + e44c04e commit 83abbce
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ before_install:
install:
- git clone https://github.com/FAForever/faf-stack.git faf-stack
&& pushd faf-stack
&& git checkout 572bef1c
&& git checkout 0eb5b8f610e5d5ba6bccc6eebca31148414135bd
&& cp -r config.template config
&& ./scripts/init-db.sh
&& popd
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.faforever.api.error;

import com.faforever.api.logging.RequestIdFilter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.slf4j.MDC;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;
Expand All @@ -16,6 +18,7 @@ public void serialize(Error error, JsonGenerator gen, SerializerProvider seriali

gen.writeStartObject();
gen.writeNumberField("code", errorCode.getCode());
gen.writeStringField("requestId", MDC.get(RequestIdFilter.REQUEST_ID_KEY));
gen.writeStringField("title", MessageFormat.format(errorCode.getTitle(), error.getArgs()));
gen.writeStringField("detail", MessageFormat.format(errorCode.getDetail(), error.getArgs()));
gen.writeObjectField("args", error.getArgs());
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/faforever/api/error/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.faforever.api.error;

import com.faforever.api.logging.RequestIdFilter;
import lombok.Data;
import org.slf4j.MDC;

import java.util.ArrayList;

@Data
public class ErrorResponse {
private final String requestId;

private final ArrayList<ErrorResult> errors = new ArrayList<>();

public ErrorResponse() {
requestId = MDC.get(RequestIdFilter.REQUEST_ID_KEY);
}

public ErrorResponse addError(ErrorResult newError) {
errors.add(newError);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ErrorResponse processValidationException(ValidationException ex) {
@ExceptionHandler(NotFoundApiException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
public ErrorResponse processValidationException(NotFoundApiException ex) {
public ErrorResponse processNotFoundException(NotFoundApiException ex) {
log.debug("Entity could not be found", ex);
return createResponseFromApiException(ex, HttpStatus.NOT_FOUND);
}
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/com/faforever/api/logging/RequestIdFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.faforever.api.logging;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.slf4j.MDC;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

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 java.io.IOException;
import java.util.UUID;

/**
* This logging interceptor sets up the MDC with the external request id (if present) or a random UUID otherwise.
*/
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
@Slf4j
public class RequestIdFilter implements Filter {

/**
* Use this key in your log pattern and for fetching they request id from MDC.
*/
public static final String REQUEST_ID_KEY = "requestId";
/**
* The default request id header. Change this if you configured a different key.
*/
private static final String REQUEST_ID_HEADER = "X-Request-ID";

@Override
public void init(FilterConfig filterConfig) {
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String requestId = null;

if (request instanceof HttpServletRequest) {
HttpServletRequest httpRequest = (HttpServletRequest) request;
requestId = httpRequest.getHeader(REQUEST_ID_HEADER);
}

if (StringUtils.isBlank(requestId)) {
requestId = UUID.randomUUID().toString();
log.trace("No header value found for key '{}'. Fallback to random UUID: {}", REQUEST_ID_HEADER, requestId);
} else {
log.trace("Request id read from header key '{}': {}", REQUEST_ID_HEADER, requestId);
}

MDC.put(REQUEST_ID_KEY, requestId);
log.trace("Adding request id key '{}' to logging context", REQUEST_ID_KEY);

try {
chain.doFilter(request, response);
} finally {
log.trace("Removing request id key '{}' from logging context", REQUEST_ID_KEY);
MDC.remove(REQUEST_ID_KEY);
}
}

@Override
public void destroy() {
}

}
4 changes: 3 additions & 1 deletion src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ faf-api:
challonge:
key: ${CHALLONGE_KEY:}
database:
schema-version: ${DATABASE_SCHEMA_VERSION:66}
schema-version: ${DATABASE_SCHEMA_VERSION:67}
mautic:
base-url: ${MAUTIC_BASE_URL:false}
client-id: ${MAUTIC_CLIENT_ID:false}
Expand Down Expand Up @@ -78,5 +78,7 @@ management:
exposure:
include: '*'
logging:
pattern:
console: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%36.36X{requestId:- no request context}]){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx"
level:
com.faforever: ${LOG_LEVEL:info}

0 comments on commit 83abbce

Please sign in to comment.