mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-01 07:46:47 +00:00
Limit cardinality of HTTP methods in request metrics
This commit is contained in:
1 parent
0d42f37a5c
commit
070aa5f0cd
3 files changed
+32
-3
No files matched your search
+14
-2
@@ -18,9 +18,11 @@ import jakarta.ws.rs.container.ContainerResponseFilter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import javax.annotation.Nullable;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.eclipse.jetty.server.Connector;
|
||||
import org.eclipse.jetty.server.HttpChannel;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
@@ -49,6 +51,9 @@ import org.whispersystems.textsecuregcm.util.ua.UserAgentUtil;
|
||||
public class MetricsHttpChannelListener implements HttpChannel.Listener, Container.Listener, LifeCycle.Listener,
|
||||
ContainerResponseFilter {
|
||||
|
||||
private static final Set<String> EXPECTED_HTTP_METHODS =
|
||||
Set.of("GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH");
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MetricsHttpChannelListener.class);
|
||||
|
||||
private record RequestInfo(String path, String method, int statusCode, @Nullable String userAgent) {
|
||||
@@ -201,14 +206,21 @@ public class MetricsHttpChannelListener implements HttpChannel.Listener, Contain
|
||||
.filter(servletPaths::contains)
|
||||
.orElse("unknown")
|
||||
);
|
||||
final String method = Optional.ofNullable(request.getMethod()).orElse("unknown");
|
||||
|
||||
// Response cannot be null, but its status might not always reflect an actual response status, since it gets
|
||||
// initialized to 200
|
||||
final int status = request.getResponse().getStatus();
|
||||
|
||||
@Nullable final String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
|
||||
|
||||
return new RequestInfo(path, method, status, userAgent);
|
||||
return new RequestInfo(path, normalizeMethod(request.getMethod()), status, userAgent);
|
||||
}
|
||||
|
||||
static String normalizeMethod(@Nullable final String method) {
|
||||
if (StringUtils.isBlank(method)) {
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
return EXPECTED_HTTP_METHODS.contains(method.toUpperCase(Locale.ROOT)) ? method : "unknown";
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -107,7 +107,7 @@ public class MetricsRequestEventListener implements RequestEventListener {
|
||||
|
||||
final List<Tag> tags = new ArrayList<>();
|
||||
tags.add(Tag.of(PATH_TAG, UriInfoUtil.getPathTemplate(event.getUriInfo())));
|
||||
tags.add(Tag.of(METHOD_TAG, event.getContainerRequest().getMethod()));
|
||||
tags.add(Tag.of(METHOD_TAG, MetricsHttpChannelListener.normalizeMethod(event.getContainerRequest().getMethod())));
|
||||
tags.add(Tag.of(STATUS_CODE_TAG, String.valueOf(Optional
|
||||
.ofNullable(event.getContainerResponse())
|
||||
.map(ContainerResponse::getStatus)
|
||||
|
||||
+17
@@ -30,9 +30,12 @@ import org.glassfish.jersey.server.ExtendedUriInfo;
|
||||
import org.glassfish.jersey.uri.UriTemplate;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.whispersystems.textsecuregcm.storage.ClientReleaseManager;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
class MetricsHttpChannelListenerTest {
|
||||
|
||||
@@ -170,7 +173,21 @@ class MetricsHttpChannelListenerTest {
|
||||
} else {
|
||||
verifyNoInteractions(requestsByVersionCounter);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void normalizeMethod(@Nullable final String originalMethod, final String expectedMethod) {
|
||||
assertEquals(expectedMethod, MetricsHttpChannelListener.normalizeMethod(originalMethod));
|
||||
}
|
||||
|
||||
private static List<Arguments> normalizeMethod() {
|
||||
return List.of(
|
||||
Arguments.arguments(null, "unknown"),
|
||||
Arguments.arguments("", "unknown"),
|
||||
Arguments.arguments("UNEXPECTED_METHOD", "unknown"),
|
||||
Arguments.arguments("GET", "GET"),
|
||||
Arguments.arguments("get", "get")
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user