( spanName: string, fn: (span: Span) => Promise<T>, attributes?: Record<string, string | number | boolean>, )
| 786 | } |
| 787 | |
| 788 | export 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. |
nothing calls this directly
no test coverage detected