Skip to content

Commit

Permalink
chore(cli-storybook): Add telemetry (#8803)
Browse files Browse the repository at this point in the history
Instruments the storybook command with telemetry. I had forgot about it
since it lives in it's own package now.

**WIP**
I had to add an empty SIGINT listener for execa to exit and the
process.on exit to trigger. I'll need to understand why...
  • Loading branch information
Josh-Walker-GM authored and jtoar committed Jul 20, 2023
1 parent fdc9a17 commit a3541cc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 deletions.
1 change: 1 addition & 0 deletions packages/cli-packages/storybook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
]
},
"dependencies": {
"@redwoodjs/cli-helpers": "5.0.0",
"@redwoodjs/project-config": "5.0.0",
"@redwoodjs/telemetry": "5.0.0",
"@storybook/addon-a11y": "7.1.0",
Expand Down
27 changes: 10 additions & 17 deletions packages/cli-packages/storybook/src/commands/storybook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import terminalLink from 'terminal-link'
import type { Argv } from 'yargs'

import c from '../lib/colors'
import { recordTelemetryAttributes } from '@redwoodjs/cli-helpers'

import { StorybookYargsOptions } from '../types'

export const command = 'storybook'
Expand Down Expand Up @@ -54,22 +55,6 @@ export function builder(
default: defaultOptions.smokeTest,
})

.check((argv) => {
if (argv.build && argv.smokeTest) {
throw new Error('Can not provide both "--build" and "--smoke-test"')
}

if (argv.build && argv.open) {
console.warn(
c.warning(
'Warning: --open option has no effect when running Storybook build'
)
)
}

return true
})

.epilogue(
`Also see the ${terminalLink(
'Redwood CLI Reference',
Expand All @@ -82,6 +67,14 @@ export async function handler(options: StorybookYargsOptions): Promise<void> {
// NOTE: We should provide some visual output before the import to increase
// the perceived performance of the command as there will be delay while we
// load the handler.
recordTelemetryAttributes({
command: 'storybook',
build: options.build,
ci: options.ci,
open: options.open,
smokeTest: options.smokeTest,
})

const { handler: storybookHandler } = await import('./storybookHandler.js')
await storybookHandler(options)
}
23 changes: 19 additions & 4 deletions packages/cli-packages/storybook/src/commands/storybookHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path'

import execa from 'execa'
import execa, { ExecaError } from 'execa'

import { getPaths } from '@redwoodjs/project-config'
// Allow import of untyped package
Expand All @@ -19,6 +19,19 @@ export async function handler({
port,
smokeTest,
}: StorybookYargsOptions) {
// Check for conflicting options
if (build && smokeTest) {
throw new Error('Can not provide both "--build" and "--smoke-test"')
}

if (build && open) {
console.warn(
c.warning(
'Warning: --open option has no effect when running Storybook build'
)
)
}

const cwd = getPaths().web.base
const staticAssetsFolder = path.join(cwd, 'public')
const execaOptions: Partial<execa.Options> = {
Expand Down Expand Up @@ -72,8 +85,10 @@ export async function handler({
try {
await execa.command(command, execaOptions)
} catch (e) {
console.log(c.error((e as Error).message))
errorTelemetry(process.argv, (e as Error).message)
process.exit(1)
if ((e as ExecaError).signal !== 'SIGINT') {
console.log(c.error((e as Error).message))
errorTelemetry(process.argv, (e as Error).message)
}
process.exit((e as ExecaError).exitCode ?? 1)
}
}
18 changes: 17 additions & 1 deletion packages/cli/src/telemetry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,23 @@ export async function startTelemetry() {
traceProvider.addSpanProcessor(traceProcessor)
traceProvider.register()

// Ensure to shutdown telemetry when the process exits
// Without any listeners for these signals, nodejs will terminate the process and will not
// trigger the exit event when doing so. This means our process.on('exit') handler will not run.
// We add a listner which either calls process.exit or if some other handler has been added,
// then we leave it to that handler to handle the signal.
// See https://nodejs.org/dist/latest/docs/api/process.html#signal-events for more info on the
// behaviour of nodejs for various signals.
for (const signal of ['SIGTERM', 'SIGINT', 'SIGHUP']) {
process.on(signal, () => {
if (process.listenerCount(signal) === 1) {
console.log(`Received ${signal} signal, exiting...`)
process.exit()
}
})
}

// Ensure to shutdown telemetry when the process exits so that we can be sure that all spans
// are ended and all data is flushed to the exporter.
process.on('exit', () => {
shutdownTelemetry()
})
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7123,6 +7123,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@redwoodjs/cli-storybook@workspace:packages/cli-packages/storybook"
dependencies:
"@redwoodjs/cli-helpers": 5.0.0
"@redwoodjs/project-config": 5.0.0
"@redwoodjs/telemetry": 5.0.0
"@storybook/addon-a11y": 7.1.0
Expand Down

0 comments on commit a3541cc

Please sign in to comment.