Skip to content

feat(tracing): add DiagnosticSource bridge for automatic instrumentation #16

@CalvinAllen

Description

@CalvinAllen

Summary

Create a bridge to automatically capture telemetry from .NET's DiagnosticSource events. Many .NET libraries (HttpClient, SqlClient, etc.) emit diagnostic events that can be converted to OpenTelemetry spans.

Background

.NET has a built-in DiagnosticSource system that libraries use to emit events:

  • HttpClient emits request/response events
  • SqlClient emits query events
  • Many other libraries participate

Currently this data is lost unless explicitly subscribed to.

Proposed API

var config = new TelemetryConfiguration
{
    // Enable automatic DiagnosticSource bridging
    EnableDiagnosticSourceBridge = true,
    
    // Or selectively enable specific sources
    DiagnosticSources = new[]
    {
        "HttpHandlerDiagnosticListener",
        "SqlClientDiagnosticListener"
    }
};

// Or configure at runtime
VsixTelemetry.SubscribeToDiagnosticSource("MyCustomSource");
VsixTelemetry.SubscribeToDiagnosticSource("HttpHandlerDiagnosticListener", 
    filter: evt => evt.Name.StartsWith("System.Net.Http"));

Events to Bridge

HttpClient

  • System.Net.Http.HttpRequestOut.Start → Span start
  • System.Net.Http.HttpRequestOut.Stop → Span end with status
  • Add: URL, method, status code, duration

SqlClient (if used)

  • System.Data.SqlClient.WriteCommandBefore → Span start
  • System.Data.SqlClient.WriteCommandAfter → Span end
  • Add: Database, command type, (sanitized) query

Implementation Notes

  • Use DiagnosticListener.AllListeners to discover sources
  • Implement IObserver<DiagnosticListener> for subscription
  • Map diagnostic events to OpenTelemetry span operations
  • Handle event payload extraction via reflection or known types
  • Consider performance impact of reflection

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions