Skip to content

Commit 830246c

Browse files
committed
Fix manual instrumentation not reporting the correct settings
1 parent d2c38b2 commit 830246c

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,19 @@ internal static CallTargetState OnMethodBegin<TTarget>(TTarget instance, object?
3636
// but in 3.7.0 we don't need to instrument them
3737
if (automaticTracer is Datadog.Trace.Tracer tracer)
3838
{
39-
PopulateSettings(values, tracer.Settings);
39+
PopulateSettings(values, tracer);
4040
}
4141

4242
return CallTargetState.GetDefault();
4343
}
4444

45-
internal static void PopulateSettings(Dictionary<string, object?> values, TracerSettings settings)
45+
internal static void PopulateSettings(Dictionary<string, object?> values, Datadog.Trace.Tracer tracer)
4646
{
4747
// record all the settings in the dictionary
48-
var mutableSettings = settings.Manager.InitialMutableSettings;
49-
var exporterSettings = settings.Manager.InitialExporterSettings;
48+
var mutableSettings = tracer.CurrentTraceSettings.Settings;
49+
// TODO: This doesn't get the "current" exporter settings, if they've been changed for code.
50+
// We don't currently provide a way to do that without subscribing to all changes, which would be overkill here.
51+
var exporterSettings = tracer.Settings.Manager.InitialExporterSettings;
5052
// This key is used to detect if the settings have been populated _at all_, so should always be sent
5153
values[TracerSettingKeyConstants.AgentUriKey] = exporterSettings.AgentUri;
5254
#pragma warning disable CS0618 // Type or member is obsolete
@@ -64,7 +66,7 @@ internal static void PopulateSettings(Dictionary<string, object?> values, Tracer
6466
values[TracerSettingKeyConstants.ServiceNameKey] = mutableSettings.ServiceName;
6567
values[TracerSettingKeyConstants.ServiceVersionKey] = mutableSettings.ServiceVersion;
6668
values[TracerSettingKeyConstants.StartupDiagnosticLogEnabledKey] = mutableSettings.StartupDiagnosticLogEnabled;
67-
values[TracerSettingKeyConstants.StatsComputationEnabledKey] = settings.StatsComputationEnabled;
69+
values[TracerSettingKeyConstants.StatsComputationEnabledKey] = tracer.Settings.StatsComputationEnabled;
6870
values[TracerSettingKeyConstants.TraceEnabledKey] = mutableSettings.TraceEnabled;
6971
values[TracerSettingKeyConstants.TracerMetricsEnabledKey] = mutableSettings.TracerMetricsEnabled;
7072

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,22 @@ public class GetUpdatedImmutableTracerSettingsIntegration
2929
{
3030
internal static CallTargetState OnMethodBegin<TTarget>(TTarget instance, ref object? automaticTracer, ref object? automaticSettings)
3131
{
32+
// In previous versions of Datadog.Trace (<3.30.0), we would replace the entire TracerSettings
33+
// object whenever the settings changed, and use that to track whether we need to update the manual
34+
// side. Now, TracerSettings is immutable for the lifetime of the application, and we instead
35+
// update the MutableSettings and ExporterSettings whenever something changes. To keep roughly the same
36+
// compatible behaviour here, we now store the current MutableSettings in `automaticSettings` to track
37+
// whether things need to update. Note that this isn't _strictly_ correct, because if the customer updates
38+
// only the exporter settings, we won't track that it's changed here. However, in PopulateSettings we _also_
39+
// don't populate the latest exporter settings there, so that's ok! Setting the exporter settings in code is
40+
// deprecated (as it's problematic for a bunch of reasons), but it's still possible, so this is a half-way
41+
// house way to handle it.
3242
if (automaticTracer is Datadog.Trace.Tracer tracer
33-
&& (automaticSettings is null || !ReferenceEquals(tracer.Settings, automaticSettings)))
43+
&& (automaticSettings is null || !ReferenceEquals(tracer.CurrentTraceSettings.Settings, automaticSettings)))
3444
{
35-
automaticSettings = tracer.Settings;
45+
automaticSettings = tracer.CurrentTraceSettings.Settings;
3646
var dict = new Dictionary<string, object?>();
37-
CtorIntegration.PopulateSettings(dict, tracer.Settings);
47+
CtorIntegration.PopulateSettings(dict, tracer);
3848
return new CallTargetState(scope: null, state: dict);
3949
}
4050

tracer/test/Datadog.Trace.Tests/ManualInstrumentation/SettingsInstrumentationTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
using System;
99
using System.Collections.Generic;
1010
using System.Linq;
11+
using System.Threading.Tasks;
1112
using Datadog.Trace.ClrProfiler.AutoInstrumentation.ManualInstrumentation;
1213
using Datadog.Trace.ClrProfiler.AutoInstrumentation.ManualInstrumentation.Configuration.TracerSettings;
1314
using Datadog.Trace.ClrProfiler.AutoInstrumentation.ManualInstrumentation.Tracer;
1415
using Datadog.Trace.Configuration;
1516
using Datadog.Trace.Configuration.ConfigurationSources;
1617
using Datadog.Trace.Telemetry.Metrics;
18+
using Datadog.Trace.TestHelpers.TestTracer;
1719
using FluentAssertions;
1820
using Xunit;
1921
using CtorIntegration = Datadog.Trace.ClrProfiler.AutoInstrumentation.ManualInstrumentation.Tracer.CtorIntegration;
@@ -219,12 +221,13 @@ public void ManualToAutomatic_CustomSettingsAreTransferredCorrectly(bool useLega
219221
}
220222

221223
[Fact]
222-
public void AutomaticToManual_ImmutableSettingsAreTransferredCorrectly()
224+
public async Task AutomaticToManual_ImmutableSettingsAreTransferredCorrectly()
223225
{
224226
var automatic = GetAndAssertAutomaticTracerSettings();
227+
await using var tracer = TracerHelper.Create(automatic);
225228

226229
Dictionary<string, object> serializedSettings = new();
227-
CtorIntegration.PopulateSettings(serializedSettings, automatic);
230+
CtorIntegration.PopulateSettings(serializedSettings, tracer);
228231

229232
var manual = new ImmutableManualSettings(serializedSettings);
230233

0 commit comments

Comments
 (0)