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

Conversation

martincostello
Copy link
Contributor

@martincostello martincostello commented Apr 8, 2024

Fixes #5518

Changes

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • Changes in public API reviewed (if applicable)

/cc @eerhardt

- Apply changes from opentelemetry-dotnet-contrib#1540.
- Minor refactoring to make it easier to add `net9.0` at a later date (see open-telemetry#5519).
Copy link

codecov bot commented Apr 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.60%. Comparing base (6250307) to head (be60873).
Report is 170 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #5520      +/-   ##
==========================================
+ Coverage   83.38%   85.60%   +2.22%     
==========================================
  Files         297      289       -8     
  Lines       12531    12588      +57     
==========================================
+ Hits        10449    10776     +327     
+ Misses       2082     1812     -270     
Flag Coverage Δ
unittests ?
unittests-Instrumentation-Experimental 24.14% <ø> (?)
unittests-Instrumentation-Stable 24.16% <ø> (?)
unittests-Solution-Experimental 85.53% <ø> (?)
unittests-Solution-Stable 85.55% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

see 62 files with indirect coverage changes

Add relevant annotations to resolves AoT warnings/errors.
Whether these are the correct fixes is another question.
Update CHANGELOG for OpenTelemetry.Exporter.OpenTelemetryProtocol.
@martincostello
Copy link
Contributor Author

I've pushed up a commit that resolves the AoT warnings - whether they're the correct fixes is a different matter, so happy to update them with the correct changes if needed.

@martincostello martincostello changed the title Improve AoT validation Improve AoT validation and fix OpenTelemetryProtocol AoT warnings Apr 8, 2024
@martincostello
Copy link
Contributor Author

Test failure is dotnet/runtime#100766.

@eerhardt
Copy link
Contributor

eerhardt commented Apr 8, 2024

Fix native Aot warnings in OpenTelemetry.Exporter.OpenTelemetryProtocol.

Do we know why these warnings weren't being caught by the existing infrastructure? cc @Yun-Ting

@martincostello
Copy link
Contributor Author

I wondered the same - my guess is that the filter used to kick off that job in CI doesn't cater for basically "any code or project change".

@eerhardt
Copy link
Contributor

eerhardt commented Apr 8, 2024

Do we know why these warnings weren't being caught by the existing infrastructure?

From doing a little bit of checking, it appears to be 2 things that aren't there today that could have caught this:

  1. Today we are only checking if ($line -like "*analysis warning IL*"), but the AOT "warnings" are being treated as "errors". When I publish the app by itself, it emits Trim analysis error IL2091.
  2. We were never checking that dotnet publish passed with exit code 0. But with this PR we are now.

So these checks should be fixing the problems in the current test.

Copy link
Contributor

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @martincostello, for catching this. The AOT compatibility test looks like it should catch errors going forward now.

@@ -8,6 +8,9 @@
<!-- this is temporary. will remove in future PR. -->
<Nullable>disable</Nullable>
<DefineConstants>BUILDING_INTERNAL_PERSISTENT_STORAGE;$(DefineConstants)</DefineConstants>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<!-- SYSLIB1100;SYSLIB1101 - Configuration.Binder: can't create instance and unsupported type -->
<NoWarn>$(NoWarn);SYSLIB1100;SYSLIB1101</NoWarn>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martincostello What code is generating these warnings is it DelegatingOptionsFactory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From #5518:

  LondonTravel.Skill failed with errors (45.7s) → artifacts\bin\LondonTravel.Skill\release\LondonTravel.Skill.dll
    ILC : Trim analysis error IL2091: Microsoft.Extensions.DependencyInjection.DelegatingOptionsFactoryServiceCollectionExtensions.RegisterOptionsFactory<T>(IServiceCollection,Func`2<IConfiguration,!!0>): 'TOptions' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in 'Microsoft.Extensions.Options.IOptionsFactory`1'. The generic parameter 'T' of 'Microsoft.Extensions.DependencyInjection.DelegatingOptionsFactoryServiceCollectionExtensions.RegisterOptionsFactory<T>(IServiceCollection,Func`2<IConfiguration,!!0>)' 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. [C:\Coding\martincostello\alexa-london-travel\src\LondonTravel.Skill\LondonTravel.Skill.csproj]
    ILC : Trim analysis error IL2091: Microsoft.Extensions.DependencyInjection.DelegatingOptionsFactoryServiceCollectionExtensions.RegisterOptionsFactory<T>(IServiceCollection,Func`2<IConfiguration,!!0>): 'TOptions' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in 'Microsoft.Extensions.Options.IOptionsFactory`1'. The generic parameter 'T' of 'Microsoft.Extensions.DependencyInjection.DelegatingOptionsFactoryServiceCollectionExtensions.RegisterOptionsFactory<T>(IServiceCollection,Func`2<IConfiguration,!!0>)' 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. [C:\Coding\martincostello\alexa-london-travel\src\LondonTravel.Skill\LondonTravel.Skill.csproj]
    ILC : Trim analysis error IL2091: Microsoft.Extensions.DependencyInjection.DelegatingOptionsFactoryServiceCollectionExtensions.<>c__DisplayClass0_0`1.<RegisterOptionsFactory>b__0(IServiceProvider): 'TOptions' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in 'Microsoft.Extensions.Options.IOptionsFactory`1'. The generic parameter 'T' of 'Microsoft.Extensions.DependencyInjection.DelegatingOptionsFactoryServiceCollectionExtensions.<>c__DisplayClass0_0`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. [C:\Coding\martincostello\alexa-london-travel\src\LondonTravel.Skill\LondonTravel.Skill.csproj]
    ILC : Trim analysis error IL2091: Microsoft.Extensions.DependencyInjection.DelegatingOptionsFactoryServiceCollectionExtensions.RegisterOptionsFactory<T>(IServiceCollection,Func`4<IServiceProvider,IConfiguration,String,!!0>): 'TOptions' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in 'Microsoft.Extensions.Options.IOptionsFactory`1'. The generic parameter 'T' of 'Microsoft.Extensions.DependencyInjection.DelegatingOptionsFactoryServiceCollectionExtensions.RegisterOptionsFactory<T>(IServiceCollection,Func`4<IServiceProvider,IConfiguration,String,!!0>)' 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. [C:\Coding\martincostello\alexa-london-travel\src\LondonTravel.Skill\LondonTravel.Skill.csproj]
    ILC : Trim analysis error IL2091: Microsoft.Extensions.DependencyInjection.DelegatingOptionsFactoryServiceCollectionExtensions.RegisterOptionsFactory<T>(IServiceCollection,Func`4<IServiceProvider,IConfiguration,String,!!0>): 'TOptions' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in 'Microsoft.Extensions.Options.IOptionsFactory`1'. The generic parameter 'T' of 'Microsoft.Extensions.DependencyInjection.DelegatingOptionsFactoryServiceCollectionExtensions.RegisterOptionsFactory<T>(IServiceCollection,Func`4<IServiceProvider,IConfiguration,String,!!0>)' 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. [C:\Coding\martincostello\alexa-london-travel\src\LondonTravel.Skill\LondonTravel.Skill.csproj]
    ILC : Trim analysis error IL2091: Microsoft.Extensions.DependencyInjection.DelegatingOptionsFactoryServiceCollectionExtensions.<>c__DisplayClass1_0`1.<RegisterOptionsFactory>b__0(IServiceProvider): 'TOptions' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in 'Microsoft.Extensions.Options.IOptionsFactory`1'. The generic parameter 'T' of 'Microsoft.Extensions.DependencyInjection.DelegatingOptionsFactoryServiceCollectionExtensions.<>c__DisplayClass1_0`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. [C:\Coding\martincostello\alexa-london-travel\src\LondonTravel.Skill\LondonTravel.Skill.csproj]
    ILC : Trim analysis error IL2091: Microsoft.Extensions.Options.DelegatingOptionsFactory`1: 'TOptions' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in 'Microsoft.Extensions.Options.IOptionsFactory`1'. The generic parameter 'TOptions' of 'Microsoft.Extensions.Options.DelegatingOptionsFactory`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. [C:\Coding\martincostello\alexa-london-travel\src\LondonTravel.Skill\LondonTravel.Skill.csproj]

You can also see the errors here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, you meant the SYSLIB* warnings - I don't know - @eerhardt presumably found them locally when working on his commit.

Copy link
Contributor

@eerhardt eerhardt Apr 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These warnings are documented in https://learn.microsoft.com/en-us/dotnet/fundamentals/syslib-diagnostics/syslib1100-1118. The code that is emitting those warnings is from the Configuration Binder Source Generator. It is happening because the IOtlpExporterOptions public property is an interface, which can't be instantiated. So the config binder emits a warning saying "FYI - I can't create one of these objects".

public IOtlpExporterOptions DefaultOptions => this.DefaultOptionsInstance;
public IOtlpExporterOptions LoggingOptions => this.LoggingOptionsInstance;
public IOtlpExporterOptions MetricsOptions => this.MetricsOptionsInstance;
public IOtlpExporterOptions TracingOptions => this.TracingOptionsInstance;

Here this is safe to suppress because we are creating instances of those objects in the ctor (which is then created through the DelegatingOptionsFactory:

services!.RegisterOptionsFactory((sp, configuration, name) => new OtlpExporterBuilderOptions(
configuration,
/* Note: We don't use name for SdkLimitOptions. There should only be
one provider for a given service collection so SdkLimitOptions is
treated as a single default instance. */
sp.GetRequiredService<IOptionsMonitor<SdkLimitOptions>>().CurrentValue,
sp.GetRequiredService<IOptionsMonitor<ExperimentalOptions>>().Get(name),
/* Note: We allow LogRecordExportProcessorOptions,
MetricReaderOptions, & ActivityExportProcessorOptions to be null
because those only exist if the corresponding signal is turned on.
Currently this extension turns on all signals so they will always be
there but that may change in the future so it is handled
defensively. */
sp.GetService<IOptionsMonitor<LogRecordExportProcessorOptions>>()?.Get(name),
sp.GetService<IOptionsMonitor<MetricReaderOptions>>()?.Get(name),
sp.GetService<IOptionsMonitor<ActivityExportProcessorOptions>>()?.Get(name)));

The way the source generated code works is this:

            if (AsConfigWithChildren(configuration.GetSection("DefaultOptions")) is IConfigurationSection section8)
            {
                global::OpenTelemetry.Exporter.IOtlpExporterOptions? temp10 = instance.DefaultOptions;
                if (temp10 is not null)
                {
                    BindCore(section8, ref temp10, defaultValueIfNotFound: false, binderOptions);
                }
                else
                {
                    throw new InvalidOperationException("Cannot create instance of type 'OpenTelemetry.Exporter.IOtlpExporterOptions' because it is missing a public instance constructor.");
                }
            }

It grabs the property first, and if it is a valid object, it binds the configuration to that object. If the object is null and someone set configuration for that object, it throws because an interface isn't instantiable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eerhardt Is there any way to suppress these closer to the thing causing the issue(s) rather than at the project level? I tried to do this...

#pragma warning disable SYSLIB1100, SYSLIB1101
        services!.Configure<OtlpExporterBuilderOptions>(name, configuration!);
#pragma warning restore SYSLIB1100, SYSLIB1101

...but it doesn't seem to work.

Copy link
Contributor

@eerhardt eerhardt Apr 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to dotnet/runtime#88865 this is supposed to work, but doesn't for some reason.

I've opened dotnet/runtime#100785 for this issue. EDIT: this was a duplicate of dotnet/runtime#92509

Copy link
Member

@CodeBlanch CodeBlanch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@CodeBlanch CodeBlanch added the pkg:OpenTelemetry.Exporter.OpenTelemetryProtocol Issues related to OpenTelemetry.Exporter.OpenTelemetryProtocol NuGet package label Apr 8, 2024
@CodeBlanch CodeBlanch merged commit dde68fa into open-telemetry:main Apr 8, 2024
50 checks passed
@martincostello martincostello deleted the improve-aot-validation branch April 9, 2024 07:24
martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request Apr 9, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
@martincostello
Copy link
Contributor Author

Confirmed fixed in my original repro project with 1.8.1-alpha.0.13 from the prerelease feed.

@CodeBlanch Is it possible to get a stable patch release containing this fix?

@CodeBlanch
Copy link
Member

@martincostello Can you give me a little info on the impact? Basically without this change OtlpExporter can't be used in applications deployed with AoT compile/trimming?

@martincostello
Copy link
Contributor Author

That's correct - the application won't compile due to errors from the AoT compiler. The only workaround is to turn off trimming/native AoT.

martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request Apr 9, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request Apr 10, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
@CodeBlanch
Copy link
Member

@martincostello We discussed this on our SIG meeting yesterday. We're going to put out a 1.8.1 release including this fix and one other one. That other one is almost done, OK if this release happens in a day or two?

@martincostello
Copy link
Contributor Author

Sure, that works for me. Thanks!

martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request Apr 13, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request Apr 13, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
CodeBlanch added a commit to CodeBlanch/opentelemetry-dotnet that referenced this pull request Apr 17, 2024
…en-telemetry#5520)

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
Co-authored-by: Mikel Blanchard <mblanchard@macrosssoftware.com>
@martincostello
Copy link
Contributor Author

@CodeBlanch Thanks for shipping the fix!

martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request Apr 18, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request Apr 18, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request Apr 29, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request May 10, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request May 15, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request May 16, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request May 21, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request May 21, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request May 21, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request May 22, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
martincostello added a commit to martincostello/alexa-london-travel that referenced this pull request May 23, 2024
Use OpenTelemetry preview packages to consume fix from open-telemetry/opentelemetry-dotnet#5520.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:OpenTelemetry.Exporter.OpenTelemetryProtocol Issues related to OpenTelemetry.Exporter.OpenTelemetryProtocol NuGet package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DelegatingOptionsFactoryServiceCollectionExtensions breaks native AoT compatibility
4 participants