Skip to content

Commit 408abd6

Browse files
committed
refactor: simplify auth telemetry flow
1 parent 23f7ba4 commit 408abd6

9 files changed

Lines changed: 402 additions & 393 deletions

File tree

src/commands.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { toError } from "./error/errorUtils";
1616
import { type FeatureSet, featureSetForVersion } from "./featureSet";
1717
import {
1818
AuthTelemetry,
19-
type AuthLoginMethod,
2019
type AuthLoginOutcome,
2120
type AuthLoginSource,
2221
type AuthLogoutOutcome,
@@ -59,7 +58,7 @@ import type { DeploymentManager } from "./deployment/deploymentManager";
5958
import type { Deployment } from "./deployment/types";
6059
import type { CredentialFailureCategory } from "./instrumentation/credentials";
6160
import type { Logger } from "./logging/logger";
62-
import type { LoginCoordinator } from "./login/loginCoordinator";
61+
import type { LoginCoordinator, LoginMethod } from "./login/loginCoordinator";
6362
import type { TelemetryService } from "./telemetry/service";
6463
import type { SpeedtestPanelFactory } from "./webviews/speedtest/speedtestPanelFactory";
6564
import type {
@@ -83,8 +82,6 @@ interface LoginArgs {
8382
readonly autoLogin?: boolean;
8483
}
8584

86-
type LoginMethodRecorder = (method: AuthLoginMethod) => void;
87-
8885
interface LoginSuccess {
8986
readonly user: User;
9087
readonly token: string;
@@ -167,28 +164,24 @@ export class Commands {
167164
}
168165
await this.traceLoginCommand(
169166
args?.autoLogin ? "auto_login" : "command",
170-
(recordMethod) => this.performLogin(args, recordMethod),
167+
(setMethod) => this.performLogin(args, setMethod),
171168
);
172169
}
173170

174171
private async traceLoginCommand(
175172
source: AuthLoginSource,
176-
run: (recordMethod: LoginMethodRecorder) => Promise<AuthLoginOutcome>,
173+
run: (
174+
setMethod: (method: LoginMethod) => void,
175+
) => Promise<AuthLoginOutcome>,
177176
): Promise<void> {
178-
let method: AuthLoginMethod = "unknown";
179-
await this.authTelemetry.traceLogin(
180-
source,
181-
() => method,
182-
() =>
183-
run((next) => {
184-
method = next;
185-
}),
177+
await this.authTelemetry.traceLogin(source, (trace) =>
178+
run((method) => trace.setMethod(method)),
186179
);
187180
}
188181

189182
private async performLogin(
190183
args: LoginArgs | undefined,
191-
recordMethod: LoginMethodRecorder,
184+
setMethod: (method: LoginMethod) => void,
192185
): Promise<AuthLoginOutcome> {
193186
this.logger.debug("Logging in");
194187

@@ -209,16 +202,18 @@ export class Commands {
209202
safeHostname,
210203
url,
211204
autoLogin: args?.autoLogin,
212-
traceLogin: false,
213-
onLoginMethod: recordMethod,
214205
});
215206

216207
if (!result.success) {
208+
if (result.method) {
209+
setMethod(result.method);
210+
}
217211
return result;
218212
}
219213

214+
setMethod(result.method);
220215
await this.completeLogin(url, safeHostname, result);
221-
return { success: true };
216+
return { success: true, method: result.method };
222217
}
223218

224219
private async completeLogin(
@@ -520,8 +515,8 @@ export class Commands {
520515
*/
521516
public async switchDeployment(): Promise<void> {
522517
this.logger.debug("Switching deployment");
523-
await this.traceLoginCommand("switch_deployment", (recordMethod) =>
524-
this.performLogin(undefined, recordMethod),
518+
await this.traceLoginCommand("switch_deployment", (setMethod) =>
519+
this.performLogin(undefined, setMethod),
525520
);
526521
}
527522

0 commit comments

Comments
 (0)