Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add client connection time metrics #1103

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/api/SignalClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {
AddTrackRequest,
ClientInfo,
ClientMetrics,
ClientMetrics_ConnectionError,
ClientMetrics_ConnectionTime,
ConnectionQualityUpdate,
DisconnectReason,
JoinResponse,
Expand Down Expand Up @@ -566,6 +569,42 @@ export class SignalClient {
});
}

sendConnectionError(error: ConnectionError) {
return this.sendRequest({
case: 'clientMetrics',
value: new ClientMetrics({
message: {
case: 'connectionError',
value: new ClientMetrics_ConnectionError({
message: error.message,
timestamp: protoInt64.parse(Date.now()),
code: error.code,
}),
},
}),
});
}

sendConnectionTimes({
signal,
publisher,
subscriber,
}: {
signal?: number;
publisher?: number;
subscriber?: number;
}) {
return this.sendRequest({
case: 'clientMetrics',
value: new ClientMetrics({
message: {
case: 'connectionTime',
value: new ClientMetrics_ConnectionTime({ signal, publisher, subscriber }),
},
}),
});
}

sendPing() {
/** send both of ping and pingReq for compatibility to old and new server */
return Promise.all([
Expand Down
16 changes: 15 additions & 1 deletion src/room/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@
unlockDisconnect();
return this.connectFuture.promise;
}

const connectStartTime = performance.now();

Check failure on line 443 in src/room/Room.ts

View workflow job for this annotation

GitHub Actions / test

'connectStartTime' is assigned a value but never used
this.setAndEmitConnectionState(ConnectionState.Connecting);
if (this.regionUrlProvider?.getServerUrl().toString() !== url) {
this.regionUrl = undefined;
Expand Down Expand Up @@ -643,6 +643,8 @@
this.engine.peerConnectionTimeout = this.connOptions.peerConnectionTimeout;
}

const connectStart = performance.now();

try {
const joinResponse = await this.connectSignal(
url,
Expand Down Expand Up @@ -681,17 +683,25 @@
throw new ConnectionError(`Connection attempt aborted`);
}

const signalConnectTime = performance.now() - connectStart;

try {
await this.engine.waitForPCInitialConnection(
this.connOptions.peerConnectionTimeout,
abortController,
);
} catch (e) {
if (e instanceof Error) {
await this.engine.client.sendConnectionError(new ConnectionError(e.message));
}

await this.engine.close();
this.recreateEngine();
throw e;
}

const pcConnectTime = performance.now() - signalConnectTime;

// also hook unload event
if (isWeb() && this.options.disconnectOnPageLeave) {
// capturing both 'pagehide' and 'beforeunload' to capture broadest set of browser behaviors
Expand All @@ -705,6 +715,10 @@
this.setAndEmitConnectionState(ConnectionState.Connected);
this.emit(RoomEvent.Connected);
this.registerConnectionReconcile();
await this.engine.client.sendConnectionTimes({
signal: signalConnectTime,
subscriber: pcConnectTime,
});
};

/**
Expand Down
Loading