Skip to content

Commit 667cda0

Browse files
committed
Answer to comments
rerere
1 parent 2b3ef5b commit 667cda0

File tree

10 files changed

+170
-616
lines changed

10 files changed

+170
-616
lines changed

tracer/src/Datadog.Trace.SourceGenerators/Configuration/ConfigKeyAliasesSwitcherGenerator.cs

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -248,52 +248,43 @@ private static CompilationUnitSyntax GenerateConfigurationKeyMatcher(Configurati
248248

249249
private static MethodDeclarationSyntax GenerateGetAliasesMethod(ConfigurationAliases configurationAliases)
250250
{
251-
var switchSections = new List<SwitchSectionSyntax>();
251+
var switchArms = new List<SwitchExpressionArmSyntax>();
252252

253253
// Add cases for keys that have aliases
254254
foreach (var alias in configurationAliases.Aliases.OrderBy(a => a.Key))
255255
{
256256
var mainKey = alias.Key;
257257
var aliasKeys = alias.Value;
258258

259-
var arrayElements = aliasKeys
260-
.OrderBy(a => a)
261-
.Select(aliasKey => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(aliasKey)))
262-
.Cast<ExpressionSyntax>()
263-
.ToArray();
264-
265-
var arrayCreation = ArrayCreationExpression(
266-
ArrayType(PredefinedType(Token(SyntaxKind.StringKeyword)))
267-
.WithRankSpecifiers(SingletonList(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression())))))
268-
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression, SeparatedList(arrayElements)));
269-
270-
var switchSection = SwitchSection()
271-
.WithLabels(
272-
SingletonList<SwitchLabelSyntax>(
273-
CaseSwitchLabel(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(mainKey)))))
274-
.WithStatements(SingletonList<StatementSyntax>(ReturnStatement(arrayCreation)));
275-
switchSections.Add(switchSection);
259+
// Create collection expression elements
260+
var collectionElements = aliasKeys
261+
.OrderBy(a => a)
262+
.Select(aliasKey => ExpressionElement(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(aliasKey))))
263+
.Cast<CollectionElementSyntax>()
264+
.ToArray();
265+
266+
// Create collection expression [ "alias1", "alias2" ]
267+
var collectionExpression = CollectionExpression(SeparatedList(collectionElements));
268+
269+
// Create switch arm: "DD_AGENT_HOST" => [ "alias1", "alias2" ],
270+
var switchArm = SwitchExpressionArm(
271+
ConstantPattern(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(mainKey))),
272+
collectionExpression);
273+
274+
switchArms.Add(switchArm);
276275
}
277276

278-
// Add default case
279-
var defaultSection = SwitchSection()
280-
.WithLabels(SingletonList<SwitchLabelSyntax>(DefaultSwitchLabel()))
281-
.WithStatements(
282-
SingletonList<StatementSyntax>(
283-
ReturnStatement(
284-
InvocationExpression(
285-
MemberAccessExpression(
286-
SyntaxKind.SimpleMemberAccessExpression,
287-
IdentifierName("Array"),
288-
GenericName("Empty")
289-
.WithTypeArgumentList(
290-
TypeArgumentList(
291-
SingletonSeparatedList<TypeSyntax>(
292-
PredefinedType(Token(SyntaxKind.StringKeyword))))))))));
293-
switchSections.Add(defaultSection);
294-
295-
var switchStatement = SwitchStatement(IdentifierName(MainKeyParamName))
296-
.WithSections(List(switchSections));
277+
// Add default case: _ => []
278+
var defaultArm = SwitchExpressionArm(
279+
DiscardPattern(),
280+
CollectionExpression());
281+
282+
switchArms.Add(defaultArm);
283+
284+
// Create switch expression: mainKey switch { ... }
285+
var switchExpression = SwitchExpression(
286+
IdentifierName(MainKeyParamName),
287+
SeparatedList(switchArms));
297288

298289
return MethodDeclaration(
299290
ArrayType(PredefinedType(Token(SyntaxKind.StringKeyword)))
@@ -311,7 +302,8 @@ private static MethodDeclarationSyntax GenerateGetAliasesMethod(ConfigurationAli
311302
Comment("/// </summary>"),
312303
Comment($"/// <param name=\"{MainKeyParamName}\">The configuration key.</param>"),
313304
Comment("/// <returns>An array of aliases for the key, or empty array if no aliases exist.</returns>"))
314-
.WithBody(Block(switchStatement));
305+
.WithExpressionBody(ArrowExpressionClause(switchExpression))
306+
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
315307
}
316308

317309
private sealed class ConfigurationAliases(Dictionary<string, string[]> aliases) : IEquatable<ConfigurationAliases>

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

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -468,61 +468,46 @@ public ClassConfigurationResultWithKey<IDictionary<string, string>> AsDictionary
468468
=> new(Telemetry, Key, recordValue: true, configurationResult: GetDictionaryResult(parser));
469469

470470
private ConfigurationResult<string> GetStringResult(Func<string, bool>? validator, Func<string, ParsingResult<string>>? converter, bool recordValue)
471-
=> converter is null
472-
? GetResult(Selectors.AsString, validator, recordValue)
473-
: GetResult(Selectors.AsStringWithConverter, validator, converter, recordValue);
471+
{
472+
var source = Source;
473+
var telemetry = Telemetry;
474+
return converter is null
475+
? GetResultWithFallback(key => source.GetString(key, telemetry, validator, recordValue))
476+
: GetResultWithFallback(key => source.GetAs(key, telemetry, converter, validator, recordValue));
477+
}
474478

475479
private ConfigurationResult<bool> GetBoolResult(Func<bool, bool>? validator, Func<string, ParsingResult<bool>>? converter)
476-
=> converter is null
477-
? GetResult(Selectors.AsBool, validator, recordValue: true)
478-
: GetResult(Selectors.AsBoolWithConverter, validator, converter, recordValue: true);
480+
{
481+
var source = Source;
482+
var telemetry = Telemetry;
483+
return converter is null
484+
? GetResultWithFallback(key => source.GetBool(key, telemetry, validator))
485+
: GetResultWithFallback(key => source.GetAs(key, telemetry, converter, validator, recordValue: true));
486+
}
479487

480488
private ConfigurationResult<int> GetInt32Result(Func<int, bool>? validator, Func<string, ParsingResult<int>>? converter)
481-
=> converter is null
482-
? GetResult(Selectors.AsInt32, validator, recordValue: true)
483-
: GetResult(Selectors.AsInt32WithConverter, validator, converter, recordValue: true);
489+
{
490+
var source = Source;
491+
var telemetry = Telemetry;
492+
return converter is null
493+
? GetResultWithFallback(key => source.GetInt32(key, telemetry, validator))
494+
: GetResultWithFallback(key => source.GetAs(key, telemetry, converter, validator, recordValue: true));
495+
}
484496

485497
private ConfigurationResult<double> GetDoubleResult(Func<double, bool>? validator, Func<string, ParsingResult<double>>? converter)
486-
=> converter is null
487-
? GetResult(Selectors.AsDouble, validator, recordValue: true)
488-
: GetResult(Selectors.AsDoubleWithConverter, validator, converter, recordValue: true);
489-
490-
private ConfigurationResult<T> GetAs<T>(Func<T, bool>? validator, Func<string, ParsingResult<T>> converter)
491-
=> GetResult(
492-
(source, key, telemetry, val, convert, recordValue) => source.GetAs(key, telemetry, convert!, val, recordValue),
493-
validator,
494-
converter,
495-
recordValue: true);
496-
497-
/// <summary>
498-
/// Gets the raw <see cref="ConfigurationResult{T}"/> from the configuration source, recording the access in telemetry
499-
/// </summary>
500-
/// <param name="selector">The method to invoke to retrieve the parameter</param>
501-
/// <param name="validator">The validator to call to decide if a provided value is valid</param>
502-
/// <param name="recordValue">If applicable, whether to record the value in configuration</param>
503-
/// <typeparam name="T">The type being retrieved</typeparam>
504-
/// <returns>The raw <see cref="ConfigurationResult{T}"/></returns>
505-
private ConfigurationResult<T> GetResult<T>(Func<IConfigurationSource, string, IConfigurationTelemetry, Func<T, bool>?, bool, ConfigurationResult<T>> selector, Func<T, bool>? validator, bool recordValue)
506498
{
507499
var source = Source;
508500
var telemetry = Telemetry;
509-
return GetResultWithFallback(key => selector(source, key, telemetry, validator, recordValue));
501+
return converter is null
502+
? GetResultWithFallback(key => source.GetDouble(key, telemetry, validator))
503+
: GetResultWithFallback(key => source.GetAs(key, telemetry, converter, validator, recordValue: true));
510504
}
511505

512-
/// <summary>
513-
/// Gets the raw <see cref="ConfigurationResult{T}"/> from the configuration source, recording the access in telemetry
514-
/// </summary>
515-
/// <param name="selector">The method to invoke to retrieve the parameter</param>
516-
/// <param name="validator">The validator to call to decide if a provided value is valid</param>
517-
/// <param name="converter">The converter to run when calling <see cref="IConfigurationSource.GetAs{T}"/></param>
518-
/// <param name="recordValue">If applicable, whether to record the value in configuration</param>
519-
/// <typeparam name="T">The type being retrieved</typeparam>
520-
/// <returns>The raw <see cref="ConfigurationResult{T}"/></returns>
521-
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)
506+
private ConfigurationResult<T> GetAs<T>(Func<T, bool>? validator, Func<string, ParsingResult<T>> converter)
522507
{
523508
var source = Source;
524509
var telemetry = Telemetry;
525-
return GetResultWithFallback(key => selector(source, key, telemetry, validator, converter, recordValue));
510+
return GetResultWithFallback(key => source.GetAs(key, telemetry, converter, validator, recordValue: true));
526511
}
527512

528513
private ConfigurationResult<IDictionary<string, string>> GetDictionaryResult(bool allowOptionalMappings, char separator)
@@ -553,7 +538,7 @@ private ConfigurationResult<T> GetResultWithFallback<T>(Func<string, Configurati
553538
return result;
554539
}
555540

556-
string[] aliases = _providedAliases ?? ConfigKeyAliasesSwitcher.GetAliases(Key);
541+
var aliases = _providedAliases ?? ConfigKeyAliasesSwitcher.GetAliases(Key);
557542

558543
foreach (var alias in aliases)
559544
{
@@ -725,33 +710,4 @@ public T OverrideWith(in ClassConfigurationResultWithKey<T> otelConfig, IConfigu
725710
return defaultValue.Value.Result;
726711
}
727712
}
728-
729-
private static class Selectors
730-
{
731-
// static accessor functions
732-
internal static readonly Func<IConfigurationSource, string, IConfigurationTelemetry, Func<string, bool>?, bool, ConfigurationResult<string>> AsString
733-
= (source, key, telemetry, validator, recordValue) => source.GetString(key, telemetry, validator, recordValue);
734-
735-
internal static readonly Func<IConfigurationSource, string, IConfigurationTelemetry, Func<bool, bool>?, bool, ConfigurationResult<bool>> AsBool
736-
= (source, key, telemetry, validator, _) => source.GetBool(key, telemetry, validator);
737-
738-
internal static readonly Func<IConfigurationSource, string, IConfigurationTelemetry, Func<int, bool>?, bool, ConfigurationResult<int>> AsInt32
739-
= (source, key, telemetry, validator, _) => source.GetInt32(key, telemetry, validator);
740-
741-
internal static readonly Func<IConfigurationSource, string, IConfigurationTelemetry, Func<double, bool>?, bool, ConfigurationResult<double>> AsDouble
742-
= (source, key, telemetry, validator, _) => source.GetDouble(key, telemetry, validator);
743-
744-
// static accessor functions with converters
745-
internal static readonly Func<IConfigurationSource, string, IConfigurationTelemetry, Func<string, bool>?, Func<string, ParsingResult<string>>, bool, ConfigurationResult<string>> AsStringWithConverter
746-
= (source, key, telemetry, validator, converter, recordValue) => source.GetAs(key, telemetry, converter, validator, recordValue);
747-
748-
internal static readonly Func<IConfigurationSource, string, IConfigurationTelemetry, Func<bool, bool>?, Func<string, ParsingResult<bool>>, bool, ConfigurationResult<bool>> AsBoolWithConverter
749-
= (source, key, telemetry, validator, converter, _) => source.GetAs(key, telemetry, converter, validator, recordValue: true);
750-
751-
internal static readonly Func<IConfigurationSource, string, IConfigurationTelemetry, Func<int, bool>?, Func<string, ParsingResult<int>>, bool, ConfigurationResult<int>> AsInt32WithConverter
752-
= (source, key, telemetry, validator, converter, _) => source.GetAs(key, telemetry, converter, validator, recordValue: true);
753-
754-
internal static readonly Func<IConfigurationSource, string, IConfigurationTelemetry, Func<double, bool>?, Func<string, ParsingResult<double>>, bool, ConfigurationResult<double>> AsDoubleWithConverter
755-
= (source, key, telemetry, validator, converter, _) => source.GetAs(key, telemetry, converter, validator, recordValue: true);
756-
}
757713
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ private MutableSettings(
265265

266266
internal IConfigurationTelemetry Telemetry { get; }
267267

268-
internal static ReadOnlyDictionary<string, string>? InitializeHeaderTags(ConfigurationBuilder.HasKeys key, bool headerTagsNormalizationFixEnabled)
268+
internal static ReadOnlyDictionary<string, string>? InitializeHeaderTags(in ConfigurationBuilder.HasKeys key, bool headerTagsNormalizationFixEnabled)
269269
=> InitializeHeaderTags(
270270
key.AsDictionaryResult(allowOptionalMappings: true),
271271
headerTagsNormalizationFixEnabled);
@@ -1001,10 +1001,12 @@ public static MutableSettings CreateInitialMutableSettings(
10011001
.AsBool(defaultValue: true);
10021002

10031003
// Filter out tags with empty keys or empty values, and trim whitespaces
1004-
var headerTags = InitializeHeaderTags(config.WithKeys(ConfigurationKeys.HeaderTags), headerTagsNormalizationFixEnabled) ?? ReadOnlyDictionary.Empty;
1004+
var headerTagsConfig = config.WithKeys(ConfigurationKeys.HeaderTags);
1005+
var headerTags = InitializeHeaderTags(in headerTagsConfig, headerTagsNormalizationFixEnabled) ?? ReadOnlyDictionary.Empty;
10051006

10061007
// Filter out tags with empty keys or empty values, and trim whitespaces
1007-
var grpcTags = InitializeHeaderTags(config.WithKeys(ConfigurationKeys.GrpcTags), headerTagsNormalizationFixEnabled: true) ?? ReadOnlyDictionary.Empty;
1008+
var grpcTagsConfig = config.WithKeys(ConfigurationKeys.GrpcTags);
1009+
var grpcTags = InitializeHeaderTags(in grpcTagsConfig, headerTagsNormalizationFixEnabled: true) ?? ReadOnlyDictionary.Empty;
10081010

10091011
var customSamplingRules = config.WithKeys(ConfigurationKeys.CustomSamplingRules).AsString();
10101012

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,16 +1377,6 @@ internal static string[] TrimSplitString(string? textValues, char[] separators)
13771377
return list.ToArray();
13781378
}
13791379

1380-
internal bool IsErrorStatusCode(int statusCode, bool serverStatusCode)
1381-
=> MutableSettings.IsErrorStatusCode(statusCode, serverStatusCode);
1382-
1383-
internal bool IsIntegrationEnabled(IntegrationId integration, bool defaultValue = true)
1384-
=> MutableSettings.IsIntegrationEnabled(integration, defaultValue);
1385-
1386-
[Obsolete(DeprecationMessages.AppAnalytics)]
1387-
internal double? GetIntegrationAnalyticsSampleRate(IntegrationId integration, bool enabledWithGlobalSetting)
1388-
=> MutableSettings.GetIntegrationAnalyticsSampleRate(integration, enabledWithGlobalSetting);
1389-
13901380
internal string GetDefaultHttpClientExclusions()
13911381
{
13921382
if (IsRunningInAzureAppService)

0 commit comments

Comments
 (0)