Unverified Commit 7693c1b1 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Defer tooling OpenTelemetry reporting

Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent 1c981d80
......@@ -1091,11 +1091,16 @@ export default async function build(
process.once('exit', () => flushTurbopackWarnings())
try {
const nextBuildSpan = trace('next-build', undefined, {
buildMode: experimentalBuildMode,
version: process.env.__NEXT_VERSION as string,
...enabledFeatures,
})
const nextBuildSpan = trace(
'next-build',
undefined,
{
buildMode: experimentalBuildMode,
version: process.env.__NEXT_VERSION as string,
...enabledFeatures,
},
true
)
NextBuildContext.nextBuildSpan = nextBuildSpan
NextBuildContext.dir = dir
......
......@@ -644,15 +644,22 @@ if (process.env.NEXT_PRIVATE_WORKER && process.send) {
)
}
startServerSpan = trace('start-dev-server', undefined, {
cpus: String(os.cpus().length),
platform: os.platform(),
'memory.freeMem': String(os.freemem()),
'memory.totalMem': String(os.totalmem()),
'memory.heapSizeLimit': String(v8.getHeapStatistics().heap_size_limit),
...enabledFeaturesFromParent,
...rageRestartAttrsFromParent,
})
startServerSpan = trace(
'start-dev-server',
undefined,
{
cpus: String(os.cpus().length),
platform: os.platform(),
'memory.freeMem': String(os.freemem()),
'memory.totalMem': String(os.totalmem()),
'memory.heapSizeLimit': String(
v8.getHeapStatistics().heap_size_limit
),
...enabledFeaturesFromParent,
...rageRestartAttrsFromParent,
},
true
)
initializeTraceState({
...exportTraceState(),
......
This diff is collapsed.
This diff is collapsed.
// Node-only app-tracer pairing. Local spans keep their existing timing, IDs and
// reporter, independently of application sampling (as in Request Insights).
import type { HrTime } from '@opentelemetry/api'
// Node-only local capture and clock/worker adapter. API/SDK work belongs to the
// deferred consumer; local recording remains independent of app sampling.
import type { TraceEvent } from './types'
import {
configureReporting,
......@@ -9,45 +8,40 @@ import {
replayCompleted,
} from './otel-reporting'
import type { ReportingRecord } from './otel-reporting'
export {
enableReporting,
flushReporting,
updateReporting,
} from './otel-reporting'
export { enableReporting, flushReporting } from './otel-reporting'
const NS_PER_SECOND = BigInt(1_000_000_000)
const NS_PER_MS = BigInt(1_000_000)
const NS_PER_US = BigInt(1_000)
let epochOffset = BigInt(Date.now()) * NS_PER_MS - process.hrtime.bigint()
export function epochTime(monotonic: bigint): HrTime {
const ns = monotonic + epochOffset
return [Number(ns / NS_PER_SECOND), Number(ns % NS_PER_SECOND)]
export function epochTime(monotonic: bigint): bigint {
return monotonic + epochOffset
}
export function startSpan(options: {
name: string
id: number
parentId?: number
attrs: Record<string, unknown>
startTime: bigint
reportingParent?: ReportingRecord
reportingRoot?: boolean
}): ReportingRecord | undefined {
return startReporting(
options.id,
options.parentId,
options.name,
epochTime(options.startTime),
options.attrs,
options.reportingParent
options.reportingParent,
options.reportingRoot
)
}
export function stopSpan(
record: ReportingRecord | undefined,
stopTime: bigint
stopTime: bigint,
tags: Record<string, unknown>
) {
endReporting(record, epochTime(stopTime))
endReporting(record, epochTime(stopTime), tags)
}
export function getClockOffset() {
......@@ -74,6 +68,8 @@ export function freshWorkerEvents(events: TraceEvent[]) {
export function replayWorkerEvents(events: TraceEvent[]) {
replayCompleted(events, (event) => ({
start: epochTime(BigInt(event.timestamp) * NS_PER_US),
end: epochTime(BigInt(event.timestamp + event.duration) * NS_PER_US),
end: epochTime(
(BigInt(event.timestamp) + BigInt(event.duration)) * NS_PER_US
),
}))
}
......@@ -57,6 +57,7 @@ export class Span {
startTime,
wallStartTimeMs,
reportingParent,
reportingRoot,
}: {
name: string
parentId?: SpanId
......@@ -66,6 +67,8 @@ export class Span {
wallStartTimeMs?: number
attrs?: Attributes
reportingParent?: ReportingRecord
// Internal entry-point marker: reserves the coordinator root descriptor.
reportingRoot?: boolean
}) {
this.id = getId()
this.parentId = parentId ?? defaultParentSpanId
......@@ -84,9 +87,9 @@ export class Span {
name,
id: this.id,
parentId: this.parentId,
attrs: this.attrs,
startTime: this._start,
reportingParent,
reportingRoot,
})
}
......@@ -124,7 +127,7 @@ export class Span {
}
}
} finally {
otel?.stopSpan(this.reporting, end)
otel?.stopSpan(this.reporting, end, this.attrs)
}
}
......@@ -170,7 +173,6 @@ export class Span {
setAttribute(key: string, value: unknown) {
this.attrs[key] = value
otel?.updateReporting(this.reporting, key, value)
}
traceFn<T>(fn: (span: Span) => T): T {
......@@ -199,9 +201,10 @@ export class Span {
export const trace = (
name: string,
parentId?: SpanId,
attrs?: { [key: string]: unknown }
attrs?: { [key: string]: unknown },
reportingRoot = false
) => {
return new Span({ name, parentId, attrs })
return new Span({ name, parentId, attrs, reportingRoot })
}
export const flushAllTraces = () => reporter.flushAll()
......
......@@ -144,6 +144,7 @@ describe('experimental tooling OTel', () => {
expect(tooling).toHaveLength(0)
return
}
expect(tooling.map((span) => span.name)).toContain(rootName)
const root = tooling.find((span) => span.name === rootName)!
expect(root).toBeDefined()
expect(root.parentSpanId).toBeUndefined()
......
This diff is collapsed.
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