MCPcopy Index your code
hub / github.com/codeaashu/claude-code / executeInSpan

Function executeInSpan

src/utils/telemetry/sessionTracing.ts:788–833  ·  view source on GitHub ↗
(
  spanName: string,
  fn: (span: Span) => Promise<T>,
  attributes?: Record<string, string | number | boolean>,
)

Source from the content-addressed store, hash-verified

786}
787
788export async function executeInSpan<T>(
789 spanName: string,
790 fn: (span: Span) => Promise<T>,
791 attributes?: Record<string, string | number | boolean>,
792): Promise<T> {
793 if (!isAnyTracingEnabled()) {
794 return fn(trace.getActiveSpan() || getTracer().startSpan('dummy'))
795 }
796
797 const tracer = getTracer()
798 const parentSpanCtx = toolContext.getStore() ?? interactionContext.getStore()
799
800 const finalAttributes = createSpanAttributes('tool', {
801 ...attributes,
802 })
803
804 const ctx = parentSpanCtx
805 ? trace.setSpan(otelContext.active(), parentSpanCtx.span)
806 : otelContext.active()
807 const span = tracer.startSpan(spanName, { attributes: finalAttributes }, ctx)
808
809 const spanId = getSpanId(span)
810 const spanContextObj: SpanContext = {
811 span,
812 startTime: Date.now(),
813 attributes: finalAttributes,
814 }
815 activeSpans.set(spanId, new WeakRef(spanContextObj))
816 strongSpans.set(spanId, spanContextObj)
817
818 try {
819 const result = await fn(span)
820 span.end()
821 activeSpans.delete(spanId)
822 strongSpans.delete(spanId)
823 return result
824 } catch (error) {
825 if (error instanceof Error) {
826 span.recordException(error)
827 }
828 span.end()
829 activeSpans.delete(spanId)
830 strongSpans.delete(spanId)
831 throw error
832 }
833}
834
835/**
836 * Start a hook execution span.

Callers

nothing calls this directly

Calls 8

isAnyTracingEnabledFunction · 0.85
getTracerFunction · 0.85
createSpanAttributesFunction · 0.85
getSpanIdFunction · 0.85
deleteMethod · 0.65
activeMethod · 0.45
setMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected