You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Replace the optional @opentelemetry/api peer dependency with a
user-provided callback approach:
- Add TraceContext interface and TraceContextProvider type
- Add onGetTraceContext callback to CopilotClientOptions
- Pass traceparent/tracestate directly on ToolInvocation for inbound context
- Remove @opentelemetry/api from peerDependencies and devDependencies
- Rewrite telemetry.ts to a simple callback-based helper (~27 lines)
- Update tests, README, and OpenTelemetry docs with wire-up examples
Users who want distributed trace propagation provide a callback:
const client = new CopilotClient({
onGetTraceContext: () => {
const carrier = {};
propagation.inject(context.active(), carrier);
return carrier;
},
});
TelemetryConfig (CLI env vars) is unchanged and requires no dependency.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy file name to clipboardExpand all lines: docs/observability/opentelemetry.md
+60-4Lines changed: 60 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,16 +80,72 @@ var client = new CopilotClient(new CopilotClientOptions
80
80
81
81
### Trace Context Propagation
82
82
83
-
Trace context is propagated automatically — no manual instrumentation is needed:
83
+
> **Most users don't need this.** The `TelemetryConfig` above is all you need to collect traces from the CLI. The trace context propagation described in this section is an **advanced feature** for applications that create their own OpenTelemetry spans and want them to appear in the **same distributed trace** as the CLI's spans.
84
84
85
-
-**SDK → CLI**: `traceparent` and `tracestate` headers from the current span/activity are included in `session.create`, `session.resume`, and `session.send` RPC calls.
86
-
-**CLI → SDK**: When the CLI invokes tool handlers, the trace context from the CLI's span is propagated so your tool code runs under the correct parent span.
85
+
The SDK can propagate W3C Trace Context (`traceparent`/`tracestate`) on JSON-RPC payloads so that your application's spans and the CLI's spans are linked in one distributed trace. This is useful when, for example, you want to see a "handle tool call" span in your app nested inside the CLI's "execute tool" span, or show the SDK call as a child of your request-handling span.
86
+
87
+
#### SDK → CLI (outbound)
88
+
89
+
For **Node.js**, provide an `onGetTraceContext` callback on the client options. This is only needed if your application already uses `@opentelemetry/api` and you want to link your spans with the CLI's spans. The SDK calls this callback before `session.create`, `session.resume`, and `session.send` RPCs:
For **Python**, **Go**, and **.NET**, trace context injection is automatic when the respective OpenTelemetry/Activity API is configured — no callback is needed.
107
+
108
+
#### CLI → SDK (inbound)
109
+
110
+
When the CLI invokes a tool handler, the `traceparent` and `tracestate` from the CLI's span are available in all languages:
111
+
112
+
-**Go**: The `ToolInvocation.TraceContext` field is a `context.Context` with the trace already restored — use it directly as the parent for your spans.
113
+
-**Python**: Trace context is automatically restored around the handler via `trace_context()` — child spans are parented to the CLI's span automatically.
114
+
-**.NET**: Trace context is automatically restored via `RestoreTraceContext()` — child `Activity` instances are parented to the CLI's span automatically.
115
+
-**Node.js**: Since the SDK has no OpenTelemetry dependency, `traceparent` and `tracestate` are passed as raw strings on the `ToolInvocation` object. Restore the context manually if needed:
Copy file name to clipboardExpand all lines: nodejs/README.md
+25-3Lines changed: 25 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -85,6 +85,7 @@ new CopilotClient(options?: CopilotClientOptions)
85
85
-`githubToken?: string` - GitHub token for authentication. When provided, takes priority over other auth methods.
86
86
-`useLoggedInUser?: boolean` - Whether to use logged-in user for authentication (default: true, but false when `githubToken` is provided). Cannot be used with `cliUrl`.
87
87
-`telemetry?: TelemetryConfig` - OpenTelemetry configuration for the CLI process. Providing this object enables telemetry — no separate flag needed. See [Telemetry](#telemetry) below.
88
+
-`onGetTraceContext?: TraceContextProvider` - Advanced: callback for linking your application's own OpenTelemetry spans into the same distributed trace as the CLI's spans. Not needed for normal telemetry collection. See [Telemetry](#telemetry) below.
The SDK supports OpenTelemetry for distributed tracing. Provide a `telemetry` config to enable trace export and automatic W3C Trace Context propagation.
608
+
The SDK supports OpenTelemetry for distributed tracing. Provide a `telemetry` config to enable trace export from the CLI process — this is all most users need:
608
609
609
610
```typescript
610
611
const client =newCopilotClient({
@@ -614,6 +615,8 @@ const client = new CopilotClient({
614
615
});
615
616
```
616
617
618
+
With just this configuration, the CLI emits spans for every session, message, and tool call to your collector. No additional dependencies or setup required.
619
+
617
620
**TelemetryConfig options:**
618
621
619
622
-`otlpEndpoint?: string` - OTLP HTTP endpoint URL
@@ -622,9 +625,28 @@ const client = new CopilotClient({
622
625
-`sourceName?: string` - Instrumentation scope name
623
626
-`captureContent?: boolean` - Whether to capture message content
624
627
625
-
Trace context (`traceparent`/`tracestate`) is automatically propagated between the SDK and CLI on `session.create`, `session.resume`, and `session.send` calls, and inbound when the CLI invokes tool handlers.
628
+
### Advanced: Trace Context Propagation
629
+
630
+
> **You don't need this for normal telemetry collection.** The `telemetry` config above is sufficient to get full traces from the CLI.
631
+
632
+
`onGetTraceContext` is only needed if your application creates its own OpenTelemetry spans and you want them to appear in the **same distributed trace** as the CLI's spans — for example, to nest a "handle tool call" span inside the CLI's "execute tool" span, or to show the SDK call as a child of your application's request-handling span.
633
+
634
+
If you're already using `@opentelemetry/api` in your app and want this linkage, provide a callback:
Inbound trace context from the CLI is available on the `ToolInvocation` object passed to tool handlers as `traceparent` and `tracestate` fields. See the [OpenTelemetry guide](../docs/observability/opentelemetry.md) for a full wire-up example.
0 commit comments