-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding request_id header to the logs through MDC Context
Signed-off-by: Sun Tan <sutan@redhat.com>
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...assembly-wsmaster-war/src/main/java/org/eclipse/che/api/deploy/RequestIdLoggerFilter.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,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() {} | ||
} |
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