Skip to content

Commit

Permalink
Adding request_id header to the logs through MDC Context
Browse files Browse the repository at this point in the history
Signed-off-by: Sun Tan <sutan@redhat.com>
  • Loading branch information
sunix committed Nov 17, 2017
1 parent 4e76b72 commit 8021e02
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2012-2017 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.api.deploy;

import java.io.IOException;
import javax.inject.Singleton;
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 org.slf4j.MDC;

@Singleton
public class RequestIdLoggerFilter implements Filter {

private static final String REQUEST_ID_HEADER = "X-Request-Id";
private static final String REQUEST_ID_MDC_KEY = "req_id";

@Override
public void init(FilterConfig filterConfig) throws ServletException {}

@Override
public final void doFilter(
ServletRequest request, ServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
final HttpServletRequest httpRequest = (HttpServletRequest) request;
String requestId = httpRequest.getHeader(REQUEST_ID_HEADER);
if (requestId != null) {
MDC.put(REQUEST_ID_MDC_KEY, requestId);
}
filterChain.doFilter(request, response);
}

@Override
public void destroy() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected void configureServlets() {
bind(CorsFilter.class).in(Singleton.class);

filter("/*").through(CorsFilter.class, corsFilterParams);
filter("/*").through(RequestIdLoggerFilter.class);

serveRegex("^/api((?!(/(ws|eventbus)($|/.*)))/.*)").with(GuiceEverrestServlet.class);
install(new org.eclipse.che.swagger.deploy.BasicSwaggerConfigurationModule());
Expand Down

0 comments on commit 8021e02

Please sign in to comment.