open-telemetry / opentelemetry-dotnet-instrumentation

OpenTelemetry .NET Automatic Instrumentation
https://opentelemetry.io
Apache License 2.0
369 stars 92 forks source link

Out-of-Process Auto-Instrumentation for .NET AOT Applications #2944

Closed rajkumar-rangaraj closed 8 months ago

rajkumar-rangaraj commented 1 year ago

Problem Statement

Auto-instrumentation for .NET applications currently relies on startup hooks, which are not supported by Ahead-of-Time (AOT) compiled applications. This lack of support for AOT-compiled apps is a significant gap in OpenTelemetry .NET Auto-Instrumentation capabilities. Additionally, some apps may fail rule engine validation and also require alternative methods for auto-instrumentation.

Goals

Proposed Solution

The central idea is to employ an out-of-process auto-instrumentation technique that leverages EventPipeSession. This will be similar in function to utilities like dotnet-counter and dotnet-trace.

// Setup EventPipeSession
var client = new DiagnosticsClient(processId);
using (EventPipeSession session = client.StartEventPipeSession(providers, requestRundown: false))
{
  var source = new EventPipeEventSource(session.EventStream);
  Task.Run(() =>
  {
      source.Dynamic.All += (TraceEvent data) => 
      {
        Console.WriteLine($"Event: {data.EventName}");
      };
    source.Process();
  });

Compatibility

This feature would be opt-in and should not break existing auto-instrumentation methods. The out-of-process method would serve as an alternative to startup hooks for AOT apps and others where in-process methods are not feasible.

Test Plan

Rollout Plan

Open Questions

  1. Are there limitations in EventPipe that could make this method not universally applicable?
  2. What is the performance overhead in real-world scenarios?
nrcventura commented 1 year ago

Other thoughts/considerations:

pellared commented 1 year ago

What about context propagation? Would it be able to work?

Maybe it would be possible to build automatic instrumentation using eBPF similar to OpenTelemetry Go Automatic Instrumentation: https://github.com/open-telemetry/opentelemetry-go-instrumentation/blob/main/docs/how-it-works.md

nrcventura commented 8 months ago

Closing per SIG discussion. Pursuing dotnet-monitor based approach.