diff --git a/Datadog.Trace.sln b/Datadog.Trace.sln index 8d324955a..a1c8e9015 100644 --- a/Datadog.Trace.sln +++ b/Datadog.Trace.sln @@ -49,9 +49,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{BAF8 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "samples\ConsoleApp\ConsoleApp.csproj", "{2A172931-3439-4563-A6E5-525924DC2F76}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed", "src\OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed\OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed.csproj", "{6690361C-CCD8-42C4-A5CF-82D554B03A3D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed", "src\OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed\OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed.csproj", "{6690361C-CCD8-42C4-A5CF-82D554B03A3D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp.SelfBootstrap", "samples\ConsoleApp.SelfBootstrap\ConsoleApp.SelfBootstrap.csproj", "{7EA51D34-8E0E-4548-8A7F-5C6F9C3533AF}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp.SelfBootstrap", "samples\ConsoleApp.SelfBootstrap\ConsoleApp.SelfBootstrap.csproj", "{7EA51D34-8E0E-4548-8A7F-5C6F9C3533AF}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTracingLibrary", "samples\OpenTracingLibrary\OpenTracingLibrary.csproj", "{7220BDBB-084D-40B3-A710-19E3C7147185}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -155,6 +157,18 @@ Global {7EA51D34-8E0E-4548-8A7F-5C6F9C3533AF}.Release|x64.Build.0 = Release|Any CPU {7EA51D34-8E0E-4548-8A7F-5C6F9C3533AF}.Release|x86.ActiveCfg = Release|Any CPU {7EA51D34-8E0E-4548-8A7F-5C6F9C3533AF}.Release|x86.Build.0 = Release|Any CPU + {7220BDBB-084D-40B3-A710-19E3C7147185}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7220BDBB-084D-40B3-A710-19E3C7147185}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7220BDBB-084D-40B3-A710-19E3C7147185}.Debug|x64.ActiveCfg = Debug|Any CPU + {7220BDBB-084D-40B3-A710-19E3C7147185}.Debug|x64.Build.0 = Debug|Any CPU + {7220BDBB-084D-40B3-A710-19E3C7147185}.Debug|x86.ActiveCfg = Debug|Any CPU + {7220BDBB-084D-40B3-A710-19E3C7147185}.Debug|x86.Build.0 = Debug|Any CPU + {7220BDBB-084D-40B3-A710-19E3C7147185}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7220BDBB-084D-40B3-A710-19E3C7147185}.Release|Any CPU.Build.0 = Release|Any CPU + {7220BDBB-084D-40B3-A710-19E3C7147185}.Release|x64.ActiveCfg = Release|Any CPU + {7220BDBB-084D-40B3-A710-19E3C7147185}.Release|x64.Build.0 = Release|Any CPU + {7220BDBB-084D-40B3-A710-19E3C7147185}.Release|x86.ActiveCfg = Release|Any CPU + {7220BDBB-084D-40B3-A710-19E3C7147185}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -168,6 +182,7 @@ Global {2A172931-3439-4563-A6E5-525924DC2F76} = {BAF8F246-3645-42AD-B1D0-0F7EAFBAB34A} {6690361C-CCD8-42C4-A5CF-82D554B03A3D} = {9E5F0022-0A50-40BF-AC6A-C3078585ECAB} {7EA51D34-8E0E-4548-8A7F-5C6F9C3533AF} = {BAF8F246-3645-42AD-B1D0-0F7EAFBAB34A} + {7220BDBB-084D-40B3-A710-19E3C7147185} = {BAF8F246-3645-42AD-B1D0-0F7EAFBAB34A} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {160A1D00-1F5B-40F8-A155-621B4459D78F} diff --git a/samples/ConsoleApp/ConsoleApp.csproj b/samples/ConsoleApp/ConsoleApp.csproj index 303e0b402..cb0dcf4e8 100644 --- a/samples/ConsoleApp/ConsoleApp.csproj +++ b/samples/ConsoleApp/ConsoleApp.csproj @@ -13,4 +13,8 @@ - \ No newline at end of file + + + + + diff --git a/samples/ConsoleApp/Program.cs b/samples/ConsoleApp/Program.cs index 40a899f75..8f7e7e099 100644 --- a/samples/ConsoleApp/Program.cs +++ b/samples/ConsoleApp/Program.cs @@ -12,6 +12,16 @@ namespace ConsoleApp private static async Task Main() { using (var activity = MyActivitySource.StartActivity("Main")) + { + await OpenTracingLibrary.Wrapper.WithOpenTracingSpanAsync("client", RunAsync); + } + + return 0; + } + + private static async Task RunAsync() + { + using (var activity = MyActivitySource.StartActivity("RunAsync")) { activity?.SetTag("foo", "bar"); @@ -24,10 +34,8 @@ namespace ConsoleApp var client = new HttpClient(); Console.WriteLine("Calling client.GetAsync"); - await regularHttpClient.GetAsync("http://127.0.0.1:8080"); + await client.GetAsync("http://127.0.0.1:8080"); Console.WriteLine("Called client.GetAsync"); - - return 0; } } } diff --git a/samples/OpenTracingLibrary/OpenTracingLibrary.csproj b/samples/OpenTracingLibrary/OpenTracingLibrary.csproj new file mode 100644 index 000000000..5efb5bf0d --- /dev/null +++ b/samples/OpenTracingLibrary/OpenTracingLibrary.csproj @@ -0,0 +1,11 @@ + + + + net452;net462;netstandard2.0 + + + + + + + diff --git a/samples/OpenTracingLibrary/Wrapper.cs b/samples/OpenTracingLibrary/Wrapper.cs new file mode 100644 index 000000000..91750962b --- /dev/null +++ b/samples/OpenTracingLibrary/Wrapper.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using OpenTracing.Util; + +namespace OpenTracingLibrary +{ + public static class Wrapper + { + public static async Task WithOpenTracingSpanAsync(string spanKind, Func wrapped) + { + var tracer = GlobalTracer.Instance; + Console.WriteLine($">>>>>>>>>>>>>>>>>>>>>>> OpenTracing.{tracer}"); + using (var scope = tracer.BuildSpan("OpenTracing Span") + .WithTag("span.kind", spanKind) + .StartActive()) + { + try + { + await wrapped(); + scope.Span.Log("action success"); + scope.Span.SetTag("action.success", true); + } + catch (Exception ex) + { + scope.Span.SetTag("error", true); + var eventData = new Dictionary + { + { "event", "error" }, + { "error.kind", "Exception" }, + { "error.object", ex }, + { "stack", ex.StackTrace.ToString() }, + }; + scope.Span.Log(eventData); + throw; + } + } + } + } +} diff --git a/src/Datadog.Trace.ClrProfiler.Native/Datadog.Trace.ClrProfiler.Native.vcxproj b/src/Datadog.Trace.ClrProfiler.Native/Datadog.Trace.ClrProfiler.Native.vcxproj index 7d8927f62..46b122dc4 100644 --- a/src/Datadog.Trace.ClrProfiler.Native/Datadog.Trace.ClrProfiler.Native.vcxproj +++ b/src/Datadog.Trace.ClrProfiler.Native/Datadog.Trace.ClrProfiler.Native.vcxproj @@ -28,7 +28,7 @@ lib\ x64 x86 - $(LIB_PATH)fmt_$(LIB_PLATFORM)-windows-static\include;$(LIB_PATH)spdlog\include + $(CORECLR_PATH)\src\pal\prebuilt\inc;$(CORECLR_PATH)\src\inc;$(VC_IncludePath);$(WindowsSDK_IncludePath);$(LIB_PATH)fmt_$(LIB_PLATFORM)-windows-static\include;$(LIB_PATH)spdlog\include $(LIB_PATH)fmt_$(LIB_PLATFORM)-windows-static\lib\fmt.lib $(LIB_PATH)fmt_$(LIB_PLATFORM)-windows-static\debug\lib\fmtd.lib @@ -69,25 +69,21 @@ true bin\$(Configuration)\$(Platform)\static\ obj\$(Configuration)\$(Platform)\static\ - $(CORECLR_PATH)\src\pal\prebuilt\inc;$(CORECLR_PATH)\src\inc;$(VC_IncludePath);$(WindowsSDK_IncludePath); true bin\$(Configuration)\x86\static\ obj\$(Configuration)\x86\static\ - $(CORECLR_PATH)\src\pal\prebuilt\inc;$(CORECLR_PATH)\src\inc;$(VC_IncludePath);$(WindowsSDK_IncludePath); false bin\$(Configuration)\$(Platform)\static\ obj\$(Configuration)\$(Platform)\static\ - $(CORECLR_PATH)\src\pal\prebuilt\inc;$(CORECLR_PATH)\src\inc;$(VC_IncludePath);$(WindowsSDK_IncludePath); false bin\$(Configuration)\x86\static\ obj\$(Configuration)\x86\static\ - $(CORECLR_PATH)\src\pal\prebuilt\inc;$(CORECLR_PATH)\src\inc;$(VC_IncludePath);$(WindowsSDK_IncludePath); diff --git a/src/OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed/Instrumentation.cs b/src/OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed/Instrumentation.cs index 8168eac3d..324bbb41d 100644 --- a/src/OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed/Instrumentation.cs +++ b/src/OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed/Instrumentation.cs @@ -2,6 +2,8 @@ using System.Diagnostics; using System.Threading; using OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed.Configuration; +using OpenTelemetry.Context.Propagation; +using OpenTelemetry.Shims.OpenTracing; using OpenTelemetry.Trace; namespace OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed @@ -11,17 +13,17 @@ namespace OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed /// public static class Instrumentation { + private static readonly Process _process = Process.GetCurrentProcess(); private static int _firstInitialization = 1; private static TracerProvider _tracerProvider; /// - /// Initialize + /// Initialize the OpenTelemetry SDK with a pre-defined set of exporters, shims, and + /// instrumentations. /// public static void Initialize() { - var p = Process.GetCurrentProcess(); - Console.WriteLine($">>>>>>>>>>>>>>>>>>>>>>> Process: {p.ProcessName}({p.Id}), starting"); if (Interlocked.Exchange(ref _firstInitialization, value: 0) != 1) { // Initialize() was already called before @@ -43,14 +45,38 @@ namespace OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed .AddSource("OpenTelemetry.AutoInstrumentation.*"); _tracerProvider = builder.Build(); - Console.WriteLine($">>>>>>>>>>>>>>>>>>>>>>> Process: {p.ProcessName}({p.Id}), initialized"); + Log("OpenTelemetry tracer initialized."); } } - catch (Exception) + catch (Exception ex) { - // TODO: Should we have our own logger like datadog has? + Log($"OpenTelemetry SDK load exception: {ex}"); + throw; + } + + try + { + // Instantiate the OpenTracing shim. The underlying OpenTelemetry tracer will create + // spans using the "OpenTelemetry.AutoInstrumentation.OpenTracingShim" source. + var openTracingShim = new TracerShim( + _tracerProvider.GetTracer("OpenTelemetry.AutoInstrumentation.OpenTracingShim"), + Propagators.DefaultTextMapPropagator); + + // This registration must occur prior to any reference to the OpenTracing tracer: + // otherwise the no-op tracer is going to be used by OpenTracing instead. + OpenTracing.Util.GlobalTracer.Register(openTracingShim); + Log("OpenTracingShim loaded."); + } + catch (Exception ex) + { + Log($"OpenTracingShim exception: {ex}"); throw; } } + + private static void Log(string message) + { + Console.WriteLine($">>>>>>>>>>>>>>>>>>>>>>> Process: {_process.ProcessName}({_process.Id}): {message}"); + } } } diff --git a/src/OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed/OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed.csproj b/src/OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed/OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed.csproj index 7316158f5..44cf31262 100644 --- a/src/OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed/OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed.csproj +++ b/src/OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed/OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed.csproj @@ -14,6 +14,7 @@ +