adding a metric for ipv4/ipv6 requests count
This commit is contained in:
parent
b5a75d3079
commit
836307b0c7
|
@ -110,7 +110,7 @@ import org.whispersystems.textsecuregcm.currency.CoinMarketCapClient;
|
||||||
import org.whispersystems.textsecuregcm.currency.CurrencyConversionManager;
|
import org.whispersystems.textsecuregcm.currency.CurrencyConversionManager;
|
||||||
import org.whispersystems.textsecuregcm.currency.FixerClient;
|
import org.whispersystems.textsecuregcm.currency.FixerClient;
|
||||||
import org.whispersystems.textsecuregcm.experiment.ExperimentEnrollmentManager;
|
import org.whispersystems.textsecuregcm.experiment.ExperimentEnrollmentManager;
|
||||||
import org.whispersystems.textsecuregcm.filters.ContentLengthFilter;
|
import org.whispersystems.textsecuregcm.filters.RequestStatisticsFilter;
|
||||||
import org.whispersystems.textsecuregcm.filters.RemoteDeprecationFilter;
|
import org.whispersystems.textsecuregcm.filters.RemoteDeprecationFilter;
|
||||||
import org.whispersystems.textsecuregcm.filters.TimestampResponseFilter;
|
import org.whispersystems.textsecuregcm.filters.TimestampResponseFilter;
|
||||||
import org.whispersystems.textsecuregcm.limits.DynamicRateLimiters;
|
import org.whispersystems.textsecuregcm.limits.DynamicRateLimiters;
|
||||||
|
@ -624,7 +624,7 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
|
||||||
.addFilter("RemoteDeprecationFilter", new RemoteDeprecationFilter(dynamicConfigurationManager))
|
.addFilter("RemoteDeprecationFilter", new RemoteDeprecationFilter(dynamicConfigurationManager))
|
||||||
.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
|
.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
|
||||||
|
|
||||||
environment.jersey().register(new ContentLengthFilter(TrafficSource.HTTP));
|
environment.jersey().register(new RequestStatisticsFilter(TrafficSource.HTTP));
|
||||||
environment.jersey().register(MultiRecipientMessageProvider.class);
|
environment.jersey().register(MultiRecipientMessageProvider.class);
|
||||||
environment.jersey().register(new MetricsApplicationEventListener(TrafficSource.HTTP));
|
environment.jersey().register(new MetricsApplicationEventListener(TrafficSource.HTTP));
|
||||||
environment.jersey()
|
environment.jersey()
|
||||||
|
@ -646,7 +646,7 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
|
||||||
clientPresenceManager, websocketScheduledExecutor));
|
clientPresenceManager, websocketScheduledExecutor));
|
||||||
webSocketEnvironment.jersey()
|
webSocketEnvironment.jersey()
|
||||||
.register(new WebsocketRefreshApplicationEventListener(accountsManager, clientPresenceManager));
|
.register(new WebsocketRefreshApplicationEventListener(accountsManager, clientPresenceManager));
|
||||||
webSocketEnvironment.jersey().register(new ContentLengthFilter(TrafficSource.WEBSOCKET));
|
webSocketEnvironment.jersey().register(new RequestStatisticsFilter(TrafficSource.WEBSOCKET));
|
||||||
webSocketEnvironment.jersey().register(MultiRecipientMessageProvider.class);
|
webSocketEnvironment.jersey().register(MultiRecipientMessageProvider.class);
|
||||||
webSocketEnvironment.jersey().register(new MetricsApplicationEventListener(TrafficSource.WEBSOCKET));
|
webSocketEnvironment.jersey().register(new MetricsApplicationEventListener(TrafficSource.WEBSOCKET));
|
||||||
webSocketEnvironment.jersey().register(new KeepAliveController(clientPresenceManager));
|
webSocketEnvironment.jersey().register(new KeepAliveController(clientPresenceManager));
|
||||||
|
|
|
@ -1,37 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2021 Signal Messenger, LLC
|
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.whispersystems.textsecuregcm.filters;
|
|
||||||
|
|
||||||
import static com.codahale.metrics.MetricRegistry.name;
|
|
||||||
|
|
||||||
import io.micrometer.core.instrument.Metrics;
|
|
||||||
import java.io.IOException;
|
|
||||||
import javax.ws.rs.container.ContainerRequestContext;
|
|
||||||
import javax.ws.rs.container.ContainerRequestFilter;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.whispersystems.textsecuregcm.metrics.TrafficSource;
|
|
||||||
|
|
||||||
public class ContentLengthFilter implements ContainerRequestFilter {
|
|
||||||
|
|
||||||
private static final String CONTENT_LENGTH_DISTRIBUTION_NAME = name(ContentLengthFilter.class, "contentLength");
|
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ContentLengthFilter.class);
|
|
||||||
private final TrafficSource trafficSource;
|
|
||||||
|
|
||||||
public ContentLengthFilter(final TrafficSource trafficeSource) {
|
|
||||||
this.trafficSource = trafficeSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void filter(final ContainerRequestContext requestContext) throws IOException {
|
|
||||||
try {
|
|
||||||
Metrics.summary(CONTENT_LENGTH_DISTRIBUTION_NAME, "trafficSource", trafficSource.name().toLowerCase()).record(requestContext.getLength());
|
|
||||||
} catch (final Exception e) {
|
|
||||||
logger.warn("Error recording content length", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,78 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2021 Signal Messenger, LLC
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.whispersystems.textsecuregcm.filters;
|
||||||
|
|
||||||
|
import static com.codahale.metrics.MetricRegistry.name;
|
||||||
|
import static java.util.Objects.requireNonNull;
|
||||||
|
|
||||||
|
import com.google.common.net.HttpHeaders;
|
||||||
|
import com.google.common.net.InetAddresses;
|
||||||
|
import io.micrometer.core.instrument.Metrics;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Inet4Address;
|
||||||
|
import java.net.Inet6Address;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.ws.rs.container.ContainerRequestContext;
|
||||||
|
import javax.ws.rs.container.ContainerRequestFilter;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.whispersystems.textsecuregcm.metrics.TrafficSource;
|
||||||
|
import org.whispersystems.textsecuregcm.util.HeaderUtils;
|
||||||
|
|
||||||
|
public class RequestStatisticsFilter implements ContainerRequestFilter {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(RequestStatisticsFilter.class);
|
||||||
|
|
||||||
|
private static final String CONTENT_LENGTH_DISTRIBUTION_NAME = name(RequestStatisticsFilter.class, "contentLength");
|
||||||
|
|
||||||
|
private static final String IP_VERSION_METRIC = name(RequestStatisticsFilter.class, "ipVersion");
|
||||||
|
|
||||||
|
private static final String TRAFFIC_SOURCE_TAG = "trafficSource";
|
||||||
|
|
||||||
|
private static final String IP_VERSION_TAG = "ipVersion";
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
private final String trafficSourceTag;
|
||||||
|
|
||||||
|
|
||||||
|
public RequestStatisticsFilter(@Nonnull final TrafficSource trafficeSource) {
|
||||||
|
this.trafficSourceTag = requireNonNull(trafficeSource).name().toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void filter(final ContainerRequestContext requestContext) throws IOException {
|
||||||
|
try {
|
||||||
|
Metrics.summary(CONTENT_LENGTH_DISTRIBUTION_NAME, TRAFFIC_SOURCE_TAG, trafficSourceTag)
|
||||||
|
.record(requestContext.getLength());
|
||||||
|
Metrics.counter(IP_VERSION_METRIC, TRAFFIC_SOURCE_TAG, trafficSourceTag, IP_VERSION_TAG, resolveIpVersion(requestContext))
|
||||||
|
.increment();
|
||||||
|
} catch (final Exception e) {
|
||||||
|
logger.warn("Error recording request statistics", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
private static String resolveIpVersion(@Nonnull final ContainerRequestContext ctx) {
|
||||||
|
return HeaderUtils.getMostRecentProxy(ctx.getHeaderString(HttpHeaders.X_FORWARDED_FOR))
|
||||||
|
.map(ipString -> {
|
||||||
|
try {
|
||||||
|
//noinspection UnstableApiUsage
|
||||||
|
final InetAddress addr = InetAddresses.forString(ipString);
|
||||||
|
if (addr instanceof Inet4Address) {
|
||||||
|
return "IPv4";
|
||||||
|
}
|
||||||
|
if (addr instanceof Inet6Address) {
|
||||||
|
return "IPv6";
|
||||||
|
}
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
// ignore illegal argument exception
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
.orElse("unresolved");
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,12 +12,12 @@ import javax.ws.rs.container.ContainerRequestContext;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.whispersystems.textsecuregcm.metrics.TrafficSource;
|
import org.whispersystems.textsecuregcm.metrics.TrafficSource;
|
||||||
|
|
||||||
class ContentLengthFilterTest {
|
class RequestStatisticsFilterTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testFilter() throws Exception {
|
void testFilter() throws Exception {
|
||||||
|
|
||||||
final ContentLengthFilter contentLengthFilter = new ContentLengthFilter(TrafficSource.WEBSOCKET);
|
final RequestStatisticsFilter requestStatisticsFilter = new RequestStatisticsFilter(TrafficSource.WEBSOCKET);
|
||||||
|
|
||||||
final ContainerRequestContext requestContext = mock(ContainerRequestContext.class);
|
final ContainerRequestContext requestContext = mock(ContainerRequestContext.class);
|
||||||
|
|
||||||
|
@ -25,8 +25,8 @@ class ContentLengthFilterTest {
|
||||||
when(requestContext.getLength()).thenReturn(Integer.MAX_VALUE);
|
when(requestContext.getLength()).thenReturn(Integer.MAX_VALUE);
|
||||||
when(requestContext.getLength()).thenThrow(RuntimeException.class);
|
when(requestContext.getLength()).thenThrow(RuntimeException.class);
|
||||||
|
|
||||||
contentLengthFilter.filter(requestContext);
|
requestStatisticsFilter.filter(requestContext);
|
||||||
contentLengthFilter.filter(requestContext);
|
requestStatisticsFilter.filter(requestContext);
|
||||||
contentLengthFilter.filter(requestContext);
|
requestStatisticsFilter.filter(requestContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue