Created
September 2, 2022 06:41
-
-
Save sophea/44662d464b2af5bd3d0e415caac6d0ae to your computer and use it in GitHub Desktop.
tracer fitler
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
package com.manulife.ap.config; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import java.util.List; | |
import javax.servlet.FilterChain; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import lombok.extern.slf4j.Slf4j; | |
import org.springframework.core.annotation.Order; | |
import org.springframework.http.HttpMethod; | |
import org.springframework.stereotype.Component; | |
import org.springframework.web.filter.OncePerRequestFilter; | |
/** | |
* It is used for IntelliJ IDEA By Manulife Digital Cambodia team. | |
* | |
* @author Sophea Mak | |
* @since September-2022 | |
*/ | |
@Slf4j | |
@Component | |
@Order(-1) | |
public class TracerRequestFilter extends OncePerRequestFilter { | |
private static final List<String> IGNORE_URI = | |
Arrays.asList("/favicon.ico", "/actuator", "/actuator/health"); | |
public TracerRequestFilter() {} | |
/** | |
* doFilterInternal. | |
* | |
* @param request http request | |
* @param response http response | |
* @param chain filter | |
* @throws ServletException servlet exception | |
* @throws IOException io exception | |
*/ | |
@Override | |
protected void doFilterInternal( | |
final HttpServletRequest request, final HttpServletResponse response, FilterChain chain) | |
throws ServletException, IOException { | |
final String path = request.getRequestURI(); | |
final String method = getMethod(request); | |
if (!ignoreUri(path)) { | |
final long start = System.currentTimeMillis(); | |
log.info("start:http:[{}]:{}", method, path); | |
try { | |
chain.doFilter(request, response); | |
} finally { | |
log.info( | |
"stop:http:[{}]:[{}}]:response-http-status-code:[{}]:{}[ms]", | |
method, | |
path, | |
response.getStatus(), | |
System.currentTimeMillis() - start); | |
} | |
} else { | |
chain.doFilter(request, response); | |
} | |
} | |
private boolean ignoreUri(String uri) { | |
return IGNORE_URI.stream().anyMatch(uri::startsWith); | |
} | |
private String getMethod(HttpServletRequest request) { | |
return HttpMethod.valueOf(request.getMethod()).name(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment