Unverified Commit 96696c2d authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add event for dev process stop (#42255)

x-ref: [slack
thread](https://vercel.slack.com/archives/C02HEJASXGD/p1667173179573409?thread_ts=1667165920.338789&channel=C02HEJASXGD&message_ts=1667173179.573409)

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent a4f9d77f
...@@ -113,7 +113,7 @@ if (process.versions.pnp === '3') { ...@@ -113,7 +113,7 @@ if (process.versions.pnp === '3') {
// Make sure commands gracefully respect termination signals (e.g. from Docker) // Make sure commands gracefully respect termination signals (e.g. from Docker)
// Allow the graceful termination to be manually configurable // Allow the graceful termination to be manually configurable
if (!process.env.NEXT_MANUAL_SIG_HANDLE) { if (!process.env.NEXT_MANUAL_SIG_HANDLE && command !== 'dev') {
process.on('SIGTERM', () => process.exit(0)) process.on('SIGTERM', () => process.exit(0))
process.on('SIGINT', () => process.exit(0)) process.on('SIGINT', () => process.exit(0))
} }
......
...@@ -11,9 +11,39 @@ import { getProjectDir } from '../lib/get-project-dir' ...@@ -11,9 +11,39 @@ import { getProjectDir } from '../lib/get-project-dir'
import { CONFIG_FILES } from '../shared/lib/constants' import { CONFIG_FILES } from '../shared/lib/constants'
import path from 'path' import path from 'path'
import type { NextConfig } from '../types' import type { NextConfig } from '../types'
import { interopDefault } from '../lib/interop-default' import type { NextConfigComplete } from '../server/config-shared'
import { Telemetry } from '../telemetry/storage' import { traceGlobals } from '../trace/shared'
import { NextConfigComplete } from '../server/config-shared'
let isTurboSession = false
let sessionStopHandled = false
let sessionStarted = Date.now()
const handleSessionStop = async () => {
if (sessionStopHandled) return
sessionStopHandled = true
const { eventCliSession } =
require('../telemetry/events/session-stopped') as typeof import('../telemetry/events/session-stopped')
const telemetry = traceGlobals.get('telemetry') as InstanceType<
typeof import('../telemetry/storage').Telemetry
>
if (!telemetry) {
process.exit(0)
}
telemetry.record(
eventCliSession({
cliCommand: 'dev',
turboFlag: isTurboSession,
durationMilliseconds: Date.now() - sessionStarted,
})
)
await telemetry.flush()
process.exit(0)
}
process.on('SIGINT', handleSessionStop)
process.on('SIGTERM', handleSessionStop)
const nextDev: cliCommand = (argv) => { const nextDev: cliCommand = (argv) => {
const validArgs: arg.Spec = { const validArgs: arg.Spec = {
...@@ -105,6 +135,7 @@ const nextDev: cliCommand = (argv) => { ...@@ -105,6 +135,7 @@ const nextDev: cliCommand = (argv) => {
} }
if (args['--turbo']) { if (args['--turbo']) {
isTurboSession = true
// check for postcss, babelrc, swc plugins // check for postcss, babelrc, swc plugins
return new Promise<void>(async (resolve) => { return new Promise<void>(async (resolve) => {
const { findConfigPath } = const { findConfigPath } =
...@@ -127,6 +158,11 @@ const nextDev: cliCommand = (argv) => { ...@@ -127,6 +158,11 @@ const nextDev: cliCommand = (argv) => {
require('../telemetry/events/version') as typeof import('../telemetry/events/version') require('../telemetry/events/version') as typeof import('../telemetry/events/version')
const { findPagesDir } = const { findPagesDir } =
require('../lib/find-pages-dir') as typeof import('../lib/find-pages-dir') require('../lib/find-pages-dir') as typeof import('../lib/find-pages-dir')
const { interopDefault } =
require('../lib/interop-default') as typeof import('../lib/interop-default')
const { setGlobal } = require('../trace') as typeof import('../trace')
const { Telemetry } =
require('../telemetry/storage') as typeof import('../telemetry/storage')
// To regenerate the TURBOPACK gradient require('gradient-string')('blue', 'red')('>>> TURBOPACK') // To regenerate the TURBOPACK gradient require('gradient-string')('blue', 'red')('>>> TURBOPACK')
const isTTY = process.stdout.isTTY const isTTY = process.stdout.isTTY
...@@ -293,6 +329,7 @@ If you cannot make the changes above, but still want to try out\nNext.js v13 wit ...@@ -293,6 +329,7 @@ If you cannot make the changes above, but still want to try out\nNext.js v13 wit
const telemetry = new Telemetry({ const telemetry = new Telemetry({
distDir, distDir,
}) })
setGlobal('telemetry', telemetry)
telemetry.record( telemetry.record(
eventCliSession(distDir, rawNextConfig as NextConfigComplete, { eventCliSession(distDir, rawNextConfig as NextConfigComplete, {
......
const EVENT_VERSION = 'NEXT_CLI_SESSION_STOPPED'
export type EventCliSessionStopped = {
cliCommand: string
nextVersion: string
nodeVersion: string
turboFlag?: boolean | null
durationMilliseconds?: number | null
}
export function eventCliSession(
event: Omit<EventCliSessionStopped, 'nextVersion' | 'nodeVersion'>
): { eventName: string; payload: EventCliSessionStopped }[] {
// This should be an invariant, if it fails our build tooling is broken.
if (typeof process.env.__NEXT_VERSION !== 'string') {
return []
}
const payload: EventCliSessionStopped = {
nextVersion: process.env.__NEXT_VERSION,
nodeVersion: process.version,
cliCommand: event.cliCommand,
durationMilliseconds: event.durationMilliseconds,
...(typeof event.turboFlag !== 'undefined'
? {
turboFlag: !!event.turboFlag,
}
: {}),
}
return [{ eventName: EVENT_VERSION, payload }]
}
...@@ -10,6 +10,7 @@ import { ...@@ -10,6 +10,7 @@ import {
waitFor, waitFor,
nextBuild, nextBuild,
nextLint, nextLint,
check,
} from 'next-test-utils' } from 'next-test-utils'
const appDir = path.join(__dirname, '..') const appDir = path.join(__dirname, '..')
...@@ -394,6 +395,61 @@ describe('Telemetry CLI', () => { ...@@ -394,6 +395,61 @@ describe('Telemetry CLI', () => {
expect(event1).toMatch(/"turboFlag": true/) expect(event1).toMatch(/"turboFlag": true/)
}) })
it('detects --turbo correctly for `next dev` stopped', async () => {
let port = await findPort()
let stderr = ''
const handleStderr = (msg) => {
stderr += msg
}
let app = await launchApp(appDir, port, {
onStderr: handleStderr,
env: {
NEXT_TELEMETRY_DEBUG: 1,
},
turbo: true,
})
if (app) {
await killApp(app)
}
await check(() => stderr, /NEXT_CLI_SESSION_STOPPED/)
const event1 = /NEXT_CLI_SESSION_STOPPED[\s\S]+?{([\s\S]+?)}/
.exec(stderr)
.pop()
expect(event1).toMatch(/"turboFlag": true/)
})
it('detects correctly for `next dev` stopped (no turbo)', async () => {
let port = await findPort()
let stderr = ''
const handleStderr = (msg) => {
stderr += msg
}
let app = await launchApp(appDir, port, {
onStderr: handleStderr,
env: {
NEXT_TELEMETRY_DEBUG: 1,
},
})
await check(() => stderr, /NEXT_CLI_SESSION_STARTED/)
if (app) {
await killApp(app)
}
await check(() => stderr, /NEXT_CLI_SESSION_STOPPED/)
const event1 = /NEXT_CLI_SESSION_STOPPED[\s\S]+?{([\s\S]+?)}/
.exec(stderr)
.pop()
expect(event1).toMatch(/"turboFlag": false/)
})
it('detect reportWebVitals correctly for `next build`', async () => { it('detect reportWebVitals correctly for `next build`', async () => {
// Case 1: When _app.js does not exist. // Case 1: When _app.js does not exist.
let build = await nextBuild(appDir, [], { let build = await nextBuild(appDir, [], {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment