From f6326f5bd984d06d7b6d9b73dd28062637eb157c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 20:21:01 +0100 Subject: [PATCH 1/2] [repo/InfluxDB] Prepare to .NET9 (#2268) --- .../InfluxDBExporterExtensions.cs | 2 ++ .../InfluxDBMetricsExporter.cs | 2 ++ .../TelegrafPrometheusWriterV1.cs | 6 +++++- .../TelegrafPrometheusWriterV2.cs | 6 +++++- .../InfluxDBMetricsExporterTests.cs | 11 ++--------- .../Utils/AssertUtils.cs | 2 +- .../Utils/InfluxDBFakeServer.cs | 18 ++++++++---------- ...xDBMetricsExporterOptionsTestExtensions.cs | 4 ++-- .../Utils/LineProtocolParser.cs | 19 ++++++++----------- .../MeterProviderBuilderTestExtensions.cs | 2 +- .../Utils/PointData.cs | 2 +- 11 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/OpenTelemetry.Exporter.InfluxDB/InfluxDBExporterExtensions.cs b/src/OpenTelemetry.Exporter.InfluxDB/InfluxDBExporterExtensions.cs index 6628e761bf..88e917cf90 100644 --- a/src/OpenTelemetry.Exporter.InfluxDB/InfluxDBExporterExtensions.cs +++ b/src/OpenTelemetry.Exporter.InfluxDB/InfluxDBExporterExtensions.cs @@ -61,6 +61,8 @@ private static IMetricsWriter CreateMetricsWriter(MetricsSchema metricsSchema) return metricsSchema switch { MetricsSchema.TelegrafPrometheusV2 => new TelegrafPrometheusWriterV2(), + MetricsSchema.TelegrafPrometheusV1 => new TelegrafPrometheusWriterV1(), + MetricsSchema.None => new TelegrafPrometheusWriterV1(), _ => new TelegrafPrometheusWriterV1(), }; } diff --git a/src/OpenTelemetry.Exporter.InfluxDB/InfluxDBMetricsExporter.cs b/src/OpenTelemetry.Exporter.InfluxDB/InfluxDBMetricsExporter.cs index 651d360162..4c5d7a45be 100644 --- a/src/OpenTelemetry.Exporter.InfluxDB/InfluxDBMetricsExporter.cs +++ b/src/OpenTelemetry.Exporter.InfluxDB/InfluxDBMetricsExporter.cs @@ -26,6 +26,8 @@ public InfluxDBMetricsExporter(IMetricsWriter writer, InfluxDBClient influxDbCli case WriteErrorEvent writeErrorEvent: InfluxDBEventSource.Log.FailedToExport(writeErrorEvent.Exception.Message); break; + default: + break; } }; } diff --git a/src/OpenTelemetry.Exporter.InfluxDB/TelegrafPrometheusWriterV1.cs b/src/OpenTelemetry.Exporter.InfluxDB/TelegrafPrometheusWriterV1.cs index 078e64916e..555e6e8873 100644 --- a/src/OpenTelemetry.Exporter.InfluxDB/TelegrafPrometheusWriterV1.cs +++ b/src/OpenTelemetry.Exporter.InfluxDB/TelegrafPrometheusWriterV1.cs @@ -125,7 +125,7 @@ public void Write(Metric metric, Resource? resource, WriteApi writeApi) .Tags(resource?.Attributes) .Timestamp(dataPoint.EndTime.UtcDateTime, WritePrecision.Ns); - if (dataPoint.TryGetHistogramMinMaxValues(out double min, out double max)) + if (dataPoint.TryGetHistogramMinMaxValues(out var min, out var max)) { pointData = pointData.Field("min", min) .Field("max", max); @@ -142,6 +142,10 @@ public void Write(Metric metric, Resource? resource, WriteApi writeApi) writeApi.WritePoint(pointData); } + break; + case MetricType.ExponentialHistogram: + break; + default: break; } } diff --git a/src/OpenTelemetry.Exporter.InfluxDB/TelegrafPrometheusWriterV2.cs b/src/OpenTelemetry.Exporter.InfluxDB/TelegrafPrometheusWriterV2.cs index df2a25e0fa..d0eda952d4 100644 --- a/src/OpenTelemetry.Exporter.InfluxDB/TelegrafPrometheusWriterV2.cs +++ b/src/OpenTelemetry.Exporter.InfluxDB/TelegrafPrometheusWriterV2.cs @@ -128,7 +128,7 @@ public void Write(Metric metric, Resource? resource, WriteApi writeApi) .Field($"{metricName}_count", metricPoint.GetHistogramCount()) .Field($"{metricName}_sum", metricPoint.GetHistogramSum()); - if (metricPoint.TryGetHistogramMinMaxValues(out double min, out double max)) + if (metricPoint.TryGetHistogramMinMaxValues(out var min, out var max)) { headPoint = headPoint .Field($"{metricName}_min", min) @@ -149,6 +149,10 @@ public void Write(Metric metric, Resource? resource, WriteApi writeApi) } } + break; + case MetricType.ExponentialHistogram: + break; + default: break; } } diff --git a/test/OpenTelemetry.Exporter.InfluxDB.Tests/InfluxDBMetricsExporterTests.cs b/test/OpenTelemetry.Exporter.InfluxDB.Tests/InfluxDBMetricsExporterTests.cs index 5528c0bb32..f9105672cf 100644 --- a/test/OpenTelemetry.Exporter.InfluxDB.Tests/InfluxDBMetricsExporterTests.cs +++ b/test/OpenTelemetry.Exporter.InfluxDB.Tests/InfluxDBMetricsExporterTests.cs @@ -12,21 +12,14 @@ namespace OpenTelemetry.Exporter.InfluxDB.Tests; public class InfluxDBMetricsExporterTests { private static readonly string OpenTelemetrySdkVersion; - private static readonly double[] TestBoundaries = new[] { 10D, 20D, 100D, 200D }; + private static readonly double[] TestBoundaries = [10D, 20D, 100D, 200D]; #pragma warning disable CA1810 // Initialize reference type static fields inline static InfluxDBMetricsExporterTests() #pragma warning restore CA1810 // Initialize reference type static fields inline { var sdkVersion = typeof(Sdk).Assembly.GetCustomAttribute()?.Version; - if (sdkVersion != null) - { - OpenTelemetrySdkVersion = Version.Parse(sdkVersion).ToString(3); - } - else - { - OpenTelemetrySdkVersion = "0.0.0"; - } + OpenTelemetrySdkVersion = sdkVersion != null ? Version.Parse(sdkVersion).ToString(3) : "0.0.0"; } [Theory] diff --git a/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/AssertUtils.cs b/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/AssertUtils.cs index 3c8d12c4ca..9d7c81b2b3 100644 --- a/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/AssertUtils.cs +++ b/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/AssertUtils.cs @@ -5,7 +5,7 @@ namespace OpenTelemetry.Exporter.InfluxDB.Tests.Utils; -public static class AssertUtils +internal static class AssertUtils { public static void HasField(TKey expectedKey, TValue expectedValue, IReadOnlyDictionary collection) where TKey : notnull diff --git a/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/InfluxDBFakeServer.cs b/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/InfluxDBFakeServer.cs index 595b733949..e332000c42 100644 --- a/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/InfluxDBFakeServer.cs +++ b/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/InfluxDBFakeServer.cs @@ -8,7 +8,7 @@ namespace OpenTelemetry.Exporter.InfluxDB.Tests.Utils; -public class InfluxDBFakeServer : IDisposable +internal class InfluxDBFakeServer : IDisposable { private static readonly char[] SplitChars = Environment.NewLine.ToCharArray(); private readonly IDisposable httpServer; @@ -16,13 +16,13 @@ public class InfluxDBFakeServer : IDisposable public InfluxDBFakeServer() { - this.lines = new BlockingCollection(); + this.lines = []; this.httpServer = TestHttpServer.RunServer( context => { - byte[] buffer = new byte[context.Request.ContentLength64]; + var buffer = new byte[context.Request.ContentLength64]; _ = context.Request.InputStream.Read(buffer, 0, buffer.Length); - string text = Encoding.UTF8.GetString(buffer); + var text = Encoding.UTF8.GetString(buffer); foreach (var line in text.Split(SplitChars, StringSplitOptions.RemoveEmptyEntries)) { this.lines.Add(line); @@ -45,11 +45,9 @@ public void Dispose() public PointData ReadPoint() { - if (this.lines.TryTake(out var line, TimeSpan.FromSeconds(5))) - { - return LineProtocolParser.ParseLine(line); - } - - throw new InvalidOperationException("Failed to read a data point from the InfluxDB server within the 5-second timeout."); + return this.lines.TryTake(out var line, TimeSpan.FromSeconds(5)) + ? LineProtocolParser.ParseLine(line) + : throw new InvalidOperationException( + "Failed to read a data point from the InfluxDB server within the 5-second timeout."); } } diff --git a/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/InfluxDBMetricsExporterOptionsTestExtensions.cs b/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/InfluxDBMetricsExporterOptionsTestExtensions.cs index c3d84543eb..93b682d07b 100644 --- a/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/InfluxDBMetricsExporterOptionsTestExtensions.cs +++ b/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/InfluxDBMetricsExporterOptionsTestExtensions.cs @@ -3,7 +3,7 @@ namespace OpenTelemetry.Exporter.InfluxDB.Tests.Utils; -public static class InfluxDBMetricsExporterOptionsTestExtensions +internal static class InfluxDBMetricsExporterOptionsTestExtensions { public static void WithDefaultTestConfiguration(this InfluxDBMetricsExporterOptions options) { @@ -11,7 +11,7 @@ public static void WithDefaultTestConfiguration(this InfluxDBMetricsExporterOpti options.Org = "MyOrg"; options.Token = "MyToken"; - // For tests we want to flush the metrics ASAP + // For tests, we want to flush the metrics ASAP options.FlushInterval = 1; } } diff --git a/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/LineProtocolParser.cs b/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/LineProtocolParser.cs index 4116cd9d6d..8ee5b7aa2d 100644 --- a/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/LineProtocolParser.cs +++ b/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/LineProtocolParser.cs @@ -5,7 +5,7 @@ namespace OpenTelemetry.Exporter.InfluxDB.Tests.Utils; -public class LineProtocolParser +internal class LineProtocolParser { private static readonly DateTime UnixEpoch = DateTime.SpecifyKind(new DateTime(1970, 1, 1), DateTimeKind.Utc); @@ -57,35 +57,32 @@ private static List> ParseTags(IEnumerable private static object ParseFieldValue(string fieldValue) { - if (bool.TryParse(fieldValue, out bool boolValue)) + if (bool.TryParse(fieldValue, out var boolValue)) { return boolValue; } #pragma warning disable CA1865 // Use char overload if (fieldValue.EndsWith("i", StringComparison.Ordinal) - && long.TryParse(fieldValue.AsSpan(0, fieldValue.Length - 1).ToString(), out long intValue)) + && long.TryParse(fieldValue.AsSpan(0, fieldValue.Length - 1).ToString(), out var intValue)) { return intValue; } #pragma warning restore CA1865 // Use char overload - if (double.TryParse(fieldValue, NumberStyles.Float, CultureInfo.InvariantCulture, out double doubleValue)) - { - return doubleValue; - } - - return fieldValue; + return double.TryParse(fieldValue, NumberStyles.Float, CultureInfo.InvariantCulture, out var doubleValue) + ? doubleValue + : fieldValue; } private static DateTime ParseTimestamp(string? timestampSection) { - if (string.IsNullOrEmpty(timestampSection) || !long.TryParse(timestampSection, out long unixTimeNanoseconds)) + if (string.IsNullOrEmpty(timestampSection) || !long.TryParse(timestampSection, out var unixTimeNanoseconds)) { throw new ArgumentException("Invalid formatted timestamp."); } - long ticks = unixTimeNanoseconds / 100; + var ticks = unixTimeNanoseconds / 100; return UnixEpoch.AddTicks(ticks); } } diff --git a/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/MeterProviderBuilderTestExtensions.cs b/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/MeterProviderBuilderTestExtensions.cs index 1df7fa1bc8..ddfa310485 100644 --- a/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/MeterProviderBuilderTestExtensions.cs +++ b/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/MeterProviderBuilderTestExtensions.cs @@ -6,7 +6,7 @@ namespace OpenTelemetry.Exporter.InfluxDB.Tests.Utils; -public static class MeterProviderBuilderTestExtensions +internal static class MeterProviderBuilderTestExtensions { public static MeterProviderBuilder ConfigureDefaultTestResource(this MeterProviderBuilder meterProviderBuilder) { diff --git a/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/PointData.cs b/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/PointData.cs index 9c159e0c9e..4d2c66ceea 100644 --- a/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/PointData.cs +++ b/test/OpenTelemetry.Exporter.InfluxDB.Tests/Utils/PointData.cs @@ -3,7 +3,7 @@ namespace OpenTelemetry.Exporter.InfluxDB.Tests.Utils; -public class PointData +internal class PointData { public string Measurement { get; init; } = null!; From 637219bf49faf2575a118423f1fe2ff0e64cbf88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 20:57:34 +0100 Subject: [PATCH 2/2] [repo/shared] Prepare to .NET9 (#2267) --- ...Telemetry.Instrumentation.SqlClient.csproj | 1 - src/Shared/ActivityInstrumentationHelper.cs | 10 ++++ .../DatabaseSemanticConventionHelper.cs | 4 +- src/Shared/GrpcTagHelper.cs | 25 ++++++--- src/Shared/NullableAttributes.cs | 2 +- src/Shared/PropertyFetcher.AOT.cs | 18 +++---- src/Shared/RequestDataHelper.cs | 2 +- .../DatabaseSemanticConventionHelperTests.cs | 52 +++++++++++-------- .../RequestDataHelperTests.cs | 15 +++--- 9 files changed, 75 insertions(+), 54 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.SqlClient/OpenTelemetry.Instrumentation.SqlClient.csproj b/src/OpenTelemetry.Instrumentation.SqlClient/OpenTelemetry.Instrumentation.SqlClient.csproj index 9a9827c62f..cdeb2cd9a4 100644 --- a/src/OpenTelemetry.Instrumentation.SqlClient/OpenTelemetry.Instrumentation.SqlClient.csproj +++ b/src/OpenTelemetry.Instrumentation.SqlClient/OpenTelemetry.Instrumentation.SqlClient.csproj @@ -15,7 +15,6 @@ - diff --git a/src/Shared/ActivityInstrumentationHelper.cs b/src/Shared/ActivityInstrumentationHelper.cs index be48827fb1..02a0d83e16 100644 --- a/src/Shared/ActivityInstrumentationHelper.cs +++ b/src/Shared/ActivityInstrumentationHelper.cs @@ -14,13 +14,23 @@ internal static class ActivityInstrumentationHelper private static Action CreateActivitySourceSetter() { +#if NET + return typeof(Activity).GetProperty("Source")! + .SetMethod!.CreateDelegate>(); +#else return (Action)typeof(Activity).GetProperty("Source")! .SetMethod!.CreateDelegate(typeof(Action)); +#endif } private static Action CreateActivityKindSetter() { +#if NET + return typeof(Activity).GetProperty("Kind")! + .SetMethod!.CreateDelegate>(); +#else return (Action)typeof(Activity).GetProperty("Kind")! .SetMethod!.CreateDelegate(typeof(Action)); +#endif } } diff --git a/src/Shared/DatabaseSemanticConventionHelper.cs b/src/Shared/DatabaseSemanticConventionHelper.cs index ef1c85cbc1..a3bc4b5e98 100644 --- a/src/Shared/DatabaseSemanticConventionHelper.cs +++ b/src/Shared/DatabaseSemanticConventionHelper.cs @@ -20,10 +20,10 @@ namespace OpenTelemetry.Internal; internal static class DatabaseSemanticConventionHelper { internal const string SemanticConventionOptInKeyName = "OTEL_SEMCONV_STABILITY_OPT_IN"; - internal static readonly char[] Separator = new[] { ',', ' ' }; + internal static readonly char[] Separator = [',', ' ']; [Flags] - public enum DatabaseSemanticConvention + internal enum DatabaseSemanticConvention { /// /// Instructs an instrumentation library to emit the old experimental Database attributes. diff --git a/src/Shared/GrpcTagHelper.cs b/src/Shared/GrpcTagHelper.cs index 9fa6dff377..d7c22b1c63 100644 --- a/src/Shared/GrpcTagHelper.cs +++ b/src/Shared/GrpcTagHelper.cs @@ -29,12 +29,7 @@ public static bool TryGetGrpcStatusCodeFromActivity(Activity activity, out int s { statusCode = -1; var grpcStatusCodeTag = activity.GetTagValue(GrpcStatusCodeTagName); - if (grpcStatusCodeTag == null) - { - return false; - } - - return int.TryParse(grpcStatusCodeTag as string, out statusCode); + return grpcStatusCodeTag != null && int.TryParse(grpcStatusCodeTag as string, out statusCode); } public static bool TryParseRpcServiceAndRpcMethod(string grpcMethod, out string rpcService, out string rpcMethod) @@ -66,9 +61,25 @@ public static ActivityStatusCode ResolveSpanStatusForGrpcStatusCode(int statusCo if (typeof(GrpcStatusCanonicalCode).IsEnumDefined(statusCode)) { - status = ((GrpcStatusCanonicalCode)statusCode) switch + status = (GrpcStatusCanonicalCode)statusCode switch { GrpcStatusCanonicalCode.Ok => ActivityStatusCode.Unset, + GrpcStatusCanonicalCode.Cancelled => ActivityStatusCode.Error, + GrpcStatusCanonicalCode.Unknown => ActivityStatusCode.Error, + GrpcStatusCanonicalCode.InvalidArgument => ActivityStatusCode.Error, + GrpcStatusCanonicalCode.DeadlineExceeded => ActivityStatusCode.Error, + GrpcStatusCanonicalCode.NotFound => ActivityStatusCode.Error, + GrpcStatusCanonicalCode.AlreadyExists => ActivityStatusCode.Error, + GrpcStatusCanonicalCode.PermissionDenied => ActivityStatusCode.Error, + GrpcStatusCanonicalCode.ResourceExhausted => ActivityStatusCode.Error, + GrpcStatusCanonicalCode.FailedPrecondition => ActivityStatusCode.Error, + GrpcStatusCanonicalCode.Aborted => ActivityStatusCode.Error, + GrpcStatusCanonicalCode.OutOfRange => ActivityStatusCode.Error, + GrpcStatusCanonicalCode.Unimplemented => ActivityStatusCode.Error, + GrpcStatusCanonicalCode.Internal => ActivityStatusCode.Error, + GrpcStatusCanonicalCode.Unavailable => ActivityStatusCode.Error, + GrpcStatusCanonicalCode.DataLoss => ActivityStatusCode.Error, + GrpcStatusCanonicalCode.Unauthenticated => ActivityStatusCode.Error, _ => ActivityStatusCode.Error, }; } diff --git a/src/Shared/NullableAttributes.cs b/src/Shared/NullableAttributes.cs index 21607db302..9706b278a1 100644 --- a/src/Shared/NullableAttributes.cs +++ b/src/Shared/NullableAttributes.cs @@ -43,7 +43,7 @@ internal sealed class MemberNotNullAttribute : Attribute /// /// The field or property member that is promised to be not-null. /// - public MemberNotNullAttribute(string member) => Members = new[] { member }; + public MemberNotNullAttribute(string member) => Members = [member]; /// Initializes the attribute with the list of field and property members. /// diff --git a/src/Shared/PropertyFetcher.AOT.cs b/src/Shared/PropertyFetcher.AOT.cs index cb8ecb1b8b..209f91cada 100644 --- a/src/Shared/PropertyFetcher.AOT.cs +++ b/src/Shared/PropertyFetcher.AOT.cs @@ -56,12 +56,7 @@ public bool TryFetch( out T? value) { var innerFetcher = this.innerFetcher; - if (innerFetcher is null) - { - return TryFetchRare(obj, this.propertyName, ref this.innerFetcher, out value); - } - - return innerFetcher.TryFetch(obj, out value); + return innerFetcher is null ? TryFetchRare(obj, this.propertyName, ref this.innerFetcher, out value) : innerFetcher.TryFetch(obj, out value); } #if NET @@ -189,7 +184,11 @@ private sealed class PropertyFetchInstantiated : PropertyFetch public PropertyFetchInstantiated(PropertyInfo property) { this.propertyName = property.Name; +#if NET + this.propertyFetch = property.GetMethod!.CreateDelegate>(); +#else this.propertyFetch = (Func)property.GetMethod!.CreateDelegate(typeof(Func)); +#endif } public override int NumberOfInnerFetchers => this.innerFetcher == null @@ -210,12 +209,7 @@ public override bool TryFetch( } var innerFetcher = this.innerFetcher; - if (innerFetcher is null) - { - return TryFetchRare(obj, this.propertyName, ref this.innerFetcher, out value); - } - - return innerFetcher.TryFetch(obj, out value); + return innerFetcher is null ? TryFetchRare(obj, this.propertyName, ref this.innerFetcher, out value) : innerFetcher.TryFetch(obj, out value); } } } diff --git a/src/Shared/RequestDataHelper.cs b/src/Shared/RequestDataHelper.cs index 80df85c552..6acce7fe40 100644 --- a/src/Shared/RequestDataHelper.cs +++ b/src/Shared/RequestDataHelper.cs @@ -19,7 +19,7 @@ internal sealed class RequestDataHelper // https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md#common-attributes private const string OtherHttpMethod = "_OTHER"; - private static readonly char[] SplitChars = new[] { ',' }; + private static readonly char[] SplitChars = [',']; #if NET private readonly FrozenDictionary knownHttpMethods; diff --git a/test/OpenTelemetry.Contrib.Shared.Tests/DatabaseSemanticConventionHelperTests.cs b/test/OpenTelemetry.Contrib.Shared.Tests/DatabaseSemanticConventionHelperTests.cs index 748ac1ea90..7b812b7f5d 100644 --- a/test/OpenTelemetry.Contrib.Shared.Tests/DatabaseSemanticConventionHelperTests.cs +++ b/test/OpenTelemetry.Contrib.Shared.Tests/DatabaseSemanticConventionHelperTests.cs @@ -9,28 +9,28 @@ namespace OpenTelemetry.Internal.Tests; public class DatabaseSemanticConventionHelperTests { - public static IEnumerable TestCases => new List - { - new object[] { null!, DatabaseSemanticConvention.Old }, - new object[] { string.Empty, DatabaseSemanticConvention.Old }, - new object[] { " ", DatabaseSemanticConvention.Old }, - new object[] { "junk", DatabaseSemanticConvention.Old }, - new object[] { "none", DatabaseSemanticConvention.Old }, - new object[] { "NONE", DatabaseSemanticConvention.Old }, - new object[] { "database", DatabaseSemanticConvention.New }, - new object[] { "DATABASE", DatabaseSemanticConvention.New }, - new object[] { "database/dup", DatabaseSemanticConvention.Dupe }, - new object[] { "DATABASE/DUP", DatabaseSemanticConvention.Dupe }, - new object[] { "junk,,junk", DatabaseSemanticConvention.Old }, - new object[] { "junk,JUNK", DatabaseSemanticConvention.Old }, - new object[] { "junk1,junk2", DatabaseSemanticConvention.Old }, - new object[] { "junk,database", DatabaseSemanticConvention.New }, - new object[] { "junk,database , database ,junk", DatabaseSemanticConvention.New }, - new object[] { "junk,database/dup", DatabaseSemanticConvention.Dupe }, - new object[] { "junk, database/dup ", DatabaseSemanticConvention.Dupe }, - new object[] { "database/dup,database", DatabaseSemanticConvention.Dupe }, - new object[] { "database,database/dup", DatabaseSemanticConvention.Dupe }, - }; + public static IEnumerable TestCases => + [ + [null!, DatabaseSemanticConvention.Old], + [string.Empty, DatabaseSemanticConvention.Old], + [" ", DatabaseSemanticConvention.Old], + ["junk", DatabaseSemanticConvention.Old], + ["none", DatabaseSemanticConvention.Old], + ["NONE", DatabaseSemanticConvention.Old], + ["database", DatabaseSemanticConvention.New], + ["DATABASE", DatabaseSemanticConvention.New], + ["database/dup", DatabaseSemanticConvention.Dupe], + ["DATABASE/DUP", DatabaseSemanticConvention.Dupe], + ["junk,,junk", DatabaseSemanticConvention.Old], + ["junk,JUNK", DatabaseSemanticConvention.Old], + ["junk1,junk2", DatabaseSemanticConvention.Old], + ["junk,database", DatabaseSemanticConvention.New], + ["junk,database , database ,junk", DatabaseSemanticConvention.New], + ["junk,database/dup", DatabaseSemanticConvention.Dupe], + ["junk, database/dup ", DatabaseSemanticConvention.Dupe], + ["database/dup,database", DatabaseSemanticConvention.Dupe], + ["database,database/dup", DatabaseSemanticConvention.Dupe], + ]; [Fact] public void VerifyFlags() @@ -56,7 +56,11 @@ public void VerifyGetSemanticConventionOptIn_UsingEnvironmentVariable(string inp { Environment.SetEnvironmentVariable(SemanticConventionOptInKeyName, input); +#if NET + var expected = Enum.Parse(expectedValue); +#else var expected = Enum.Parse(typeof(DatabaseSemanticConvention), expectedValue); +#endif Assert.Equal(expected, GetSemanticConventionOptIn(new ConfigurationBuilder().AddEnvironmentVariables().Build())); } finally @@ -73,7 +77,11 @@ public void VerifyGetSemanticConventionOptIn_UsingIConfiguration(string input, s .AddInMemoryCollection(new Dictionary { [SemanticConventionOptInKeyName] = input }) .Build(); +#if NET + var expected = Enum.Parse(expectedValue); +#else var expected = Enum.Parse(typeof(DatabaseSemanticConvention), expectedValue); +#endif Assert.Equal(expected, GetSemanticConventionOptIn(configuration)); } } diff --git a/test/OpenTelemetry.Contrib.Shared.Tests/RequestDataHelperTests.cs b/test/OpenTelemetry.Contrib.Shared.Tests/RequestDataHelperTests.cs index f0c3a975cd..3795504a75 100644 --- a/test/OpenTelemetry.Contrib.Shared.Tests/RequestDataHelperTests.cs +++ b/test/OpenTelemetry.Contrib.Shared.Tests/RequestDataHelperTests.cs @@ -8,14 +8,13 @@ namespace OpenTelemetry.Internal.Tests; public class RequestDataHelperTests : IDisposable { public static IEnumerable MappingVersionProtocolToVersionData => - new List - { - new object[] { new Version(1, 0), "1.0" }, - new object[] { new Version(1, 1), "1.1" }, - new object[] { new Version(2, 0), "2" }, - new object[] { new Version(3, 0), "3" }, - new object[] { new Version(7, 6, 5), "7.6.5" }, - }; + [ + [new Version(1, 0), "1.0"], + [new Version(1, 1), "1.1"], + [new Version(2, 0), "2"], + [new Version(3, 0), "3"], + [new Version(7, 6, 5), "7.6.5"], + ]; [Theory] [InlineData("GET", "GET")]