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

Method wrap

packages/next/src/server/lib/trace/tracer.ts:436–471  ·  view source on GitHub ↗
(...args: Array<any>)

Source from the content-addressed store, hash-verified

434 fn: T
435 ): T
436 public wrap(...args: Array<any>) {
437 const tracer = this
438 const [name, options, fn] =
439 args.length === 3 ? args : [args[0], {}, args[1]]
440
441 if (
442 !NextVanillaSpanAllowlist.has(name) &&
443 process.env.NEXT_OTEL_VERBOSE !== '1'
444 ) {
445 return fn
446 }
447
448 return function (this: any) {
449 let optionsObj = options
450 if (typeof optionsObj === 'function' && typeof fn === 'function') {
451 optionsObj = optionsObj.apply(this, arguments)
452 }
453
454 const lastArgId = arguments.length - 1
455 const cb = arguments[lastArgId]
456
457 if (typeof cb === 'function') {
458 const scopeBoundCb = tracer.getContext().bind(context.active(), cb)
459 return tracer.trace(name, optionsObj, (_span, done) => {
460 arguments[lastArgId] = function (err: any) {
461 done?.(err)
462 return scopeBoundCb.apply(this, arguments)
463 }
464
465 return fn.apply(this, arguments)
466 })
467 } else {
468 return tracer.trace(name, optionsObj, () => fn.apply(this, arguments))
469 }
470 }
471 }
472
473 public startSpan(type: SpanTypes): Span
474 public startSpan(type: SpanTypes, options: TracerSpanOptions): Span

Callers

nothing calls this directly

Calls 7

getContextMethod · 0.65
traceMethod · 0.65
doneFunction · 0.50
hasMethod · 0.45
applyMethod · 0.45
bindMethod · 0.45
activeMethod · 0.45

Tested by

no test coverage detected