Skip to content

Commit 958d2a2

Browse files
andrewlockveerbia
authored andcommitted
Make TracerSettings properly immutable and remove ImmutableTracerSettings (#6415)
Merge `TracerSettings` with `ImmutableTracerSettings` This stack of PRs is about doing one-shot configuration instead of mutation. We never mutate these in the tracer after creation, so there's no need for the separate types. - Make the properties in `TracerSettings` get-only. - Make the collections in `TracerSettings` readonly. - Move logic that used to be in the constructor of `ImmutableTracerSettings` into `TracerSettings` - e.g. Service/Version/Env were being changed based on DD_TAGS values. Moved that to TracerSettings and (importantly) added missing telemetry recording of these values. - Added missing recording of _effective_ `DisabledInstegrations` - Moving this logic caused some _tests_ to be broken (checking default values). Updated the expected values of those tests in a single - Replace all usages of `ImmutableTracerSettings` with `TracerSettings` - Move `ITracer` to Datadog.Trace.Manual - It's only used there, and references the manual-version of `ImmutableTracerSettings` which we _want_ to keep. - Move `Immutable*Tests` into appropriate file and tweak - Replace mutations with initialization in tests All covered by existing tests (I hope) 🤞 There's still a _lot_ of scope to improve this Part of Stack - #6370 - #6376 - #6385 - #6386 - #6397 - #6399 - #6400 - #6405 - #6408 - #6415 👈 This PR
1 parent e5ad4f6 commit 958d2a2

File tree

91 files changed

+732
-872
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+732
-872
lines changed

tracer/src/Datadog.Trace.Manual/Datadog.Trace.Manual.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
<Compile Include="..\Datadog.Trace\IScope.cs" />
3636
<Compile Include="..\Datadog.Trace\ISpan.cs" />
3737
<Compile Include="..\Datadog.Trace\ISpanContext.cs" />
38-
<Compile Include="..\Datadog.Trace\ITracer.cs" />
3938
<Compile Include="..\Datadog.Trace\SamplingPriority.cs" />
4039
<Compile Include="..\Datadog.Trace\SamplingPriorityValues.cs" />
4140
<Compile Include="..\Datadog.Trace\SpanCreationSettings.cs" />
File renamed without changes.

tracer/src/Datadog.Trace.Tools.Runner/Utils.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,7 @@ public static string GetEnvironmentVariable(string key, string defaultValue = nu
406406
configurationSource.Add(new NameValueConfigurationSource(env, ConfigurationOrigins.EnvVars));
407407
configurationSource.Add(GlobalConfigurationSource.Instance);
408408

409-
var tracerSettings = new TracerSettings(configurationSource, new ConfigurationTelemetry(), new OverrideErrorLog());
410-
var settings = new ImmutableTracerSettings(tracerSettings, unusedParamNotToUsePublicApi: true);
409+
var settings = new TracerSettings(configurationSource, new ConfigurationTelemetry(), new OverrideErrorLog());
411410

412411
Log.Debug("Creating DiscoveryService for: {AgentUri}", settings.Exporter.AgentUri);
413412
var discoveryService = DiscoveryService.Create(
@@ -425,7 +424,7 @@ public static string GetEnvironmentVariable(string key, string defaultValue = nu
425424
using (cts.Token.Register(
426425
() =>
427426
{
428-
WriteError($"Error connecting to the Datadog Agent at {tracerSettings.Exporter.AgentUri}.");
427+
WriteError($"Error connecting to the Datadog Agent at {settings.Exporter.AgentUri}.");
429428
tcs.TrySetResult(null);
430429
}))
431430
{

tracer/src/Datadog.Trace/Agent/StatsAggregator.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ internal class StatsAggregator : IStatsAggregator
4646

4747
private int _currentBuffer;
4848

49-
internal StatsAggregator(IApi api, ImmutableTracerSettings settings, IDiscoveryService discoveryService)
49+
internal StatsAggregator(IApi api, TracerSettings settings, IDiscoveryService discoveryService)
5050
{
5151
_api = api;
5252
_processExit = new TaskCompletionSource<bool>();
@@ -91,7 +91,7 @@ internal StatsAggregator(IApi api, ImmutableTracerSettings settings, IDiscoveryS
9191

9292
public bool? CanComputeStats { get; private set; } = null;
9393

94-
public static IStatsAggregator Create(IApi api, ImmutableTracerSettings settings, IDiscoveryService discoveryService)
94+
public static IStatsAggregator Create(IApi api, TracerSettings settings, IDiscoveryService discoveryService)
9595
{
9696
return settings.StatsComputationEnabled ? new StatsAggregator(api, settings, discoveryService) : new NullStatsAggregator();
9797
}

tracer/src/Datadog.Trace/Agent/TraceSamplers/RareSampler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal class RareSampler : ITraceChunkSampler
1818
private readonly HashSet<StatsAggregationKey> _keys = new();
1919
private readonly Queue<StatsAggregationKey> _cache = new();
2020

21-
public RareSampler(ImmutableTracerSettings settings)
21+
public RareSampler(TracerSettings settings)
2222
{
2323
IsEnabled = settings.IsRareSamplerEnabled;
2424
}

tracer/src/Datadog.Trace/Ci/Agent/ApmAgentWriter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal class ApmAgentWriter : IEventWriter
2525
private static Span[]? _spanArray;
2626
private readonly AgentWriter _agentWriter;
2727

28-
public ApmAgentWriter(ImmutableTracerSettings settings, Action<Dictionary<string, float>> updateSampleRates, IDiscoveryService discoveryService, int maxBufferSize = DefaultMaxBufferSize)
28+
public ApmAgentWriter(TracerSettings settings, Action<Dictionary<string, float>> updateSampleRates, IDiscoveryService discoveryService, int maxBufferSize = DefaultMaxBufferSize)
2929
{
3030
var partialFlushEnabled = settings.Exporter.PartialFlushEnabled;
3131
var apiRequestFactory = TracesTransportStrategy.Get(settings.Exporter);

tracer/src/Datadog.Trace/Ci/CITracerManager.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal class CITracerManager : TracerManager
2929
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor<CITracerManager>();
3030

3131
public CITracerManager(
32-
ImmutableTracerSettings settings,
32+
TracerSettings settings,
3333
IAgentWriter agentWriter,
3434
IScopeManager scopeManager,
3535
IDogStatsd statsd,
@@ -145,7 +145,7 @@ public void WriteEvent(IEvent @event)
145145
internal class LockedManager : CITracerManager, ILockedTracer
146146
{
147147
public LockedManager(
148-
ImmutableTracerSettings settings,
148+
TracerSettings settings,
149149
IAgentWriter agentWriter,
150150
IScopeManager scopeManager,
151151
IDogStatsd statsd,

tracer/src/Datadog.Trace/Ci/CITracerManagerFactory.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public CITracerManagerFactory(CIVisibilitySettings settings, IDiscoveryService d
3939
}
4040

4141
protected override TracerManager CreateTracerManagerFrom(
42-
ImmutableTracerSettings settings,
42+
TracerSettings settings,
4343
IAgentWriter agentWriter,
4444
IScopeManager scopeManager,
4545
IDogStatsd statsd,
@@ -64,24 +64,24 @@ protected override TracerManager CreateTracerManagerFrom(
6464
return new CITracerManager(settings, agentWriter, scopeManager, statsd, runtimeMetrics, logSubmissionManager, telemetry, discoveryService, dataStreamsManager, defaultServiceName, gitMetadataTagsProvider, traceSampler, spanSampler, remoteConfigurationManager, dynamicConfigurationManager, tracerFlareManager);
6565
}
6666

67-
protected override ITelemetryController CreateTelemetryController(ImmutableTracerSettings settings, IDiscoveryService discoveryService)
67+
protected override ITelemetryController CreateTelemetryController(TracerSettings settings, IDiscoveryService discoveryService)
6868
{
6969
return TelemetryFactory.Instance.CreateCiVisibilityTelemetryController(settings, discoveryService, isAgentAvailable: !_settings.Agentless);
7070
}
7171

72-
protected override IGitMetadataTagsProvider GetGitMetadataTagsProvider(ImmutableTracerSettings settings, IScopeManager scopeManager, ITelemetryController telemetry)
72+
protected override IGitMetadataTagsProvider GetGitMetadataTagsProvider(TracerSettings settings, IScopeManager scopeManager, ITelemetryController telemetry)
7373
{
7474
return new CIGitMetadataTagsProvider(telemetry);
7575
}
7676

77-
protected override ITraceSampler GetSampler(ImmutableTracerSettings settings)
77+
protected override ITraceSampler GetSampler(TracerSettings settings)
7878
{
7979
return new CISampler();
8080
}
8181

82-
protected override bool ShouldEnableRemoteConfiguration(ImmutableTracerSettings settings) => false;
82+
protected override bool ShouldEnableRemoteConfiguration(TracerSettings settings) => false;
8383

84-
protected override IAgentWriter GetAgentWriter(ImmutableTracerSettings settings, IDogStatsd statsd, Action<Dictionary<string, float>> updateSampleRates, IDiscoveryService discoveryService)
84+
protected override IAgentWriter GetAgentWriter(TracerSettings settings, IDogStatsd statsd, Action<Dictionary<string, float>> updateSampleRates, IDiscoveryService discoveryService)
8585
{
8686
// Check for agentless scenario
8787
if (_settings.Agentless)
@@ -107,7 +107,7 @@ protected override IAgentWriter GetAgentWriter(ImmutableTracerSettings settings,
107107
return new ApmAgentWriter(settings, updateSampleRates, discoveryService, traceBufferSize);
108108
}
109109

110-
protected override IDiscoveryService GetDiscoveryService(ImmutableTracerSettings settings)
110+
protected override IDiscoveryService GetDiscoveryService(TracerSettings settings)
111111
=> _discoveryService;
112112
}
113113
}

tracer/src/Datadog.Trace/Ci/CIVisibility.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public static void Initialize()
133133

134134
// Initialize Tracer
135135
Log.Information("Initialize Test Tracer instance");
136-
TracerManager.ReplaceGlobalManager(new ImmutableTracerSettings(tracerSettings, true), new CITracerManagerFactory(settings, discoveryService, eventPlatformProxyEnabled, UseLockedTracerManager));
136+
TracerManager.ReplaceGlobalManager(tracerSettings, new CITracerManagerFactory(settings, discoveryService, eventPlatformProxyEnabled, UseLockedTracerManager));
137137
_ = Tracer.Instance;
138138

139139
// Initialize FrameworkDescription
@@ -192,7 +192,7 @@ internal static void InitializeFromRunner(CIVisibilitySettings settings, IDiscov
192192

193193
// Initialize Tracer
194194
Log.Information("Initialize Test Tracer instance");
195-
TracerManager.ReplaceGlobalManager(new ImmutableTracerSettings(tracerSettings, true), new CITracerManagerFactory(settings, discoveryService, eventPlatformProxyEnabled, UseLockedTracerManager));
195+
TracerManager.ReplaceGlobalManager(tracerSettings, new CITracerManagerFactory(settings, discoveryService, eventPlatformProxyEnabled, UseLockedTracerManager));
196196
_ = Tracer.Instance;
197197

198198
// Initialize FrameworkDescription
@@ -402,12 +402,12 @@ internal static string GetServiceNameFromRepository(string? repository)
402402
return string.Empty;
403403
}
404404

405-
internal static IApiRequestFactory GetRequestFactory(ImmutableTracerSettings settings)
405+
internal static IApiRequestFactory GetRequestFactory(TracerSettings settings)
406406
{
407407
return GetRequestFactory(settings, TimeSpan.FromSeconds(15));
408408
}
409409

410-
internal static IApiRequestFactory GetRequestFactory(ImmutableTracerSettings tracerSettings, TimeSpan timeout)
410+
internal static IApiRequestFactory GetRequestFactory(TracerSettings tracerSettings, TimeSpan timeout)
411411
{
412412
IApiRequestFactory? factory = null;
413413
var exporterSettings = tracerSettings.Exporter;

tracer/src/Datadog.Trace/Ci/IntelligentTestRunnerClient.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public IntelligentTestRunnerClient(string? workingDirectory, CIVisibilitySetting
8787
_getRepositoryUrlTask = GetRepositoryUrlAsync();
8888
_getBranchNameTask = GetBranchNameAsync();
8989
_getShaTask = GetCommitShaAsync();
90-
_apiRequestFactory = CIVisibility.GetRequestFactory(new ImmutableTracerSettings(_settings.TracerSettings, true), TimeSpan.FromSeconds(45));
90+
_apiRequestFactory = CIVisibility.GetRequestFactory(_settings.TracerSettings, TimeSpan.FromSeconds(45));
9191

9292
const string settingsUrlPath = "api/v2/libraries/tests/services/setting";
9393
const string searchCommitsUrlPath = "api/v2/git/repository/search_commits";
@@ -166,7 +166,7 @@ public IntelligentTestRunnerClient(string? workingDirectory, CIVisibilitySetting
166166
}
167167
}
168168

169-
internal static Dictionary<string, string>? GetCustomTestsConfigurations(IDictionary<string, string> globalTags)
169+
internal static Dictionary<string, string>? GetCustomTestsConfigurations(IReadOnlyDictionary<string, string> globalTags)
170170
{
171171
Dictionary<string, string>? customConfiguration = null;
172172
if (globalTags is not null)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ internal static void ConfigureSettingsWithManualOverrides(Dictionary<string, obj
5151
var settings = new TracerSettings(source, new ConfigurationTelemetry(), new OverrideErrorLog());
5252

5353
// Update the global instance
54-
Trace.Tracer.ConfigureInternal(new ImmutableTracerSettings(settings, true));
54+
Trace.Tracer.ConfigureInternal(settings);
5555
}
5656
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal static CallTargetState OnMethodBegin<TTarget>(TTarget instance, object?
4242
return CallTargetState.GetDefault();
4343
}
4444

45-
internal static void PopulateSettings(Dictionary<string, object?> values, ImmutableTracerSettings settings)
45+
internal static void PopulateSettings(Dictionary<string, object?> values, TracerSettings settings)
4646
{
4747
// record all the settings in the dictionary
4848
// This key is used to detect if the settings have been populated _at all_, so should always be sent

tracer/src/Datadog.Trace/ClrProfiler/ServerlessInstrumentation/ServerlessMiniAgent.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal static class ServerlessMiniAgent
1616
{
1717
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(ServerlessMiniAgent));
1818

19-
internal static string GetMiniAgentPath(PlatformID os, ImmutableTracerSettings settings)
19+
internal static string GetMiniAgentPath(PlatformID os, TracerSettings settings)
2020
{
2121
if (!settings.IsRunningInGCPFunctions && !settings.IsRunningMiniAgentInAzureFunctions)
2222
{
@@ -52,7 +52,7 @@ internal static string GetMiniAgentPath(PlatformID os, ImmutableTracerSettings s
5252
return Path.Combine(rustBinaryPathRoot, rustBinaryPathOsFolder, rustBinaryName);
5353
}
5454

55-
internal static void StartServerlessMiniAgent(ImmutableTracerSettings settings)
55+
internal static void StartServerlessMiniAgent(TracerSettings settings)
5656
{
5757
var serverlessMiniAgentPath = ServerlessMiniAgent.GetMiniAgentPath(Environment.OSVersion.Platform, settings);
5858
if (string.IsNullOrEmpty(serverlessMiniAgentPath))

tracer/src/Datadog.Trace/Configuration/DynamicConfigurationManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private static void OnConfigurationChanged(ConfigurationBuilder settings)
8383
GlobalSamplingRate = settings.WithKeys(ConfigurationKeys.GlobalSamplingRate).AsDouble(),
8484
// SpanSamplingRules = settings.WithKeys(ConfigurationKeys.SpanSamplingRules).AsString(),
8585
LogsInjectionEnabled = settings.WithKeys(ConfigurationKeys.LogsInjectionEnabled).AsBool(),
86-
HeaderTags = headerTags == null ? null : new ReadOnlyDictionary<string, string>(headerTags),
86+
HeaderTags = headerTags,
8787
// ServiceNameMappings = serviceNameMappings == null ? null : new ReadOnlyDictionary<string, string>(serviceNameMappings)
8888
GlobalTags = globalTags == null ? null : new ReadOnlyDictionary<string, string>(globalTags)
8989
};

tracer/src/Datadog.Trace/Configuration/GitMetadataTagsProvider.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ namespace Datadog.Trace.Configuration;
2020
internal class GitMetadataTagsProvider : IGitMetadataTagsProvider
2121
{
2222
private readonly ITelemetryController _telemetry;
23-
private readonly ImmutableTracerSettings _immutableTracerSettings;
23+
private readonly TracerSettings _immutableTracerSettings;
2424
private readonly IScopeManager _scopeManager;
2525
private GitMetadata? _cachedGitTags = null;
2626
private int _tryCount = 0;
2727

28-
public GitMetadataTagsProvider(ImmutableTracerSettings immutableTracerSettings, IScopeManager scopeManager, ITelemetryController telemetry)
28+
public GitMetadataTagsProvider(TracerSettings immutableTracerSettings, IScopeManager scopeManager, ITelemetryController telemetry)
2929
{
3030
_immutableTracerSettings = immutableTracerSettings;
3131
_scopeManager = scopeManager;

0 commit comments

Comments
 (0)