-
Notifications
You must be signed in to change notification settings - Fork 764
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
[AOT] Fix RuntimeContext warnings #4460
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #4460 +/- ##
==========================================
- Coverage 85.16% 85.02% -0.14%
==========================================
Files 316 319 +3
Lines 12583 12614 +31
==========================================
+ Hits 10716 10725 +9
- Misses 1867 1889 +22
|
7a578ad
to
bf8a530
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a changelog entry: "A System.RuntimeException will be thrown if a custom RuntimeContextSlot is used."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
8188408
to
ebee07a
Compare
@@ -15,6 +15,7 @@ | |||
// </copyright> | |||
|
|||
#nullable enable | |||
#pragma warning disable 0436 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should remove this. Here is what is happening:
we have 2 projects:
OpenTelemetry.Api
(targets netstandard2.0;net462)
OpenTelemetry
(targets net6.0;netstandard2.1;netstandard2.0;net462) and references OpenTelemetry.Api
The UnconditionalSuppressMessageAttribute
is defined in net6.0
, but isn't defined in netstandard2.0
. When we build OpenTelemetry.Api
, we are putting the type UnconditionalSuppressMessageAttribute
as an internal type into OpenTelemetery.Api
, since that attribute isn't defined in netstandard2.0
.
Normally this is fine because only that assembly sees types that are internal. However, OpenTelemetry
can see OpenTelemetry.Api
's internals because it has an InternalsVisibleTo relationship.
When OpenTelemetry
builds for net6.0
, it sees 2 different UnconditionalSuppressMessageAttribute
types - one in the OpenTelemetry.Api
netstandard2.0
assembly (through internals visible to). and one in .NET itself. So the compiler complains.
The easiest fix for this is to change the TargetFrameworks property in OpenTelemetry.Api.csproj
to be:
<TargetFrameworks>net6.0;netstandard2.0;net462</TargetFrameworks>
(add net6.0
)
Then when OpenTelemetry
builds for net6.0, it won't see the attribute from OpenTelemetry.Api
anymore, since it won't be there. It will only see the attribute from the framework in net6.0
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Shims\RequiresDynamicCodeAttribute.cs" Link ="Includes\RequiresDynamicCodeAttribute.cs" /> | ||
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Shims\RequiresUnreferencedCodeAttribute.cs" Link="Includes\RequiresUnreferencedCodeAttribute.cs" /> | ||
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Shims\UnconditionalSuppressMessageAttribute.cs" Link ="Includes\UnconditionalSuppressMessageAttribute.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Shims\RequiresDynamicCodeAttribute.cs" Link ="Includes\RequiresDynamicCodeAttribute.cs" /> | |
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Shims\RequiresUnreferencedCodeAttribute.cs" Link="Includes\RequiresUnreferencedCodeAttribute.cs" /> | |
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Shims\UnconditionalSuppressMessageAttribute.cs" Link ="Includes\UnconditionalSuppressMessageAttribute.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" /> | |
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Shims\RequiresDynamicCodeAttribute.cs" Link ="Includes\RequiresDynamicCodeAttribute.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" /> | |
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Shims\RequiresUnreferencedCodeAttribute.cs" Link="Includes\RequiresUnreferencedCodeAttribute.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" /> | |
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Shims\UnconditionalSuppressMessageAttribute.cs" Link ="Includes\UnconditionalSuppressMessageAttribute.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" /> |
…/opentelemetry-dotnet into yunl/runtimeContextEnum
@@ -201,7 +204,12 @@ public bool ContainsBatchProcessor(BaseProcessor<LogRecord> processor) | |||
} | |||
|
|||
/// <inheritdoc /> | |||
protected override bool TryCreateLogger(string? name, out Logger? logger) | |||
protected override bool TryCreateLogger( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Since the previous version doesn't have a net6.0 target, run ApiCompat for net6.0 against the netstandard2.0 target.
d8373a5
to
35173c8
Compare
// </copyright> | ||
|
||
#nullable enable | ||
#if !NET7_0_OR_GREATER |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need a net7.0
target for this to work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This attribute is in the runtime of .net7.0 and .net8.0: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.requiresdynamiccodeattribute?view=net-7.0
Therefore, we will only need to compile this file if it is not net7.0 or greater.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's basically future proofing the code. That way if/when we add a net7.0+ target, this code doesn't get compiled for it. Yes it isn't strictly necessary now, but it is saving some effort/confusion for a future dev who adds a new TFM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This kind of forces us to always add the new target to the API project as well. If in the future, the SDK project also ends up using this attribute, we wouldn't be able to add a net7.0+ target only to the SDK project as it would complain about the attribute existing in both OpenTelemetry.Api
(because of InternalsVisible) and System.Runtime
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have a recommendation to resolve this? The only other option I could imagine is to #if NET7_OR_GREATER
where we need to use this attribute AND add a net7.0
target now in order to use the attribute.
This kind of forces us to always add the new target to the API project as well
Using InternalsVisibleTo across separate NuGet packages isn't recommended, since the package versions can get out of sync and users will see errors.
In general, matching TFMs across these packages is preferrable. It makes maintenance much easier. It is how we manage our packages we ship out of dotnet/runtime. They all have a consistent set of targets. For the 8.0-* packages it is: net6.0;net7.0;net8.0;netstandard2.0;net462
. For example see https://www.nuget.org/packages/System.Collections.Immutable/8.0.0-preview.4.23259.5#dependencies-body-tab:
@@ -17,6 +17,8 @@ | |||
using OpenTelemetry.Context; | |||
using OpenTelemetry.Internal; | |||
|
|||
#pragma warning disable RS0026 // Do not add multiple overloads with optional parameters. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should scope it down to individual methods instead of suppressing the warning for the entire file.
Consider using the SuppressMessage attribute for each of the methods that it warns about.
src/OpenTelemetry.Api/Internal/Shims/RequiresUnreferencedCodeAttribute.cs
Show resolved
Hide resolved
This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or Pushing will instruct the bot to automatically remove the label. This bot runs once per day. |
@Yun-Ting - what's the status on this PR? I think we should get this change merged, as it fixes the major blocking issue when using OpenTelemetry in an AOT'd application. |
@eerhardt We have decided to drop the support for allowing custom types. That makes the changes very simple. This PR had originally started with that, but we wanted to understand how difficult it would be to continue supporting the custom types. Sorry for the confusion. Here's the PR #4542 |
Hi @eerhardt, after discussions, we decided to drop the custom type and make this a breaking change: As adapting AOT attributes to different .NET framework / runtime would add another layer of complexity when it comes to maintaining the code base (i.e. https://github.com/open-telemetry/opentelemetry-dotnet/blob/9ba6626f7e94b94e6e4e51f78f51090fde80b215/src/OpenTelemetry.Api/Internal/Shims/RequiresUnreferencedCodeAttribute.cs#LL18C1-L18C46). (I had been discussed with @vitek-karas in making the internal AOT attributes a package with the preprocessors but we haven't had a conclusion yet.) |
Changes
Fixed 3 AoT warnings:
Towards #3429
Related to: #3429 (comment)
Enumerate 4 different
ContextSlotType
:AsyncLocalRuntimeContextSlot
(the default type),ThreadLocalRuntimeContextSlot
,RemotingRuntimeContextSlot
andReflectionRuntimeContextSlot
.Handle
var type = ContextSlotType.MakeGenericType(typeof(T));
by adding attributes to mark
ReflectionRuntimeContextSlotFactory
trimming/AoT unsafe, that requires using Reflection to get the user-accustomed RuntimeContextSlot.Merge requirement checklist
CHANGELOG.md
files updated for non-trivial changes