From 378d976c9a6bf6fa7570428ad03baf0ccad60da8 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Thu, 1 Jun 2023 09:47:53 -0500 Subject: [PATCH 1/3] [AOT] Resolve ConfigurationExtensions and EventSource warnings - ConfigurationBinder.GetValue uses Reflection to bind IConfiguration values to strongly typed objects. ConfigurationExtensions.TryGetStringValue was using ConfigurationBinder to get a string value from IConfiguration, which is causing a warning. However, IConfiguration values are already strings, so this is unnecessary. It is also not performant because calling ConfigurationBinder allocates objects and uses TypeDescriptor. - Additionally, suppress 2 EventSource warnings while I'm making changes. Contributes to #3429 --- .../Implementation/AspNetCoreInstrumentationEventSource.cs | 3 +++ .../Internal/Options/ConfigurationExtensions.cs | 6 +++--- .../AotCompatibilityTests.cs | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/AspNetCoreInstrumentationEventSource.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/AspNetCoreInstrumentationEventSource.cs index 7d2fc64b405..9b52e6bd6c7 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/AspNetCoreInstrumentationEventSource.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/AspNetCoreInstrumentationEventSource.cs @@ -14,6 +14,7 @@ // limitations under the License. // +using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Tracing; using OpenTelemetry.Internal; @@ -57,12 +58,14 @@ public void RequestIsFilteredOut(string handlerName, string eventName, string op this.WriteEvent(2, handlerName, eventName, operationName); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe.")] [Event(3, Message = "Filter threw exception, request will not be collected. HandlerName: '{0}', EventName: '{1}', OperationName: '{2}', Exception: {3}.", Level = EventLevel.Error)] public void RequestFilterException(string handlerName, string eventName, string operationName, string exception) { this.WriteEvent(3, handlerName, eventName, operationName, exception); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe.")] [Event(4, Message = "Enrich threw exception. HandlerName: '{0}', EventName: '{1}', OperationName: '{2}', Exception: {3}.", Level = EventLevel.Warning)] public void EnrichmentException(string handlerName, string eventName, string operationName, string exception) { diff --git a/src/OpenTelemetry/Internal/Options/ConfigurationExtensions.cs b/src/OpenTelemetry/Internal/Options/ConfigurationExtensions.cs index dc0abc051a4..eea4b5aef7f 100644 --- a/src/OpenTelemetry/Internal/Options/ConfigurationExtensions.cs +++ b/src/OpenTelemetry/Internal/Options/ConfigurationExtensions.cs @@ -45,7 +45,7 @@ public static bool TryGetStringValue( #endif out string? value) { - value = configuration.GetValue(key, null); + value = configuration[key] is string configValue ? configValue : null; return !string.IsNullOrWhiteSpace(value); } @@ -125,7 +125,7 @@ public static IServiceCollection RegisterOptionsFactory( Debug.Assert(services != null, "services was null"); Debug.Assert(optionsFactoryFunc != null, "optionsFactoryFunc was null"); - services!.TryAddSingleton>(sp => + services.TryAddSingleton>(sp => { return new DelegatingOptionsFactory( (c, n) => optionsFactoryFunc!(c), @@ -146,7 +146,7 @@ public static IServiceCollection RegisterOptionsFactory( Debug.Assert(services != null, "services was null"); Debug.Assert(optionsFactoryFunc != null, "optionsFactoryFunc was null"); - services!.TryAddSingleton>(sp => + services.TryAddSingleton>(sp => { return new DelegatingOptionsFactory( (c, n) => optionsFactoryFunc!(sp, c, n), diff --git a/test/OpenTelemetry.AotCompatibility.Tests/AotCompatibilityTests.cs b/test/OpenTelemetry.AotCompatibility.Tests/AotCompatibilityTests.cs index 78ef604fcad..1349b9205f8 100644 --- a/test/OpenTelemetry.AotCompatibility.Tests/AotCompatibilityTests.cs +++ b/test/OpenTelemetry.AotCompatibility.Tests/AotCompatibilityTests.cs @@ -85,7 +85,7 @@ public void EnsureAotCompatibility() Assert.True(process.ExitCode == 0, "Publishing the AotCompatibility app failed. See test output for more details."); var warnings = expectedOutput.ToString().Split('\n', '\r').Where(line => line.Contains("warning IL")); - Assert.Equal(48, warnings.Count()); + Assert.Equal(43, warnings.Count()); } } } From 2cbdd968e70768b4435bbe0f2a275d6a0fd0d356 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Thu, 1 Jun 2023 10:08:02 -0500 Subject: [PATCH 2/3] Fix build by including UnconditionalSuppressMessageAttribute for netstandard. --- .../OpenTelemetry.Instrumentation.AspNetCore.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj b/src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj index cc04eed9bd2..f2881a4ca1e 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj @@ -15,6 +15,7 @@ + From 3973231a448f787f64e40583f0c6945a10f42faf Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Thu, 1 Jun 2023 16:47:52 -0500 Subject: [PATCH 3/3] Increase publish AOT timeout --- .../AotCompatibilityTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/OpenTelemetry.AotCompatibility.Tests/AotCompatibilityTests.cs b/test/OpenTelemetry.AotCompatibility.Tests/AotCompatibilityTests.cs index 1349b9205f8..2b9cf77d9c3 100644 --- a/test/OpenTelemetry.AotCompatibility.Tests/AotCompatibilityTests.cs +++ b/test/OpenTelemetry.AotCompatibility.Tests/AotCompatibilityTests.cs @@ -81,7 +81,7 @@ public void EnsureAotCompatibility() process.Start(); process.BeginOutputReadLine(); - Assert.True(process.WaitForExit(milliseconds: 180_000), "dotnet publish command timed out after 180 seconds."); + Assert.True(process.WaitForExit(milliseconds: 240_000), "dotnet publish command timed out after 240 seconds."); Assert.True(process.ExitCode == 0, "Publishing the AotCompatibility app failed. See test output for more details."); var warnings = expectedOutput.ToString().Split('\n', '\r').Where(line => line.Contains("warning IL"));