diff --git a/docs/observability/opentelemetry.md b/docs/observability/opentelemetry.md index ad86145e40..b3932ce66a 100644 --- a/docs/observability/opentelemetry.md +++ b/docs/observability/opentelemetry.md @@ -152,29 +152,36 @@ When the CLI invokes a tool handler, the `traceparent` and `tracestate` from the ```typescript +import { defineTool } from "@github/copilot-sdk"; import { propagation, context, trace } from "@opentelemetry/api"; -session.registerTool(myTool, async (args, invocation) => { - // Restore the CLI's trace context as the active context - const carrier = { - traceparent: invocation.traceparent, - tracestate: invocation.tracestate, - }; - const parentCtx = propagation.extract(context.active(), carrier); - - // Create a child span under the CLI's span - const tracer = trace.getTracer("my-app"); - return context.with(parentCtx, () => - tracer.startActiveSpan("my-tool", async (span) => { - try { - const result = await doWork(args); - return result; - } finally { - span.end(); - } - }) - ); +const myTool = defineTool("my-tool", { + description: "Do work", + handler: async (args, invocation) => { + // Restore the CLI's trace context as the active context + const carrier = { + traceparent: invocation.traceparent, + tracestate: invocation.tracestate, + }; + const parentCtx = propagation.extract(context.active(), carrier); + + // Create a child span under the CLI's span + const tracer = trace.getTracer("my-app"); + return context.with(parentCtx, () => + tracer.startActiveSpan("my-tool", async (span) => { + try { + const result = await doWork(args); + return result; + } finally { + span.end(); + } + }) + ); + }, }); + +// Tool handlers are registered when the session is created. +const session = await client.createSession({ tools: [myTool] }); ``` ### Per-language dependencies