Liking cljdoc? Tell your friends :D

OpenTelemetry instrumentation for Copilot SDK

This guide shows how to add OpenTelemetry tracing to your Copilot SDK applications.

Built-in telemetry support

The SDK has built-in support for configuring OpenTelemetry on the runtime and propagating W3C Trace Context between the SDK and runtime. Provide a TelemetryConfig when creating the client to opt in:

Node.js / TypeScript
import { CopilotClient } from "copilot-sdk-supercharged";

const client = new CopilotClient({
    mode: "coding-agent",
    observability: {
        telemetry: {
            otlpEndpoint: "http://localhost:4318",
        },
    },
});
Python
from copilot import CopilotClient

client = CopilotClient(
    mode="coding-agent",
    observability={
        "telemetry": {
            "otlp_endpoint": "http://localhost:4318",
        }
    }
)
Go
package main

import (
	copilot "github.com/microsoft/copilot-sdk/go/v2"
)

func main() {
	client := copilot.NewClient(&copilot.ClientOptions{
		Mode: copilot.ModeCodingAgent,
		Observability: copilot.ClientObservabilityOptions{
			Telemetry: &copilot.TelemetryConfig{
				OTLPEndpoint: "http://localhost:4318",
			},
		},
	})
	_ = client
}
client := copilot.NewClient(&copilot.ClientOptions{
	Mode: copilot.ModeCodingAgent,
	Observability: copilot.ClientObservabilityOptions{
		Telemetry: &copilot.TelemetryConfig{
			OTLPEndpoint: "http://localhost:4318",
		},
	},
})

.NET
var client = new CopilotClient(new CopilotClientOptions
{
    Mode = CopilotClientMode.CodingAgent,
    Observability = new ClientObservabilityOptions
    {
        Telemetry = new TelemetryConfig
        {
            OtlpEndpoint = "http://localhost:4318",
        }
    }
});
Java
import com.microsoft.copilot.CopilotClient;
import com.microsoft.copilot.rpc.*;

var client = new CopilotClient(new CopilotClientOptions().setMode(CopilotClientMode.CODING_AGENT)
        .setObservability(new ClientObservabilityOptions()
                .setTelemetry(new TelemetryConfig()
                        .setOtlpEndpoint("http://localhost:4318"))));
Rust
use copilot_sdk::{Client, ClientOptions, TelemetryConfig};

let client = Client::start(ClientOptions::new(copilot_sdk::ClientMode::CodingAgent).with_observability(|__sdk_group| {
    __sdk_group
        .with_telemetry(TelemetryConfig::new().with_otlp_endpoint("http://localhost:4318"))
}))
.await?;

TelemetryConfig options

OptionNode.jsPythonGo.NETJavaRustDescription
OTLP endpointotlpEndpointotlp_endpointOTLPEndpointOtlpEndpointotlpEndpointotlp_endpointOTLP HTTP endpoint URL
OTLP protocolotlpProtocolotlp_protocolOTLPProtocolOtlpProtocolotlpProtocolotlp_protocolOTLP HTTP protocol for all signals: "http/json" or "http/protobuf"
File pathfilePathfile_pathFilePathFilePathfilePathfile_pathFile path for JSON-lines trace output
Exporter typeexporterTypeexporter_typeExporterTypeExporterTypeexporterTypeexporter_type"otlp-http" or "file"
Source namesourceNamesource_nameSourceNameSourceNamesourceNamesource_nameInstrumentation scope name
Capture contentcaptureContentcapture_contentCaptureContentCaptureContentcaptureContentcapture_contentWhether to capture message content

The OTLP protocol field configures the runtime's "otlp-http" exporter for all signals. Leave it unset to use the runtime default, or set it to "http/protobuf" to export protobuf over HTTP.

TelemetryConfig configures a runtime that the client owns, whether it runs in process (the default) or as a spawned child process. When the client attaches to an existing runtime server (for example, with RuntimeConnection.forTcp(host, port)), the SDK rejects the telemetry setting; configure telemetry in that runtime's environment instead, for example with COPILOT_OTEL_ENABLED=true and OTEL_EXPORTER_OTLP_ENDPOINT.

Images and multimodal tool results

Content remains opt-in. Prompt capture includes supported user image attachments as base64 blob parts with modality=image and the image MIME type. Tool output capture records gen_ai.tool.call.result as an object, including available client-facing multimodal content. The subsequent conversation inference instead records the model-facing tool response: client-only images are not substituted for model input. Interned images are resolved locally; unavailable, unsupported, and oversized model-facing images retain their omission behavior. Local asset hashes are never reported as provider file IDs. Telemetry does not read image paths: attachments need materialized bytes or resolvable asset data.

Multimodal tool results use the existing session events and INTERNAL execute_tool spans, regardless of which tool produced the image. Tool-origin images require tool-output capture, not assistant-response capture. Changing capture policy clears the binary cache. The existing feature-gated image HTTP trace carrier continues to identify the tool span.

Skill and command telemetry

The runtime follows the GenAI skill semantic conventions by refining existing execute_tool spans, not adding a separate skill operation. A loader is named execute_tool skill {skill}. An unambiguously identified resource or script is named execute_tool {tool} {skill} {resource}, with a skill-relative resource name. Other command spans append the actual known launcher name.

  • gen_ai.skill.name and gen_ai.skill.resource.name: Available independently of content capture
  • gen_ai.skill.description and gen_ai.skill.source.uri: Require content capture through TelemetryConfig
  • process.executable.name: The directly launched executable, such as the shell launcher, not a guessed command token
  • process.executable.path: An absolute known path; requires content capture
  • process.exit.code: An integer, including zero, when a command-executing tool reports an exit; unavailable exits and pre-spawn failures are omitted

Poll tools do not inherit process attributes from the command they read. Sandboxed executable identities are omitted when the sandbox backend does not expose them. A nonzero exit does not imply that the tool itself failed. Skill definitions and directory listings are not named skill resources. Preloaded skills produce invocation events without a fabricated tool span. Repeated preparation of the same selected-agent skill context emits one receipt; changed content or provenance emits another. Child sessions report their own preloads independently. Detached command spans identify the directly spawned Unix sh supervisor or selected Windows PowerShell executable.

Custom tools that override built-in names do not inherit native skill or process attributes. Visible, enabled skills still own resources when model invocation is disabled. Script attribution uses the post-hook command and the selected shell's directory; it is omitted when startup scripts or profiles make the execution context uncertain.

While managed telemetry settings are unresolved, pending tracking buffers only capture-independent tool facts. Descriptions, source URIs, and executable paths observed before the policy resolves are omitted, even if capture is later enabled. The final policy governs replay; disabled or cancelled tracking discards the buffer.

These are breaking telemetry-name changes: use gen_ai.skill.name instead of github.copilot.tool.parameters.skill_name or github.copilot.skill.name, and use gen_ai.skill.source.uri instead of github.copilot.skill.path. Local source values are file URIs, not bare paths. The github.copilot.skill.invoked event and its content, source-category, trigger, and plugin fields remain Copilot-specific. Skill content retains its existing capture gate.

The skill attributes and span refinements have Development stability; the process attributes have Release Candidate stability.

Trace context propagation

Most users don't need this. The TelemetryConfig above is all you need to collect traces from the runtime. 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 runtime's spans.

The SDK can propagate W3C Trace Context (traceparent/tracestate) on JSON-RPC payloads so that your application's spans and the runtime'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 runtime's "execute tool" span, or show the SDK call as a child of your request-handling span.

For cost attribution alongside traces, subscribe to assistant.usage events and inspect apiEndpoint (AssistantUsageApiEndpoint) to see whether a turn used Chat Completions, Responses, or Anthropic Messages; see Streaming events.

Manual and automatic conversation compaction also emit a separate chat {model} span for each logical model dispatch, falling back to chat when the model is unavailable, and mark it with github.copilot.request.type = "compaction". The span covers retries and ends on success, failure, or cancellation. Successful responses include provider-reported token usage and AI credits when available, even if the resulting summary is not applied. Compaction contributes to the existing gen_ai.client.inference.usage.* token counters, gen_ai.client.inference.operation.input_tokens and gen_ai.client.inference.operation.output_tokens histograms, and gen_ai.client.inference.duration histogram. These metrics aggregate foreground and compaction work; github.copilot.request.type is only a span attribute, not a metric dimension for separating them. Each dispatch counts once toward the originating active agent or subagent's gen_ai.invoke_agent.inference_calls, including failures; idle manual compaction has no active invocation to charge. Compaction does not emit an additional assistant.usage event or change session billing, and its spans do not capture the conversation or summary text. Charges not reported by the provider cannot be recovered from these spans.

SDK → runtime (outbound)

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 runtime's spans. The SDK calls this callback before session.create, session.resume, and session.send RPCs:

import { CopilotClient } from "copilot-sdk-supercharged";
import { propagation, context } from "@opentelemetry/api";

const client = new CopilotClient({
    mode: "coding-agent",
    observability: {
        telemetry: { otlpEndpoint: "http://localhost:4318" },
        onGetTraceContext: () => {
            const carrier: Record<string, string> = {};
            propagation.inject(context.active(), carrier);
            return carrier; // { traceparent: "00-...", tracestate: "..." }
        },
    },
});

For Python, Go, and .NET, trace context injection is automatic when the respective OpenTelemetry/Activity API is configured—no callback is needed. Go injects and extracts trace context with the global propagator, which drops it until the application installs one, for example with otel.SetTextMapPropagator(propagation.TraceContext{}).

Runtime → SDK (inbound)

When the runtime invokes a tool handler, the traceparent and tracestate from the runtime's span are available in every language except Java:

  • Go: The ToolInvocation.TraceContext field is a context.Context with the trace already restored—use it directly as the parent for your spans.
  • Python: Trace context is automatically restored around the handler via trace_context()—child spans are parented to the runtime's span automatically.
  • .NET: Configure AddSource("microsoft.copilot") to export copilot.tool_handler parents under runtime spans; without listeners, only context propagates.
  • 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:
declare function doWork(args: unknown): Promise<string>;

import { defineTool } from "copilot-sdk-supercharged";
import { propagation, context, trace } from "@opentelemetry/api";

const myTool = defineTool("my-tool", {
    description: "Do work",
    handler: async (args, invocation) => {
        // Restore the runtime'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 runtime'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: {
        definitions: [myTool],
    },
});
import { defineTool } from "copilot-sdk-supercharged";
import { propagation, context, trace } from "@opentelemetry/api";

const myTool = defineTool("my-tool", {
    description: "Do work",
    handler: async (args, invocation) => {
        // Restore the runtime'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 runtime'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: {
        definitions: [myTool],
    },
});

Per-language dependencies

LanguageDependencyNotes
Node.js—No dependency; provide onGetTraceContext callback for outbound propagation
Pythonopentelemetry-apiInstall with pip install "copilot-sdk-supercharged[telemetry]"
Gogo.opentelemetry.io/otelRequired dependency
.NET—Uses built-in System.Diagnostics.Activity
Java—No dependency; the SDK does not inject trace context on outbound calls or pass it to tool handlers

References

Can you improve this documentation? These fine people already did:
copilot-cli-release-app[bot], Jeremiah Isaacson, Stephen Toub, copilot-sdk-mirror-writer[bot], examon, Patrick Nikoletich, Sunbrye Ly, Ed Burns, Bruno Borges, Logan Rosen & Steve Sanderson
Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close