Skip to content

Commit 97acd66

Browse files
committed
Add additional TryHandleResult overload to work directly with types instead of "DefaultResult<T>"
1 parent 92f4f1a commit 97acd66

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,37 @@ internal readonly struct ConfigurationBuilder(IConfigurationSource source, IConf
2525

2626
public HasKeys WithKeys(string key, string fallbackKey1, string fallbackKey2, string fallbackKey3) => new(_source, _telemetry, key, fallbackKey1, fallbackKey2, fallbackKey3);
2727

28+
private static bool TryHandleResult<T>(
29+
IConfigurationTelemetry telemetry,
30+
string key,
31+
ConfigurationResult<T> result,
32+
bool recordValue,
33+
Func<T>? getDefaultValue,
34+
[NotNullIfNotNull(nameof(getDefaultValue))] out T? value)
35+
where T : notnull
36+
{
37+
if (result is { Result: { } ddResult, IsValid: true })
38+
{
39+
value = ddResult;
40+
return true;
41+
}
42+
43+
// don't have a default value, so caller (which knows what the <T> is needs
44+
// to return the default value. Necessary because we can't create a generic
45+
// method that works "correctly" for both value types and reference types.
46+
if (getDefaultValue is null)
47+
{
48+
value = default;
49+
return false;
50+
}
51+
52+
var defaultValue = getDefaultValue();
53+
RecordTelemetry(telemetry, key, recordValue, defaultValue);
54+
55+
value = defaultValue;
56+
return true;
57+
}
58+
2859
private static bool TryHandleResult<T>(
2960
IConfigurationTelemetry telemetry,
3061
string key,
@@ -56,6 +87,31 @@ private static bool TryHandleResult<T>(
5687
return true;
5788
}
5889

90+
private static void RecordTelemetry<T>(IConfigurationTelemetry telemetry, string key, bool recordValue, T defaultValue)
91+
{
92+
switch (defaultValue)
93+
{
94+
case int intVal:
95+
telemetry.Record(key, intVal, ConfigurationOrigins.Default);
96+
break;
97+
case double doubleVal:
98+
telemetry.Record(key, doubleVal, ConfigurationOrigins.Default);
99+
break;
100+
case bool boolVal:
101+
telemetry.Record(key, boolVal, ConfigurationOrigins.Default);
102+
break;
103+
case string stringVal:
104+
telemetry.Record(key, stringVal, recordValue, ConfigurationOrigins.Default);
105+
break;
106+
case null: // can't actually be called in practice
107+
break;
108+
default:
109+
// TODO: this shouldn't be calleable in practice, we need to revise it
110+
telemetry.Record(key, defaultValue.ToString(), recordValue, ConfigurationOrigins.Default);
111+
break;
112+
}
113+
}
114+
59115
private static void RecordTelemetry<T>(IConfigurationTelemetry telemetry, string key, bool recordValue, DefaultResult<T> defaultValue)
60116
{
61117
switch (defaultValue.Result)

0 commit comments

Comments
 (0)