Skip to content

Commit bb2c5ca

Browse files
committed
Handle aliases in ConfigurationBuilder.cs and remove overloads for fallbacks
1 parent 3b18c4e commit bb2c5ca

File tree

6 files changed

+48
-83
lines changed

6 files changed

+48
-83
lines changed

tracer/src/Datadog.Trace/AppSec/SecuritySettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public SecuritySettings(IConfigurationSource? source, IConfigurationTelemetry te
124124
UserEventsAutoInstrumentationMode = UserTrackingIdentMode;
125125
}
126126

127-
ApiSecurityEnabled = config.WithKeys(ConfigurationKeys.AppSec.ApiSecurityEnabled, "DD_EXPERIMENTAL_API_SECURITY_ENABLED")
127+
ApiSecurityEnabled = config.WithKeys(ConfigurationKeys.AppSec.ApiSecurityEnabled)
128128
.AsBool(true);
129129

130130
ApiSecuritySampleDelay = config.WithKeys(ConfigurationKeys.AppSec.ApiSecuritySampleDelay)

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

Lines changed: 37 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -493,23 +493,9 @@ private ConfigurationResult<T> GetAs<T>(Func<T, bool>? validator, Func<string, P
493493
/// <returns>The raw <see cref="ConfigurationResult{T}"/></returns>
494494
private ConfigurationResult<T> GetResult<T>(Func<IConfigurationSource, string, IConfigurationTelemetry, Func<T, bool>?, bool, ConfigurationResult<T>> selector, Func<T, bool>? validator, bool recordValue)
495495
{
496-
var result = selector(Source, Key, Telemetry, validator, recordValue);
497-
if (result.ShouldFallBack && FallbackKey1 is not null)
498-
{
499-
result = selector(Source, FallbackKey1, Telemetry, validator, recordValue);
500-
}
501-
502-
if (result.ShouldFallBack && FallbackKey2 is not null)
503-
{
504-
result = selector(Source, FallbackKey2, Telemetry, validator, recordValue);
505-
}
506-
507-
if (result.ShouldFallBack && FallbackKey3 is not null)
508-
{
509-
result = selector(Source, FallbackKey3, Telemetry, validator, recordValue);
510-
}
511-
512-
return result;
496+
var source = Source;
497+
var telemetry = Telemetry;
498+
return GetResultWithFallback(key => selector(source, key, telemetry, validator, recordValue));
513499
}
514500

515501
/// <summary>
@@ -523,62 +509,50 @@ private ConfigurationResult<T> GetResult<T>(Func<IConfigurationSource, string, I
523509
/// <returns>The raw <see cref="ConfigurationResult{T}"/></returns>
524510
private ConfigurationResult<T> GetResult<T>(Func<IConfigurationSource, string, IConfigurationTelemetry, Func<T, bool>?, Func<string, ParsingResult<T>>, bool, ConfigurationResult<T>> selector, Func<T, bool>? validator, Func<string, ParsingResult<T>> converter, bool recordValue)
525511
{
526-
var result = selector(Source, Key, Telemetry, validator, converter, recordValue);
527-
if (result.ShouldFallBack && FallbackKey1 is not null)
528-
{
529-
result = selector(Source, FallbackKey1, Telemetry, validator, converter, recordValue);
530-
}
531-
532-
if (result.ShouldFallBack && FallbackKey2 is not null)
533-
{
534-
result = selector(Source, FallbackKey2, Telemetry, validator, converter, recordValue);
535-
}
536-
537-
if (result.ShouldFallBack && FallbackKey3 is not null)
538-
{
539-
result = selector(Source, FallbackKey3, Telemetry, validator, converter, recordValue);
540-
}
541-
542-
return result;
512+
var source = Source;
513+
var telemetry = Telemetry;
514+
return GetResultWithFallback(key => selector(source, key, telemetry, validator, converter, recordValue));
543515
}
544516

545517
private ConfigurationResult<IDictionary<string, string>> GetDictionaryResult(bool allowOptionalMappings, char separator)
546518
{
547-
var result = Source.GetDictionary(Key, Telemetry, validator: null, allowOptionalMappings, separator);
548-
if (result.ShouldFallBack && FallbackKey1 is not null)
549-
{
550-
result = Source.GetDictionary(FallbackKey1, Telemetry, validator: null, allowOptionalMappings, separator);
551-
}
552-
553-
if (result.ShouldFallBack && FallbackKey2 is not null)
554-
{
555-
result = Source.GetDictionary(FallbackKey2, Telemetry, validator: null, allowOptionalMappings, separator);
556-
}
557-
558-
if (result.ShouldFallBack && FallbackKey3 is not null)
559-
{
560-
result = Source.GetDictionary(FallbackKey3, Telemetry, validator: null, allowOptionalMappings, separator);
561-
}
562-
563-
return result;
519+
var source = Source;
520+
var telemetry = Telemetry;
521+
return GetResultWithFallback(key => source.GetDictionary(key, telemetry, validator: null, allowOptionalMappings, separator));
564522
}
565523

566524
private ConfigurationResult<IDictionary<string, string>> GetDictionaryResult(Func<string, IDictionary<string, string>> parser)
567525
{
568-
var result = Source.GetDictionary(Key, Telemetry, validator: null, parser);
569-
if (result.ShouldFallBack && FallbackKey1 is not null)
570-
{
571-
result = Source.GetDictionary(FallbackKey1, Telemetry, validator: null, parser);
572-
}
526+
var source = Source;
527+
var telemetry = Telemetry;
528+
return GetResultWithFallback(key => source.GetDictionary(key, telemetry, validator: null, parser));
529+
}
573530

574-
if (result.ShouldFallBack && FallbackKey2 is not null)
531+
/// <summary>
532+
/// Common method that handles key resolution and alias fallback logic
533+
/// </summary>
534+
/// <param name="selector">The method to call for each key</param>
535+
/// <typeparam name="T">The type being retrieved</typeparam>
536+
/// <returns>The raw <see cref="ConfigurationResult{T}"/></returns>
537+
private ConfigurationResult<T> GetResultWithFallback<T>(Func<string, ConfigurationResult<T>> selector)
538+
{
539+
var hasAllKeys = _allKeys is not null;
540+
var canonicalKey = hasAllKeys ? _allKeys![0] : Key;
541+
var result = selector(canonicalKey);
542+
if (!result.ShouldFallBack)
575543
{
576-
result = Source.GetDictionary(FallbackKey2, Telemetry, validator: null, parser);
544+
return result;
577545
}
578546

579-
if (result.ShouldFallBack && FallbackKey3 is not null)
547+
string[] aliases = !hasAllKeys ? ConfigKeyAliasesSwitcher.GetAliases(Key) : [_allKeys![1], _allKeys[2]];
548+
549+
foreach (var alias in aliases)
580550
{
581-
result = Source.GetDictionary(FallbackKey3, Telemetry, validator: null, parser);
551+
result = selector(alias);
552+
if (!result.ShouldFallBack)
553+
{
554+
break;
555+
}
582556
}
583557

584558
return result;
@@ -606,10 +580,9 @@ public static StructConfigurationResultWithKey<bool> Create(IConfigurationTeleme
606580
public static StructConfigurationResultWithKey<int> Create(IConfigurationTelemetry telemetry, string key, ConfigurationResult<int> configurationResult)
607581
=> new(telemetry, key, configurationResult);
608582

609-
public static StructConfigurationResultWithKey<double> Create(IConfigurationTelemetry telemetry, string key, ConfigurationResult<double> configurationResult)
610-
=> new(telemetry, key, configurationResult);
583+
public static StructConfigurationResultWithKey<double> Create(IConfigurationTelemetry telemetry, string key, ConfigurationResult<double> configurationResult) => new(telemetry, key, configurationResult);
611584

612-
[return:NotNullIfNotNull(nameof(defaultValue))]
585+
[return: NotNullIfNotNull(nameof(defaultValue))]
613586
public T? WithDefault(T? defaultValue)
614587
{
615588
if (ConfigurationResult is { Result: { } ddResult, IsValid: true })
@@ -623,7 +596,7 @@ public static StructConfigurationResultWithKey<double> Create(IConfigurationTele
623596

624597
public T WithDefault(T defaultValue)
625598
{
626-
if (ConfigurationResult is { Result: { } ddResult, IsValid: true })
599+
if (ConfigurationResult is { Result: var ddResult, IsValid: true })
627600
{
628601
return ddResult;
629602
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,11 @@ public Raw(IConfigurationSource source, IConfigurationTelemetry telemetry)
445445
TracesUnixDomainSocketPath = config.WithKeys(ConfigurationKeys.TracesUnixDomainSocketPath).AsString();
446446

447447
TraceAgentHost = config
448-
.WithKeys(ConfigurationKeys.AgentHost, "DD_TRACE_AGENT_HOSTNAME", "DATADOG_TRACE_AGENT_HOSTNAME")
448+
.WithKeys(ConfigurationKeys.AgentHost)
449449
.AsString();
450450

451451
TraceAgentPort = config
452-
.WithKeys(ConfigurationKeys.AgentPort, "DATADOG_TRACE_AGENT_PORT")
452+
.WithKeys(ConfigurationKeys.AgentPort)
453453
.AsInt32();
454454

455455
MetricsUrl = config.WithKeys(ConfigurationKeys.MetricsUri).AsString();

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ public static MutableSettings Create(
607607
};
608608

609609
globalTags = config
610-
.WithKeys(ConfigurationKeys.GlobalTags, "DD_TRACE_GLOBAL_TAGS")
610+
.WithKeys(ConfigurationKeys.GlobalTags)
611611
.AsDictionaryResult(parser: updatedTagsParser)
612612
.OverrideWith(
613613
RemapOtelTags(in otelTags),
@@ -621,7 +621,7 @@ public static MutableSettings Create(
621621
else
622622
{
623623
globalTags = config
624-
.WithKeys(ConfigurationKeys.GlobalTags, "DD_TRACE_GLOBAL_TAGS")
624+
.WithKeys(ConfigurationKeys.GlobalTags)
625625
.AsDictionaryResult()
626626
.OverrideWith(
627627
RemapOtelTags(in otelTags),
@@ -642,7 +642,7 @@ public static MutableSettings Create(
642642

643643
var otelServiceName = config.WithKeys(ConfigurationKeys.OpenTelemetry.ServiceName).AsStringResult();
644644
var serviceName = config
645-
.WithKeys(ConfigurationKeys.ServiceName, "DD_SERVICE_NAME")
645+
.WithKeys(ConfigurationKeys.ServiceName)
646646
.AsStringResult()
647647
.OverrideWith(in otelServiceName, errorLog);
648648

@@ -728,7 +728,7 @@ public static MutableSettings Create(
728728

729729
#pragma warning disable 618 // this parameter has been replaced but may still be used
730730
var maxTracesSubmittedPerSecond = config
731-
.WithKeys(ConfigurationKeys.TraceRateLimit, ConfigurationKeys.MaxTracesSubmittedPerSecond)
731+
.WithKeys(ConfigurationKeys.TraceRateLimit)
732732
#pragma warning restore 618
733733
.AsInt32(defaultValue: 100);
734734

@@ -775,17 +775,13 @@ public static MutableSettings Create(
775775
.AsBool(defaultValue: false);
776776

777777
var httpServerErrorStatusCodesString = config
778-
#pragma warning disable 618 // This config key has been replaced but may still be used
779-
.WithKeys(ConfigurationKeys.HttpServerErrorStatusCodes, ConfigurationKeys.DeprecatedHttpServerErrorStatusCodes)
780-
#pragma warning restore 618
778+
.WithKeys(ConfigurationKeys.HttpServerErrorStatusCodes)
781779
.AsString(defaultValue: "500-599");
782780

783781
var httpServerErrorStatusCodes = ParseHttpCodesToArray(httpServerErrorStatusCodesString);
784782

785783
var httpClientErrorStatusCodesString = config
786-
#pragma warning disable 618 // This config key has been replaced but may still be used
787-
.WithKeys(ConfigurationKeys.HttpClientErrorStatusCodes, ConfigurationKeys.DeprecatedHttpClientErrorStatusCodes)
788-
#pragma warning restore 618
784+
.WithKeys(ConfigurationKeys.HttpClientErrorStatusCodes)
789785
.AsString(defaultValue: "400-499");
790786

791787
var httpClientErrorStatusCodes = ParseHttpCodesToArray(httpClientErrorStatusCodesString);

tracer/src/Datadog.Trace/Debugger/ExceptionAutoInstrumentation/ExceptionReplaySettings.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ public ExceptionReplaySettings(IConfigurationSource? source, IConfigurationTelem
2222
source ??= NullConfigurationSource.Instance;
2323
var config = new ConfigurationBuilder(source, telemetry);
2424

25-
#pragma warning disable CS0612 // Type or member is obsolete
26-
var erEnabledResult = config.WithKeys(ConfigurationKeys.Debugger.ExceptionReplayEnabled, fallbackKey: ConfigurationKeys.Debugger.ExceptionDebuggingEnabled).AsBoolResult();
27-
#pragma warning restore CS0612 // Type or member is obsolete
25+
var erEnabledResult = config.WithKeys(ConfigurationKeys.Debugger.ExceptionReplayEnabled).AsBoolResult();
2826
Enabled = erEnabledResult.WithDefault(false);
2927
CanBeEnabled = erEnabledResult.ConfigurationResult is not { IsValid: true, Result: false };
3028

tracer/src/Datadog.Trace/RemoteConfigurationManagement/RemoteConfigurationSettings.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ public RemoteConfigurationSettings(IConfigurationSource? configurationSource, IC
2424
TracerVersion = TracerConstants.ThreePartVersion;
2525

2626
var pollInterval = new ConfigurationBuilder(configurationSource, telemetry)
27-
#pragma warning disable CS0618
28-
.WithKeys(ConfigurationKeys.Rcm.PollInterval, ConfigurationKeys.Rcm.PollIntervalInternal)
29-
#pragma warning restore CS0618
27+
.WithKeys(ConfigurationKeys.Rcm.PollInterval)
3028
.AsDouble(DefaultPollIntervalSeconds, pollInterval => pollInterval is > 0 and <= 5);
3129

3230
PollInterval = TimeSpan.FromSeconds(pollInterval.Value);

0 commit comments

Comments
 (0)