Skip to content

Conversation

@reachanihere
Copy link

@reachanihere reachanihere commented Jan 25, 2026

Summary

Add session lifecycle events to enable external observability integration.

Based on feedback, OpenTelemetry instrumentation is better suited for a separate Aspire package rather than being built into the core SDK. This PR adds the minimal hooks needed to enable that external integration.

Changes

New Events on CopilotClient

Event Description
SessionCreated Fires when a session is created, provides CopilotSession instance
SessionDestroyed Fires when a session is disposed, provides session ID

Usage

var client = new CopilotClient();

client.SessionCreated += session =>
{
    // Subscribe to session events for telemetry
    session.On(evt =>
    {
        // Handle AssistantUsageEvent, ToolExecutionStartEvent, etc.
    });
};

client.SessionDestroyed += sessionId =>
{
    // Cleanup telemetry resources
};

This enables external packages (e.g., GitHub.Copilot.SDK.Aspire) to hook into session lifecycle and use session.On(...) to capture all events for OpenTelemetry instrumentation.

Closes #181

@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.

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.

@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
@SteveSandersonMS
Copy link
Contributor

Thanks for contributing this, @reachanihere!

Rather than integrating otel concepts/dependencies into the SDK directly here (and diverging from the other-language SDKs), the way we'd normally approach this is by suggesting there be an Aspire-specific package for the GitHub Copilot SDK (e.g., Aspire.GitHub.Copilot) and that's where the otel concepts would be added.

Of course for this to be possible, we need CopilotClient here to have the necessary extensibility so that other external code can plug in callbacks to know what telemetry to emit. My guess is that's almost covered already - if you have access to a session instance, you can already call .On(...) to get notifications of all its events.

Perhaps the one missing piece is some way to register a callback on CopilotClient to get notifications when sessions are created and destroyed, so you can also hook into their events globally. If we added that, do you think that would unlock what's needed for this scenario?

@reachanihere
Copy link
Author

reachanihere commented Jan 26, 2026

Thank you a lot for your feedback, @SteveSandersonMS! Totally agree, keeping the SDK lean and letting observability live in a separate Aspire package would be a better approach.

I think the session lifecycle hooks would unblock this scenario. I also verified that all the events I was using (turns, tools, inference, usage, etc.) are already exposed via session.On(...), so the only missing piece would be knowing when sessions are created/destroyed.

Would it be ok if I update this PR to just add those hooks (and removing all OTel implementation)? And for the Aspire package itself, is that something your team would maintain alongside the SDK?

@SteveSandersonMS
Copy link
Contributor

SteveSandersonMS commented Jan 26, 2026

@reachanihere Yes, that sounds great. Thanks!

And for the Aspire package itself, is that something your team would maintain alongside the SDK?

Not currently. We're trying to keep our focus more tightly defined.

anirsharma and others added 10 commits January 26, 2026 15:41
…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
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>
- Add _disposeLock to synchronize ProcessEvent and Dispose
- Properly dispose all orphaned activities during Dispose
- Prevents race conditions and resource leaks
@reachanihere reachanihere force-pushed the feature/opentelemetry-support branch from 4df6a5e to 64af6b9 Compare January 26, 2026 17:14
@reachanihere reachanihere changed the title feat(dotnet): Add OpenTelemetry support for distributed tracing and metrics Add session lifecycle hooks for observability Jan 26, 2026
@reachanihere
Copy link
Author

@reachanihere Yes, that sounds great. Thanks!

And for the Aspire package itself, is that something your team would maintain alongside the SDK?

Not currently. We're trying to keep our focus more tightly defined.

Thanks @SteveSandersonMS, request to please review the updated PR

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 2 out of 2 changed files in this pull request and generated 5 comments.


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

Tests verify:
- SessionCreated fires when sessions are created
- SessionDestroyed fires when sessions are disposed
- Events fire correctly for multiple sessions
- SessionCreated fires when sessions are resumed
When ResumeSessionAsync replaces an existing session, clear the old
session's OnDisposed callback to prevent it from firing SessionDestroyed
if disposed later.

Added test to verify old session disposal doesn't fire spurious events.
@reachanihere reachanihere force-pushed the feature/opentelemetry-support branch from 5fb9f14 to 63adfee Compare January 26, 2026 18:25
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

3 participants