Skip to content

Commit 2772023

Browse files
authored
Enforce that you can't update exporter settings to point to UDS on Windows (#7721)
## Summary of changes Enforces that you can't _change_ the `AgentUri` to be a UDS Uri if you're on Windows ## Reason for change The trace exporter doesn't work with UDS on Windows, so we have a check in `TracerSettings` that disables the pipeline if we find this scenario. However, user's can still _change_ the agent URI at runtime in code (😭). We currently assume that the data pipeline won't be toggled at runtime (we _do_ allow for reconfiguring it in general, but not for completely removing or reintroducing). Changing this to allow the scenario would be a pain, so instead this PR blocks you from setting a UDS URI in code if you're on Windows. The good news is that as far as I can tell, noone does this today, so while _technically_ it could be considered a breaking change, I think it's ok. ## Implementation details - Throw an `ArgumentException` in the Datadog.Trace.Manual library, if you're on Windows (or .NET FX) and you try to set a UDS agent URI (using the same "detection" we do in `ExporterSettings`. - Add a check in the Instrumentation of `Tracer.Configure()` to make sure it hasn't slipped through. This could happen if a customer was using an old version of the Datadog.Trace NuGet package with a newer version of auto instrumentation. Note that this adds two additional framework references for .NET Core 3.1+, to check if we're on Windows. ## Test coverage Added an extra step to the manual instrumentation integration test to confirm we throw ## Other details https://datadoghq.atlassian.net/browse/LANGPLAT-819 Part of a config stack - #7522 - #7525 - #7530 - #7532 - #7543 - #7544 - #7721 👈 - #7722 - #7695 - #7723 - #7724
1 parent 82c95d6 commit 2772023

File tree

6 files changed

+75
-1
lines changed

6 files changed

+75
-1
lines changed

tracer/src/Datadog.Trace.Manual/Configuration/TracerSettings.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,23 @@ public Uri AgentUri
397397
get => _agentUri.Value;
398398
[Browsable(false)]
399399
[EditorBrowsable(EditorBrowsableState.Never)]
400-
set => _agentUri = _agentUri.Override(value);
400+
set
401+
{
402+
if (
403+
#if !NETFRAMEWORK
404+
System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows) &&
405+
#endif
406+
value.OriginalString.StartsWith("unix://", StringComparison.OrdinalIgnoreCase))
407+
{
408+
throw new ArgumentException(
409+
$"Error setting {nameof(AgentUri)} to '{value}'. " +
410+
$"{nameof(AgentUri)} can not be set to a UDS endpoint in code when running on Windows. " +
411+
"If you need to use UDS on Windows, set the environment variable DD_TRACE_AGENT_URL instead to " +
412+
"ensure the app starts with the correct configuration");
413+
}
414+
415+
_agentUri = _agentUri.Override(value);
416+
}
401417
}
402418

403419
/// <summary>

tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/ManualInstrumentation/Tracer/ConfigureIntegration.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// </copyright>
55

66
#nullable enable
7+
using System;
78
using System.Collections.Generic;
89
using System.ComponentModel;
910
using Datadog.Trace.ClrProfiler.CallTarget;
@@ -46,6 +47,23 @@ internal static void ConfigureSettingsWithManualOverrides(Dictionary<string, obj
4647
{
4748
TelemetryFactory.Metrics.Record(PublicApiUsage.Tracer_Configure);
4849

50+
// There's an edge case in our APIs where if a user changes the agent URI to UDS on Windows
51+
// then we can no longer use the trace exporter, but this is something that we assume is
52+
// immutable today. To work around this edge case, we block updating the exporter settings to
53+
// point to UDS when you're running on Windows. Note that it's fine to set to UDS _initially_,
54+
// it's only _updating_ it to UDS on Windows that we block
55+
if (FrameworkDescription.Instance.IsWindows()
56+
&& values.TryGetValue(TracerSettingKeyConstants.AgentUriKey, out var raw)
57+
&& raw is Uri uri
58+
&& uri.OriginalString.StartsWith(ExporterSettings.UnixDomainSocketPrefix, StringComparison.OrdinalIgnoreCase))
59+
{
60+
InvalidConfigurationException.Throw(
61+
$"Error changing AgentUri. " +
62+
"AgentUri can not be set to a UDS endpoint in code when running on Windows. " +
63+
"If you need to use UDS on Windows, set the environment variable DD_TRACE_AGENT_URL instead to " +
64+
"ensure the app starts with the correct configuration");
65+
}
66+
4967
// Is this from calling new TracerSettings() or TracerSettings.Global?
5068
var isFromDefaults = values.TryGetValue(TracerSettingKeyConstants.IsFromDefaultSourcesKey, out var value) && value is true;
5169

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// <copyright file="InvalidConfigurationException.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
#nullable enable
7+
8+
using System.Diagnostics.CodeAnalysis;
9+
using Datadog.Trace.ClrProfiler.CallTarget;
10+
11+
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.ManualInstrumentation.Tracer;
12+
13+
/// <summary>
14+
/// An exception indicating that an invalid value was specified for a setting in the public API.
15+
/// </summary>
16+
internal class InvalidConfigurationException(string message) : CallTargetBubbleUpException(message)
17+
{
18+
[DoesNotReturn]
19+
public static void Throw(string message) => throw new InvalidConfigurationException(message);
20+
}

tracer/test/Datadog.Trace.Tests/Snapshots/PublicApiTests.Datadog.Trace.Manual.AssemblyReferencesHaveNotChanged.net6.0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ System.Linq, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
66
System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
77
System.Runtime.CompilerServices.Unsafe, Version=4.0.6.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
88
System.Runtime.Extensions, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
9+
System.Runtime.InteropServices.RuntimeInformation, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
910
System.Threading, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

tracer/test/Datadog.Trace.Tests/Snapshots/PublicApiTests.Datadog.Trace.Manual.AssemblyReferencesHaveNotChanged.netcoreapp3.1.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ System.Linq, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
66
System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
77
System.Runtime.CompilerServices.Unsafe, Version=4.0.6.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
88
System.Runtime.Extensions, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
9+
System.Runtime.InteropServices.RuntimeInformation, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
910
System.Threading, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

tracer/test/test-applications/integrations/Samples.ManualInstrumentation/Program.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Net;
66
using System.Net.Http;
77
using System.Runtime.CompilerServices;
8+
using System.Runtime.InteropServices;
89
using System.Text;
910
using System.Threading;
1011
using System.Threading.Tasks;
@@ -318,6 +319,23 @@ async Task OtherStuff()
318319
// Manually disable debug logs
319320
GlobalSettings.SetDebugEnabled(false);
320321

322+
// Try to reconfigure tracer to use UDS on Windows. This should throw an exception
323+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
324+
{
325+
try
326+
{
327+
var invalidSettings = TracerSettings.FromDefaultSources();
328+
invalidSettings.ServiceName = "InvalidSettings";
329+
invalidSettings.AgentUri = new Uri("unix://apm.socket");
330+
Tracer.Configure(invalidSettings);
331+
ThrowIf(true, "We should not be able to configure the tracer with UDS on Windows");
332+
}
333+
catch
334+
{
335+
LogAndAssertCurrentSettings(Tracer.Instance, "RejectedInvalidSettings", settings);
336+
}
337+
}
338+
321339
// Force flush
322340
await Tracer.Instance.ForceFlushAsync();
323341

0 commit comments

Comments
 (0)