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

Use native Rust support for async traits in LogExporter::export() method #2374

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

lalitb
Copy link
Member

@lalitb lalitb commented Dec 2, 2024

Changes

Continuation to #2143, more to show feasibility, performance improvements, and have discussion before raising the PR for eventual review, this PR demonstrate using async traits support in Rust exporter. The support was added in Rust v1.75, while the msrv as of now is v1.70 for otel-sdk

The change is:
Existing:

#[async_trait]
pub trait LogExporter: Send + Sync + Debug {
  async fn export(&mut self, batch: LogBatch<'_>) -> LogResult<()>;
}

PR:

pub trait LogExporter: Send + Sync + Debug {
    fn export<'a>(
        &'a mut self,
        batch: &'a LogBatch<'a>,
    ) -> impl std::future::Future<Output = LogResult<()>> + Send + 'a;
}

Have modified the stress test to use LogExporter to demonstrate the improvement:
main with modified stress test:
main...lalitb:opentelemetry-rust:main-stress-with-exporter
6,348,766 iterations/sec
5,725,142 iterations/sec
6,020,324 iterations/sec
5,814,542 iterations/sec

this PR with modified stress test:
9,550,832 iterations/sec
11,045,635 iterations/sec
10,632,124 iterations/sec
10,212,681 iterations/sec
9,810,356 iterations/sec

There are improvements of ~71%, but it need to bump msrv to 1.75. Also, most of the improvement won't be the periodic

Thanks,
Lalit

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

@lalitb lalitb requested a review from a team as a code owner December 2, 2024 22:34
@@ -82,7 +80,11 @@ pub trait LogExporter: Send + Sync + Debug {
/// A `LogResult<()>`, which is a result type indicating either a successful export (with
/// `Ok(())`) or an error (`Err(LogError)`) if the export operation failed.
///
async fn export(&mut self, batch: LogBatch<'_>) -> LogResult<()>;
fn export<'a>(
&'a mut self,
Copy link
Member

Choose a reason for hiding this comment

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

@lalitb I think it maybe better to first check how to avoid requiring mut reference for exporting, before evaluating the results of this perf test, as that requirement has forced the need of mutex and introduced contention and hence stress tests shows way low throughput than before.

Copy link
Member Author

@lalitb lalitb Dec 3, 2024

Choose a reason for hiding this comment

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

@cijothomas If we remove the mut self-reference from the LogExporter::export() method, it will force the exporter implementation to rely on interior mutability to modify its internal state. This would involve using synchronization mechanisms like locks or atomic operations, which add complexity and overhead. But in the case of SimpleLogProcessor and BatchLogProcessor, these methods are always called sequentially, so there’s no real benefit to introducing this extra layer of synchronization. It would just make the code more complicated and less performant without actually solving any problem.

The use-case for having a non-mutating export method would be in reentrant log processors, where LogProcessor::emit() -> LogProcessor::export() could be invoked concurrently from multiple user threads. In such scenarios, interior mutability would be necessary if the exporter implementation needs to manage internal state. However, this is not relevant for the current simple and batch processors, which operate in a strictly sequential manner.

Actually, there are 4 different exporter scenarios to think about -

  • sync/concurrent:
    fn export<'a>(&'a self, batch: &'a LogBatch<'a>) -> LogResult<()>;
  • sync/sequential,
   fn export<'a>(&'a mut self, batch: &'a LogBatch<'a>) -> LogResult<()>;
  • async/concurrent
  fn export<'a>(&'a self, batch: &'a LogBatch<'a>) -> impl std::future::Future<Output = LogResult<()>> + Send + 'a;
  • async/sequential
 fn export<'a>(&'a mut self, batch: &'a LogBatch<'a>) -> impl std::future::Future<Output = LogResult<()>> + Send + 'a;

Currently:

  • The SimpleLogProcessor and BatchLogProcessor invoke the exporter in async/sequential mode.
  • The ReentrantProcessor, used by ETW and user_events in the contrib repository, operates the exporter in sync/concurrent mode.

To ensure clarity and extensibility, we need to properly model these scenarios within the design.

Copy link
Member Author

@lalitb lalitb Dec 3, 2024

Choose a reason for hiding this comment

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

Just to add, we don't currently have specific use-case for sync/sequential, and async/concurrent, they are mentioned above for completeness of all the scenarios.

@@ -247,11 +247,16 @@ mod tests {

#[async_trait]
Copy link
Member

Choose a reason for hiding this comment

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

we don't need async-trait anymore?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, this can be safely removed.

Copy link
Member

Choose a reason for hiding this comment

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

unresolving to remove async_trait

Copy link

codecov bot commented Dec 6, 2024

Codecov Report

Attention: Patch coverage is 28.26087% with 132 lines in your changes missing coverage. Please review.

Project coverage is 79.3%. Comparing base (c726c4d) to head (8c34380).

Files with missing lines Patch % Lines
opentelemetry-otlp/src/exporter/http/logs.rs 0.0% 36 Missing ⚠️
opentelemetry-otlp/src/exporter/tonic/logs.rs 0.0% 28 Missing ⚠️
opentelemetry-stdout/src/logs/exporter.rs 0.0% 25 Missing ⚠️
opentelemetry-otlp/src/logs.rs 0.0% 19 Missing ⚠️
opentelemetry-sdk/src/logs/log_processor.rs 76.0% 11 Missing ⚠️
opentelemetry-appender-tracing/src/layer.rs 0.0% 10 Missing ⚠️
opentelemetry-otlp/src/exporter/http/mod.rs 0.0% 2 Missing ⚠️
opentelemetry-otlp/src/exporter/tonic/mod.rs 0.0% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #2374     +/-   ##
=======================================
- Coverage   79.4%   79.3%   -0.2%     
=======================================
  Files        122     122             
  Lines      21708   21736     +28     
=======================================
- Hits       17255   17250      -5     
- Misses      4453    4486     +33     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lalitb
Copy link
Member Author

lalitb commented Dec 7, 2024

@lalitb I think it maybe better to first check how to avoid requiring mut reference for exporting, before evaluating the results of this perf test, as that requirement has forced the need of mutex and introduced contention and hence stress tests shows way low throughput than before.

OK, have updated this PR after removing the mut self in #2380. This eliminates the need of mutex for exporter in stress test. The stress result are now similar to earlier i.e., without the inclusion of exporter. Which means, adding futures::block_on() on native async-method doesn't introduce much of latency.

Before this PR (the regression introduced by #2380 because of inclusion of exporter ~44 -> ~40):

Number of threads: 16
Throughput: 40,468,186 iterations/sec
Throughput: 40,792,480 iterations/sec
Throughput: 40,456,227 iterations/sec
Throughput: 40,455,461 iterations/sec

After this PR:

Throughput: 44,793,174 iterations/sec
Throughput: 45,321,168 iterations/sec
Throughput: 45,387,969 iterations/sec
Throughput: 45,629,648 iterations/sec
Throughput: 45,607,326 iterations/sec
Throughput: 45,303,331 iterations/sec
Throughput: 45,634,108 iterations/sec

The cons of introducing the native-async in trait would be

  • Bump the msrv to 1.75
  • The exporter interface is no longer object-safe. Which means, it can't be used in dynamic dispatch (e.g., via dyn Trait). This introduced slight complexity in otel-otlp, where dyn dispatch was used for http and tonic exporters.

Current export method could be simplified a bit for lifetime specifiers. I will look into that. But in general, open for discussion if we should bump the msrv.

@cijothomas cijothomas added this to the 0.28.0 milestone Dec 10, 2024
@cijothomas
Copy link
Member

Bump the msrv to 1.75
The exporter interface is no longer object-safe. Which means, it can't be used in dynamic dispatch (e.g., via dyn Trait). This introduced slight complexity in otel-otlp, where dyn dispatch was used for http and tonic exporters.

Based on today's SIG discussion:
1.75 bump is acceptable.
the exporter interface not being object safe is an easy to fix issue as this PR already demonstrates fixing it for OTLP.

Awaiting more feedback.

@lalitb lalitb mentioned this pull request Dec 12, 2024
4 tasks
@lalitb lalitb changed the title Draft to Discuss: Use native Rust support for async traits in LogExporter::export() method Use native Rust support for async traits in LogExporter::export() method Dec 13, 2024
@lalitb
Copy link
Member Author

lalitb commented Dec 13, 2024

This is ready for review now, with #2417 to bump msrv is merged

@@ -34,8 +34,12 @@ struct NoopExporter {

#[async_trait]
impl LogExporter for NoopExporter {
async fn export(&self, _: LogBatch<'_>) -> LogResult<()> {
LogResult::Ok(())
#[allow(clippy::manual_async_fn)]
Copy link
Member Author

@lalitb lalitb Dec 13, 2024

Choose a reason for hiding this comment

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

This annotation is required for false positive clippy warning - rust-lang/rust-clippy#12664

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe have it on the project level until #12664 is addressed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Adding #[allow(clippy::manual_async_fn)] at the package level in Cargo.toml is not supported—it only works as an attribute in Rust source files. To suppress this lint effectively, it needs to be added in lib.rs or mod.rs for the main crate and separately for benches (and likely tests as well). It seems, adding at method level seems cleaner as of now. We can revisit this if similar implementation is done for Spans, and the issue is not fixed by then.

@lalitb lalitb added the integration tests Run integration tests label Dec 13, 2024
@@ -58,35 +58,40 @@ impl TonicLogsClient {

#[async_trait]
Copy link
Member

Choose a reason for hiding this comment

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

we can remove async_trait from everywhere now.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is done, thanks.

@@ -108,7 +108,16 @@ impl HasHttpConfig for LogExporterBuilder<HttpExporterBuilderSet> {
/// OTLP exporter that sends log data
#[derive(Debug)]
pub struct LogExporter {
client: Box<dyn opentelemetry_sdk::export::logs::LogExporter>,
//client: Box<dyn opentelemetry_sdk::export::logs::LogExporter>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can remove this now

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, removed. thanks

}

#[derive(Debug)]
enum LogExporterInner {
Copy link
Contributor

Choose a reason for hiding this comment

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

The name can be better IMO. Maybe SupportedTransportClient. Inner just don't convey this is a enum clearly

Copy link
Contributor

Choose a reason for hiding this comment

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

Not a blocking issue though

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. Have renamed it to SupportedTransportClient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration tests Run integration tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants