Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.yammer.dropwizard.servlets;
 
 
 import javax.servlet.*;
import static com.yammer.dropwizard.util.Servlets.getFullUrl;

A servlet filter which logs the methods and URIs of requests which take longer than a given duration of time to complete.
@SuppressWarnings("UnusedDeclaration")
public class SlowRequestFilter implements Filter {
    private static final Logger LOGGER = LoggerFactory.getLogger(SlowRequestFilter.class);
    private final long threshold;

    
Creates a filter which logs requests which take longer than 1 second.
    public SlowRequestFilter() {
        this(Duration.seconds(1));
    }

    
Creates a filter which logs requests which take longer than the given duration.

Parameters:
threshold the threshold for considering a request slow
    public SlowRequestFilter(Duration threshold) {
        this. = threshold.toNanoseconds();
    }
    @Override
    public void init(FilterConfig filterConfigthrows ServletException { /* unused */ }
    @Override
    public void destroy() { /* unused */ }
    @Override
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chainthrows IOExceptionServletException {
        final HttpServletRequest req = (HttpServletRequestrequest;
        final long startTime = System.nanoTime();
        try {
            chain.doFilter(requestresponse);
        } finally {
            final long elapsedMS = ..toMillis(System.nanoTime() - startTime);
            if (elapsedMS >= ) {
                .warn("Slow request: {} {} ({}ms)",
                            req.getMethod(),
                            getFullUrl(req), elapsedMS);
            }
        }
    }
New to GrepCode? Check out our FAQ X