Skip to content

Commit e67d44e

Browse files
authored
[AOT] Resolve ConfigurationExtensions and EventSource warnings (#4534)
1 parent e787019 commit e67d44e

File tree

4 files changed

+9
-5
lines changed

4 files changed

+9
-5
lines changed

src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/AspNetCoreInstrumentationEventSource.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// limitations under the License.
1515
// </copyright>
1616

17+
using System.Diagnostics.CodeAnalysis;
1718
using System.Diagnostics.Tracing;
1819
using OpenTelemetry.Internal;
1920

@@ -57,12 +58,14 @@ public void RequestIsFilteredOut(string handlerName, string eventName, string op
5758
this.WriteEvent(2, handlerName, eventName, operationName);
5859
}
5960

61+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe.")]
6062
[Event(3, Message = "Filter threw exception, request will not be collected. HandlerName: '{0}', EventName: '{1}', OperationName: '{2}', Exception: {3}.", Level = EventLevel.Error)]
6163
public void RequestFilterException(string handlerName, string eventName, string operationName, string exception)
6264
{
6365
this.WriteEvent(3, handlerName, eventName, operationName, exception);
6466
}
6567

68+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe.")]
6669
[Event(4, Message = "Enrich threw exception. HandlerName: '{0}', EventName: '{1}', OperationName: '{2}', Exception: {3}.", Level = EventLevel.Warning)]
6770
public void EnrichmentException(string handlerName, string eventName, string operationName, string exception)
6871
{

src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.GrpcNetClient\StatusCanonicalCode.cs" Link="Includes\StatusCanonicalCode.cs" />
1616
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
1717
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\HttpSemanticConventionHelper.cs" Link="Includes\HttpSemanticConventionHelper.cs" />
18+
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Shims\UnconditionalSuppressMessageAttribute.cs" Link="Includes\UnconditionalSuppressMessageAttribute.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
1819
</ItemGroup>
1920

2021
<ItemGroup>

src/OpenTelemetry/Internal/Options/ConfigurationExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static bool TryGetStringValue(
4545
#endif
4646
out string? value)
4747
{
48-
value = configuration.GetValue<string?>(key, null);
48+
value = configuration[key] is string configValue ? configValue : null;
4949

5050
return !string.IsNullOrWhiteSpace(value);
5151
}
@@ -125,7 +125,7 @@ public static IServiceCollection RegisterOptionsFactory<T>(
125125
Debug.Assert(services != null, "services was null");
126126
Debug.Assert(optionsFactoryFunc != null, "optionsFactoryFunc was null");
127127

128-
services!.TryAddSingleton<IOptionsFactory<T>>(sp =>
128+
services.TryAddSingleton<IOptionsFactory<T>>(sp =>
129129
{
130130
return new DelegatingOptionsFactory<T>(
131131
(c, n) => optionsFactoryFunc!(c),
@@ -146,7 +146,7 @@ public static IServiceCollection RegisterOptionsFactory<T>(
146146
Debug.Assert(services != null, "services was null");
147147
Debug.Assert(optionsFactoryFunc != null, "optionsFactoryFunc was null");
148148

149-
services!.TryAddSingleton<IOptionsFactory<T>>(sp =>
149+
services.TryAddSingleton<IOptionsFactory<T>>(sp =>
150150
{
151151
return new DelegatingOptionsFactory<T>(
152152
(c, n) => optionsFactoryFunc!(sp, c, n),

test/OpenTelemetry.AotCompatibility.Tests/AotCompatibilityTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ public void EnsureAotCompatibility()
8181
process.Start();
8282
process.BeginOutputReadLine();
8383

84-
Assert.True(process.WaitForExit(milliseconds: 180_000), "dotnet publish command timed out after 180 seconds.");
84+
Assert.True(process.WaitForExit(milliseconds: 240_000), "dotnet publish command timed out after 240 seconds.");
8585
Assert.True(process.ExitCode == 0, "Publishing the AotCompatibility app failed. See test output for more details.");
8686

8787
var warnings = expectedOutput.ToString().Split('\n', '\r').Where(line => line.Contains("warning IL"));
88-
Assert.Equal(48, warnings.Count());
88+
Assert.Equal(43, warnings.Count());
8989
}
9090
}
9191
}

0 commit comments

Comments
 (0)