MCPcopy Create free account
hub / github.com/Noumena-Network/code / executeInSpan

Function executeInSpan

src/utils/telemetry/sessionTracing.ts:789–834  ·  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

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

Callers

nothing calls this directly

Calls 8

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

Tested by

no test coverage detected