Skip to content

Commit 53fc6eb

Browse files
authored
Refactor telemetry in ConfigurationBuilder (#7354)
## Summary of changes Refactor `ConfigurationBuilder` ## Reason for change As part of some upcoming work, we need to record additional telemetry about default values (so that these can be exposed to customers). As an indirect consequence, it's preferable to provide an explicit default value instead of a `Func<T>`, so this refactoring is _mostly_ using `T` instead of `Func<T>` where it makes sense. ## Implementation details Mostly standard refactoring stuff. Added some overloads, inlined some methods, moved some other things around. Nothing drastic. One major difference is that `DefaultResult<T>` now _requires_ that you provide an explicit associated "telemetry value" for recording, whereas it was previously optional. However, we always provided it in practice, so this is a noop. ## Test coverage Should all be covered by existing tests. ## Other details
1 parent 0e1a44e commit 53fc6eb

File tree

8 files changed

+272
-178
lines changed

8 files changed

+272
-178
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public SecuritySettings(IConfigurationSource? source, IConfigurationTelemetry te
6060

6161
WafTimeoutMicroSeconds = (ulong)config
6262
.WithKeys(ConfigurationKeys.AppSec.WafTimeout)
63-
.GetAs<int>(
64-
getDefaultValue: () => 100_000, // Default timeout of 100 ms, only extreme conditions should cause timeout
63+
.AsInt32(
64+
defaultValue: 100_000, // Default timeout of 100 ms, only extreme conditions should cause timeout
6565
converter: ParseWafTimeout,
6666
validator: wafTimeout => wafTimeout > 0);
6767

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

Lines changed: 254 additions & 146 deletions
Large diffs are not rendered by default.

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

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,18 @@
44
// </copyright>
55

66
#nullable enable
7-
using System.Collections.Generic;
87

98
namespace Datadog.Trace.Configuration.ConfigurationSources.Telemetry;
109

11-
internal readonly record struct DefaultResult<T>
10+
internal readonly struct DefaultResult<T>(T result, string telemetryValue)
1211
{
13-
private readonly string? _telemetryValue;
14-
15-
public DefaultResult(T result, string? telemetryValue)
16-
{
17-
Result = result;
18-
_telemetryValue = telemetryValue;
19-
}
20-
2112
/// <summary>
2213
/// Gets the value to use as the default result
2314
/// </summary>
24-
public T Result { get; }
15+
public T Result { get; } = result;
2516

2617
/// <summary>
2718
/// Gets a string representation of the result to use in telemetry.
2819
/// </summary>
29-
public string? TelemetryValue => _telemetryValue ?? Result?.ToString();
30-
31-
public static implicit operator DefaultResult<T>(T result)
32-
=> result is IDictionary<string, string>
33-
? new(result, telemetryValue: string.Empty) // we don't want to call ToString() on these
34-
: new(result, null);
20+
public string TelemetryValue { get; } = telemetryValue;
3521
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ internal TracerSettings(IConfigurationSource? source, IConfigurationTelemetry te
381381
MetadataSchemaVersion = config
382382
.WithKeys(ConfigurationKeys.MetadataSchemaVersion)
383383
.GetAs(
384-
() => new DefaultResult<SchemaVersion>(SchemaVersion.V0, "V0"),
384+
defaultValue: new DefaultResult<SchemaVersion>(SchemaVersion.V0, "V0"),
385385
converter: x => x switch
386386
{
387387
"v1" or "V1" => SchemaVersion.V1,
@@ -451,7 +451,7 @@ internal TracerSettings(IConfigurationSource? source, IConfigurationTelemetry te
451451

452452
CustomSamplingRulesFormat = config.WithKeys(ConfigurationKeys.CustomSamplingRulesFormat)
453453
.GetAs(
454-
getDefaultValue: () => new DefaultResult<string>(SamplingRulesFormat.Glob, "glob"),
454+
defaultValue: new DefaultResult<string>(SamplingRulesFormat.Glob, "glob"),
455455
converter: value =>
456456
{
457457
// We intentionally report invalid values as "valid" in the converter,
@@ -603,7 +603,7 @@ internal TracerSettings(IConfigurationSource? source, IConfigurationTelemetry te
603603
PropagationBehaviorExtract = config
604604
.WithKeys(ConfigurationKeys.PropagationBehaviorExtract)
605605
.GetAs(
606-
() => new DefaultResult<ExtractBehavior>(ExtractBehavior.Continue, "continue"),
606+
defaultValue: new(ExtractBehavior.Continue, "continue"),
607607
converter: x => x.ToLowerInvariant() switch
608608
{
609609
"continue" => ExtractBehavior.Continue,
@@ -706,7 +706,7 @@ internal TracerSettings(IConfigurationSource? source, IConfigurationTelemetry te
706706
DbmPropagationMode = config
707707
.WithKeys(ConfigurationKeys.DbmPropagationMode)
708708
.GetAs(
709-
() => new DefaultResult<DbmPropagationLevel>(DbmPropagationLevel.Disabled, nameof(DbmPropagationLevel.Disabled)),
709+
defaultValue: new(DbmPropagationLevel.Disabled, nameof(DbmPropagationLevel.Disabled)),
710710
converter: x => ToDbmPropagationInput(x) ?? ParsingResult<DbmPropagationLevel>.Failure(),
711711
validator: null);
712712

@@ -1382,7 +1382,7 @@ internal static TracerSettings FromDefaultSourcesInternal()
13821382
{
13831383
var configurationDictionary = config
13841384
.WithKeys(key)
1385-
.AsDictionary(allowOptionalMappings: true, () => new Dictionary<string, string>());
1385+
.AsDictionary(allowOptionalMappings: true, () => new Dictionary<string, string>(), string.Empty);
13861386

13871387
if (configurationDictionary == null)
13881388
{

tracer/src/Datadog.Trace/Iast/Settings/IastSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public IastSettings(IConfigurationSource source, IConfigurationTelemetry telemet
8080
TelemetryVerbosity = config
8181
.WithKeys(ConfigurationKeys.Iast.TelemetryVerbosity)
8282
.GetAs(
83-
getDefaultValue: () => IastMetricsVerbosityLevel.Information,
83+
defaultValue: new(IastMetricsVerbosityLevel.Information, "information"),
8484
converter: value => value.ToLowerInvariant() switch
8585
{
8686
"off" => IastMetricsVerbosityLevel.Off,

tracer/src/Datadog.Trace/Logging/DirectSubmission/DirectLogSubmissionSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public DirectLogSubmissionSettings(IConfigurationSource? source, IConfigurationT
6969
MinimumLevel = config
7070
.WithKeys(ConfigurationKeys.DirectLogSubmission.MinimumLevel)
7171
.GetAs(
72-
() => new DefaultResult<DirectSubmissionLogLevel>(DefaultMinimumLevel, nameof(DirectSubmissionLogLevel.Information)),
72+
defaultValue: new(DefaultMinimumLevel, nameof(DirectSubmissionLogLevel.Information)),
7373
converter: x => DirectSubmissionLogLevelExtensions.Parse(x) ?? ParsingResult<DirectSubmissionLogLevel>.Failure(),
7474
validator: null);
7575

tracer/src/Datadog.Trace/Logging/Internal/DatadogLoggingFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ private static string GetDefaultLogDirectory(IConfigurationSource source, IConfi
252252
var maxLogFileSize = new ConfigurationBuilder(source, telemetry)
253253
.WithKeys(ConfigurationKeys.MaxLogFileSize)
254254
.GetAs(
255-
() => DefaultMaxLogFileSize,
255+
defaultValue: new(DefaultMaxLogFileSize, DefaultMaxLogFileSize.ToString(CultureInfo.InvariantCulture)),
256256
converter: x => long.TryParse(x, out var maxLogSize)
257257
? maxLogSize
258258
: ParsingResult<long>.Failure(),

tracer/test/Datadog.Trace.Tests/Configuration/Telemetry/ConfigurationBuilderTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ public void GetAs_ReturnsTheExpectedValue(string value, string expected)
564564
var actual = new ConfigurationBuilder(source, _telemetry)
565565
.WithKeys("key")
566566
.GetAs<Guid>(
567-
getDefaultValue: () => _default,
567+
defaultValue: new(_default, Default),
568568
validator: null,
569569
converter: _converter);
570570

@@ -584,7 +584,7 @@ public void GetAs_ReturnsTheExpectedValueWithNullableGuid(string value, string e
584584
var actual = new ConfigurationBuilder(source, _telemetry)
585585
.WithKeys("key")
586586
.GetAs<Guid?>(
587-
getDefaultValue: () => _default,
587+
defaultValue: new(_default, Default),
588588
validator: null,
589589
converter: _nullableConverter);
590590

@@ -605,7 +605,7 @@ public void GetAs_RecordsTheDefaultValueInTelemetry(string value)
605605
var actual = new ConfigurationBuilder(source, telemetry)
606606
.WithKeys(key)
607607
.GetAs<Guid?>(
608-
getDefaultValue: () => _default,
608+
defaultValue: new(_default, Default),
609609
validator: null,
610610
converter: _nullableConverter);
611611

@@ -615,7 +615,7 @@ public void GetAs_RecordsTheDefaultValueInTelemetry(string value)
615615
.OrderByDescending(x => x.SeqId)
616616
.FirstOrDefault()
617617
.Value;
618-
finalValue.Should().Be(_default.ToString());
618+
finalValue.Should().Be(Default);
619619
}
620620

621621
[Theory]
@@ -677,7 +677,7 @@ public void GetAs_ReturnsDefaultWhenValidationFails(string value, string expecte
677677
var actual = new ConfigurationBuilder(source, _telemetry)
678678
.WithKeys("key")
679679
.GetAs<Guid>(
680-
getDefaultValue: () => _default,
680+
defaultValue: new(_default, Default),
681681
validator: x => x.ToString()[0] != '5',
682682
converter: _converter);
683683

0 commit comments

Comments
 (0)