Skip to content

Commit d6ca402

Browse files
authored
Improve ssh analytics (#2)
1 parent d9478e1 commit d6ca402

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

src/extension.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ export async function activate(context: vscode.ExtensionContext) {
6969
}
7070
}));
7171

72-
// For analitycs, will be called by gitpod-remote extension
73-
context.subscriptions.push(vscode.commands.registerCommand('__gitpod.getLocalMachineId', () => {
74-
return vscode.env.machineId;
75-
}));
76-
7772
const authProvider = new GitpodAuthenticationProvider(context, logger, telemetry);
7873
const remoteConnector = new RemoteConnector(context, logger, telemetry);
7974
context.subscriptions.push(authProvider);
@@ -89,6 +84,10 @@ export async function activate(context: vscode.ExtensionContext) {
8984
}
9085
}));
9186

87+
if (await remoteConnector.checkRemoteConnectionSuccessful()) {
88+
context.subscriptions.push(vscode.commands.registerCommand('gitpod.api.autoTunnel', remoteConnector.autoTunnelCommand, remoteConnector));
89+
}
90+
9291
if (!context.globalState.get<boolean>(FIRST_INSTALL_KEY, false)) {
9392
await context.globalState.update(FIRST_INSTALL_KEY, true);
9493
telemetry.sendTelemetryEvent('gitpod_desktop_installation', { kind: 'install' });

src/remoteConnector.ts

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ export default class RemoteConnector extends Disposable {
107107
super();
108108

109109
this.releaseStaleLocks();
110-
111-
if (vscode.env.remoteName && context.extension.extensionKind === vscode.ExtensionKind.UI) {
112-
this._register(vscode.commands.registerCommand('gitpod.api.autoTunnel', this.autoTunnelCommand.bind(this)));
113-
}
114110
}
115111

116112
private releaseStaleLocks(): void {
@@ -666,8 +662,6 @@ export default class RemoteConnector extends Disposable {
666662
if (password) {
667663
await this.showSSHPasswordModal(password);
668664
}
669-
670-
this.telemetry.sendTelemetryEvent('vscode_desktop_ssh', { kind: 'gateway', status: 'connected', ...params });
671665
} catch (e) {
672666
this.telemetry.sendTelemetryEvent('vscode_desktop_ssh', { kind: 'gateway', status: 'failed', reason: e.toString(), ...params });
673667
if (e instanceof NoSSHGatewayError) {
@@ -706,8 +700,6 @@ export default class RemoteConnector extends Disposable {
706700
const localAppDestData = await this.getWorkspaceLocalAppSSHDestination(params);
707701
sshDestination = localAppDestData.localAppSSHDest;
708702
localAppSSHConfigPath = localAppDestData.localAppSSHConfigPath;
709-
710-
this.telemetry.sendTelemetryEvent('vscode_desktop_ssh', { kind: 'local-app', status: 'connected', ...params });
711703
} catch (e) {
712704
this.logger.error(`Failed to connect ${params.workspaceId} Gitpod workspace:`, e);
713705
if (e instanceof LocalAppError) {
@@ -734,6 +726,8 @@ export default class RemoteConnector extends Disposable {
734726

735727
await this.updateRemoteSSHConfig(usingSSHGateway, localAppSSHConfigPath);
736728

729+
await this.context.globalState.update(sshDestination!, { ...params, usingSSHGateway });
730+
737731
vscode.commands.executeCommand(
738732
'vscode.openFolder',
739733
vscode.Uri.parse(`vscode-remote://ssh-remote+${sshDestination}${uri.path || '/'}`),
@@ -748,7 +742,7 @@ export default class RemoteConnector extends Disposable {
748742
const configKey = `config/${authority}`;
749743
const localAppconfig = this.context.globalState.get<LocalAppConfig>(configKey);
750744
if (!localAppconfig || checkRunning(localAppconfig.pid) !== true) {
751-
// Do nothing if we are using SSH gateway
745+
// Do nothing if we are using SSH gateway and local app is not running
752746
return;
753747
}
754748
}
@@ -766,4 +760,53 @@ export default class RemoteConnector extends Disposable {
766760
this.logger.error('Failed to disable auto tunneling:', e);
767761
}
768762
}
763+
764+
public async checkRemoteConnectionSuccessful() {
765+
const isRemoteExtensionHostRunning = async () => {
766+
try {
767+
// Invoke command from gitpot-remote extension to test if connection is successful
768+
await vscode.commands.executeCommand('__gitpod.getGitpodRemoteLogsUri');
769+
return true;
770+
} catch {
771+
return false;
772+
}
773+
};
774+
775+
const remoteUri = vscode.workspace.workspaceFile || vscode.workspace.workspaceFolders?.[0].uri;
776+
if (vscode.env.remoteName === 'ssh-remote' && this.context.extension.extensionKind === vscode.ExtensionKind.UI && remoteUri) {
777+
const [, sshDestStr] = remoteUri.authority.split('+');
778+
const sshDest: { user: string; hostName: string } = JSON.parse(Buffer.from(sshDestStr, 'hex').toString('utf8'));
779+
const gitpodHost = vscode.workspace.getConfiguration('gitpod').get<string>('host')!;
780+
const host = new URL(gitpodHost).host;
781+
const connectionSuccessful = sshDest.hostName.endsWith(host) && await isRemoteExtensionHostRunning();
782+
783+
const connectionInfo = this.context.globalState.get<SSHConnectionParams & { usingSSHGateway: boolean }>(sshDestStr);
784+
if (connectionInfo) {
785+
const kind = connectionInfo.usingSSHGateway ? 'gateway' : 'local-app';
786+
if (connectionSuccessful) {
787+
this.telemetry.sendTelemetryEvent('vscode_desktop_ssh', {
788+
kind,
789+
status: 'connected',
790+
instanceId: connectionInfo.instanceId,
791+
workspaceId: connectionInfo.workspaceId,
792+
gitpodHost: connectionInfo.gitpodHost
793+
});
794+
} else {
795+
this.telemetry.sendTelemetryEvent('vscode_desktop_ssh', {
796+
kind,
797+
status: 'failed',
798+
reason: 'remote-ssh extension: connection failed',
799+
instanceId: connectionInfo.instanceId,
800+
workspaceId: connectionInfo.workspaceId,
801+
gitpodHost: connectionInfo.gitpodHost
802+
});
803+
}
804+
await this.context.globalState.update(remoteUri.authority, undefined);
805+
}
806+
807+
return connectionSuccessful;
808+
}
809+
810+
return false;
811+
}
769812
}

0 commit comments

Comments
 (0)