Skip to content

Commit

Permalink
Add logging for slow http requests
Browse files Browse the repository at this point in the history
The threshold is 1s by default and can be configured.
  • Loading branch information
Brutus5000 committed Aug 22, 2021
1 parent f1679b9 commit 351ab99
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/faforever/api/config/FafApiProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class FafApiProperties {
private Tutorial tutorial = new Tutorial();
private Nodebb nodebb = new Nodebb();
private Recaptcha recaptcha = new Recaptcha();
private Monitoring monitoring = new Monitoring();

@Data
public static class OAuth2 {
Expand Down Expand Up @@ -267,4 +268,9 @@ public static class Recaptcha {
private boolean enabled;
private String secret;
}

@Data
public static class Monitoring {
private double slowRequestThresholdSeconds = 1.0;
}
}
12 changes: 1 addition & 11 deletions src/main/java/com/faforever/api/logging/RequestIdFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
Expand All @@ -34,16 +33,11 @@ public class RequestIdFilter implements Filter {
*/
private static final String REQUEST_ID_HEADER = "X-Request-ID";

@Override
public void init(FilterConfig filterConfig) {
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String requestId = null;

if (request instanceof HttpServletRequest) {
HttpServletRequest httpRequest = (HttpServletRequest) request;
if (request instanceof HttpServletRequest httpRequest) {
requestId = httpRequest.getHeader(REQUEST_ID_HEADER);
}

Expand All @@ -65,8 +59,4 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
}
}

@Override
public void destroy() {
}

}
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());
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class ExeUploaderControllerTest {
private FafApiProperties fafApiProperties;
@MockBean
private FafApiProperties.Deployment deployment;
@MockBean
private FafApiProperties.Monitoring monitoring;

@Mock
private MockMultipartFile file;
Expand All @@ -48,8 +50,10 @@ public void init(MockMvc mvc) {
@BeforeEach
public void setUp() {
when(fafApiProperties.getDeployment()).thenReturn(deployment);
when(fafApiProperties.getMonitoring()).thenReturn(monitoring);
when(deployment.getAllowedExeExtension()).thenReturn("exe");
when(deployment.getTestingExeUploadKey()).thenReturn(API_KEY);
when(monitoring.getSlowRequestThresholdSeconds()).thenReturn(1.0);
file = new MockMultipartFile("file",
"ForgedAlliance.exe",
"application/octet-stream",
Expand Down

0 comments on commit 351ab99

Please sign in to comment.