Add metric for closed connection age to KeepAliveController

This commit is contained in:
Chris Eager 2024-10-17 16:39:06 -05:00 committed by Chris Eager
parent 1faa1a5abc
commit 584fd06b88
2 changed files with 19 additions and 10 deletions

View File

@ -10,7 +10,10 @@ import static com.codahale.metrics.MetricRegistry.name;
import io.dropwizard.auth.Auth;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@ -33,8 +36,9 @@ public class KeepAliveController {
private final ClientPresenceManager clientPresenceManager;
private static final String NO_LOCAL_SUBSCRIPTION_COUNTER_NAME = name(KeepAliveController.class,
"noLocalSubscription");
private static final String CLOSED_CONNECTION_AGE_DISTRIBUTION_NAME = name(KeepAliveController.class,
"closedConnectionAge");
public KeepAliveController(final ClientPresenceManager clientPresenceManager) {
this.clientPresenceManager = clientPresenceManager;
@ -46,16 +50,20 @@ public class KeepAliveController {
maybeAuth.ifPresent(auth -> {
if (!clientPresenceManager.isLocallyPresent(auth.getAccount().getUuid(), auth.getAuthenticatedDevice().getId())) {
final Duration age = Duration.between(context.getClient().getCreated(), Instant.now());
logger.debug("***** No local subscription found for {}::{}; age = {}ms, User-Agent = {}",
auth.getAccount().getUuid(), auth.getAuthenticatedDevice().getId(),
System.currentTimeMillis() - context.getClient().getCreatedTimestamp(),
auth.getAccount().getUuid(), auth.getAuthenticatedDevice().getId(), age.toMillis(),
context.getClient().getUserAgent());
context.getClient().close(1000, "OK");
Metrics.counter(NO_LOCAL_SUBSCRIPTION_COUNTER_NAME,
Tags.of(UserAgentTagUtil.getPlatformTag(context.getClient().getUserAgent())))
.increment();
Timer.builder(CLOSED_CONNECTION_AGE_DISTRIBUTION_NAME)
.tags(Tags.of(UserAgentTagUtil.getPlatformTag(context.getClient().getUserAgent())))
.publishPercentileHistogram(true)
.register(Metrics.globalRegistry)
.record(age);
}
});

View File

@ -7,6 +7,7 @@ package org.whispersystems.websocket;
import com.google.common.net.HttpHeaders;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -31,7 +32,7 @@ public class WebSocketClient {
private final RemoteEndpoint remoteEndpoint;
private final WebSocketMessageFactory messageFactory;
private final Map<Long, CompletableFuture<WebSocketResponseMessage>> pendingRequestMapper;
private final long created;
private final Instant created;
public WebSocketClient(Session session, RemoteEndpoint remoteEndpoint, WebSocketMessageFactory messageFactory,
Map<Long, CompletableFuture<WebSocketResponseMessage>> pendingRequestMapper) {
@ -39,7 +40,7 @@ public class WebSocketClient {
this.remoteEndpoint = remoteEndpoint;
this.messageFactory = messageFactory;
this.pendingRequestMapper = pendingRequestMapper;
this.created = System.currentTimeMillis();
this.created = Instant.now();
}
public CompletableFuture<WebSocketResponseMessage> sendRequest(String verb, String path,
@ -78,7 +79,7 @@ public class WebSocketClient {
return session.getUpgradeRequest().getHeader(HttpHeaders.USER_AGENT);
}
public long getCreatedTimestamp() {
public Instant getCreated() {
return this.created;
}