You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
managed loader: refactor for testability, add unit tests (#7594)
## Summary of changes
Refactored the managed loader
(`Datadog.Trace.ClrProfiler.Managed.Loader`) to improve testability and
added comprehensive unit tests.
## Reason for change
The managed loader code was difficult to test due to tight coupling with
environment variables and filesystem access. This refactoring enables
better test coverage and makes the code more maintainable.
## Implementation details
- **New abstractions**:
- Added `IEnvironmentVariableProvider` and `EnvironmentVariableProvider`
to abstract environment variable access, enabling dependency injection
for tests
- Implemented as `readonly struct` with generic type parameters to avoid
heap allocations and boxing
- **Refactored startup logic**:
- Moved TFM directory computation into dedicated methods that can be
tested independently
- Separated directory existence checks from path computation
- Improved error messages and logging consistency
- **Code improvements**:
- Added nullable reference type annotations throughout
- Standardized Boolean environment variable parsing to match tracer
conventions
- Cleaned up comments and improved code clarity
## Test coverage
Added 5 new test files with coverage of:
- Environment variable reading and Boolean parsing
(`EnvironmentVariableProviderTests`)
- Log directory resolution across platforms (`StartupLoggerTests`)
- TFM directory computation for .NET Core (`StartupNetCoreTests`)
- TFM directory computation for .NET Framework
(`StartupNetFrameworkTests`)
- Edge cases and error conditions
- Added `MockEnvironmentVariableProvider` for test isolation
## Other details
This refactoring is preparatory work for #7568 which makes
`DD_DOTNET_TRACER_HOME` optional, but the changes were hard to test.
---------
Co-authored-by: Claude <[email protected]>
Co-authored-by: Andrew Lock <[email protected]>
StartupLogger.Log($"The tracer home directory cannot be found at '{fullPath}', based on the DD_DOTNET_TRACER_HOME value '{tracerHomeDirectory}' and current directory {Environment.CurrentDirectory}");
41
-
returnnull;
38
+
// version is 3.1 or 5.0
39
+
managedLibrariesDirectory="netcoreapp3.1";
42
40
}
43
-
44
-
// We use the List/Array approach due the number of files in the tracer home folder (7 in netstandard, 2 netcoreapp3.1+)
// Only load the main profiler into the default Assembly Load Context.
94
-
// If Datadog.Trace or other libraries are provided by the NuGet package their loads are handled in the following two ways.
95
-
// 1) The AssemblyVersion is greater than or equal to the version used by Datadog.Trace, the assembly
100
+
// Only load the main profiler into the default AssemblyLoadContext.
101
+
// If the NuGet package provides Datadog.Trace or other libraries, loading them is handled in the following two ways:
102
+
// 1) If the AssemblyVersion is greater than or equal to the version used by Datadog.Trace, the assembly
96
103
// will load successfully and will not invoke this resolve event.
97
-
// 2) The AssemblyVersion is lower than the version used by Datadog.Trace, the assembly will fail to load
104
+
// 2) If the AssemblyVersion is lower than the version used by Datadog.Trace, the assembly will fail to load
98
105
// and invoke this resolve event. It must be loaded in a separate AssemblyLoadContext since the application will only
99
-
// load the originally referenced version
100
-
StartupLogger.Debug("Loading {0} with DependencyLoadContext.LoadFromAssemblyPath",path);
101
-
varassembly=DependencyLoadContext.LoadFromAssemblyPath(path);// Load unresolved framework and third-party dependencies into a custom Assembly Load Context
varassembly=DependencyLoadContext.LoadFromAssemblyPath(path);// Load unresolved framework and third-party dependencies into a custom AssemblyLoadContext
StartupLogger.Log($"The tracer home directory cannot be found at '{fullPath}', based on the DD_DOTNET_TRACER_HOME value '{tracerHomeDirectory}' and current directory {Environment.CurrentDirectory}");
0 commit comments