Skip to content

Commit

Permalink
fixup! feat(cli): instrument cli with telemetry events
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Dec 7, 2023
1 parent 9147661 commit 96db8d5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
26 changes: 24 additions & 2 deletions packages/@sanity/cli/src/__telemetry__/cli.telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,30 @@ export const CliStart = defineEvent<{
description: 'User ran the sanity cli',
})

export const CliCommand = defineTrace<{commandName: string}>({
interface CLITraceData {
/**
* Command flags, without the core options (help, debug, version etc)
*/
groupOrCommand: string

/**
* Command arguments, eg any arguments after `sanity <command>` (no flags)
*/
commandArguments: string[]

/**
* Arguments after the ended argument list (--)
*/
extraArguments: string[]
coreOptions: {
help: boolean
debug: boolean
version: boolean
}
}

export const CliCommand = defineTrace<CLITraceData>({
name: 'cliCommand',
version: 1,
description: 'User ran a cli action',
description: 'User runs a cli command',
})
24 changes: 14 additions & 10 deletions packages/@sanity/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,14 @@ export async function runCli(cliRoot: string, {cliVersion}: {cliVersion: string}

const {logger: telemetry, flush: flushTelemetry} = createTelemetryStore({env: process.env})

telemetry.log(CliStart, {
nodeVersion: process.version,
cliVersion: pkg.version,
platform: process.platform,
cpuArch: process.arch,
})

// UGLY HACK: process.exit(<code>) causes abrupt exit, we want to flush telemetry before exiting
installProcessExitHack(flushTelemetry)

telemetry.log(CliStart, {
cliVersion: pkg.version,
nodeVersion: process.version,
cpuArch: process.arch,
cliVersion: pkg.version,
platform: process.platform,
cpuArch: process.arch,
})

const args = parseArguments()
Expand Down Expand Up @@ -122,12 +115,23 @@ export async function runCli(cliRoot: string, {cliVersion}: {cliVersion: string}
const cliRunner = getCliRunner(commands)
const cliCommandTrace = telemetry.trace(CliCommand)

cliCommandTrace.log({commandName: args.groupOrCommand})
cliCommandTrace.log({
groupOrCommand: args.groupOrCommand,
extraArguments: args.extraArguments,
commandArguments: args.argsWithoutOptions,
coreOptions: {
help: args.coreOptions.help,
version: args.coreOptions.version,
debug: args.coreOptions.debug,
},
})

cliRunner
.runCommand(args.groupOrCommand, args, {
...options,
telemetry: cliCommandTrace.newContext(args.groupOrCommand),
})
.then(() => cliCommandTrace.complete())
.catch(async (err) => {
await flushTelemetry()
const error = typeof err.details === 'string' ? err.details : err
Expand Down

0 comments on commit 96db8d5

Please sign in to comment.