-
Notifications
You must be signed in to change notification settings - Fork 580
feat(dotnet): Add OpenTelemetry support for distributed tracing and metrics #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat(dotnet): Add OpenTelemetry support for distributed tracing and metrics #179
Conversation
…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
There was a problem hiding this 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.
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>
There was a problem hiding this 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
Summary
GitHub.Copilot.SDKfor distributed tracingGitHub.Copilot.SDKfor metricsFeatures
Spans
copilot.sessioncopilot.turncopilot.tool_executioncopilot.subagentcopilot.hookcopilot.inferenceMetrics
copilot.tokens.input/copilot.tokens.output- Token counterscopilot.cost.total- Cost countercopilot.tool_executions- Tool execution countercopilot.errors- Error countercopilot.duration- Duration histogramEnabling Telemetry
Telemetry is disabled by default. Enable via:
Then configure OpenTelemetry:
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 withcopilot.session.id,copilot.turn.id,gen_ai.systemcopilot.inference- LLM call withgen_ai.operation.name,gen_ai.response.modelAttributes follow GenAI Semantic Conventions and integrate with any OpenTelemetry-compatible backend.
New files
src/Telemetry/CopilotTelemetry.cs- Main telemetry classsrc/Telemetry/OpenTelemetryConstants.cs- Span/attribute constantssrc/Telemetry/SessionTelemetryTracker.cs- Event → span conversiontest/TelemetryTests.cs- Unit testsCloses #181