Skip to content

Commit

Permalink
add startup hook
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspimentel committed Jan 28, 2020
1 parent 2f536a1 commit 4dfccb5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
21 changes: 15 additions & 6 deletions src/Datadog.Trace.ClrProfiler.Managed.Loader/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public partial class Startup
{
/// <summary>
/// Initializes static members of the <see cref="Startup"/> 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.
/// </summary>
static Startup()
{
Expand All @@ -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());
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/Datadog.Trace.ClrProfiler.Managed/Instrumentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,14 @@ public static bool ProfilerAttached
}
}
}

/// <summary>
/// Initializes global instrumentation values.
/// </summary>
public static void Initialize()
{
// ensure the global Tracer instance is created so it subscribes to DiagnosticSource events
var tracer = Tracer.Instance;
}
}
}
5 changes: 2 additions & 3 deletions src/Datadog.Trace.ClrProfiler.Native/cor_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 4dfccb5

Please sign in to comment.