Unverified Commit 83657b6b authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Check if parentspan is provided in profiler (#22061)

Co-authored-by: 's avatarDale Bustad <dale@divmain.com>
parent 5c246702
...@@ -25,6 +25,17 @@ ...@@ -25,6 +25,17 @@
"port": 9229, "port": 9229,
"outFiles": ["${workspaceFolder}/packages/next/dist/**/*"] "outFiles": ["${workspaceFolder}/packages/next/dist/**/*"]
}, },
{
"name": "Launch app build trace",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "yarn",
"runtimeArgs": ["run", "trace-debug", "build", "test/integration/basic"],
"skipFiles": ["<node_internals>/**"],
"port": 9229,
"outFiles": ["${workspaceFolder}/packages/next/dist/**/*"]
},
{ {
"name": "Launch app production", "name": "Launch app production",
"type": "node", "type": "node",
......
...@@ -3,6 +3,7 @@ import api, { Span } from '@opentelemetry/api' ...@@ -3,6 +3,7 @@ import api, { Span } from '@opentelemetry/api'
export const tracer = api.trace.getTracer('next', process.env.__NEXT_VERSION) export const tracer = api.trace.getTracer('next', process.env.__NEXT_VERSION)
const compilerStacks = new WeakMap() const compilerStacks = new WeakMap()
const compilerStoppedSpans = new WeakMap()
export function stackPush(compiler: any, spanName: string, attrs?: any): any { export function stackPush(compiler: any, spanName: string, attrs?: any): any {
let stack = compilerStacks.get(compiler) let stack = compilerStacks.get(compiler)
...@@ -13,18 +14,20 @@ export function stackPush(compiler: any, spanName: string, attrs?: any): any { ...@@ -13,18 +14,20 @@ export function stackPush(compiler: any, spanName: string, attrs?: any): any {
span = tracer.startSpan(spanName, attrs ? attrs() : undefined) span = tracer.startSpan(spanName, attrs ? attrs() : undefined)
} else { } else {
const parent = stack[stack.length - 1] const parent = stack[stack.length - 1]
tracer.withSpan(parent, () => { if (parent) {
tracer.withSpan(parent, () => {
span = tracer.startSpan(spanName, attrs ? attrs() : undefined)
})
} else {
span = tracer.startSpan(spanName, attrs ? attrs() : undefined) span = tracer.startSpan(spanName, attrs ? attrs() : undefined)
}) }
} }
stack.push(span) stack.push(span)
return span return span
} }
export function stackPop(compiler: any, span: any) { export function stackPop(compiler: any, span: any, associatedName?: string) {
span.end()
let stack = compilerStacks.get(compiler) let stack = compilerStacks.get(compiler)
if (!stack) { if (!stack) {
console.warn( console.warn(
...@@ -32,15 +35,40 @@ export function stackPop(compiler: any, span: any) { ...@@ -32,15 +35,40 @@ export function stackPop(compiler: any, span: any) {
) )
return return
} }
const poppedSpan = stack.pop()
if (poppedSpan !== span) { let stoppedSpans: Set<Span> = compilerStoppedSpans.get(compiler)
stack.push(poppedSpan) if (!stoppedSpans) {
const spanIdx = stack.indexOf(span) stoppedSpans = new Set()
console.warn('Attempted to pop span that was not at top of stack.') compilerStoppedSpans.set(compiler, stoppedSpans)
if (spanIdx !== -1) { }
console.info( if (stoppedSpans.has(span)) {
`Span was found at index ${spanIdx} with stack size ${stack.length}` console.warn(
`Attempted to terminate tracing span that was already stopped for ${associatedName}`
)
return
}
while (true) {
let poppedSpan = stack.pop()
if (poppedSpan === span) {
stoppedSpans.add(poppedSpan)
span.end()
stoppedSpans.add(span)
break
} else if (poppedSpan === undefined || stack.indexOf(span) === -1) {
// We've either reached the top of the stack or the stack doesn't contain
// the span for another reason.
console.warn(`Tracing span was not found in stack for: ${associatedName}`)
stoppedSpans.add(span)
span.end()
break
} else if (stack.indexOf(span) !== -1) {
console.warn(
`Attempted to pop span that was not at top of stack for: ${associatedName}`
) )
stoppedSpans.add(poppedSpan)
poppedSpan.end()
} }
} }
} }
......
...@@ -54,7 +54,13 @@ export class ProfilingPlugin { ...@@ -54,7 +54,13 @@ export class ProfilingPlugin {
onSetSpan?.(span) onSetSpan?.(span)
}) })
stopHook.tap(pluginName, () => { stopHook.tap(pluginName, () => {
stackPop(this.compiler, span) // `stopHook` may be triggered when `startHook` has not in cases
// where `stopHook` is used as the terminating event for more
// than one pair of hooks.
if (!span) {
return
}
stackPop(this.compiler, span, spanName)
}) })
} }
...@@ -66,7 +72,7 @@ export class ProfilingPlugin { ...@@ -66,7 +72,7 @@ export class ProfilingPlugin {
} }
}) })
stopHook.tap(pluginName, () => { stopHook.tap(pluginName, () => {
stackPop(this.compiler, span) stackPop(this.compiler, span, spanName)
}) })
} }
......
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