Skip to content

Commit 2824931

Browse files
committed
Special case for IntegrationSettings.cs keys
1 parent ed76b74 commit 2824931

File tree

8 files changed

+65
-67
lines changed

8 files changed

+65
-67
lines changed

tracer/src/Datadog.Trace/Configuration/ConfigurationSources/GlobalConfigurationSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ internal static bool TryLoadJsonConfigurationFile(IConfigurationSource configura
106106

107107
// if environment variable is not set, look for default file name in the current directory
108108
var configurationFileName = new ConfigurationBuilder(configurationSource, telemetry)
109-
.WithKeys(ConfigurationKeys.ConfigurationFileName, "DD_DOTNET_TRACER_CONFIG_FILE")
109+
.WithKeys(ConfigurationKeys.ConfigurationFileName)
110110
.AsString(
111111
getDefaultValue: () => Path.Combine(baseDirectory ?? GetCurrentDirectory(), "datadog.json"),
112112
validator: null);

tracer/src/Datadog.Trace/Configuration/ConfigurationSources/Telemetry/ConfigurationBuilder.cs

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,46 @@ internal readonly struct ConfigurationBuilder(IConfigurationSource source, IConf
1919

2020
public HasKeys WithKeys(string key) => new(_source, _telemetry, key);
2121

22-
public HasKeys WithKeys(string key, string fallbackKey) => new(_source, _telemetry, key, fallbackKey);
23-
24-
public HasKeys WithKeys(string key, string fallbackKey1, string fallbackKey2) => new(_source, _telemetry, key, fallbackKey1, fallbackKey2);
25-
26-
public HasKeys WithKeys(string key, string fallbackKey1, string fallbackKey2, string fallbackKey3) => new(_source, _telemetry, key, fallbackKey1, fallbackKey2, fallbackKey3);
27-
28-
internal readonly struct HasKeys
22+
public HasKeys WithIntegrationKey(string integrationName) => new(
23+
_source,
24+
_telemetry,
25+
string.Format(IntegrationSettings.IntegrationEnabledKey, integrationName.ToUpperInvariant()),
26+
[
27+
string.Format(IntegrationSettings.IntegrationEnabledKey, integrationName),
28+
$"DD_{integrationName}_ENABLED"
29+
]);
30+
31+
public HasKeys WithIntegrationAnalyticsKey(string integrationName) => new(
32+
_source,
33+
_telemetry,
34+
#pragma warning disable 618 // App analytics is deprecated, but still used
35+
string.Format(IntegrationSettings.AnalyticsEnabledKey, integrationName.ToUpperInvariant()),
36+
[
37+
string.Format(IntegrationSettings.AnalyticsEnabledKey, integrationName),
38+
#pragma warning restore 618
39+
$"DD_{integrationName}_ANALYTICS_ENABLED"
40+
]);
41+
42+
public HasKeys WithIntegrationAnalyticsSampleRateKey(string integrationName) => new(
43+
_source,
44+
_telemetry,
45+
#pragma warning disable 618 // App analytics is deprecated, but still used
46+
string.Format(IntegrationSettings.AnalyticsSampleRateKey, integrationName.ToUpperInvariant()),
47+
[
48+
string.Format(IntegrationSettings.AnalyticsSampleRateKey, integrationName),
49+
#pragma warning restore 618
50+
$"DD_{integrationName}_ANALYTICS_SAMPLE_RATE"
51+
]);
52+
53+
internal readonly struct HasKeys(IConfigurationSource source, IConfigurationTelemetry telemetry, string key, string[]? providedAliases = null)
2954
{
30-
public HasKeys(IConfigurationSource source, IConfigurationTelemetry telemetry, string key, string? fallbackKey1 = null, string? fallbackKey2 = null, string? fallbackKey3 = null)
31-
{
32-
Source = source;
33-
Telemetry = telemetry;
34-
Key = key;
35-
FallbackKey1 = fallbackKey1;
36-
FallbackKey2 = fallbackKey2;
37-
FallbackKey3 = fallbackKey3;
38-
}
39-
40-
private IConfigurationSource Source { get; }
41-
42-
private IConfigurationTelemetry Telemetry { get; }
43-
44-
private string Key { get; }
55+
private readonly string[]? _providedAliases = providedAliases;
4556

46-
private string? FallbackKey1 { get; }
57+
private IConfigurationSource Source { get; } = source;
4758

48-
private string? FallbackKey2 { get; }
59+
private IConfigurationTelemetry Telemetry { get; } = telemetry;
4960

50-
private string? FallbackKey3 { get; }
61+
private string Key { get; } = key;
5162

5263
// ****************
5364
// String accessors
@@ -536,15 +547,13 @@ private ConfigurationResult<IDictionary<string, string>> GetDictionaryResult(Fun
536547
/// <returns>The raw <see cref="ConfigurationResult{T}"/></returns>
537548
private ConfigurationResult<T> GetResultWithFallback<T>(Func<string, ConfigurationResult<T>> selector)
538549
{
539-
var hasAllKeys = _allKeys is not null;
540-
var canonicalKey = hasAllKeys ? _allKeys![0] : Key;
541-
var result = selector(canonicalKey);
550+
var result = selector(Key);
542551
if (!result.ShouldFallBack)
543552
{
544553
return result;
545554
}
546555

547-
string[] aliases = !hasAllKeys ? ConfigKeyAliasesSwitcher.GetAliases(Key) : [_allKeys![1], _allKeys[2]];
556+
string[] aliases = _providedAliases ?? ConfigKeyAliasesSwitcher.GetAliases(Key);
548557

549558
foreach (var alias in aliases)
550559
{

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

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#nullable enable
77

88
using System;
9+
using System.Collections.Generic;
910
using Datadog.Trace.Configuration.Telemetry;
1011

1112
namespace Datadog.Trace.Configuration
@@ -18,7 +19,7 @@ public class IntegrationSettings : IEquatable<IntegrationSettings>
1819
/// <summary>
1920
/// Configuration key pattern for enabling or disabling an integration.
2021
/// </summary>
21-
public const string IntegrationEnabled = "DD_TRACE_{0}_ENABLED";
22+
public const string IntegrationEnabledKey = "DD_TRACE_{0}_ENABLED";
2223

2324
/// <summary>
2425
/// Configuration key pattern for enabling or disabling Analytics in an integration.
@@ -50,31 +51,19 @@ internal IntegrationSettings(string integrationName, IConfigurationSource? sourc
5051

5152
// We don't record these in telemetry, because they're blocked anyway
5253
var config = new ConfigurationBuilder(source ?? NullConfigurationSource.Instance, NullConfigurationTelemetry.Instance);
53-
var upperName = integrationName.ToUpperInvariant();
5454
Enabled = isExplicitlyDisabled
5555
? false
5656
: config
57-
.WithKeys(
58-
string.Format(IntegrationEnabled, upperName),
59-
string.Format(IntegrationEnabled, integrationName),
60-
$"DD_{integrationName}_ENABLED")
61-
.AsBool()
62-
?? fallback?.Enabled;
57+
.WithIntegrationKey(integrationName)
58+
.AsBool() ?? fallback?.Enabled;
6359

6460
#pragma warning disable 618 // App analytics is deprecated, but still used
6561
AnalyticsEnabled = config
66-
.WithKeys(
67-
string.Format(AnalyticsEnabledKey, upperName),
68-
string.Format(AnalyticsEnabledKey, integrationName),
69-
$"DD_{integrationName}_ANALYTICS_ENABLED")
70-
.AsBool()
71-
?? fallback?.AnalyticsEnabled;
62+
.WithIntegrationAnalyticsKey(integrationName)
63+
.AsBool() ?? fallback?.AnalyticsEnabled;
7264

7365
AnalyticsSampleRate = config
74-
.WithKeys(
75-
string.Format(AnalyticsSampleRateKey, upperName),
76-
string.Format(AnalyticsSampleRateKey, integrationName),
77-
$"DD_{integrationName}_ANALYTICS_SAMPLE_RATE")
66+
.WithIntegrationAnalyticsSampleRateKey(integrationName)
7867
.AsDouble(fallback?.AnalyticsSampleRate ?? 1.0);
7968
#pragma warning restore 618
8069
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ internal TracerSettings(IConfigurationSource? source, IConfigurationTelemetry te
132132
? ParsingResult<bool>.Success(result: false)
133133
: ParsingResult<bool>.Failure());
134134
IsActivityListenerEnabled = config
135-
.WithKeys(ConfigurationKeys.FeatureFlags.OpenTelemetryEnabled, "DD_TRACE_ACTIVITY_LISTENER_ENABLED")
135+
.WithKeys(ConfigurationKeys.FeatureFlags.OpenTelemetryEnabled)
136136
.AsBoolResult()
137137
.OverrideWith(in otelActivityListenerEnabled, ErrorLog, defaultValue: false);
138138

@@ -219,7 +219,7 @@ not null when string.Equals(x, "http/protobuf", StringComparison.OrdinalIgnoreCa
219219
validator: null);
220220

221221
OtlpMetricsProtocol = config
222-
.WithKeys(ConfigurationKeys.OpenTelemetry.ExporterOtlpMetricsProtocol, ConfigurationKeys.OpenTelemetry.ExporterOtlpProtocol)
222+
.WithKeys(ConfigurationKeys.OpenTelemetry.ExporterOtlpMetricsProtocol)
223223
.GetAs(
224224
defaultValue: new(OtlpProtocol.Grpc, "grpc"),
225225
converter: x => x switch
@@ -254,14 +254,14 @@ not null when string.Equals(x, "http/json", StringComparison.OrdinalIgnoreCase)
254254
converter: uriString => new Uri(uriString));
255255

256256
OtlpMetricsHeaders = config
257-
.WithKeys(ConfigurationKeys.OpenTelemetry.ExporterOtlpMetricsHeaders, ConfigurationKeys.OpenTelemetry.ExporterOtlpHeaders)
257+
.WithKeys(ConfigurationKeys.OpenTelemetry.ExporterOtlpMetricsHeaders)
258258
.AsDictionaryResult(separator: '=')
259259
.WithDefault(new DefaultResult<IDictionary<string, string>>(new Dictionary<string, string>(), "[]"))
260260
.Where(kvp => !string.IsNullOrWhiteSpace(kvp.Key))
261261
.ToDictionary(kvp => kvp.Key.Trim(), kvp => kvp.Value?.Trim() ?? string.Empty);
262262

263263
OtlpMetricsTimeoutMs = config
264-
.WithKeys(ConfigurationKeys.OpenTelemetry.ExporterOtlpMetricsTimeoutMs, ConfigurationKeys.OpenTelemetry.ExporterOtlpTimeoutMs)
264+
.WithKeys(ConfigurationKeys.OpenTelemetry.ExporterOtlpMetricsTimeoutMs)
265265
.AsInt32(defaultValue: 10_000);
266266

267267
OtlpMetricsTemporalityPreference = config
@@ -514,14 +514,14 @@ not null when string.Equals(value, "otlp", StringComparison.OrdinalIgnoreCase) =
514514
converter: otelConverter);
515515

516516
PropagationStyleInject = config
517-
.WithKeys(ConfigurationKeys.PropagationStyleInject, "DD_PROPAGATION_STYLE_INJECT", ConfigurationKeys.PropagationStyle)
518-
.GetAsClassResult(
519-
validator: injectionValidator, // invalid individual values are rejected later
520-
converter: style => TrimSplitString(style, commaSeparator))
521-
.OverrideWith(in otelPropagation, ErrorLog, getDefaultPropagationHeaders);
517+
.WithKeys(ConfigurationKeys.PropagationStyleInject)
518+
.GetAsClassResult(
519+
validator: injectionValidator, // invalid individual values are rejected later
520+
converter: style => TrimSplitString(style, commaSeparator))
521+
.OverrideWith(in otelPropagation, ErrorLog, getDefaultPropagationHeaders);
522522

523523
PropagationStyleExtract = config
524-
.WithKeys(ConfigurationKeys.PropagationStyleExtract, "DD_PROPAGATION_STYLE_EXTRACT", ConfigurationKeys.PropagationStyle)
524+
.WithKeys(ConfigurationKeys.PropagationStyleExtract)
525525
.GetAsClassResult(
526526
validator: injectionValidator, // invalid individual values are rejected later
527527
converter: style => TrimSplitString(style, commaSeparator))
@@ -572,7 +572,7 @@ not null when string.Equals(value, "otlp", StringComparison.OrdinalIgnoreCase) =
572572
.Value;
573573

574574
IpHeader = config
575-
.WithKeys(ConfigurationKeys.IpHeader, ConfigurationKeys.AppSec.CustomIpHeader)
575+
.WithKeys(ConfigurationKeys.IpHeader)
576576
.AsString();
577577

578578
IpHeaderEnabled = config

tracer/test/Datadog.Trace.ClrProfiler.Managed.Tests/DbScopeFactoryTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ public async Task CreateDbCommandScope_ReturnNullForAdoNetDisabledIntegration(Ty
102102

103103
var tracerSettings = TracerSettings.Create(new()
104104
{
105-
{ string.Format(IntegrationSettings.IntegrationEnabled, integrationName), "true" },
106-
{ string.Format(IntegrationSettings.IntegrationEnabled, nameof(IntegrationId.AdoNet)), "false" },
105+
{ string.Format(IntegrationSettings.IntegrationEnabledKey, integrationName), "true" },
106+
{ string.Format(IntegrationSettings.IntegrationEnabledKey, nameof(IntegrationId.AdoNet)), "false" },
107107
});
108108
await using var tracer = TracerHelper.CreateWithFakeAgent(tracerSettings);
109109

@@ -750,7 +750,7 @@ private static ScopedTracer CreateTracerWithIntegrationEnabled(string integratio
750750
// Set up tracer
751751
var tracerSettings = TracerSettings.Create(new()
752752
{
753-
{ string.Format(IntegrationSettings.IntegrationEnabled, integrationName), enabled },
753+
{ string.Format(IntegrationSettings.IntegrationEnabledKey, integrationName), enabled },
754754
});
755755
return TracerHelper.Create(tracerSettings);
756756
}

tracer/test/Datadog.Trace.Tests/Configuration/IntegrationSettingsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void SettingsRespectsOverride(bool initiallyEnabled)
9090
var name = nameof(IntegrationId.Kafka);
9191
var source = new NameValueConfigurationSource(new()
9292
{
93-
{ string.Format(IntegrationSettings.IntegrationEnabled, name), initiallyEnabled.ToString() },
93+
{ string.Format(IntegrationSettings.IntegrationEnabledKey, name), initiallyEnabled.ToString() },
9494
});
9595

9696
var settings = new IntegrationSettings(name, source: source, isExplicitlyDisabled: true);
@@ -105,7 +105,7 @@ public void SettingsRespectsOriginalIfNotOverridden(bool initiallyEnabled)
105105
var name = nameof(IntegrationId.Kafka);
106106
var source = new NameValueConfigurationSource(new()
107107
{
108-
{ string.Format(IntegrationSettings.IntegrationEnabled, name), initiallyEnabled.ToString() },
108+
{ string.Format(IntegrationSettings.IntegrationEnabledKey, name), initiallyEnabled.ToString() },
109109
});
110110

111111
var settings = new IntegrationSettings(name, source: source, isExplicitlyDisabled: false);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void GetIntegrationEnabled_SupportedValues_ReturnsExpectedValues(int id)
3131
{
3232
var integrationId = (IntegrationId)id;
3333
var name = IntegrationRegistry.GetName(integrationId).ToUpperInvariant();
34-
var enabledKey = string.Format(IntegrationSettings.IntegrationEnabled, name);
34+
var enabledKey = string.Format(IntegrationSettings.IntegrationEnabledKey, name);
3535

3636
var actual = ManualInstrumentationLegacyConfigurationSource.GetIntegrationEnabled(enabledKey);
3737

@@ -57,7 +57,7 @@ public void GetIntegrationEnabled_ForUnsupportedValues_ReturnsNull(int id)
5757
{
5858
var integrationId = (IntegrationId)id;
5959
var name = IntegrationRegistry.GetName(integrationId).ToUpperInvariant();
60-
var enabledKey = string.Format(IntegrationSettings.IntegrationEnabled, name);
60+
var enabledKey = string.Format(IntegrationSettings.IntegrationEnabledKey, name);
6161

6262
var actual = ManualInstrumentationLegacyConfigurationSource.GetIntegrationEnabled(enabledKey);
6363

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ private static TracerSettings GetAndAssertAutomaticTracerSettings()
341341
{ ConfigurationKeys.TraceEnabled, false },
342342
{ ConfigurationKeys.TracerMetricsEnabled, true },
343343
{ ConfigurationKeys.AgentUri, "http://localhost:1234" },
344-
{ string.Format(IntegrationSettings.IntegrationEnabled, nameof(IntegrationId.Aerospike)), "false" },
344+
{ string.Format(IntegrationSettings.IntegrationEnabledKey, nameof(IntegrationId.Aerospike)), "false" },
345345
{ string.Format(IntegrationSettings.AnalyticsEnabledKey, nameof(IntegrationId.Grpc)), "true" },
346346
{ string.Format(IntegrationSettings.AnalyticsSampleRateKey, nameof(IntegrationId.Couchbase)), 0.5 },
347347
});

0 commit comments

Comments
 (0)