From 4dfccb5c44358c45859f972a69e297afd945f92b Mon Sep 17 00:00:00 2001 From: Lucas Pimentel-Ordyna Date: Tue, 28 Jan 2020 18:16:59 -0500 Subject: [PATCH] add startup hook --- .../Startup.cs | 21 +++++++++++++------ .../Instrumentation.cs | 9 ++++++++ .../cor_profiler.cpp | 5 ++--- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/Datadog.Trace.ClrProfiler.Managed.Loader/Startup.cs b/src/Datadog.Trace.ClrProfiler.Managed.Loader/Startup.cs index 0accc2e0523b..1599af3a3397 100644 --- a/src/Datadog.Trace.ClrProfiler.Managed.Loader/Startup.cs +++ b/src/Datadog.Trace.ClrProfiler.Managed.Loader/Startup.cs @@ -10,7 +10,7 @@ public partial class Startup { /// /// Initializes static members of the class. - /// This method also attempts to load the Datadog.Trace.ClrProfiler.Managed. NET assembly. + /// This method also attempts to load the Datadog.Trace.ClrProfiler.Managed .NET assembly. /// static Startup() { @@ -21,16 +21,25 @@ static Startup() internal static string ManagedProfilerDirectory { get; } - private static bool TryLoadManagedAssembly() + private static void TryLoadManagedAssembly() { try { - Assembly.Load(new AssemblyName("Datadog.Trace.ClrProfiler.Managed, Version=1.11.1.0, Culture=neutral, PublicKeyToken=def86d061d0d2eeb")); - return true; + var assembly = Assembly.Load("Datadog.Trace.ClrProfiler.Managed, Version=1.11.1.0, Culture=neutral, PublicKeyToken=def86d061d0d2eeb"); + + if (assembly != null) + { + // call method Datadog.Trace.ClrProfiler.Instrumentation.Initialize() + var type = assembly.GetType("Datadog.Trace.ClrProfiler.Instrumentation", throwOnError: false); + var method = type?.GetRuntimeMethod("Initialize", parameters: new Type[0]); + method?.Invoke(obj: null, parameters: null); + } } - catch + catch (Exception e) { - return false; + Console.Error.WriteLine(); + Console.Error.WriteLine("Error calling Datadog.Trace.ClrProfiler.Instrumentation.Initialize():"); + Console.Error.WriteLine(e.ToString()); } } } diff --git a/src/Datadog.Trace.ClrProfiler.Managed/Instrumentation.cs b/src/Datadog.Trace.ClrProfiler.Managed/Instrumentation.cs index c1d8efcd6def..fc7e5a4766d2 100644 --- a/src/Datadog.Trace.ClrProfiler.Managed/Instrumentation.cs +++ b/src/Datadog.Trace.ClrProfiler.Managed/Instrumentation.cs @@ -32,5 +32,14 @@ public static bool ProfilerAttached } } } + + /// + /// Initializes global instrumentation values. + /// + public static void Initialize() + { + // ensure the global Tracer instance is created so it subscribes to DiagnosticSource events + var tracer = Tracer.Instance; + } } } diff --git a/src/Datadog.Trace.ClrProfiler.Native/cor_profiler.cpp b/src/Datadog.Trace.ClrProfiler.Native/cor_profiler.cpp index 574f09839190..ec2b09fc928f 100644 --- a/src/Datadog.Trace.ClrProfiler.Native/cor_profiler.cpp +++ b/src/Datadog.Trace.ClrProfiler.Native/cor_profiler.cpp @@ -214,7 +214,7 @@ HRESULT STDMETHODCALLTYPE CorProfiler::AssemblyLoadFinished(AssemblyID assembly_ if (ws.str() == ToWSTRING(PROFILER_VERSION)) { Info("AssemblyLoadFinished: Datadog.Trace.ClrProfiler.Managed v", ws.str(), " matched profiler version v", PROFILER_VERSION); managed_profiler_loaded_app_domains.insert(assembly_info.app_domain_id); - + if (runtime_information_.is_desktop() && corlib_module_loaded && assembly_info.app_domain_id == corlib_app_domain_id) { Info("AssemblyLoadFinished: Datadog.Trace.ClrProfiler.Managed was loaded domain-neutral"); @@ -478,8 +478,7 @@ HRESULT STDMETHODCALLTYPE CorProfiler::JITCompilationStarted( caller.name, "()"); } - if (!ProfilerAssemblyIsLoadedIntoAppDomain(module_metadata->app_domain_id) && - first_jit_compilation_app_domains.find(module_metadata->app_domain_id) == + if (first_jit_compilation_app_domains.find(module_metadata->app_domain_id) == first_jit_compilation_app_domains.end()) { first_jit_compilation_app_domains.insert(module_metadata->app_domain_id); hr = RunILStartupHook(module_metadata->metadata_emit, module_id,