dd-trace-dotnet icon indicating copy to clipboard operation
dd-trace-dotnet copied to clipboard

[Profiler] Introduced `DD_PROFILER_SKIPPED_METHODS` configuration to exclude specific methods from sampling

Open GreenMatan opened this issue 1 year ago • 4 comments

Summary of changes

This PR introduces a new configuration setting to address a customer issue where the profiler would sample threads transitioning into unmanaged code via P/Invoke, potentially causing crashes due to unsafe operations.

The new configuration, DD_PROFILER_SKIPPED_METHODS, allows users to specify methods that should be excluded from sampling.

Configuration

Key: DD_PROFILER_SKIPPED_METHODS Value: string Value Syntax:

  • Entries are semi-colon (;) delimited
  • The full type name is specified outside of braces ([ and ])
  • Methods are specified inside the braces and are comma delimited
  • Generic methods only need the method name, but generic type names must include the number of generic type variables, like `1 to specify a generic type with one generic type variable
DD_PROFILER_SKIPPED_METHODS="Namespace1.Class1[Method1,Method2];Namespace1.GenericTypeWithOneTypeVariable`1[ExecuteAsync]"

Reason for change

This change was driven by a customer support case involving profiler instability when managed threads transitioned into unmanaged code via P/Invoke are sampled by the profiler.

Implementation details

A new integration type, ProfilerSkippedMethodIntegration, was added. Based on the existing Trace Methods instrumentation, this integration tracks method entry and exit points to mark threads as unsafe for sampling while within these methods.

The shared structure between the profiler's native component and the managed code, TraceContextInfo, has been extended to include a new field _threadMetaInfo (cherry-picked from 5891c12f133fdde9894f0514a027cd7302232bde), which tracks the state of the thread:

  • On method entry, _threadMetaInfo is set to 1, marking the thread as unsafe to sample.
  • On method exit, it is reset to 0, allowing sampling again.

This ensures the profiler can dynamically adjust sampling behavior based on the thread's current execution context.

Test coverage

UnsafeToUnwindTest that was added in 5891c12f133fdde9894f0514a027cd7302232bde, verifies the correct behavior of the profiler when methods are marked as unsafe for sampling.

Fixes

DEBUG-3052

GreenMatan avatar Oct 10 '24 08:10 GreenMatan