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

.NET 7 - OpenTelemetry.SuppressInstrumentationScope cannot be initialized with PublishTrimmed=true #4600

Closed
Kielek opened this issue Jun 16, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@Kielek
Copy link
Contributor

Kielek commented Jun 16, 2023

Bug Report

List of all OpenTelemetry NuGet
packages
and version that you are
using (e.g. OpenTelemetry 1.0.2):

    <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.5.0" />
    <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.5.0" />
    <PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.5.0-beta.1" />
    <PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.5.0-beta.1" />
    <PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.5.0-beta.1" />
    <PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.0.0-rc9.10" />
    <PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.5.0" />
    <PackageReference Include="OpenTelemetry.ResourceDetectors.Container" Version="1.0.0-beta.4" />

Runtime version (e.g. net462, net48, netcoreapp3.1, net6.0 etc. You can
find this information from the *.csproj file):

.NET 7 (works fine for .NET 6) - tested with Windows and Alpine runtime

Symptom

OpenTelemetry cannot be initialized when published with PublishTrimmed=True and -p:TrimMode=Link

What is the expected behavior?

No Exception. OTel is working.

What is the actual behavior?

Exception while initializing OTel

2023-06-16 07:28:12 Unhandled exception. System.TypeInitializationException: The type initializer for 'OpenTelemetry.SuppressInstrumentationScope' threw an exception.
2023-06-16 07:28:12  ---> System.NullReferenceException: Object reference not set to an instance of an object.
2023-06-16 07:28:12    at OpenTelemetry.Context.RuntimeContext.RegisterSlot[T](String)
2023-06-16 07:28:12    at OpenTelemetry.SuppressInstrumentationScope..cctor()
2023-06-16 07:28:12    --- End of inner exception stack trace ---
2023-06-16 07:28:12    at OpenTelemetry.SuppressInstrumentationScope.get_IsSuppressed()
2023-06-16 07:28:12    at OpenTelemetry.Sdk.get_SuppressInstrumentation()
2023-06-16 07:28:12    at Microsoft.Extensions.DependencyInjection.ProviderBuilderServiceCollectionExtensions.AddOpenTelemetrySharedProviderBuilderServices(IServiceCollection)
2023-06-16 07:28:12    at OpenTelemetry.OpenTelemetryBuilder..ctor(IServiceCollection)
2023-06-16 07:28:12    at Microsoft.Extensions.DependencyInjection.OpenTelemetryServicesExtensions.AddOpenTelemetry(IServiceCollection)
2023-06-16 07:28:12    at Program.<Main>$(String[]) in /usr/src/app/src/Program.cs:line 42
2023-06-16 07:28:12    at Program.<Main>(String[])

Reproduce

Reproducible using opentelemetry-demo:

  1. Download OpenTelemetry demo with changes from this commit open-telemetry/opentelemetry-demo@23ae56a
  2. Execute docker compose build cartservice and docker compose up.
  3. Check logs for cartservice docker.

or
go to
src\cartservice
and execute

dotnet restore ./src/cartservice.csproj -r win-x64
dotnet publish ./src/cartservice.csproj -p:PublishSingleFile=true -r win-x64 --self-contained true -p:PublishTrimmed=True -p:TrimMode=Link -c Release -o /cartservice --no-restore

Then execute created application

Additional Context

It is working fine for .NET 6.

Temporary workaround for otel-demo open-telemetry/opentelemetry-demo@d24e7ce

@Kielek Kielek added the bug Something isn't working label Jun 16, 2023
Kielek added a commit to Kielek/opentelemetry-demo that referenced this issue Jun 16, 2023
@Kielek Kielek changed the title .NET 7 - OpenTelemetry.SuppressInstrumentationScope cannot be initialized with PublishTrimmed-true .NET 7 - OpenTelemetry.SuppressInstrumentationScope cannot be initialized with PublishTrimmed=true Jun 16, 2023
@vitek-karas
Copy link
Contributor

Do you get any trim warnings when publishing the app?
OpenTelemetry has not been made trim compatible yet, so I would expect there to be warnings which point out that the app may not work.

#3429 tracks the work to make OpenTelemetry AOT compatible which will also make it trim compatible.

@Kielek
Copy link
Contributor Author

Kielek commented Jun 20, 2023

@vitek-karas, thanks for explanations. IMO, the strange behavior is that the same operation is working fine for .NET 6, but not for .NET 7.

List of warnings.

(Publish target) (1) ->
       (_RunILLink target) ->
         NugeCachePath\.nuget\packages\opentelemetry.api\1.5.0\lib\netstandard2.0\OpenTelemetry.Api.dll : warning IL2104: Assembly 'OpenTelemetry.Api' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries [C:\GitHub\opentelemetry
       -demo\src\cartservice\src\cartservice.csproj]
         NugeCachePath\.nuget\packages\opentelemetry.instrumentation.aspnetcore\1.5.0-beta.1\lib\net7.0\OpenTelemetry.Instrumentation.AspNetCore.dll : warning IL2104: Assembly 'OpenTelemetry.Instrumentation.AspNetCore' produced trim warnings. For more information
        see https://aka.ms/dotnet-illink/libraries [C:\GitHub\opentelemetry-demo\src\cartservice\src\cartservice.csproj]
         NugeCachePath\.nuget\packages\opentelemetry.instrumentation.grpcnetclient\1.5.0-beta.1\lib\netstandard2.1\OpenTelemetry.Instrumentation.GrpcNetClient.dll : warning IL2104: Assembly 'OpenTelemetry.Instrumentation.GrpcNetClient' produced trim warnings. For
        more information see https://aka.ms/dotnet-illink/libraries [C:\GitHub\opentelemetry-demo\src\cartservice\src\cartservice.csproj]
         NugeCachePath\.nuget\packages\opentelemetry.instrumentation.http\1.5.0-beta.1\lib\netstandard2.0\OpenTelemetry.Instrumentation.Http.dll : warning IL2104: Assembly 'OpenTelemetry.Instrumentation.Http' produced trim warnings. For more information see https
       ://aka.ms/dotnet-illink/libraries [C:\GitHub\opentelemetry-demo\src\cartservice\src\cartservice.csproj]
         NugeCachePath\.nuget\packages\opentelemetry.instrumentation.stackexchangeredis\1.0.0-rc9.10\lib\netstandard2.0\OpenTelemetry.Instrumentation.StackExchangeRedis.dll : warning IL2104: Assembly 'OpenTelemetry.Instrumentation.StackExchangeRedis' produced tri
       m warnings. For more information see https://aka.ms/dotnet-illink/libraries [C:\GitHub\opentelemetry-demo\src\cartservice\src\cartservice.csproj]
         NugeCachePath\.nuget\packages\pipelines.sockets.unofficial\2.2.2\lib\net5.0\Pipelines.Sockets.Unofficial.dll : warning IL2104: Assembly 'Pipelines.Sockets.Unofficial' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries
        [C:\GitHub\opentelemetry-demo\src\cartservice\src\cartservice.csproj]

Please close if you think that it will be handled by #3429.

@vitek-karas
Copy link
Contributor

The tooling only makes one promise: If there are no warnings, the app should work the same when trimmed as well.
There's no guarantee of the trimmed app's behavior if there's even one warning reported by the tools. It may or may not work. And additionally, the behavior may change release to release. Which seems to be the case here - we're evolving the trimmer and apparently there's been some change in it which made your case not work. But it's very likely that this is caused by some code which produces a warning, so the trimmer is allowed to make such changes.

@Kielek
Copy link
Contributor Author

Kielek commented Jun 20, 2023

Thanks for explanation. Closing.

@Kielek Kielek closed this as completed Jun 20, 2023
cartersocha pushed a commit to open-telemetry/opentelemetry-demo that referenced this issue Jun 21, 2023
* [cartservice] Update to .NET 7

* Add changelog entry

* Avoid trimming application due
open-telemetry/opentelemetry-dotnet#4600
AlexanderWert pushed a commit to AlexanderWert/opentelemetry-demo that referenced this issue Jul 24, 2023
* [cartservice] Update to .NET 7

* Add changelog entry

* Avoid trimming application due
open-telemetry/opentelemetry-dotnet#4600
jmichalak9 pushed a commit to jmichalak9/opentelemetry-demo that referenced this issue Mar 22, 2024
* [cartservice] Update to .NET 7

* Add changelog entry

* Avoid trimming application due
open-telemetry/opentelemetry-dotnet#4600
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants