Unverified Commit 0e1463f9 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Telemetry: track failed builds and build bundler usage (#77943)

This:

- Tracks build failures as a separate event, `NEXT_BUILD_FAILED`
- Added `bundler` property to both `NEXT_BUILD_COMPLETED` and `NEXT_BUILD_FAILED`
- Requires a server-side update

Alternatives considered:

Considered adding a `success: boolean` property to the existing `EventBuildCompleted`. There are a few issues with this:

- Expands the semantics of the existing event. This may have unintended consequences if we don’t change how we report on these events to filter success state
- This event requires page paths and timing information from the compiler, neither of which are guaranteed to exist on failures

Test Plan:
- Added a syntax error to a module and built with `NEXT_TELEMETRY_DEBUG=1`. Verified the failure event is sent along with the bundler value
- Verified bundler values are now sent for webpack, rspack, and Turbopack on build successes
parent 733c5705
...@@ -107,6 +107,7 @@ import { ...@@ -107,6 +107,7 @@ import {
EVENT_BUILD_FEATURE_USAGE, EVENT_BUILD_FEATURE_USAGE,
eventPackageUsedInGetServerSideProps, eventPackageUsedInGetServerSideProps,
eventBuildCompleted, eventBuildCompleted,
eventBuildFailed,
} from '../telemetry/events' } from '../telemetry/events'
import type { EventBuildFeatureUsage } from '../telemetry/events' import type { EventBuildFeatureUsage } from '../telemetry/events'
import { Telemetry } from '../telemetry/storage' import { Telemetry } from '../telemetry/storage'
...@@ -210,6 +211,8 @@ import { isPersistentCachingEnabled } from '../shared/lib/turbopack/utils' ...@@ -210,6 +211,8 @@ import { isPersistentCachingEnabled } from '../shared/lib/turbopack/utils'
import { inlineStaticEnv } from '../lib/inline-static-env' import { inlineStaticEnv } from '../lib/inline-static-env'
import { populateStaticEnv } from '../lib/static-env' import { populateStaticEnv } from '../lib/static-env'
import { durationToString } from './duration-to-string' import { durationToString } from './duration-to-string'
import { traceGlobals } from '../trace/shared'
import { extractNextErrorCode } from '../lib/error-telemetry-utils'
type Fallback = null | boolean | string type Fallback = null | boolean | string
...@@ -826,6 +829,7 @@ export default async function build( ...@@ -826,6 +829,7 @@ export default async function build(
const isCompileMode = experimentalBuildMode === 'compile' const isCompileMode = experimentalBuildMode === 'compile'
const isGenerateMode = experimentalBuildMode === 'generate' const isGenerateMode = experimentalBuildMode === 'generate'
NextBuildContext.isCompileMode = isCompileMode NextBuildContext.isCompileMode = isCompileMode
const buildStartTime = Date.now()
let loadedConfig: NextConfigComplete | undefined let loadedConfig: NextConfigComplete | undefined
try { try {
...@@ -1471,6 +1475,7 @@ export default async function build( ...@@ -1471,6 +1475,7 @@ export default async function build(
telemetry.record( telemetry.record(
eventBuildCompleted(pagesPaths, { eventBuildCompleted(pagesPaths, {
bundler: 'turbopack',
durationInSeconds: Math.round(compilerDuration), durationInSeconds: Math.round(compilerDuration),
totalAppPagesCount, totalAppPagesCount,
}) })
...@@ -1558,6 +1563,7 @@ export default async function build( ...@@ -1558,6 +1563,7 @@ export default async function build(
telemetry.record( telemetry.record(
eventBuildCompleted(pagesPaths, { eventBuildCompleted(pagesPaths, {
bundler: getBundlerForTelemetry(isTurbopack),
durationInSeconds, durationInSeconds,
totalAppPagesCount, totalAppPagesCount,
}) })
...@@ -1573,6 +1579,7 @@ export default async function build( ...@@ -1573,6 +1579,7 @@ export default async function build(
telemetry.record( telemetry.record(
eventBuildCompleted(pagesPaths, { eventBuildCompleted(pagesPaths, {
bundler: getBundlerForTelemetry(isTurbopack),
durationInSeconds: compilerDuration, durationInSeconds: compilerDuration,
totalAppPagesCount, totalAppPagesCount,
}) })
...@@ -3705,6 +3712,18 @@ export default async function build( ...@@ -3705,6 +3712,18 @@ export default async function build(
await shutdownPromise await shutdownPromise
}) })
} catch (e) {
const telemetry: Telemetry | undefined = traceGlobals.get('telemetry')
if (telemetry) {
telemetry.record(
eventBuildFailed({
bundler: getBundlerForTelemetry(isTurbopack),
errorCode: getErrorCodeForTelemetry(e),
durationInSeconds: Math.floor((Date.now() - buildStartTime) / 1000),
})
)
}
throw e
} finally { } finally {
// Ensure we wait for lockfile patching if present // Ensure we wait for lockfile patching if present
await lockfilePatchPromise.cur await lockfilePatchPromise.cur
...@@ -3751,3 +3770,32 @@ function warnAboutTurbopackBuilds(config?: NextConfigComplete) { ...@@ -3751,3 +3770,32 @@ function warnAboutTurbopackBuilds(config?: NextConfigComplete) {
Log.warn(warningStr) Log.warn(warningStr)
} }
function getBundlerForTelemetry(isTurbopack: boolean) {
if (isTurbopack) {
return 'turbopack'
}
if (process.env.NEXT_RSPACK) {
return 'rspack'
}
return 'webpack'
}
function getErrorCodeForTelemetry(err: unknown) {
const code = extractNextErrorCode(err)
if (code != null) {
return code
}
if (err instanceof Error && 'code' in err && typeof err.code === 'string') {
return err.code
}
if (err instanceof Error) {
return err.name
}
return 'Unknown'
}
...@@ -54,6 +54,7 @@ export function eventLintCheckCompleted(event: EventLintCheckCompleted): { ...@@ -54,6 +54,7 @@ export function eventLintCheckCompleted(event: EventLintCheckCompleted): {
const EVENT_BUILD_COMPLETED = 'NEXT_BUILD_COMPLETED' const EVENT_BUILD_COMPLETED = 'NEXT_BUILD_COMPLETED'
type EventBuildCompleted = { type EventBuildCompleted = {
bundler: 'webpack' | 'rspack' | 'turbopack'
durationInSeconds: number durationInSeconds: number
totalPageCount: number totalPageCount: number
hasDunderPages: boolean hasDunderPages: boolean
...@@ -85,6 +86,20 @@ export function eventBuildCompleted( ...@@ -85,6 +86,20 @@ export function eventBuildCompleted(
} }
} }
const EVENT_BUILD_FAILED = 'NEXT_BUILD_FAILED'
type EventBuildFailed = {
bundler: 'webpack' | 'rspack' | 'turbopack'
errorCode: string
durationInSeconds: number
}
export function eventBuildFailed(event: EventBuildFailed) {
return {
eventName: EVENT_BUILD_FAILED,
payload: event,
}
}
const EVENT_BUILD_OPTIMIZED = 'NEXT_BUILD_OPTIMIZED' const EVENT_BUILD_OPTIMIZED = 'NEXT_BUILD_OPTIMIZED'
type EventBuildOptimized = { type EventBuildOptimized = {
durationInSeconds: number durationInSeconds: number
......
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