-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The threshold is 1s by default and can be configured.
- Loading branch information
1 parent
f1679b9
commit 351ab99
Showing
4 changed files
with
65 additions
and
11 deletions.
There are no files selected for viewing
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
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
54 changes: 54 additions & 0 deletions
54
src/main/java/com/faforever/api/logging/SlowRequestDetectionFilter.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,54 @@ | ||
package com.faforever.api.logging; | ||
|
||
import com.faforever.api.config.FafApiProperties; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StopWatch; | ||
import org.springframework.util.StringUtils; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import java.io.IOException; | ||
|
||
/** | ||
* This logging interceptor checks for slow http calls and logs the whole request url. | ||
*/ | ||
@Component | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class SlowRequestDetectionFilter implements Filter { | ||
|
||
private final FafApiProperties properties; | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
StopWatch stopWatch = new StopWatch(); | ||
log.trace("Start stopwatch for performance measuring"); | ||
stopWatch.start(); | ||
|
||
try { | ||
chain.doFilter(request, response); | ||
} finally { | ||
stopWatch.stop(); | ||
log.trace("Stop stopwatch for performance measuring, total time: {}", stopWatch.getTotalTimeSeconds()); | ||
|
||
if (request instanceof HttpServletRequest httpRequest && | ||
stopWatch.getTotalTimeSeconds() > properties.getMonitoring().getSlowRequestThresholdSeconds()) { | ||
StringBuffer urlBuffer = httpRequest.getRequestURL(); | ||
|
||
if (StringUtils.hasText(httpRequest.getQueryString())) { | ||
urlBuffer.append("?"); | ||
urlBuffer.append(httpRequest.getQueryString()); | ||
} | ||
|
||
log.warn("Slow request detected: {} seconds @ {}", stopWatch.getTotalTimeSeconds(), urlBuffer.toString()); | ||
} | ||
} | ||
} | ||
|
||
} |
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