Skip to content

Commit

Permalink
Add SampleHelpers.CreateScope (#2427)
Browse files Browse the repository at this point in the history
Lets you create a Datadog Scope in integration tests without having to reference the Datadog.Trace library directly
  • Loading branch information
andrewlock authored Feb 13, 2022
1 parent cda56dd commit 6989e88
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tracer/test/test-applications/Samples.Shared/SampleHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace Samples
public class SampleHelpers
{
private static readonly Type NativeMethodsType = Type.GetType("Datadog.Trace.ClrProfiler.NativeMethods, Datadog.Trace");
private static readonly Type TracerType = Type.GetType("Datadog.Trace.Tracer, Datadog.Trace");
private static readonly MethodInfo GetTracerInstance = TracerType.GetProperty("Instance").GetMethod;
private static readonly MethodInfo StartActiveMethod = TracerType.GetMethod("StartActive", types: new[] { typeof(string) });

public static bool IsProfilerAttached()
{
Expand Down Expand Up @@ -52,6 +55,12 @@ public static void RunShutDownTasks(object caller)
}
}

public static IDisposable CreateScope(string operationName)
{
var tracer = GetTracerInstance.Invoke(null, Array.Empty<object>());
return (IDisposable) StartActiveMethod.Invoke(tracer, new object[] { operationName });
}

public static IEnumerable<KeyValuePair<string,string>> GetDatadogEnvironmentVariables()
{
var prefixes = new[] { "COR_", "CORECLR_", "DD_", "DATADOG_" };
Expand Down

0 comments on commit 6989e88

Please sign in to comment.