Skip to content

Commit c29eb56

Browse files
committed
Move configure call out of remote config and config in code integrations
1 parent 8b09597 commit c29eb56

File tree

3 files changed

+31
-123
lines changed

3 files changed

+31
-123
lines changed

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

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -56,68 +56,14 @@ internal static void ConfigureSettingsWithManualOverrides(Dictionary<string, obj
5656
: new ManualInstrumentationConfigurationSource(values, isFromDefaults);
5757

5858
// We need to save this immediately, even if there's no manifest changes in the final settings
59+
// so that it can be picked up by other configuration updaters, e.g. remote config
5960
GlobalConfigurationSource.UpdateManualConfigurationSource(manualConfig);
60-
61-
var tracerSettings = Datadog.Trace.Tracer.Instance.Settings;
6261
var dynamicConfig = GlobalConfigurationSource.DynamicConfigurationSource;
63-
var initialSettings = isFromDefaults
64-
? tracerSettings.InitialMutableSettings
65-
: MutableSettings.CreateWithoutDefaultSources(tracerSettings);
66-
67-
// TODO: these will eventually live elsewhere
68-
var currentSettings = tracerSettings.MutableSettings;
69-
70-
var manualTelemetry = new ConfigurationTelemetry();
71-
var newMutableSettings = MutableSettings.CreateUpdatedMutableSettings(
72-
dynamicConfig,
73-
manualConfig,
74-
initialSettings,
75-
tracerSettings,
76-
manualTelemetry,
77-
new OverrideErrorLog()); // TODO: We'll later report these
7862

79-
var isSameMutableSettings = currentSettings.Equals(newMutableSettings);
80-
81-
// The only exporter setting we currently _allow_ to change is the AgentUri, but if that does change,
82-
// it can mean that _everything_ about the exporter settings changes. To minimize the work to do, and
83-
// to simplify comparisons, we try to read the agent url from the manual setting. If it's missing, not
84-
// set, or unchanged, there's no need to update the exporter settings. In the future, ExporterSettings
85-
// will live separate from TracerSettings entirely.
86-
var exporterTelemetry = new ConfigurationTelemetry();
87-
var newRawExporterSettings = ExporterSettings.Raw.CreateUpdatedFromManualConfig(
88-
tracerSettings.Exporter.RawSettings,
89-
manualConfig,
90-
exporterTelemetry,
91-
isFromDefaults);
92-
var isSameExporterSettings = tracerSettings.Exporter.RawSettings.Equals(newRawExporterSettings);
93-
94-
if (isSameMutableSettings && isSameExporterSettings)
63+
var wasUpdated = Datadog.Trace.Tracer.Instance.TracerManager.SettingsManager.UpdateSettings(dynamicConfig, manualConfig, TelemetryFactory.Config);
64+
if (wasUpdated)
9565
{
96-
Log.Debug("No changes detected in the new configuration in code");
97-
// Even though there were no "real" changes, there may be _effective_ changes in telemetry that
98-
// need to be recorded (e.g. the customer set the value in code but it was already set via
99-
// env vars). We _should_ record exporter settings too, but that introduces a bunch of complexity
100-
// which we'll resolve later anyway, so just have that gap for now (it's very niche).
101-
// If there are changes, they're recorded automatically in ConfigureInternal
102-
manualTelemetry.CopyTo(TelemetryFactory.Config);
103-
return;
66+
Log.Information("Setting updates made via configuration in code were applied");
10467
}
105-
106-
Log.Information("Applying new configuration in code");
107-
TracerSettings newSettings;
108-
if (isSameExporterSettings)
109-
{
110-
newSettings = tracerSettings with { MutableSettings = newMutableSettings };
111-
}
112-
else
113-
{
114-
var exporterSettings = new ExporterSettings(newRawExporterSettings, exporterTelemetry);
115-
newSettings = isSameMutableSettings
116-
? tracerSettings with { Exporter = exporterSettings }
117-
: tracerSettings with { MutableSettings = newMutableSettings, Exporter = exporterSettings };
118-
}
119-
120-
// Update the global instance
121-
Trace.Tracer.Configure(newSettings);
12268
}
12369
}

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

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using System.Threading;
1414
using System.Threading.Tasks;
1515
using Datadog.Trace.Configuration.ConfigurationSources;
16-
using Datadog.Trace.Configuration.ConfigurationSources.Telemetry;
1716
using Datadog.Trace.Configuration.Telemetry;
1817
using Datadog.Trace.Debugger;
1918
using Datadog.Trace.Debugger.Configurations;
@@ -74,72 +73,16 @@ internal static void OnlyForTests_ApplyConfiguration(IConfigurationSource dynami
7473

7574
private static void OnConfigurationChanged(IConfigurationSource dynamicConfig)
7675
{
77-
var tracerSettings = Tracer.Instance.Settings;
78-
var manualSource = GlobalConfigurationSource.ManualConfigurationSource;
79-
var mutableSettings = manualSource.UseDefaultSources
80-
? tracerSettings.InitialMutableSettings
81-
: MutableSettings.CreateWithoutDefaultSources(tracerSettings);
76+
var manualConfig = GlobalConfigurationSource.ManualConfigurationSource;
8277

8378
// We save this immediately, even if there's no manifest changes in the final settings
79+
// so that it can be picked up by other configuration updaters, e.g. config in code
8480
GlobalConfigurationSource.UpdateDynamicConfigConfigurationSource(dynamicConfig);
8581

86-
OnConfigurationChanged(
87-
dynamicConfig,
88-
manualSource,
89-
mutableSettings,
90-
tracerSettings,
91-
// TODO: In the future this will 'live' elsewhere
92-
currentSettings: tracerSettings.MutableSettings,
93-
new ConfigurationTelemetry(),
94-
new OverrideErrorLog()); // TODO: We'll later report these
95-
}
96-
97-
private static void OnConfigurationChanged(
98-
IConfigurationSource dynamicConfig,
99-
ManualInstrumentationConfigurationSourceBase manualConfig,
100-
MutableSettings initialSettings,
101-
TracerSettings tracerSettings,
102-
MutableSettings currentSettings,
103-
ConfigurationTelemetry telemetry,
104-
OverrideErrorLog errorLog)
105-
{
106-
var newMutableSettings = MutableSettings.CreateUpdatedMutableSettings(
107-
dynamicConfig,
108-
manualConfig,
109-
initialSettings,
110-
tracerSettings,
111-
telemetry,
112-
errorLog);
113-
114-
TracerSettings newSettings;
115-
if (currentSettings.Equals(newMutableSettings))
82+
var wasUpdated = Tracer.Instance.TracerManager.SettingsManager.UpdateSettings(dynamicConfig, manualConfig, TelemetryFactory.Config);
83+
if (wasUpdated)
11684
{
117-
Log.Debug("No changes detected in the new dynamic configuration");
118-
// Even though there were no "real" changes, there may be _effective_ changes in telemetry that
119-
// need to be recorded (e.g. the customer set the value in code but it was already set via
120-
// env vars). We _should_ record exporter settings too, but that introduces a bunch of complexity
121-
// which we'll resolve later anyway, so just have that gap for now (it's very niche).
122-
// If there are changes, they're recorded automatically in Tracer.Configure()
123-
telemetry.CopyTo(TelemetryFactory.Config);
124-
newSettings = tracerSettings;
125-
}
126-
else
127-
{
128-
Log.Information("Applying new dynamic configuration");
129-
130-
newSettings = tracerSettings with { MutableSettings = newMutableSettings };
131-
132-
/*
133-
if (debugLogsEnabled != null && debugLogsEnabled.Value != GlobalSettings.Instance.DebugEnabled)
134-
{
135-
GlobalSettings.SetDebugEnabled(debugLogsEnabled.Value);
136-
Security.Instance.SetDebugEnabled(debugLogsEnabled.Value);
137-
138-
NativeMethods.UpdateSettings(new[] { ConfigurationKeys.DebugEnabled }, new[] { debugLogsEnabled.Value ? "1" : "0" });
139-
}
140-
*/
141-
142-
Tracer.Configure(newSettings);
85+
Log.Information("Setting updates made via configuration in code were applied");
14386
}
14487

14588
// TODO: This might not record the config in the correct order in future, but would require
@@ -172,7 +115,7 @@ private static void OnConfigurationChanged(
172115

173116
var newDebuggerSettings = oldDebuggerSettings with { DynamicSettings = dynamicDebuggerSettings };
174117

175-
DebuggerManager.Instance.UpdateConfiguration(newSettings, newDebuggerSettings)
118+
DebuggerManager.Instance.UpdateConfiguration(Tracer.Instance.Settings, newDebuggerSettings)
176119
.ContinueWith(t => Log.Error(t?.Exception, "Error updating dynamic configuration for debugger"), TaskContinuationOptions.OnlyOnFaulted);
177120
}
178121

tracer/src/Datadog.Trace/TracerManager.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ private static TracerManager CreateInitializedTracer(TracerSettings settings, Tr
668668
if (_firstInitialization)
669669
{
670670
_firstInitialization = false;
671-
OneTimeSetup(newManager.Settings);
671+
OneTimeSetup(newManager.Settings, newManager.SettingsManager);
672672
}
673673

674674
if (newManager.PerTraceSettings.Settings.StartupDiagnosticLogEnabled)
@@ -679,7 +679,7 @@ private static TracerManager CreateInitializedTracer(TracerSettings settings, Tr
679679
return newManager;
680680
}
681681

682-
private static void OneTimeSetup(TracerSettings tracerSettings)
682+
private static void OneTimeSetup(TracerSettings tracerSettings, SettingsManager settingsManager)
683683
{
684684
// Register callbacks to make sure we flush the traces before exiting
685685
LifetimeManager.Instance.AddAsyncShutdownTask(RunShutdownTasksAsync);
@@ -689,6 +689,25 @@ private static void OneTimeSetup(TracerSettings tracerSettings)
689689

690690
// Record the service discovery metadata
691691
ServiceDiscoveryHelper.StoreTracerMetadata(tracerSettings);
692+
693+
// Register for rebuilding the settings on changes
694+
// TODO: This is only temporary, we want to _stop_ rebuilding everything whenever settings change in the future
695+
// We also don't bother to dispose this because we never unsubscribe
696+
settingsManager.SubscribeToChanges(updatedSettings =>
697+
{
698+
var newSettings = updatedSettings switch
699+
{
700+
{ Exporter: { } e, Mutable: { } m } => Tracer.Instance.Settings with { Exporter = e, MutableSettings = m },
701+
{ Exporter: { } e } => Tracer.Instance.Settings with { Exporter = e },
702+
{ Mutable: { } m } => Tracer.Instance.Settings with { MutableSettings = m },
703+
_ => null,
704+
};
705+
if (newSettings != null)
706+
{
707+
// Update the global instance
708+
Trace.Tracer.Configure(newSettings);
709+
}
710+
});
692711
}
693712

694713
private static Task RunShutdownTasksAsync(Exception ex) => RunShutdownTasksAsync(_instance, _heartbeatTimer);

0 commit comments

Comments
 (0)