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