組み込みのテレメトリのサポート
SDK には、CLI プロセスで OpenTelemetry を構成し、SDK と CLI の間で W3C トレース コンテキストを伝達するためのサポートが組み込まれています。 オプトインするクライアントを作成するときに TelemetryConfig を指定します。
import { CopilotClient } from "@github/copilot-sdk";
const client = new CopilotClient({
telemetry: {
otlpEndpoint: "http://localhost:4318",
},
});
from copilot import CopilotClient
client = CopilotClient(
telemetry={
"otlp_endpoint": "http://localhost:4318",
},
)
client, err := copilot.NewClient(copilot.ClientOptions{
Telemetry: &copilot.TelemetryConfig{
OTLPEndpoint: "http://localhost:4318",
},
})
var client = new CopilotClient(new CopilotClientOptions
{
Telemetry = new TelemetryConfig
{
OtlpEndpoint = "http://localhost:4318",
},
});
import com.github.copilot.sdk.CopilotClient;
import com.github.copilot.sdk.json.*;
var client = new CopilotClient(new CopilotClientOptions()
.setTelemetry(new TelemetryConfig()
.setOtlpEndpoint("http://localhost:4318"))
);
use github_copilot_sdk::{Client, ClientOptions, TelemetryConfig};
let client = Client::start(ClientOptions::new()
.with_telemetry(TelemetryConfig::new()
.with_otlp_endpoint("http://localhost:4318"))
).await?;
TelemetryConfig オプション
| オプション | Node.js | Python | Go | .NET | Java | Rust | 説明 |
|---|---|---|---|---|---|---|---|
| OTLP エンドポイント | otlpEndpoint | otlp_endpoint | OTLPEndpoint | OtlpEndpoint | otlpEndpoint | otlp_endpoint | OTLP HTTP エンドポイント URL |
| ファイルパス | filePath | file_path | FilePath | FilePath | filePath | file_path | JSON 行トレース出力のファイル パス |
| エクスポーターの種類 | exporterType | exporter_type | ExporterType | ExporterType | exporterType | exporter_type | |
"otlp-http" または "file" | |||||||
| ソース名 | sourceName | source_name | SourceName | SourceName | sourceName | source_name | 計装範囲名 |
| コンテンツをキャプチャする | captureContent | capture_content | CaptureContent | CaptureContent | captureContent | capture_content | メッセージの内容をキャプチャするかどうか |
トレース コンテキストの伝達
ほとんどのユーザーはこれを必要としません。 上記の
TelemetryConfigは、CLI からトレースを収集するために必要なすべてです。 このセクションで説明するトレース コンテキスト伝達は、独自の OpenTelemetry スパンを作成し、CLI のスパンと同じ分散トレースに表示するアプリケーションの高度な機能です。
SDK は、JSON-RPC ペイロードに W3C トレース コンテキスト (traceparent/tracestate) を伝達できるため、アプリケーションのスパンと CLI のスパンが 1 つの分散トレースにリンクされます。 これは、たとえば、アプリ内の「ツール呼び出しを処理」スパンが CLI の「ツールを実行」スパンの内側に入れ子になった状態で表示したい場合や、SDK 呼び出しをリクエスト処理スパンの子として表示したい場合に便利です。
SDK → CLI (送信)
Node.jsの場合は、クライアント オプションにonGetTraceContextコールバックを指定します。 これは、アプリケーションで既に @opentelemetry/api を使用しており、スパンを CLI のスパンにリンクする場合にのみ必要です。 SDK は、RPC を session.create、 session.resume、および session.send する前に、このコールバックを呼び出します。
import { CopilotClient } from "@github/copilot-sdk";
import { propagation, context } from "@opentelemetry/api";
const client = new CopilotClient({
telemetry: { otlpEndpoint: "http://localhost:4318" },
onGetTraceContext: () => {
const carrier: Record<string, string> = {};
propagation.inject(context.active(), carrier);
return carrier; // { traceparent: "00-...", tracestate: "..." }
},
});
Python、Go、および .NET では、それぞれの OpenTelemetry/Activity API が構成されている場合、トレース コンテキストの挿入は自動的に行われます。コールバックは必要ありません。
CLI → SDK(インバウンド)
CLI がツール ハンドラーを呼び出すと、CLI のスパンからの traceparent と tracestate は、すべての言語で使用できます。
- Go:
ToolInvocation.TraceContextフィールドは、すでに復元されたトレースを含むcontext.Contextです。これをスパンの親としてそのまま使用してください。 - Python: トレース コンテキストは、
trace_context()を介してハンドラーの周囲に自動的に復元されます。子スパンは CLI のスパンに自動的に親されます。 - .NET: トレース コンテキストは
RestoreTraceContext()を介して自動的に復元されます。子Activityインスタンスは CLI のスパンに自動的に親されます。 - Node.js: SDK には OpenTelemetry 依存関係がないため、
traceparentとtracestateは、ToolInvocationオブジェクトに生の文字列として渡されます。 必要に応じて、コンテキストを手動で復元します。
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();
}
})
);
});
言語ごとの依存関係
| Language | 依存関係 | メモ |
|---|---|---|
| Node.js | — | 依存関係なし。外向き伝播用のonGetTraceContextコールバックを提供する |
| Python | opentelemetry-api | を使用してインストールする pip install copilot-sdk[telemetry] |
| Go | go.opentelemetry.io/otel | 必要な依存関係 |
| .NET | — | 組み込みを使用 System.Diagnostics.Activity |
| Java | io.opentelemetry:opentelemetry-api | SDK ベースのセットアップにこの依存関係を追加します。OpenTelemetry Java エージェントまたは SDK が構成されている場合、トレース コンテキストの挿入は自動的に行われます |