| import { reporter } from './report' |
| import type { SpanId, TraceEvent, TraceState } from './types' |
|
|
| const NUM_OF_MICROSEC_IN_NANOSEC = BigInt('1000') |
| const NUM_OF_MILLISEC_IN_NANOSEC = BigInt('1000000') |
| let count = 0 |
| const getId = () => { |
| count++ |
| return count |
| } |
| let defaultParentSpanId: SpanId | undefined |
| let shouldSaveTraceEvents: boolean | undefined |
| let savedTraceEvents: TraceEvent[] = [] |
|
|
| const RECORD_SPAN_THRESHOLD_MS = parseInt( |
| process.env.NEXT_TRACE_SPAN_THRESHOLD_MS ?? '-1' |
| ) |
|
|
| |
| |
| export enum SpanStatus { |
| Started = 'started', |
| Stopped = 'stopped', |
| } |
|
|
| interface Attributes { |
| [key: string]: string |
| } |
|
|
| export class Span { |
| private name: string |
| private id: SpanId |
| private parentId?: SpanId |
| private attrs: { [key: string]: any } |
| private status: SpanStatus |
| private now: number |
|
|
| |
| private _start: bigint |
|
|
| constructor({ |
| name, |
| parentId, |
| attrs, |
| startTime, |
| }: { |
| name: string |
| parentId?: SpanId |
| startTime?: bigint |
| attrs?: Attributes |
| }) { |
| this.name = name |
| this.parentId = parentId ?? defaultParentSpanId |
| this.attrs = attrs ? { ...attrs } : {} |
|
|
| this.status = SpanStatus.Started |
| this.id = getId() |
| this._start = startTime || process.hrtime.bigint() |
| |
| |
| |
| |
| |
| this.now = Date.now() |
| } |
|
|
| |
| |
| |
| |
| stop(stopTime?: bigint) { |
| if (this.status === SpanStatus.Stopped) { |
| |
| |
| return |
| } |
| const end: bigint = stopTime || process.hrtime.bigint() |
| const duration = (end - this._start) / NUM_OF_MICROSEC_IN_NANOSEC |
| this.status = SpanStatus.Stopped |
| if (duration > Number.MAX_SAFE_INTEGER) { |
| throw new Error(`Duration is too long to express as float64: ${duration}`) |
| } |
| const timestamp = this._start / NUM_OF_MICROSEC_IN_NANOSEC |
| const traceEvent: TraceEvent = { |
| name: this.name, |
| duration: Number(duration), |
| timestamp: Number(timestamp), |
| id: this.id, |
| parentId: this.parentId, |
| tags: this.attrs, |
| startTime: this.now, |
| } |
| if (duration > RECORD_SPAN_THRESHOLD_MS * 1000) { |
| reporter.report(traceEvent) |
| if (shouldSaveTraceEvents) { |
| savedTraceEvents.push(traceEvent) |
| } |
| } |
| } |
|
|
| traceChild(name: string, attrs?: Attributes) { |
| return new Span({ name, parentId: this.id, attrs }) |
| } |
|
|
| manualTraceChild( |
| name: string, |
| |
| startTime?: bigint, |
| |
| stopTime?: bigint, |
| attrs?: Attributes |
| ) { |
| |
| const correction = |
| process.hrtime.bigint() - BigInt(Date.now()) * NUM_OF_MILLISEC_IN_NANOSEC |
| const span = new Span({ |
| name, |
| parentId: this.id, |
| attrs, |
| startTime: startTime ? startTime + correction : process.hrtime.bigint(), |
| }) |
| span.stop(stopTime ? stopTime + correction : process.hrtime.bigint()) |
| } |
|
|
| getId() { |
| return this.id |
| } |
|
|
| setAttribute(key: string, value: string) { |
| this.attrs[key] = value |
| } |
|
|
| traceFn<T>(fn: (span: Span) => T): T { |
| try { |
| return fn(this) |
| } finally { |
| this.stop() |
| } |
| } |
|
|
| async traceAsyncFn<T>(fn: (span: Span) => T | Promise<T>): Promise<T> { |
| try { |
| return await fn(this) |
| } finally { |
| this.stop() |
| } |
| } |
| } |
|
|
| export const trace = ( |
| name: string, |
| parentId?: SpanId, |
| attrs?: { [key: string]: string } |
| ) => { |
| return new Span({ name, parentId, attrs }) |
| } |
|
|
| export const flushAllTraces = (opts?: { end: boolean }) => |
| reporter.flushAll(opts) |
|
|
| |
| |
| |
| export const exportTraceState = (): TraceState => ({ |
| defaultParentSpanId, |
| lastId: count, |
| shouldSaveTraceEvents, |
| }) |
| export const initializeTraceState = (state: TraceState) => { |
| count = state.lastId |
| defaultParentSpanId = state.defaultParentSpanId |
| shouldSaveTraceEvents = state.shouldSaveTraceEvents |
| } |
|
|
| export function getTraceEvents(): TraceEvent[] { |
| return savedTraceEvents |
| } |
|
|
| export function recordTraceEvents(events: TraceEvent[]) { |
| for (const traceEvent of events) { |
| reporter.report(traceEvent) |
| if (traceEvent.id > count) { |
| count = traceEvent.id + 1 |
| } |
| } |
| if (shouldSaveTraceEvents) { |
| savedTraceEvents.push(...events) |
| } |
| } |
|
|
| export const clearTraceEvents = () => (savedTraceEvents = []) |
|
|