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

feat: add async backtrace layer #2765

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 61 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ layers-all = [
"layers-minitrace",
"layers-throttle",
"layers-await-tree",
"layers-async-backtrace",
dqhl76 marked this conversation as resolved.
Show resolved Hide resolved
]
# Enable layers chaos support
layers-chaos = ["dep:rand"]
Expand All @@ -96,6 +97,8 @@ layers-otel-trace = ["dep:opentelemetry"]
layers-throttle = ["dep:governor"]
# Enable layers await-tree support.
layers-await-tree = ["dep:await-tree"]
# Enable layers async-backtrace support.
layers-async-backtrace = ["dep:async-backtrace"]

services-azblob = [
"dep:sha2",
Expand Down Expand Up @@ -183,6 +186,7 @@ path = "tests/behavior/main.rs"

[dependencies]
anyhow = { version = "1.0.30", features = ["std"] }
async-backtrace = { version = "0.2.6", optional = true }
async-compat = "0.2"
async-tls = { version = "0.11", optional = true }
async-trait = "0.1.68"
Expand Down
137 changes: 137 additions & 0 deletions core/src/layers/async_backtrace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use crate::raw::*;
use crate::*;

use async_trait::async_trait;

/// Add Efficient, logical 'stack' traces of async functions for the underlying services.
///
/// # Async Backtrace
///
/// async-backtrace allows developers to get a stack trace of the async functions.
/// Read more about [async-backtrace](https://docs.rs/async-backtrace/latest/async_backtrace/)
///
/// # Examples
///
/// ```
/// use anyhow::Result;
/// use opendal::layers::AsyncBacktraceLayer;
/// use opendal::services;
/// use opendal::Operator;
/// use opendal::Scheme;
///
/// let _ = Operator::new(services::Memory::default())
/// .expect("must init")
/// .layer(AsyncBacktraceLayer::new())
/// .finish();
/// ```

#[derive(Clone, Default)]
pub struct AsyncBacktraceLayer;

impl<A: Accessor> Layer<A> for AsyncBacktraceLayer {
type LayeredAccessor = AsyncBacktraceAccessor<A>;

fn layer(&self, accessor: A) -> Self::LayeredAccessor {
AsyncBacktraceAccessor { inner: accessor }
}
}

#[derive(Debug, Clone)]
pub struct AsyncBacktraceAccessor<A: Accessor> {
inner: A,
}

#[async_trait]
impl<A: Accessor> LayeredAccessor for AsyncBacktraceAccessor<A> {
type Inner = A;
type Reader = A::Reader;
type BlockingReader = A::BlockingReader;
type Writer = A::Writer;
type BlockingWriter = A::BlockingWriter;
type Appender = A::Appender;
type Pager = A::Pager;
type BlockingPager = A::BlockingPager;

fn inner(&self) -> &Self::Inner {
&self.inner
}

#[async_backtrace::framed]
async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
self.inner.read(path, args).await
}

#[async_backtrace::framed]
async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
self.inner.write(path, args).await
}

#[async_backtrace::framed]
async fn append(&self, path: &str, args: OpAppend) -> Result<(RpAppend, Self::Appender)> {
self.inner.append(path, args).await
}

#[async_backtrace::framed]
async fn copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> {
self.inner.copy(from, to, args).await
}

#[async_backtrace::framed]
async fn rename(&self, from: &str, to: &str, args: OpRename) -> Result<RpRename> {
self.inner.rename(from, to, args).await
}

#[async_backtrace::framed]
async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
self.inner.stat(path, args).await
}

#[async_backtrace::framed]
async fn delete(&self, path: &str, args: OpDelete) -> Result<RpDelete> {
self.inner.delete(path, args).await
}

#[async_backtrace::framed]
async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Pager)> {
self.inner.list(path, args).await
}

#[async_backtrace::framed]
async fn batch(&self, args: OpBatch) -> Result<RpBatch> {
self.inner.batch(args).await
}

#[async_backtrace::framed]
async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
self.inner.presign(path, args).await
}

fn blocking_read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::BlockingReader)> {
self.inner.blocking_read(path, args)
}

fn blocking_write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::BlockingWriter)> {
self.inner.blocking_write(path, args)
}

fn blocking_list(&self, path: &str, args: OpList) -> Result<(RpList, Self::BlockingPager)> {
self.inner.blocking_list(path, args)
}
}
7 changes: 7 additions & 0 deletions core/src/layers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,14 @@ mod throttle;
pub use self::oteltrace::OtelTraceLayer;
#[cfg(feature = "layers-throttle")]
pub use self::throttle::ThrottleLayer;

#[cfg(feature = "layers-await-tree")]
mod await_tree;
#[cfg(feature = "layers-await-tree")]
pub use self::await_tree::AwaitTreeLayer;

#[cfg(feature = "layers-async-backtrace")]
mod async_backtrace;

#[cfg(feature = "layers-async-backtrace")]
pub use self::async_backtrace::AsyncBacktraceLayer;