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
Copy file name to clipboardExpand all lines: docs/getting-started.md
+114Lines changed: 114 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1395,6 +1395,119 @@ await using var session = await client.CreateSessionAsync(new()
1395
1395
1396
1396
---
1397
1397
1398
+
## Telemetry & Observability
1399
+
1400
+
The Copilot SDK supports [OpenTelemetry](https://opentelemetry.io/) for distributed tracing. Provide a `telemetry` configuration to the client to enable trace export from the CLI process and automatic [W3C Trace Context](https://www.w3.org/TR/trace-context/) propagation between the SDK and CLI.
1401
+
1402
+
### Enabling Telemetry
1403
+
1404
+
Pass a `telemetry` (or `Telemetry`) config when creating the client. This is the opt-in — no separate "enabled" flag is needed.
| Exporter type |`exporterType`|`exporter_type`|`ExporterType`|`ExporterType`|`"otlp-http"` or `"file"`|
1483
+
| Source name |`sourceName`|`source_name`|`SourceName`|`SourceName`| Instrumentation scope name |
1484
+
| Capture content |`captureContent`|`capture_content`|`CaptureContent`|`CaptureContent`| Whether to capture message content |
1485
+
1486
+
### File Export
1487
+
1488
+
To write traces to a local file instead of an OTLP endpoint:
1489
+
1490
+
<!-- docs-validate: skip -->
1491
+
```typescript
1492
+
const client =newCopilotClient({
1493
+
telemetry: {
1494
+
filePath: "./traces.jsonl",
1495
+
exporterType: "file",
1496
+
},
1497
+
});
1498
+
```
1499
+
1500
+
### Trace Context Propagation
1501
+
1502
+
Trace context is propagated automatically — no manual instrumentation is needed:
1503
+
1504
+
-**SDK → CLI**: `traceparent` and `tracestate` headers from the current span/activity are included in `session.create`, `session.resume`, and `session.send` RPC calls.
1505
+
-**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.
Copy file name to clipboardExpand all lines: docs/observability/opentelemetry.md
+153-1Lines changed: 153 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,158 @@
1
1
# OpenTelemetry Instrumentation for Copilot SDK
2
2
3
-
This guide shows how to add OpenTelemetry tracing to your Copilot SDK applications using GenAI semantic conventions.
3
+
This guide shows how to add OpenTelemetry tracing to your Copilot SDK applications.
4
+
5
+
## Built-in Telemetry Support
6
+
7
+
The SDK has built-in support for configuring OpenTelemetry on the CLI process and propagating W3C Trace Context between the SDK and CLI. Provide a `TelemetryConfig` when creating the client to opt in:
| Exporter type |`exporterType`|`exporter_type`|`ExporterType`|`ExporterType`|`"otlp-http"` or `"file"`|
78
+
| Source name |`sourceName`|`source_name`|`SourceName`|`SourceName`| Instrumentation scope name |
79
+
| Capture content |`captureContent`|`capture_content`|`CaptureContent`|`CaptureContent`| Whether to capture message content |
80
+
81
+
### Trace Context Propagation
82
+
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
+
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:
The rest of this guide shows how to add your own OpenTelemetry spans around SDK operations using GenAI semantic conventions. This is complementary to the built-in `TelemetryConfig` above — you can use both together.
0 commit comments