Skip to content

Commit

Permalink
Improve logging for gateway connection (#2665)
Browse files Browse the repository at this point in the history
* Log value of _trace field
* Only use handleCallbackError
  • Loading branch information
MinnDevelopment committed Apr 23, 2024
1 parent 2767fbb commit e82f4dc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Expand Up @@ -146,6 +146,7 @@ dependencies {
testImplementation(libs.mockito)
testImplementation(libs.assertj)
testImplementation(libs.commons.lang3)
testImplementation(libs.logback.classic)
}


Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Expand Up @@ -8,6 +8,7 @@ dependencyResolutionManagement {
library("jackson-databind", "com.fasterxml.jackson.core", "jackson-databind").versionRef("jackson")
bundle("jackson", listOf("jackson-core", "jackson-databind"))

library("logback-classic", "ch.qos.logback", "logback-classic" ).version("1.5.6")
library("opus", "club.minnced", "opus-java" ).version("1.1.1")
library("findbugs", "com.google.code.findbugs", "jsr305" ).version("3.0.2")
library("websocket-client", "com.neovisionaries", "nv-websocket-client" ).version("2.14")
Expand Down
Expand Up @@ -79,11 +79,9 @@ public class WebSocketClient extends WebSocketAdapter implements WebSocketListen
{
public static final ThreadLocal<Boolean> WS_THREAD = ThreadLocal.withInitial(() -> false);
public static final Logger LOG = JDALogger.getLog(WebSocketClient.class);
public static final int IDENTIFY_DELAY = 5;
public static final int ZLIB_SUFFIX = 0x0000FFFF;

protected static final String INVALIDATE_REASON = "INVALIDATE_SESSION";
protected static final long IDENTIFY_BACKOFF = TimeUnit.SECONDS.toMillis(SessionController.IDENTIFY_DELAY); // same as 1000 * IDENTIFY_DELAY
protected static final long IDENTIFY_BACKOFF = TimeUnit.SECONDS.toMillis(SessionController.IDENTIFY_DELAY);

protected final JDAImpl api;
protected final JDA.ShardInfo shardInfo;
Expand All @@ -94,6 +92,7 @@ public class WebSocketClient extends WebSocketAdapter implements WebSocketListen
protected final GatewayEncoding encoding;

public WebSocket socket;
protected String traceMetadata = null;
protected volatile String sessionId = null;
protected final Object readLock = new Object();
protected Decompressor decompressor;
Expand Down Expand Up @@ -980,6 +979,8 @@ protected void onDispatch(DataObject raw)
handlers.get("READY").handle(responseTotal, raw);
sessionId = content.getString("session_id");
resumeUrl = content.getString("resume_gateway_url", null);
traceMetadata = content.opt("_trace").map(String::valueOf).orElse(null);
LOG.debug("Received READY with _trace {}", traceMetadata);
break;
case "RESUMED":
reconnectTimeoutS = 2;
Expand Down Expand Up @@ -1089,8 +1090,19 @@ protected DataObject handleBinary(byte[] binary) throws DataFormatException
}
}

@Override
public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception
{
handleError(cause);
}

@Override
public void onError(WebSocket websocket, WebSocketException cause) throws Exception
{
handleError(cause);
}

private void handleError(Throwable cause)
{
if (cause.getCause() instanceof SocketTimeoutException)
{
Expand All @@ -1102,7 +1114,7 @@ else if (cause.getCause() instanceof IOException)
}
else
{
LOG.error("There was an error in the WebSocket connection", cause);
LOG.error("There was an error in the WebSocket connection. Trace: {}", traceMetadata, cause);
api.handleEvent(new ExceptionEvent(api, cause, true));
}
}
Expand Down

0 comments on commit e82f4dc

Please sign in to comment.