feat: improve metric logic to accurately report success data

This commit is contained in:
Cheng Liu 2024-06-27 16:46:28 -07:00
parent f084797670
commit 8a307923a7
No known key found for this signature in database
GPG key ID: EEC8452F7DB85CD6
2 changed files with 42 additions and 25 deletions

View file

@ -21,6 +21,7 @@ export class CommandService {
@inject(HelpTextService) private _helpTextService!: HelpTextService;
@inject(TerminalService) private _terminalService!: TerminalService;
@inject(TelemetryService) private _telemetryService!: TelemetryService;
private _hasInternalError: boolean = false;
public register<O extends {}>(command: ICommand<O>): void {
const { cmd, description, builder, handler, getHelp } = command;
@ -36,19 +37,23 @@ export class CommandService {
},
async (args) => {
process.exitCode = 1;
this._hasInternalError = false;
try {
terminal.writeVerboseLine(`Invoking command "${commandName}" with args ${JSON.stringify(args)}`);
const stopwatch: Stopwatch = Stopwatch.start();
await handler(args, terminalService);
terminal.writeVerboseLine(`Invoked command "${commandName}" done (${stopwatch.toString()})`);
stopwatch.stop();
this._telemetryService.collectTelemetry({
commandName,
args: process.argv.slice(2),
durationInSeconds: stopwatch.duration,
startTimestampMs: stopwatch.startTime,
endTimestampMs: stopwatch.endTime
});
if (!this._hasInternalError) {
// Only report success data
this._telemetryService.collectTelemetry({
commandName,
args: process.argv.slice(2),
durationInSeconds: stopwatch.duration,
startTimestampMs: stopwatch.startTime,
endTimestampMs: stopwatch.endTime
});
}
// eslint-disable-next-line require-atomic-updates
process.exitCode = 0;
} catch (e) {
@ -60,4 +65,8 @@ export class CommandService {
);
this._helpTextService.set(commandName, getHelp());
}
public setHasInternalError(): void {
this._hasInternalError = true;
}
}

View file

@ -6,6 +6,7 @@ import { Service } from '../decorator';
import { TerminalService } from './TerminalService';
import { Stopwatch } from '../logic/Stopwatch';
import { TelemetryService } from './TelemetryService';
import { CommandService } from './CommandService';
/**
* @alpha
@ -34,6 +35,7 @@ export class GitService {
private _isSparseCheckoutMode: boolean | undefined;
@inject(TerminalService) private _terminalService!: TerminalService;
@inject(TelemetryService) private _telemetryService!: TelemetryService;
@inject(CommandService) private _commandService!: CommandService;
public setGitConfig(
k: string,
@ -248,14 +250,18 @@ export class GitService {
this._terminalService.terminal.writeDebugLine(`Invoked git command done (${stopwatch.toString()})`);
this._terminalService.writeTaskFooter();
stopwatch.stop();
this._telemetryService.collectTelemetry({
commandName: args[0],
args: args.slice(1),
durationInSeconds: stopwatch.duration,
startTimestampMs: stopwatch.startTime,
endTimestampMs: stopwatch.endTime,
isRawGitCommand: true
});
if (result.status === 0) {
this._telemetryService.collectTelemetry({
commandName: args[0],
args: args.slice(1),
durationInSeconds: stopwatch.duration,
startTimestampMs: stopwatch.startTime,
endTimestampMs: stopwatch.endTime,
isRawGitCommand: true
});
} else {
this._commandService.setHasInternalError();
}
return result;
}
@ -274,14 +280,16 @@ export class GitService {
});
this._terminalService.terminal.writeDebugLine(`Invoked git command done (${stopwatch.toString()})`);
stopwatch.stop();
this._telemetryService.collectTelemetry({
commandName: args[0],
args: args.slice(1),
durationInSeconds: stopwatch.duration,
startTimestampMs: stopwatch.startTime,
endTimestampMs: stopwatch.endTime,
isRawGitCommand: true
});
if (result.status === 0) {
this._telemetryService.collectTelemetry({
commandName: args[0],
args: args.slice(1),
durationInSeconds: stopwatch.duration,
startTimestampMs: stopwatch.startTime,
endTimestampMs: stopwatch.endTime,
isRawGitCommand: true
});
}
this._processResult(result);
return result.stdout.toString();
}
@ -492,8 +500,8 @@ Please specify a directory on the command line
const { terminal } = this._terminalService;
terminal.writeDebugLine(`Running git ${lsRemoteArgs.join(' ')}...`);
const childProcess: child_process.ChildProcess = Executable.spawn(gitPath, lsRemoteArgs, {
currentWorkingDirectory,
stdio: ['ignore', 'pipe', 'pipe']
currentWorkingDirectory,
stdio: ['ignore', 'pipe', 'pipe']
});
if (!childProcess.stdout || !childProcess.stderr) {
terminal.writeDebugLine(`Failed to spawn git process, fallback to spawnSync`);