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