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

Tracing: tag spans as failed if their runner fn fails (#91206)

This adds a dedicated attribute, `failed`, to a span representing if the
procedure failed. It's then set if the runner function threw.

Test Plan: Added an e2e test
parent 21b9f6ba
...@@ -133,6 +133,9 @@ export class Span { ...@@ -133,6 +133,9 @@ export class Span {
traceFn<T>(fn: (span: Span) => T): T { traceFn<T>(fn: (span: Span) => T): T {
try { try {
return fn(this) return fn(this)
} catch (e) {
this.attrs['failed'] = true
throw e
} finally { } finally {
this.stop() this.stop()
} }
...@@ -141,6 +144,9 @@ export class Span { ...@@ -141,6 +144,9 @@ export class Span {
async traceAsyncFn<T>(fn: (span: Span) => T | Promise<T>): Promise<T> { async traceAsyncFn<T>(fn: (span: Span) => T | Promise<T>): Promise<T> {
try { try {
return await fn(this) return await fn(this)
} catch (e) {
this.attrs['failed'] = true
throw e
} finally { } finally {
this.stop() this.stop()
} }
......
import { nextTestSetup, isNextStart } from 'e2e-utils'
import { join } from 'path'
import { readFileSync } from 'fs'
import type { TraceEvent } from 'next/dist/trace'
function parseTraceFile(tracePath: string): TraceEvent[] {
const content = readFileSync(tracePath, 'utf8')
const events: TraceEvent[] = []
for (const line of content.trim().split('\n').filter(Boolean)) {
events.push(...(JSON.parse(line) as TraceEvent[]))
}
return events
}
describe('build-failed-trace', () => {
if (!isNextStart) {
return
}
const { next } = nextTestSetup({
files: __dirname,
skipStart: true,
skipDeployment: true,
})
it('should mark the next-build span as failed when the build fails', async () => {
const { exitCode } = await next.build()
expect(exitCode).not.toBe(0)
const tracePath = join(next.testDir, '.next', 'trace')
const events = parseTraceFile(tracePath)
const nextBuildEvent = events.find((e) => e.name === 'next-build')
expect(nextBuildEvent).toBeDefined()
expect(nextBuildEvent!.tags).toMatchObject({ failed: true })
})
})
module.exports = {
redirects: async () => {
throw new Error('intentional build failure for trace testing')
},
}
export default function Page() {
return <p>hello world</p>
}
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