|
|
import type { FetchEventResult } from '../../web/types' |
|
|
import type { TextMapSetter } from '@opentelemetry/api' |
|
|
import type { SpanTypes } from './constants' |
|
|
import { LogSpanAllowList, NextVanillaSpanAllowlist } from './constants' |
|
|
|
|
|
import type { |
|
|
ContextAPI, |
|
|
Span, |
|
|
SpanOptions, |
|
|
Tracer, |
|
|
AttributeValue, |
|
|
TextMapGetter, |
|
|
} from 'next/dist/compiled/@opentelemetry/api' |
|
|
import { isThenable } from '../../../shared/lib/is-thenable' |
|
|
|
|
|
let api: typeof import('next/dist/compiled/@opentelemetry/api') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (process.env.NEXT_RUNTIME === 'edge') { |
|
|
api = require('@opentelemetry/api') as typeof import('@opentelemetry/api') |
|
|
} else { |
|
|
try { |
|
|
api = require('@opentelemetry/api') as typeof import('@opentelemetry/api') |
|
|
} catch (err) { |
|
|
api = |
|
|
require('next/dist/compiled/@opentelemetry/api') as typeof import('next/dist/compiled/@opentelemetry/api') |
|
|
} |
|
|
} |
|
|
|
|
|
const { context, propagation, trace, SpanStatusCode, SpanKind, ROOT_CONTEXT } = |
|
|
api |
|
|
|
|
|
export class BubbledError extends Error { |
|
|
constructor( |
|
|
public readonly bubble?: boolean, |
|
|
public readonly result?: FetchEventResult |
|
|
) { |
|
|
super() |
|
|
} |
|
|
} |
|
|
|
|
|
export function isBubbledError(error: unknown): error is BubbledError { |
|
|
if (typeof error !== 'object' || error === null) return false |
|
|
return error instanceof BubbledError |
|
|
} |
|
|
|
|
|
const closeSpanWithError = (span: Span, error?: Error) => { |
|
|
if (isBubbledError(error) && error.bubble) { |
|
|
span.setAttribute('next.bubble', true) |
|
|
} else { |
|
|
if (error) { |
|
|
span.recordException(error) |
|
|
} |
|
|
span.setStatus({ code: SpanStatusCode.ERROR, message: error?.message }) |
|
|
} |
|
|
span.end() |
|
|
} |
|
|
|
|
|
type TracerSpanOptions = Omit<SpanOptions, 'attributes'> & { |
|
|
parentSpan?: Span |
|
|
spanName?: string |
|
|
attributes?: Partial<Record<AttributeNames, AttributeValue | undefined>> |
|
|
hideSpan?: boolean |
|
|
} |
|
|
|
|
|
interface NextTracer { |
|
|
getContext(): ContextAPI |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
trace<T>( |
|
|
type: SpanTypes, |
|
|
fn: (span?: Span, done?: (error?: Error) => any) => Promise<T> |
|
|
): Promise<T> |
|
|
trace<T>( |
|
|
type: SpanTypes, |
|
|
fn: (span?: Span, done?: (error?: Error) => any) => T |
|
|
): T |
|
|
trace<T>( |
|
|
type: SpanTypes, |
|
|
options: TracerSpanOptions, |
|
|
fn: (span?: Span, done?: (error?: Error) => any) => Promise<T> |
|
|
): Promise<T> |
|
|
trace<T>( |
|
|
type: SpanTypes, |
|
|
options: TracerSpanOptions, |
|
|
fn: (span?: Span, done?: (error?: Error) => any) => T |
|
|
): T |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wrap<T = (...args: Array<any>) => any>(type: SpanTypes, fn: T): T |
|
|
wrap<T = (...args: Array<any>) => any>( |
|
|
type: SpanTypes, |
|
|
options: TracerSpanOptions, |
|
|
fn: T |
|
|
): T |
|
|
wrap<T = (...args: Array<any>) => any>( |
|
|
type: SpanTypes, |
|
|
options: (...args: any[]) => TracerSpanOptions, |
|
|
fn: T |
|
|
): T |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
startSpan(type: SpanTypes): Span |
|
|
startSpan(type: SpanTypes, options: TracerSpanOptions): Span |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getActiveScopeSpan(): Span | undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getTracePropagationData(): ClientTraceDataEntry[] |
|
|
} |
|
|
|
|
|
type NextAttributeNames = |
|
|
| 'next.route' |
|
|
| 'next.page' |
|
|
| 'next.rsc' |
|
|
| 'next.segment' |
|
|
| 'next.span_name' |
|
|
| 'next.span_type' |
|
|
| 'next.clientComponentLoadCount' |
|
|
type OTELAttributeNames = `http.${string}` | `net.${string}` |
|
|
type AttributeNames = NextAttributeNames | OTELAttributeNames |
|
|
|
|
|
|
|
|
const rootSpanAttributesStore = new Map< |
|
|
number, |
|
|
Map<AttributeNames, AttributeValue | undefined> |
|
|
>() |
|
|
const rootSpanIdKey = api.createContextKey('next.rootSpanId') |
|
|
let lastSpanId = 0 |
|
|
const getSpanId = () => lastSpanId++ |
|
|
|
|
|
export interface ClientTraceDataEntry { |
|
|
key: string |
|
|
value: string |
|
|
} |
|
|
|
|
|
const clientTraceDataSetter: TextMapSetter<ClientTraceDataEntry[]> = { |
|
|
set(carrier, key, value) { |
|
|
carrier.push({ |
|
|
key, |
|
|
value, |
|
|
}) |
|
|
}, |
|
|
} |
|
|
|
|
|
class NextTracerImpl implements NextTracer { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private getTracerInstance(): Tracer { |
|
|
return trace.getTracer('next.js', '0.0.1') |
|
|
} |
|
|
|
|
|
public getContext(): ContextAPI { |
|
|
return context |
|
|
} |
|
|
|
|
|
public getTracePropagationData(): ClientTraceDataEntry[] { |
|
|
const activeContext = context.active() |
|
|
const entries: ClientTraceDataEntry[] = [] |
|
|
propagation.inject(activeContext, entries, clientTraceDataSetter) |
|
|
return entries |
|
|
} |
|
|
|
|
|
public getActiveScopeSpan(): Span | undefined { |
|
|
return trace.getSpan(context?.active()) |
|
|
} |
|
|
|
|
|
public withPropagatedContext<T, C>( |
|
|
carrier: C, |
|
|
fn: () => T, |
|
|
getter?: TextMapGetter<C> |
|
|
): T { |
|
|
const activeContext = context.active() |
|
|
if (trace.getSpanContext(activeContext)) { |
|
|
|
|
|
return fn() |
|
|
} |
|
|
const remoteContext = propagation.extract(activeContext, carrier, getter) |
|
|
return context.with(remoteContext, fn) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public trace<T>( |
|
|
type: SpanTypes, |
|
|
fn: (span?: Span, done?: (error?: Error) => any) => Promise<T> |
|
|
): Promise<T> |
|
|
public trace<T>( |
|
|
type: SpanTypes, |
|
|
fn: (span?: Span, done?: (error?: Error) => any) => T |
|
|
): T |
|
|
public trace<T>( |
|
|
type: SpanTypes, |
|
|
options: TracerSpanOptions, |
|
|
fn: (span?: Span, done?: (error?: Error) => any) => Promise<T> |
|
|
): Promise<T> |
|
|
public trace<T>( |
|
|
type: SpanTypes, |
|
|
options: TracerSpanOptions, |
|
|
fn: (span?: Span, done?: (error?: Error) => any) => T |
|
|
): T |
|
|
public trace<T>(...args: Array<any>) { |
|
|
const [type, fnOrOptions, fnOrEmpty] = args |
|
|
|
|
|
|
|
|
const { |
|
|
fn, |
|
|
options, |
|
|
}: { |
|
|
fn: (span?: Span, done?: (error?: Error) => any) => T | Promise<T> |
|
|
options: TracerSpanOptions |
|
|
} = |
|
|
typeof fnOrOptions === 'function' |
|
|
? { |
|
|
fn: fnOrOptions, |
|
|
options: {}, |
|
|
} |
|
|
: { |
|
|
fn: fnOrEmpty, |
|
|
options: { ...fnOrOptions }, |
|
|
} |
|
|
|
|
|
const spanName = options.spanName ?? type |
|
|
|
|
|
if ( |
|
|
(!NextVanillaSpanAllowlist.includes(type) && |
|
|
process.env.NEXT_OTEL_VERBOSE !== '1') || |
|
|
options.hideSpan |
|
|
) { |
|
|
return fn() |
|
|
} |
|
|
|
|
|
|
|
|
let spanContext = this.getSpanContext( |
|
|
options?.parentSpan ?? this.getActiveScopeSpan() |
|
|
) |
|
|
let isRootSpan = false |
|
|
|
|
|
if (!spanContext) { |
|
|
spanContext = context?.active() ?? ROOT_CONTEXT |
|
|
isRootSpan = true |
|
|
} else if (trace.getSpanContext(spanContext)?.isRemote) { |
|
|
isRootSpan = true |
|
|
} |
|
|
|
|
|
const spanId = getSpanId() |
|
|
|
|
|
options.attributes = { |
|
|
'next.span_name': spanName, |
|
|
'next.span_type': type, |
|
|
...options.attributes, |
|
|
} |
|
|
|
|
|
return context.with(spanContext.setValue(rootSpanIdKey, spanId), () => |
|
|
this.getTracerInstance().startActiveSpan( |
|
|
spanName, |
|
|
options, |
|
|
(span: Span) => { |
|
|
const startTime = |
|
|
'performance' in globalThis && 'measure' in performance |
|
|
? globalThis.performance.now() |
|
|
: undefined |
|
|
|
|
|
const onCleanup = () => { |
|
|
rootSpanAttributesStore.delete(spanId) |
|
|
if ( |
|
|
startTime && |
|
|
process.env.NEXT_OTEL_PERFORMANCE_PREFIX && |
|
|
LogSpanAllowList.includes(type || ('' as any)) |
|
|
) { |
|
|
performance.measure( |
|
|
`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-${( |
|
|
type.split('.').pop() || '' |
|
|
).replace( |
|
|
/[A-Z]/g, |
|
|
(match: string) => '-' + match.toLowerCase() |
|
|
)}`, |
|
|
{ |
|
|
start: startTime, |
|
|
end: performance.now(), |
|
|
} |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
if (isRootSpan) { |
|
|
rootSpanAttributesStore.set( |
|
|
spanId, |
|
|
new Map( |
|
|
Object.entries(options.attributes ?? {}) as [ |
|
|
AttributeNames, |
|
|
AttributeValue | undefined, |
|
|
][] |
|
|
) |
|
|
) |
|
|
} |
|
|
try { |
|
|
if (fn.length > 1) { |
|
|
return fn(span, (err) => closeSpanWithError(span, err)) |
|
|
} |
|
|
|
|
|
const result = fn(span) |
|
|
if (isThenable(result)) { |
|
|
|
|
|
return result |
|
|
.then((res) => { |
|
|
span.end() |
|
|
|
|
|
|
|
|
return res |
|
|
}) |
|
|
.catch((err) => { |
|
|
closeSpanWithError(span, err) |
|
|
throw err |
|
|
}) |
|
|
.finally(onCleanup) |
|
|
} else { |
|
|
span.end() |
|
|
onCleanup() |
|
|
} |
|
|
|
|
|
return result |
|
|
} catch (err: any) { |
|
|
closeSpanWithError(span, err) |
|
|
onCleanup() |
|
|
throw err |
|
|
} |
|
|
} |
|
|
) |
|
|
) |
|
|
} |
|
|
|
|
|
public wrap<T = (...args: Array<any>) => any>(type: SpanTypes, fn: T): T |
|
|
public wrap<T = (...args: Array<any>) => any>( |
|
|
type: SpanTypes, |
|
|
options: TracerSpanOptions, |
|
|
fn: T |
|
|
): T |
|
|
public wrap<T = (...args: Array<any>) => any>( |
|
|
type: SpanTypes, |
|
|
options: (...args: any[]) => TracerSpanOptions, |
|
|
fn: T |
|
|
): T |
|
|
public wrap(...args: Array<any>) { |
|
|
const tracer = this |
|
|
const [name, options, fn] = |
|
|
args.length === 3 ? args : [args[0], {}, args[1]] |
|
|
|
|
|
if ( |
|
|
!NextVanillaSpanAllowlist.includes(name) && |
|
|
process.env.NEXT_OTEL_VERBOSE !== '1' |
|
|
) { |
|
|
return fn |
|
|
} |
|
|
|
|
|
return function (this: any) { |
|
|
let optionsObj = options |
|
|
if (typeof optionsObj === 'function' && typeof fn === 'function') { |
|
|
optionsObj = optionsObj.apply(this, arguments) |
|
|
} |
|
|
|
|
|
const lastArgId = arguments.length - 1 |
|
|
const cb = arguments[lastArgId] |
|
|
|
|
|
if (typeof cb === 'function') { |
|
|
const scopeBoundCb = tracer.getContext().bind(context.active(), cb) |
|
|
return tracer.trace(name, optionsObj, (_span, done) => { |
|
|
arguments[lastArgId] = function (err: any) { |
|
|
done?.(err) |
|
|
return scopeBoundCb.apply(this, arguments) |
|
|
} |
|
|
|
|
|
return fn.apply(this, arguments) |
|
|
}) |
|
|
} else { |
|
|
return tracer.trace(name, optionsObj, () => fn.apply(this, arguments)) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
public startSpan(type: SpanTypes): Span |
|
|
public startSpan(type: SpanTypes, options: TracerSpanOptions): Span |
|
|
public startSpan(...args: Array<any>): Span { |
|
|
const [type, options]: [string, TracerSpanOptions | undefined] = args as any |
|
|
|
|
|
const spanContext = this.getSpanContext( |
|
|
options?.parentSpan ?? this.getActiveScopeSpan() |
|
|
) |
|
|
return this.getTracerInstance().startSpan(type, options, spanContext) |
|
|
} |
|
|
|
|
|
private getSpanContext(parentSpan?: Span) { |
|
|
const spanContext = parentSpan |
|
|
? trace.setSpan(context.active(), parentSpan) |
|
|
: undefined |
|
|
|
|
|
return spanContext |
|
|
} |
|
|
|
|
|
public getRootSpanAttributes() { |
|
|
const spanId = context.active().getValue(rootSpanIdKey) as number |
|
|
return rootSpanAttributesStore.get(spanId) |
|
|
} |
|
|
|
|
|
public setRootSpanAttribute(key: AttributeNames, value: AttributeValue) { |
|
|
const spanId = context.active().getValue(rootSpanIdKey) as number |
|
|
const attributes = rootSpanAttributesStore.get(spanId) |
|
|
if (attributes) { |
|
|
attributes.set(key, value) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
const getTracer = (() => { |
|
|
const tracer = new NextTracerImpl() |
|
|
|
|
|
return () => tracer |
|
|
})() |
|
|
|
|
|
export { getTracer, SpanStatusCode, SpanKind } |
|
|
export type { NextTracer, Span, SpanOptions, ContextAPI, TracerSpanOptions } |
|
|
|