From 966e4c55c470361fb1af62424e3e050aa2117f81 Mon Sep 17 00:00:00 2001 From: Lucas Pimentel Date: Tue, 18 Jun 2024 15:09:49 -0400 Subject: [PATCH] reduce code duplication --- .../JsonConfigurationSource.cs | 25 +++++++++++-------- .../DynamicConfigurationTests.cs | 16 +----------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/tracer/src/Datadog.Trace/Configuration/ConfigurationSources/JsonConfigurationSource.cs b/tracer/src/Datadog.Trace/Configuration/ConfigurationSources/JsonConfigurationSource.cs index 492ab1f0a4ef..fcf84fbd30ce 100644 --- a/tracer/src/Datadog.Trace/Configuration/ConfigurationSources/JsonConfigurationSource.cs +++ b/tracer/src/Datadog.Trace/Configuration/ConfigurationSources/JsonConfigurationSource.cs @@ -343,16 +343,7 @@ bool ITelemeteredConfigurationSource.IsPresent(string key) try { - var valueAsString = token switch - { - null => null, - _ => token.Type switch - { - JTokenType.Null or JTokenType.None or JTokenType.Undefined => null, - JTokenType.String => token.Value(), - _ => token.ToString(Formatting.None) // serialize back into json - } - }; + var valueAsString = JTokenToString(token); if (valueAsString is not null) { @@ -381,6 +372,20 @@ bool ITelemeteredConfigurationSource.IsPresent(string key) return null; } + internal static string? JTokenToString(JToken? token) + { + return token switch + { + null => null, + _ => token.Type switch + { + JTokenType.Null or JTokenType.None or JTokenType.Undefined => null, // handle null-like values + JTokenType.String => token.Value(), // return the underlying string value + _ => token.ToString(Formatting.None) // serialize back into json + } + }; + } + /// ConfigurationResult>? ITelemeteredConfigurationSource.GetDictionary(string key, IConfigurationTelemetry telemetry, Func, bool>? validator) => GetDictionary(key, telemetry, validator, allowOptionalMappings: false, separator: ':'); diff --git a/tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/DynamicConfigurationTests.cs b/tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/DynamicConfigurationTests.cs index c46c03371ff9..00c1f5fe69c5 100644 --- a/tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/DynamicConfigurationTests.cs +++ b/tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/DynamicConfigurationTests.cs @@ -209,25 +209,11 @@ static string FlattenJsonArray(JToken json) return string.Empty; } - static string JTokenToString(JToken token) - { - return token switch - { - null => null, - _ => token.Type switch - { - JTokenType.Null or JTokenType.None or JTokenType.Undefined => null, - JTokenType.String => token.Value(), - _ => token.ToString(Formatting.None) // serialize back into json - } - }; - } - // json["runtime_metrics_enabled"]?.Value().Should().Be(expectedConfig.RuntimeMetricsEnabled); // json["debug"]?.Value().Should().Be(expectedConfig.DebugLogsEnabled); json["log_injection_enabled"]?.Value().Should().Be(expectedConfig.LogInjectionEnabled); json["sample_rate"]?.Value().Should().Be(expectedConfig.TraceSampleRate); - JTokenToString(json["remote_sampling_rules"]).Should().Be(expectedConfig.TraceSamplingRules); + JsonConfigurationSource.JTokenToString(json["remote_sampling_rules"]).Should().Be(expectedConfig.TraceSamplingRules); // json["span_sampling_rules"]?.Value().Should().Be(expectedConfig.SpanSamplingRules); // json["data_streams_enabled"]?.Value().Should().Be(expectedConfig.DataStreamsEnabled); FlattenJsonArray(json["header_tags"]).Should().Be(expectedConfig.TraceHeaderTags ?? string.Empty);