Skip to content

Conversation

@reachanihere
Copy link

@reachanihere reachanihere commented Jan 25, 2026

Summary

  • Add opt-in OpenTelemetry instrumentation to the .NET SDK
  • ActivitySource GitHub.Copilot.SDK for distributed tracing
  • Meter GitHub.Copilot.SDK for metrics
  • Follows OpenTelemetry GenAI Semantic Conventions

Features

Spans

Span Name Description
copilot.session Root span for a session
copilot.turn Assistant turn lifecycle
copilot.tool_execution Tool execution
copilot.subagent Subagent execution
copilot.hook Hook execution
copilot.inference LLM inference call with token usage

Metrics

  • copilot.tokens.input / copilot.tokens.output - Token counters
  • copilot.cost.total - Cost counter
  • copilot.tool_executions - Tool execution counter
  • copilot.errors - Error counter
  • copilot.duration - Duration histogram

Enabling Telemetry

Telemetry is disabled by default. Enable via:

// Option 1: AppContext switch
AppContext.SetSwitch("GitHub.Copilot.EnableOpenTelemetry", true);

// Option 2: Environment variable
// GITHUB_COPILOT_ENABLE_OPEN_TELEMETRY=true

Then configure OpenTelemetry:

services.AddOpenTelemetry()
    .WithTracing(t => t.AddSource("GitHub.Copilot.SDK"))
    .WithMetrics(m => m.AddMeter("GitHub.Copilot.SDK"));

Test plan

  • Unit tests for all span types (all tests passing)

  • Build succeeds with no warnings

  • Created a simple console app to test the instrumentation. Real OpenTelemetry spans captured from a live Copilot CLI session with telemetry enabled via AppContext.SetSwitch("GitHub.Copilot.EnableOpenTelemetry", true):

    • copilot.turn - Turn lifecycle with copilot.session.id, copilot.turn.id, gen_ai.system
    • copilot.inference - LLM call with gen_ai.operation.name, gen_ai.response.model

    Attributes follow GenAI Semantic Conventions and integrate with any OpenTelemetry-compatible backend.

    image

New files

  • src/Telemetry/CopilotTelemetry.cs - Main telemetry class
  • src/Telemetry/OpenTelemetryConstants.cs - Span/attribute constants
  • src/Telemetry/SessionTelemetryTracker.cs - Event → span conversion
  • test/TelemetryTests.cs - Unit tests

Closes #181

…etrics

Add opt-in OpenTelemetry instrumentation to the .NET SDK, enabling distributed
tracing and metrics collection for Copilot sessions.

## Features
- ActivitySource "GitHub.Copilot.SDK" for distributed tracing
- Meter "GitHub.Copilot.SDK" for metrics
- Spans for session, turn, tool execution, subagent, hook, and inference events
- Metrics for token usage, cost, tool executions, errors, and duration
- Follows OpenTelemetry GenAI Semantic Conventions

## Enabling Telemetry
Telemetry is disabled by default. Enable via:
- AppContext switch: `GitHub.Copilot.EnableOpenTelemetry`
- Environment variable: `GITHUB_COPILOT_ENABLE_OPEN_TELEMETRY=true`

## New Files
- src/Telemetry/CopilotTelemetry.cs - Main telemetry class
- src/Telemetry/OpenTelemetryConstants.cs - Span/attribute constants
- src/Telemetry/SessionTelemetryTracker.cs - Event to span conversion
- test/TelemetryTests.cs - Unit tests
@reachanihere reachanihere requested a review from a team as a code owner January 25, 2026 01:03
Copilot AI review requested due to automatic review settings January 25, 2026 01:03
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds opt-in OpenTelemetry instrumentation to the .NET SDK for distributed tracing and metrics, following OpenTelemetry GenAI Semantic Conventions.

Changes:

  • Adds telemetry infrastructure with ActivitySource and Meter for spans and metrics
  • Implements SessionTelemetryTracker to convert session events into OpenTelemetry spans
  • Integrates telemetry tracking into the CopilotSession lifecycle
  • Adds comprehensive unit tests (10 tests) for telemetry functionality
  • Documents the observability features in README with examples

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
dotnet/src/Telemetry/CopilotTelemetry.cs Main telemetry class providing ActivitySource, Meter, and metric recording methods with opt-in enable/disable logic
dotnet/src/Telemetry/OpenTelemetryConstants.cs Defines constants for span names, attribute names, and metric names following GenAI semantic conventions
dotnet/src/Telemetry/SessionTelemetryTracker.cs Converts session events to OpenTelemetry spans, managing lifecycle of session, turn, tool, subagent, hook, and inference activities
dotnet/src/Session.cs Integrates telemetry tracker into session, instantiating it on construction and disposing on cleanup
dotnet/src/GitHub.Copilot.SDK.csproj Adds OpenTelemetry.Api package reference and InternalsVisibleTo for test project
dotnet/test/TelemetryTests.cs Comprehensive unit tests covering all major span types and telemetry scenarios
dotnet/README.md Documents how to enable and configure OpenTelemetry, lists available spans and metrics, includes example trace

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

reachanihere and others added 4 commits January 25, 2026 01:13
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Add _disposeLock to synchronize ProcessEvent and Dispose
- Properly dispose all orphaned activities during Dispose
- Prevents race conditions and resource leaks
@reachanihere reachanihere marked this pull request as draft January 25, 2026 02:37
@reachanihere reachanihere marked this pull request as ready for review January 25, 2026 02:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add OpenTelemetry support for .NET SDK

2 participants