Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.17k stars 4.54k forks source link

[FEATURE REQ] Azure.Monitor.OpenTelemetry.AspNetCore is not AoT friendly #43856

Open martincostello opened 2 months ago

martincostello commented 2 months ago

Library name

Azure.Monitor.OpenTelemetry.AspNetCore

Please describe the feature.

An application using native AoT that references Azure.Monitor.OpenTelemetry.AspNetCore receives warnings related to native AoT and trimming associated with the library:

  API succeeded with warnings (75.7s) → artifacts\publish\API\release\
    ILC : Trim analysis warning IL2026: Azure.Monitor.OpenTelemetry.AspNetCore.DefaultAzureMonitorOptions.Configure(AzureMonitorOptions): Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration,Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Cannot statically analyze the type of instance so its members may be trimmed.
    ILC : AOT analysis warning IL3050: Azure.Monitor.OpenTelemetry.AspNetCore.DefaultAzureMonitorOptions.Configure(AzureMonitorOptions): Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration,Object)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types.
    ILC : Trim analysis warning IL2026: OpenTelemetry.ResourceDetectors.Azure.AzureVmMetaDataRequestor.GetAzureVmMetaDataResponseDefault(): Using member 'System.Text.Json.JsonSerializer.Deserialize<AzureVmMetadataResponse>(String,JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.
    ILC : AOT analysis warning IL3050: OpenTelemetry.ResourceDetectors.Azure.AzureVmMetaDataRequestor.GetAzureVmMetaDataResponseDefault(): Using member 'System.Text.Json.JsonSerializer.Deserialize<AzureVmMetadataResponse>(String,JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.
    ILC : Trim analysis warning IL2070: OpenTelemetry.Instrumentation.PropertyFetcher`1.PropertyFetch.Create(TypeInfo,String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'System.Reflection.TypeInfo.DeclaredProperties.get'. The parameter 'type' of method 'OpenTelemetry.Instrumentation.PropertyFetcher`1.PropertyFetch.Create(TypeInfo,String)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
    ILC : Trim analysis warning IL2070: OpenTelemetry.Instrumentation.PropertyFetcher`1.PropertyFetch.Create(TypeInfo,String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperty(String)'. The parameter 'type' of method 'OpenTelemetry.Instrumentation.PropertyFetcher`1.PropertyFetch.Create(TypeInfo,String)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
    ILC : AOT analysis warning IL3050: OpenTelemetry.Instrumentation.PropertyFetcher`1.PropertyFetch.<Create>g__DynamicInstantiationHelper|2_2(Type,PropertyInfo): Using member 'System.Reflection.MethodInfo.MakeGenericMethod(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.

It looks like a good proportion of them could be fixed by using the configuration and JSON source generators.

I considered looking into this myself, but the repo is quite big and doesn't use .NET 8 yet, so I imagine wouldn't be a trivial undertaking to use. Its usage probably depends on the same things as #41150.

github-actions[bot] commented 2 months ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @cijothomas @rajkumar-rangaraj @reyang @TimothyMothra @vishweshbankwar.

martincostello commented 2 months ago

FYI this was with 1.1.1. I tried 1.2.0-beta.3, but that added more warnings:

ILC : Trim analysis warning IL2026: Azure.Monitor.OpenTelemetry.AspNetCore.DefaultAzureMonitorOptions.Configure(AzureMonitorOptions): Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration,Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Cannot statically analyze the type of instance so its members may be trimmed. [/home/runner/work/api/api/src/API/API.csproj]
ILC : AOT analysis warning IL3050: Azure.Monitor.OpenTelemetry.AspNetCore.DefaultAzureMonitorOptions.Configure(AzureMonitorOptions): Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration,Object)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types. [/home/runner/work/api/api/src/API/API.csproj]
ILC : Trim analysis warning IL2026: Azure.Monitor.OpenTelemetry.AspNetCore.OpenTelemetryBuilderExtensions.AddVendorInstrumentationIfPackageNotReferenced(TracerProviderBuilder): Using member 'OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddSqlClientInstrumentation(TracerProviderBuilder)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming is not yet supported with SqlClient instrumentation. [/home/runner/work/api/api/src/API/API.csproj]
ILC : Trim analysis warning IL2026: Azure.Monitor.OpenTelemetry.AspNetCore.AzureMonitorAspNetCoreEventSource.ServiceCallFailed(String,Int32,String): Using member 'System.Diagnostics.Tracing.EventSource.WriteEvent(Int32,Object[])' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. [/home/runner/work/api/api/src/API/API.csproj]
ILC : Trim analysis warning IL2026: Azure.Monitor.OpenTelemetry.AspNetCore.AzureMonitorAspNetCoreEventSource.ServiceCallFailedWithServiceError(String,Int32,String,String,String): Using member 'System.Diagnostics.Tracing.EventSource.WriteEvent(Int32,Object[])' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. [/home/runner/work/api/api/src/API/API.csproj]
ILC : Trim analysis warning IL2026: Azure.Monitor.OpenTelemetry.AspNetCore.AzureMonitorAspNetCoreEventSource.ProcessCountersCpuCounter(Int64,Int64,Double,Int32,Double): Using member 'System.Diagnostics.Tracing.EventSource.WriteEvent(Int32,Object[])' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. [/home/runner/work/api/api/src/API/API.csproj]
ILC : Trim analysis warning IL2026: Azure.Monitor.OpenTelemetry.AspNetCore.Internals.Profiling.ProfilingSessionEventSource.ResourceAttributes(IEnumerable`1<KeyValuePair`2<String,String>>): Using member 'System.Diagnostics.Tracing.EventSource.WriteEvent(Int32,Object[])' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. [/home/runner/work/api/api/src/API/API.csproj]
ILC : Trim analysis warning IL2026: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.DerivedMetric`1.CreateProjection(): Using member 'System.Linq.Expressions.Expression.Property(Expression,String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Creating Expressions requires unreferenced code because the members being referenced by the Expression may be trimmed. [/home/runner/work/api/api/src/API/API.csproj]
ILC : Trim analysis warning IL2026: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceFieldExpression(ParameterExpression,String,Filter`1.FieldNameType<!0>): Using member 'System.Linq.Expressions.Expression.Property(Expression,String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Creating Expressions requires unreferenced code because the members being referenced by the Expression may be trimmed. [/home/runner/work/api/api/src/API/API.csproj]
ILC : Trim analysis warning IL2026: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.CreateListAccessExpression(ParameterExpression,String,MethodInfo,Type,String): Using member 'System.Linq.Expressions.Expression.Property(Expression,String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Creating Expressions requires unreferenced code because the members being referenced by the Expression may be trimmed. [/home/runner/work/api/api/src/API/API.csproj]
ILC : Trim analysis warning IL2026: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.CreateDictionaryAccessExpression(ParameterExpression,String,MethodInfo,Type,String): Using member 'System.Linq.Expressions.Expression.Property(Expression,String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Creating Expressions requires unreferenced code because the members being referenced by the Expression may be trimmed. [/home/runner/work/api/api/src/API/API.csproj]
ILC : Trim analysis warning IL2070: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.<>c.<GetPropertyTypeFromFieldName>b__38_0(Type,String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperty(String,BindingFlags)'. The parameter 'type' of method 'Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.<>c.<GetPropertyTypeFromFieldName>b__38_0(Type,String)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [/home/runner/work/api/api/src/API/API.csproj]
ILC : Trim analysis warning IL2090: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceComparatorExpressionForAnyFieldCondition(ParameterExpression): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties(BindingFlags)'. The generic parameter 'TTelemetry' of 'Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [/home/runner/work/api/api/src/API/API.csproj]
ILC : Trim analysis warning IL2026: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceComparatorExpressionForAnyFieldCondition(ParameterExpression): Using member 'System.Linq.Expressions.Expression.Property(Expression,String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Creating Expressions requires unreferenced code because the members being referenced by the Expression may be trimmed. [/home/runner/work/api/api/src/API/API.csproj]
ILC : Trim analysis warning IL2026: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceComparatorExpressionForAnyFieldCondition(ParameterExpression): Using member 'System.Linq.Expressions.Expression.Property(Expression,String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Creating Expressions requires unreferenced code because the members being referenced by the Expression may be trimmed. [/home/runner/work/api/api/src/API/API.csproj]
ILC : AOT analysis warning IL3050: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceComparatorExpressionForSingleFieldCondition(Expression,Type,Boolean): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [/home/runner/work/api/api/src/API/API.csproj]
ILC : AOT analysis warning IL3050: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceComparatorExpressionForSingleFieldCondition(Expression,Type,Boolean): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [/home/runner/work/api/api/src/API/API.csproj]
ILC : AOT analysis warning IL3050: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceComparatorExpressionForSingleFieldCondition(Expression,Type,Boolean): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [/home/runner/work/api/api/src/API/API.csproj]
ILC : AOT analysis warning IL3050: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceComparatorExpressionForSingleFieldCondition(Expression,Type,Boolean): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [/home/runner/work/api/api/src/API/API.csproj]
ILC : AOT analysis warning IL3050: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceComparatorExpressionForSingleFieldCondition(Expression,Type,Boolean): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [/home/runner/work/api/api/src/API/API.csproj]
ILC : AOT analysis warning IL3050: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceComparatorExpressionForSingleFieldCondition(Expression,Type,Boolean): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [/home/runner/work/api/api/src/API/API.csproj]
ILC : AOT analysis warning IL3050: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceComparatorExpressionForSingleFieldCondition(Expression,Type,Boolean): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [/home/runner/work/api/api/src/API/API.csproj]
ILC : AOT analysis warning IL3050: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceComparatorExpressionForSingleFieldCondition(Expression,Type,Boolean): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [/home/runner/work/api/api/src/API/API.csproj]
ILC : AOT analysis warning IL3050: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceComparatorExpressionForSingleFieldCondition(Expression,Type,Boolean): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [/home/runner/work/api/api/src/API/API.csproj]
ILC : AOT analysis warning IL3050: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceComparatorExpressionForSingleFieldCondition(Expression,Type,Boolean): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [/home/runner/work/api/api/src/API/API.csproj]
ILC : AOT analysis warning IL3050: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceComparatorExpressionForSingleFieldCondition(Expression,Type,Boolean): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [/home/runner/work/api/api/src/API/API.csproj]
ILC : AOT analysis warning IL3050: Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Filtering.Filter`1.ProduceComparatorExpressionForSingleFieldCondition(Expression,Type,Boolean): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [/home/runner/work/api/api/src/API/API.csproj]
TimothyMothra commented 2 months ago

Hello @martincostello , This is on our backlog, but we don't have an ETA.

Our Exporter package is AOT compatible today. Does this meet your needs?

martincostello commented 2 months ago

I can look into it later this week and see how I go.

I've already added the OTel packages for use with Aspire, I then added the Monitor package after finding samples in the docs for when I deploy to Azure App Service.

martincostello commented 2 months ago

@TimothyMothra Thanks for the pointer - I've switched over to that package, and I get the same metrics but without the native AoT warnings, albeit for a slight increase in code to configure things.