( toolName: string, toolAttributes?: Record<string, string | number | boolean>, toolInput?: string, )
| 464 | } |
| 465 | |
| 466 | export function startToolSpan( |
| 467 | toolName: string, |
| 468 | toolAttributes?: Record<string, string | number | boolean>, |
| 469 | toolInput?: string, |
| 470 | ): Span { |
| 471 | // Start Perfetto span regardless of OTel tracing state |
| 472 | const perfettoSpanId = isPerfettoTracingEnabled() |
| 473 | ? startToolPerfettoSpan(toolName, toolAttributes) |
| 474 | : undefined |
| 475 | |
| 476 | if (!isAnyTracingEnabled()) { |
| 477 | // Still track Perfetto span even if OTel is disabled |
| 478 | if (perfettoSpanId) { |
| 479 | const dummySpan = trace.getActiveSpan() || getTracer().startSpan('dummy') |
| 480 | const spanId = getSpanId(dummySpan) |
| 481 | const spanContextObj: SpanContext = { |
| 482 | span: dummySpan, |
| 483 | startTime: Date.now(), |
| 484 | attributes: { 'span.type': 'tool', tool_name: toolName }, |
| 485 | perfettoSpanId, |
| 486 | } |
| 487 | activeSpans.set(spanId, new WeakRef(spanContextObj)) |
| 488 | toolContext.enterWith(spanContextObj) |
| 489 | return dummySpan |
| 490 | } |
| 491 | return trace.getActiveSpan() || getTracer().startSpan('dummy') |
| 492 | } |
| 493 | |
| 494 | const tracer = getTracer() |
| 495 | const parentSpanCtx = interactionContext.getStore() |
| 496 | |
| 497 | const attributes = createSpanAttributes('tool', { |
| 498 | tool_name: toolName, |
| 499 | ...toolAttributes, |
| 500 | }) |
| 501 | |
| 502 | const ctx = parentSpanCtx |
| 503 | ? trace.setSpan(otelContext.active(), parentSpanCtx.span) |
| 504 | : otelContext.active() |
| 505 | const span = tracer.startSpan('claude_code.tool', { attributes }, ctx) |
| 506 | |
| 507 | // Add experimental tool input attributes |
| 508 | if (toolInput) { |
| 509 | addBetaToolInputAttributes(span, toolName, toolInput) |
| 510 | } |
| 511 | |
| 512 | const spanId = getSpanId(span) |
| 513 | const spanContextObj: SpanContext = { |
| 514 | span, |
| 515 | startTime: Date.now(), |
| 516 | attributes, |
| 517 | perfettoSpanId, |
| 518 | } |
| 519 | activeSpans.set(spanId, new WeakRef(spanContextObj)) |
| 520 | |
| 521 | toolContext.enterWith(spanContextObj) |
| 522 | |
| 523 | return span |
no test coverage detected