MCPcopy
hub / github.com/vercel/next.js / reportSpanRecursively

Function reportSpanRecursively

scripts/trace-dd.mjs:29–78  ·  view source on GitHub ↗
(tracer, trace, parentSpan)

Source from the content-addressed store, hash-verified

27 * Create, reports spans recursively with its inner child spans.
28 */
29const reportSpanRecursively = (tracer, trace, parentSpan) => {
30 // build-* span contains tags with path to the modules, trying to clean up if possible
31 const isBuildModule = trace.name.startsWith('build-module-')
32 if (isBuildModule) {
33 trace.packageName = getPackageName(trace.tags.name)
34 // replace name to cleaned up pkg name
35 trace.tags.name = trace.packageName
36 if (trace.children) {
37 const queue = [...trace.children]
38 trace.children = []
39 for (const e of queue) {
40 if (e.name.startsWith('build-module-')) {
41 const pkgName = getPackageName(e.tags.name)
42 if (!trace.packageName || pkgName !== trace.packageName) {
43 trace.children.push(e)
44 } else {
45 if (e.children) queue.push(...e.children)
46 }
47 }
48 }
49 }
50 }
51
52 /**
53 * interface TraceEvent {
54 * traceId: string;
55 * parentId: number;
56 * name: string;
57 * id: number;
58 * startTime: number;
59 * timestamp: number;
60 * duration: number;
61 * tags: Record<string, any>
62 * }
63 */
64 let span = tracer.startSpan(trace.name, {
65 startTime: trace.startTime,
66 childOf: parentSpan,
67 tags: Object.keys(trace?.tags).length > 0 ? trace?.tags : undefined,
68 })
69
70 // Spans should be reported in chronological order
71 trace.children?.sort((a, b) => a.startTime - b.startTime)
72 trace.children?.forEach((childTrace) =>
73 reportSpanRecursively(tracer, childTrace, span)
74 )
75
76 span.finish(trace.startTime + trace.duration / 1000)
77 return span
78}
79
80/**
81 * Read generated trace from file system, augment & sent it to the remote tracer.

Callers 1

collectTracesFunction · 0.70

Calls 8

startsWithMethod · 0.80
getPackageNameFunction · 0.70
pushMethod · 0.65
startSpanMethod · 0.65
keysMethod · 0.65
finishMethod · 0.65
sortMethod · 0.45
forEachMethod · 0.45

Tested by

no test coverage detected