Description
Noticed while stress testing Logs, but should be applicable to Tracing as well, as they follow the same pattern, where Logger (Tracer), holds a Weak
ref to LoggerProvider (TracerProvider), and in hoth path (logger.EmitLog, span.Record), the weak reference to provider is upgraded to Arc, to obtain things like processor list, resource etc. from the provider. This weak -> Arc upgrade seems to be the bottleneck.
Here's how I tested:
- Ran the stress test for logs, in my box, and it shows ~7M/sec throughput.
- Modified the NoOpLogProcessor in the above test to return
false
, which should make the throughput skyrocket as we don't have to do anything like creating LogRecord or invoke processors etc. This did increase the throughput to ~35M/sec, but I was expecting 200+M/sec, as I get similar throughout when using tracing+no-op-tracing-subscriber, without any OpenTelemetry component. - After some manual explorations, found that the upgrading of weak to Arc in the hot path of checking if event is enabled is the bottleneck.
- I refactored the code so that Logger is holding an Arc instead of Weak (draft code ), so there is no longer the need for upgrading to Arc in hotpath. With this change, the throughput shot to ~250M/sec (in line with what was seen with using tracing+no-op subscriber alone).
The weak ref to provider was introduced for Tracing (and then followed for Logging), in this PR. Using Arc does mean that the Provider won't be dropped and shutdown won't be signaled until the last Logger is dropped, but that seems okay to me. In the case of Logging, Logger's are held only by the appenders (tokio-tracing subscribers etc), which, when dropped, will drop their Logger as well, allowing provider to be dropped....
I do not know if this has any further implications, especially for Tracer/Span case.
The (perf) issue is applicable to Span as well as this upgrade occurs (I think) twice - when span begins and when span ends...
Opening this issue to get feedback on this from experts and to check if this draft code is a reasonable direction to further explore.