Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve AoT validation and fix OpenTelemetryProtocol AoT warnings #5520

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/verifyaotcompat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
fail-fast: false # ensures the entire test matrix is run, even if one permutation fails
matrix:
os: [ ubuntu-latest ]
os: [ ubuntu-latest, windows-latest ]
version: [ net8.0 ]

runs-on: ${{ matrix.os }}
Expand All @@ -24,4 +24,3 @@ jobs:
- name: publish AOT testApp, assert static analysis warning count, and run the app
shell: pwsh
run: .\build\test-aot-compatibility.ps1 ${{ matrix.version }}

26 changes: 17 additions & 9 deletions build/test-aot-compatibility.ps1
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
param([string]$targetNetFramework)

$rootDirectory = Split-Path $PSScriptRoot -Parent
$publishOutput = dotnet publish $rootDirectory/test/OpenTelemetry.AotCompatibility.TestApp/OpenTelemetry.AotCompatibility.TestApp.csproj -nodeReuse:false /p:UseSharedCompilation=false /p:ExposeExperimentalFeatures=true
$publishOutput = dotnet publish $rootDirectory/test/OpenTelemetry.AotCompatibility.TestApp/OpenTelemetry.AotCompatibility.TestApp.csproj --framework $targetNetFramework -nodeReuse:false /p:UseSharedCompilation=false /p:ExposeExperimentalFeatures=true

$actualWarningCount = 0

foreach ($line in $($publishOutput -split "`r`n"))
{
if ($line -like "*analysis warning IL*")
if (($line -like "*analysis warning IL*") -or ($line -like "*analysis error IL*"))
{
Write-Host $line

$actualWarningCount += 1
}
}

pushd $rootDirectory/test/OpenTelemetry.AotCompatibility.TestApp/bin/Release/$targetNetFramework/linux-x64
Write-Host "Actual warning count is:", $actualWarningCount
$expectedWarningCount = 0

if ($LastExitCode -ne 0)
{
Write-Host "There was an error while publishing AotCompatibility Test App. LastExitCode is:", $LastExitCode
Write-Host $publishOutput
}

$runtime = $IsWindows ? "win-x64" : ($IsMacOS ? "macos-x64" : "linux-x64")
$app = $IsWindows ? "./OpenTelemetry.AotCompatibility.TestApp.exe" : "./OpenTelemetry.AotCompatibility.TestApp"

Push-Location $rootDirectory/test/OpenTelemetry.AotCompatibility.TestApp/bin/Release/$targetNetFramework/$runtime

Write-Host "Executing test App..."
./OpenTelemetry.AotCompatibility.TestApp
$app
Write-Host "Finished executing test App"

if ($LastExitCode -ne 0)
{
Write-Host "There was an error while executing AotCompatibility Test App. LastExitCode is:", $LastExitCode
}

popd

Write-Host "Actual warning count is:", $actualWarningCount
$expectedWarningCount = 0
Pop-Location

$testPassed = 0
if ($actualWarningCount -ne $expectedWarningCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#nullable enable

#if NET8_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using Microsoft.Extensions.Configuration;
using OpenTelemetry.Exporter;
using OpenTelemetry.Internal;
Expand Down Expand Up @@ -36,6 +39,10 @@ public static class OpenTelemetryBuilderOtlpExporterExtensions
/// </remarks>
/// <param name="builder"><see cref="IOpenTelemetryBuilder"/>.</param>
/// <returns>Supplied <see cref="IOpenTelemetryBuilder"/> for chaining calls.</returns>
#if NET8_0_OR_GREATER
[RequiresDynamicCode("Binding strongly typed objects to configuration values may require generating dynamic code at runtime.")]
[RequiresUnreferencedCode("TOptions's dependent types may have their members trimmed. Ensure all required members are preserved.")]
martincostello marked this conversation as resolved.
Show resolved Hide resolved
#endif
public static IOpenTelemetryBuilder UseOtlpExporter(
this IOpenTelemetryBuilder builder)
=> UseOtlpExporter(builder, name: null, configuration: null, configure: null);
Expand All @@ -51,6 +58,10 @@ public static IOpenTelemetryBuilder UseOtlpExporter(
/// signal automatically if the protocol is set to <see
/// cref="OtlpExportProtocol.HttpProtobuf"/>.
/// </param>
#if NET8_0_OR_GREATER
[RequiresDynamicCode("Binding strongly typed objects to configuration values may require generating dynamic code at runtime.")]
[RequiresUnreferencedCode("TOptions's dependent types may have their members trimmed. Ensure all required members are preserved.")]
#endif
public static IOpenTelemetryBuilder UseOtlpExporter(
this IOpenTelemetryBuilder builder,
OtlpExportProtocol protocol,
Expand All @@ -73,6 +84,10 @@ public static IOpenTelemetryBuilder UseOtlpExporter(
/// <returns><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)" path="/returns"/></returns>
/// <param name="builder"><see cref="IOpenTelemetryBuilder"/>.</param>
/// <param name="configure">Callback action for configuring <see cref="OtlpExporterBuilder"/>.</param>
#if NET8_0_OR_GREATER
[RequiresDynamicCode("Binding strongly typed objects to configuration values may require generating dynamic code at runtime.")]
[RequiresUnreferencedCode("TOptions's dependent types may have their members trimmed. Ensure all required members are preserved.")]
#endif
internal static IOpenTelemetryBuilder UseOtlpExporter(
this IOpenTelemetryBuilder builder,
Action<OtlpExporterBuilder> configure)
Expand All @@ -97,6 +112,10 @@ internal static IOpenTelemetryBuilder UseOtlpExporter(
/// </list>
/// </para>
/// </param>
#if NET8_0_OR_GREATER
[RequiresDynamicCode("Binding strongly typed objects to configuration values may require generating dynamic code at runtime.")]
[RequiresUnreferencedCode("TOptions's dependent types may have their members trimmed. Ensure all required members are preserved.")]
#endif
internal static IOpenTelemetryBuilder UseOtlpExporter(
this IOpenTelemetryBuilder builder,
IConfiguration configuration)
Expand Down Expand Up @@ -126,6 +145,10 @@ internal static IOpenTelemetryBuilder UseOtlpExporter(
/// </para>
/// </param>
/// <param name="configure">Optional callback action for configuring <see cref="OtlpExporterBuilder"/>.</param>
#if NET8_0_OR_GREATER
[RequiresDynamicCode("Binding strongly typed objects to configuration values may require generating dynamic code at runtime.")]
[RequiresUnreferencedCode("TOptions's dependent types may have their members trimmed. Ensure all required members are preserved.")]
#endif
internal static IOpenTelemetryBuilder UseOtlpExporter(
this IOpenTelemetryBuilder builder,
string? name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#nullable enable

using System.Diagnostics;
#if NET8_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
Expand All @@ -21,6 +24,10 @@ internal sealed class OtlpExporterBuilder

private readonly string name;

#if NET8_0_OR_GREATER
[RequiresDynamicCode("Binding strongly typed objects to configuration values may require generating dynamic code at runtime.")]
[RequiresUnreferencedCode("TOptions's dependent types may have their members trimmed. Ensure all required members are preserved.")]
#endif
internal OtlpExporterBuilder(
IServiceCollection services,
string? name,
Expand Down Expand Up @@ -119,6 +126,10 @@ public OtlpExporterBuilder ConfigureTracingProcessorOptions(
return this;
}

#if NET8_0_OR_GREATER
[RequiresDynamicCode("Binding strongly typed objects to configuration values may require generating dynamic code at runtime.")]
[RequiresUnreferencedCode("TOptions's dependent types may have their members trimmed. Ensure all required members are preserved.")]
#endif
private static void BindConfigurationToOptions(IServiceCollection services, string name, IConfiguration configuration)
{
Debug.Assert(services != null, "services was null");
Expand Down
7 changes: 7 additions & 0 deletions src/Shared/Options/DelegatingOptionsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ example of how that works.
#nullable enable

using System.Diagnostics;
#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using Microsoft.Extensions.Configuration;

namespace Microsoft.Extensions.Options;
Expand All @@ -24,7 +27,11 @@ namespace Microsoft.Extensions.Options;
/// Implementation of <see cref="IOptionsFactory{TOptions}"/>.
/// </summary>
/// <typeparam name="TOptions">The type of options being requested.</typeparam>
#if NET6_0_OR_GREATER
internal sealed class DelegatingOptionsFactory<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TOptions> :
#else
internal sealed class DelegatingOptionsFactory<TOptions> :
#endif
IOptionsFactory<TOptions>
where TOptions : class
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#nullable enable

using System.Diagnostics;
#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
Expand All @@ -12,7 +15,11 @@ namespace Microsoft.Extensions.DependencyInjection;

internal static class DelegatingOptionsFactoryServiceCollectionExtensions
{
#if NET6_0_OR_GREATER
public static IServiceCollection RegisterOptionsFactory<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(
#else
public static IServiceCollection RegisterOptionsFactory<T>(
#endif
this IServiceCollection services,
Func<IConfiguration, T> optionsFactoryFunc)
where T : class
Expand All @@ -33,7 +40,11 @@ public static IServiceCollection RegisterOptionsFactory<T>(
return services!;
}

#if NET6_0_OR_GREATER
public static IServiceCollection RegisterOptionsFactory<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(
#else
public static IServiceCollection RegisterOptionsFactory<T>(
#endif
this IServiceCollection services,
Func<IServiceProvider, IConfiguration, string, T> optionsFactoryFunc)
where T : class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>$(TargetFrameworksForAotCompatibilityTests)</TargetFramework>
<TargetFrameworks>$(TargetFrameworksForAotCompatibilityTests)</TargetFrameworks>
<PublishAot>true</PublishAot>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
<SelfContained>true</SelfContained>
Expand Down
Loading