open-telemetry / opentelemetry-dotnet

The OpenTelemetry .NET Client
https://opentelemetry.io
Apache License 2.0
3.18k stars 753 forks source link

Adding OpenTelemetry to a native AOT app adds a lot of app size #5785

Open eerhardt opened 1 month ago

eerhardt commented 1 month ago

Package

OpenTelemetry

Package Version

Package Name Version
OpenTelemetry.Extensions.OpenTelemetryProtocol 1.9.0
OpenTelemetry.Extensions.Hosting 1.9.0
OpenTelemetry.Extensions.AspNetCore 1.9.0
OpenTelemetry.Extensions.Http 1.9.0
OpenTelemetry.Extensions.Runtime 1.9.0

Runtime Version

net9.0

Description

Adding OpenTelemetry to our native AOT'd ASP.NET Core "TodosApi" app in https://github.com/aspnet/Benchmarks/pull/2014 shows the app size increase from 18.1 MB to 22.2 MB. About half of that size is from the OTLP Exporter. If I remove the OTLP Exporter, it drops back down to 20.0 MB.

For loo

Steps to Reproduce

dotnet publish the app before and after https://github.com/aspnet/Benchmarks/pull/2014. Measure the app size in bin\Release\net9.0\win-x64\publish.

Expected Result

The app size change should be as minimal as possible.

Actual Result

The app gets about 22% larger when enabling OTel.

Additional Context

Using https://github.com/MichalStrehovsky/sizoscope, you can get some insight into what is taking all the size.

The first thing that jumps out to me is the usage of Regex:

image

This is coming from

https://github.com/open-telemetry/opentelemetry-dotnet/blob/6ec151072910b024e0e47f7a87de25695a14a502/src/OpenTelemetry/Trace/TracerProviderSdk.cs#L254-L259

The unfortunate thing about this is not only app size, but also on native AOT'd apps, Regex's that don't use the source generator are always interpreted. So if that Regex is called often, it will slow down the app compared to a "normal" .NET app.

For the OTLP Exporter, it looks like most of the app size is coming from Grpc:

image

and from generic collections of structs:

image

vishweshbankwar commented 1 month ago

@eerhardt - OTLP exporter part is WIP https://github.com/open-telemetry/opentelemetry-dotnet/pull/5731

cijothomas commented 1 week ago

@rajkumar-rangaraj will continue/take-over the WIP PR to make this in 1.10

cijothomas commented 1 week ago

Can be closed as duplicate of https://github.com/open-telemetry/opentelemetry-dotnet/issues/5730 as 5730 will remove the dependency on protobuf, and that'll indirectly fix this issue too.

eerhardt commented 1 week ago

Can be closed as duplicate of #5730 as 5730 will remove the dependency on protobuf, and that'll indirectly fix this issue too.

What about any other size issues beyond protobuf? For example, the Regex dependency I listed above.

cijothomas commented 1 week ago

Re-opening as there are other things beyond protobuf that needs a revisit. Removing 1.10 milestone as only protobuf is planned to be addressed for 1.10

cijothomas commented 1 week ago

So if that Regex is called often, it will slow down the app compared to a "normal" .NET app.

That particular one is not in hot path. Its only called when ActivitySource is created, not for every Activity creation.

eerhardt commented 1 week ago

That particular one is not in hot path. Its only called when ActivitySource is created, not for every Activity creation.

The size of the app isn't affected whether it is on the hot path or not.

cijothomas commented 1 week ago

That particular one is not in hot path. Its only called when ActivitySource is created, not for every Activity creation.

The size of the app isn't affected whether it is on the hot path or not.

That reply was only a response to the “slow down the app” part, not the size issue.