Skip to content

Commit 40e9e70

Browse files
committed
Refactor Int32 and double accessors to avoid a closure
1 parent fdbd540 commit 40e9e70

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,23 @@ public bool AsBool(bool defaultValue, Func<bool, bool>? validator)
307307
public int? AsInt32(int? defaultValue, Func<int, bool>? validator, Func<string, ParsingResult<int>>? converter)
308308
{
309309
var result = GetInt32Result(validator, converter);
310-
Func<DefaultResult<int>>? getDefaultValue = defaultValue.HasValue ? () => defaultValue.Value : null;
311-
return TryHandleResult(Telemetry, Key, result, recordValue: true, getDefaultValue, out var value) ? value : null;
310+
if (result is { Result: { } ddResult, IsValid: true })
311+
{
312+
return ddResult;
313+
}
314+
315+
if (defaultValue is not { } value)
316+
{
317+
return null;
318+
}
319+
320+
Telemetry.Record(Key, value, ConfigurationOrigins.Default);
321+
return value;
312322
}
313323

324+
// ****************
325+
// Double accessors
326+
// ****************
314327
public double? AsDouble() => AsDouble(defaultValue: null, validator: null);
315328

316329
public double AsDouble(double defaultValue) => AsDouble(defaultValue, validator: null).Value;
@@ -325,8 +338,18 @@ public bool AsBool(bool defaultValue, Func<bool, bool>? validator)
325338
public double? AsDouble(double? defaultValue, Func<double, bool>? validator, Func<string, ParsingResult<double>>? converter)
326339
{
327340
var result = GetDoubleResult(validator, converter);
328-
Func<DefaultResult<double>>? getDefaultValue = defaultValue.HasValue ? () => defaultValue.Value : null;
329-
return TryHandleResult(Telemetry, Key, result, recordValue: true, getDefaultValue, out var value) ? value : null;
341+
if (result is { Result: { } ddResult, IsValid: true })
342+
{
343+
return ddResult;
344+
}
345+
346+
if (defaultValue is not { } value)
347+
{
348+
return null;
349+
}
350+
351+
Telemetry.Record(Key, value, ConfigurationOrigins.Default);
352+
return value;
330353
}
331354

332355
// ****************

0 commit comments

Comments
 (0)