Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cli-storybook): Add telemetry #8803

Merged
merged 8 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.0.27",
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()
Tobbe marked this conversation as resolved.
Show resolved Hide resolved
}
})
}

// 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 @@ -7591,6 +7591,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.0.27
Expand Down