From ba9bced8c9f9a99720951946139f7a8ca458fb6a Mon Sep 17 00:00:00 2001 From: Zach Montoya Date: Fri, 17 Apr 2020 12:21:13 -0700 Subject: [PATCH] Enable the HttpSocketsHandler behind opt-in flag (#709) Enable the HttpSocketsHandler integration only when `DD_HttpSocketsHandler_ENABLED=true`. Enable this in the CI test as well. --- .../Properties/launchSettings.json | 1 + .../Integrations/HttpMessageHandlerIntegration.cs | 7 ++++++- src/Datadog.Trace/Configuration/TracerSettings.cs | 6 ++++++ .../HttpClientTests.cs | 1 + 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/samples/Samples.HttpMessageHandler/Properties/launchSettings.json b/samples/Samples.HttpMessageHandler/Properties/launchSettings.json index cfeb72c36225..6bd9bb8f6395 100644 --- a/samples/Samples.HttpMessageHandler/Properties/launchSettings.json +++ b/samples/Samples.HttpMessageHandler/Properties/launchSettings.json @@ -12,6 +12,7 @@ "CORECLR_PROFILER": "{846F5F1C-F9AE-4B07-969E-05C26BC060D8}", "CORECLR_PROFILER_PATH": "$(ProjectDir)$(OutputPath)profiler-lib\\Datadog.Trace.ClrProfiler.Native.dll", + "DD_HttpSocketsHandler_ENABLED": "true", "DD_DOTNET_TRACER_HOME": "$(ProjectDir)$(OutputPath)profiler-lib", "DD_INTEGRATIONS": "$(ProjectDir)$(OutputPath)profiler-lib\\integrations.json" }, diff --git a/src/Datadog.Trace.ClrProfiler.Managed/Integrations/HttpMessageHandlerIntegration.cs b/src/Datadog.Trace.ClrProfiler.Managed/Integrations/HttpMessageHandlerIntegration.cs index 3ef5ecd3ed4b..6f355b74aae7 100644 --- a/src/Datadog.Trace.ClrProfiler.Managed/Integrations/HttpMessageHandlerIntegration.cs +++ b/src/Datadog.Trace.ClrProfiler.Managed/Integrations/HttpMessageHandlerIntegration.cs @@ -169,7 +169,7 @@ private static async Task SendAsyncInternal( HttpRequestMessage request, CancellationToken cancellationToken) { - if (!(handler is HttpClientHandler || reportedType.FullName.Equals("System.Net.Http.SocketsHttpHandler", StringComparison.OrdinalIgnoreCase)) || + if (!(handler is HttpClientHandler || IsSocketsHttpHandlerEnabled(reportedType)) || !IsTracingEnabled(request)) { // skip instrumentation @@ -205,6 +205,11 @@ private static async Task SendAsyncInternal( } } + private static bool IsSocketsHttpHandlerEnabled(Type reportedType) + { + return Tracer.Instance.Settings.IsOptInIntegrationEnabled("HttpSocketsHandler") && reportedType.FullName.Equals("System.Net.Http.SocketsHttpHandler", StringComparison.OrdinalIgnoreCase); + } + private static bool IsTracingEnabled(HttpRequestMessage request) { if (request.Headers.TryGetValues(HttpHeaderNames.TracingEnabled, out var headerValues)) diff --git a/src/Datadog.Trace/Configuration/TracerSettings.cs b/src/Datadog.Trace/Configuration/TracerSettings.cs index c2cfdc57be17..9780801255cc 100644 --- a/src/Datadog.Trace/Configuration/TracerSettings.cs +++ b/src/Datadog.Trace/Configuration/TracerSettings.cs @@ -248,6 +248,12 @@ internal bool IsIntegrationEnabled(string name) return TraceEnabled && !disabled; } + internal bool IsOptInIntegrationEnabled(string name) + { + bool disabled = Integrations[name].Enabled != true || DisabledIntegrationNames.Contains(name); + return TraceEnabled && !disabled; + } + internal double? GetIntegrationAnalyticsSampleRate(string name, bool enabledWithGlobalSetting) { var integrationSettings = Integrations[name]; diff --git a/test/Datadog.Trace.ClrProfiler.IntegrationTests/HttpClientTests.cs b/test/Datadog.Trace.ClrProfiler.IntegrationTests/HttpClientTests.cs index a0bc982ca41b..5afc26984ea9 100644 --- a/test/Datadog.Trace.ClrProfiler.IntegrationTests/HttpClientTests.cs +++ b/test/Datadog.Trace.ClrProfiler.IntegrationTests/HttpClientTests.cs @@ -16,6 +16,7 @@ public HttpClientTests(ITestOutputHelper output) : base("HttpMessageHandler", output) { SetEnvironmentVariable("DD_TRACE_DOMAIN_NEUTRAL_INSTRUMENTATION", "true"); + SetEnvironmentVariable("DD_HttpSocketsHandler_ENABLED", "true"); } [Fact]