Skip to content

Load Datadog.Trace.ClrProfiler.Managed via the AppDomain.AssemblyResolve event#505

Merged
zacharycmontoya merged 63 commits into
masterfrom
zach/stability/assembly-resolve-logic
Sep 18, 2019
Merged

Load Datadog.Trace.ClrProfiler.Managed via the AppDomain.AssemblyResolve event#505
zacharycmontoya merged 63 commits into
masterfrom
zach/stability/assembly-resolve-logic

Conversation

@zacharycmontoya

@zacharycmontoya zacharycmontoya commented Sep 5, 2019

Copy link
Copy Markdown
Contributor

Product changes:

  • In Datadog.Trace.ClrProfiler.Managed.Loader add the AppDomain.AssemblyResolve event handler to load managed profiler dependencies from disk where the profiler is installed. This would remove the need to install files into the GAC or add the Datadog.Trace.ClrProfiler.Managed NuGet package to receive auto-instrumentation.
  • Stop adding assembly references to the managed profiler code in ModuleLoadFinished and instead do that operation in JITCompilationStarted. Because of this delayed reference, the runtime will not be able to resolve the assembly reference automatically (perhaps avoiding the SecurityGrant exceptions we have seen in the past!) and will rely on the newly implemented AppDomain.AssemblyResolve event to find the managed profiler code.
  • Build Datadog.Trace.ClrProfiler.Managed.Loader for netcoreapp2.0 and net45 because reading environment variables (to obtain profiler file path) is only supported on netstandard1.3 and higher.

Build changes:

  • Combine common settings in Directory.Build.props for reproductions, reproduction-dependencies, and samples into ./Test.Common.props
  • For Windows-ready xUnit integration tests, add the RunOnWindows=True trait
  • Produce managed profiler assets by running dotnet publish Datadog.Trace.ClrProfiler.Managed for each target framework
  • Stop building Datadog.Trace.ClrProfiler.Managed.Loader.csproj inside Datadog.Trace.proj's BuildCpp target and instead build it separately whenever we build Datadog.Trace.ClrProfiler.Native

(edit: formatting)

…tandard2.0.

The net45 target framework will be used for all .NET Framework applications and netstandard2.0 for all .NET Core applications.
…lies to the build output for netstandard2.0. This allows apps using the netstandard2.0 profiler to run.
@zacharycmontoya zacharycmontoya added area:builds project files, build scripts, pipelines, versioning, releases, packages area:native-library Automatic instrumentation native C++ code (Datadog.Trace.ClrProfiler.Native) labels Sep 5, 2019
@zacharycmontoya zacharycmontoya self-assigned this Sep 5, 2019
Comment thread src/Datadog.Trace.ClrProfiler.Managed.Loader/Startup.cs Outdated
- Make Datadog.Trace.ClrProfiler.ManagedLoader target netcoreapp2.0 (instead of netstandard2.0) to use AssemblyLoadContext class
- Add implementation class for AssemblyLoadContext
- Always load our main Datadog assemblies into the LoadFrom context but on .NET Core, load the dependencies into a separate Assembly Load Context
…p target of Datadog.Trace.proj and explicitly build the project in previous CI steps.
…o using the profiler from the profiler directory.
@zacharycmontoya zacharycmontoya marked this pull request as ready for review September 13, 2019 17:55
@zacharycmontoya zacharycmontoya requested a review from a team as a code owner September 13, 2019 17:55
Comment thread src/Datadog.Trace.ClrProfiler.Managed.Loader/Startup.NetCore.cs Outdated
Comment thread src/Datadog.Trace.ClrProfiler.Native/cor_profiler.cpp Outdated
Comment thread samples/Samples.HttpMessageHandler/Program.cs
Comment thread samples/Samples.SqlServer/Properties/launchSettings.json
"CORECLR_PROFILER_PATH": "$(ProjectDir)$(OutputPath)profiler-lib\\Datadog.Trace.ClrProfiler.Native.dll",

"DD_DOTNET_TRACER_HOME": "$(ProjectDir)$(OutputPath)profiler-lib",
"DD_INTEGRATIONS": "$(ProjectDir)$(OutputPath)profiler-lib\\integrations.json"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we drop DD_INTEGRATIONS and figure it out from DD_DOTNET_TRACER_HOME instead? (Can be separate PR)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anywhere where we might need the flexibility of an integrations file outside of the profiler directory? Perhaps we could default to the DD_DOTNET_TRACER_HOME directory and use DD_INTEGRATIONS as a sort of override?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default to the DD_DOTNET_TRACER_HOME directory and use DD_INTEGRATIONS as a sort of override

I like this idea. The latter also lets us define multiple json files. We should probably also rename that to DD_DOTNET_INTEGRATIONS at some point.

Let's leave as-is for this PR and come back to that later.

Comment thread src/Datadog.Trace.ClrProfiler.Managed.Loader/Startup.NetCore.cs Outdated
Comment thread src/Datadog.Trace.ClrProfiler.Managed.Loader/Startup.cs
Add DD_DOTNET_TRACER_HOME to Windows MSI.
Comment thread test/Datadog.Trace.TestHelpers/EnvironmentHelper.cs
@lucaspimentel lucaspimentel added area:test-apps apps used to test integrations area:tests unit tests, integration tests labels Sep 16, 2019
Comment thread src/Datadog.Trace.ClrProfiler.Managed.Loader/Startup.NetCore.cs Outdated
lucaspimentel
lucaspimentel previously approved these changes Sep 16, 2019

@lucaspimentel lucaspimentel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@zacharycmontoya zacharycmontoya force-pushed the zach/stability/assembly-resolve-logic branch from ba8fdbd to 74ef326 Compare September 16, 2019 23:10

@colin-higgins colin-higgins left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic work!

@zacharycmontoya zacharycmontoya merged commit 7b96de3 into master Sep 18, 2019
@zacharycmontoya zacharycmontoya deleted the zach/stability/assembly-resolve-logic branch September 18, 2019 15:10
macrogreg pushed a commit that referenced this pull request Aug 20, 2021
…lve event (#505)

Product changes:
- In `Datadog.Trace.ClrProfiler.Managed.Loader` add the `AppDomain.AssemblyResolve` event handler to load managed profiler dependencies from disk where the profiler is installed. This would remove the need to install files into the GAC or add the `Datadog.Trace.ClrProfiler.Managed` NuGet package to receive auto-instrumentation.
- Stop adding assembly references to the managed profiler code in `ModuleLoadFinished` and instead do that operation in `JITCompilationStarted`. Because of this delayed reference, the runtime will not be able to resolve the assembly reference automatically (perhaps avoiding the SecurityGrant exceptions we have seen in the past!) and will rely on the newly implemented `AppDomain.AssemblyResolve` event to find the managed profiler code.
- Build `Datadog.Trace.ClrProfiler.Managed.Loader` for `netcoreapp2.0` and `net45` because reading environment variables (to obtain profiler file path) is only supported on `netstandard1.3` and higher.

Build changes:
- Combine common settings in `Directory.Build.props` for reproductions, reproduction-dependencies, and samples into `./Test.Common.props`
- For Windows-ready xUnit integration tests, add the `RunOnWindows=True` trait
- Produce managed profiler assets by running `dotnet publish Datadog.Trace.ClrProfiler.Managed` for each target framework
- Stop building `Datadog.Trace.ClrProfiler.Managed.Loader.csproj` inside `Datadog.Trace.proj`'s `BuildCpp` target and instead build it separately whenever we build `Datadog.Trace.ClrProfiler.Native`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:builds project files, build scripts, pipelines, versioning, releases, packages area:native-library Automatic instrumentation native C++ code (Datadog.Trace.ClrProfiler.Native) area:test-apps apps used to test integrations area:tests unit tests, integration tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants