use std::fmt::Debug;
use std::future::Future;
use std::sync::Arc;
use tracing::Span;
use crate::raw::*;
use crate::*;
pub struct TracingLayer;
impl<A: Access> Layer<A> for TracingLayer {
type LayeredAccess = TracingAccessor<A>;
fn layer(&self, inner: A) -> Self::LayeredAccess {
TracingAccessor { inner }
}
}
#[derive(Debug)]
pub struct TracingAccessor<A> {
inner: A,
}
impl<A: Access> LayeredAccess for TracingAccessor<A> {
type Inner = A;
type Reader = TracingWrapper<A::Reader>;
type Writer = TracingWrapper<A::Writer>;
type Lister = TracingWrapper<A::Lister>;
type Deleter = TracingWrapper<A::Deleter>;
type BlockingReader = TracingWrapper<A::BlockingReader>;
type BlockingWriter = TracingWrapper<A::BlockingWriter>;
type BlockingLister = TracingWrapper<A::BlockingLister>;
type BlockingDeleter = TracingWrapper<A::BlockingDeleter>;
fn inner(&self) -> &Self::Inner {
&self.inner
}
#[tracing::instrument(level = "debug")]
fn info(&self) -> Arc<AccessorInfo> {
self.inner.info()
}
#[tracing::instrument(level = "debug", skip(self))]
async fn create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> {
self.inner.create_dir(path, args).await
}
#[tracing::instrument(level = "debug", skip(self))]
async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
self.inner
.read(path, args)
.await
.map(|(rp, r)| (rp, TracingWrapper::new(Span::current(), r)))
}
#[tracing::instrument(level = "debug", skip(self))]
async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
self.inner
.write(path, args)
.await
.map(|(rp, r)| (rp, TracingWrapper::new(Span::current(), r)))
}
#[tracing::instrument(level = "debug", skip(self))]
async fn copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> {
self.inner().copy(from, to, args).await
}
#[tracing::instrument(level = "debug", skip(self))]
async fn rename(&self, from: &str, to: &str, args: OpRename) -> Result<RpRename> {
self.inner().rename(from, to, args).await
}
#[tracing::instrument(level = "debug", skip(self))]
async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
self.inner.stat(path, args).await
}
#[tracing::instrument(level = "debug", skip(self))]
async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
self.inner
.delete()
.await
.map(|(rp, r)| (rp, TracingWrapper::new(Span::current(), r)))
}
#[tracing::instrument(level = "debug", skip(self))]
async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
self.inner
.list(path, args)
.await
.map(|(rp, s)| (rp, TracingWrapper::new(Span::current(), s)))
}
#[tracing::instrument(level = "debug", skip(self))]
async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
self.inner.presign(path, args).await
}
#[tracing::instrument(level = "debug", skip(self))]
fn blocking_create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> {
self.inner.blocking_create_dir(path, args)
}
#[tracing::instrument(level = "debug", skip(self))]
fn blocking_read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::BlockingReader)> {
self.inner
.blocking_read(path, args)
.map(|(rp, r)| (rp, TracingWrapper::new(Span::current(), r)))
}
#[tracing::instrument(level = "debug", skip(self))]
fn blocking_write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::BlockingWriter)> {
self.inner
.blocking_write(path, args)
.map(|(rp, r)| (rp, TracingWrapper::new(Span::current(), r)))
}
#[tracing::instrument(level = "debug", skip(self))]
fn blocking_copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> {
self.inner().blocking_copy(from, to, args)
}
#[tracing::instrument(level = "debug", skip(self))]
fn blocking_rename(&self, from: &str, to: &str, args: OpRename) -> Result<RpRename> {
self.inner().blocking_rename(from, to, args)
}
#[tracing::instrument(level = "debug", skip(self))]
fn blocking_stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
self.inner.blocking_stat(path, args)
}
#[tracing::instrument(level = "debug", skip(self))]
fn blocking_delete(&self) -> Result<(RpDelete, Self::BlockingDeleter)> {
self.inner
.blocking_delete()
.map(|(rp, r)| (rp, TracingWrapper::new(Span::current(), r)))
}
#[tracing::instrument(level = "debug", skip(self))]
fn blocking_list(&self, path: &str, args: OpList) -> Result<(RpList, Self::BlockingLister)> {
self.inner
.blocking_list(path, args)
.map(|(rp, it)| (rp, TracingWrapper::new(Span::current(), it)))
}
}
pub struct TracingWrapper<R> {
span: Span,
inner: R,
}
impl<R> TracingWrapper<R> {
fn new(span: Span, inner: R) -> Self {
Self { span, inner }
}
}
impl<R: oio::Read> oio::Read for TracingWrapper<R> {
#[tracing::instrument(
parent = &self.span,
level = "trace",
skip_all)]
async fn read(&mut self) -> Result<Buffer> {
self.inner.read().await
}
}
impl<R: oio::BlockingRead> oio::BlockingRead for TracingWrapper<R> {
#[tracing::instrument(
parent = &self.span,
level = "trace",
skip_all)]
fn read(&mut self) -> Result<Buffer> {
self.inner.read()
}
}
impl<R: oio::Write> oio::Write for TracingWrapper<R> {
#[tracing::instrument(
parent = &self.span,
level = "trace",
skip_all)]
fn write(&mut self, bs: Buffer) -> impl Future<Output = Result<()>> + MaybeSend {
self.inner.write(bs)
}
#[tracing::instrument(
parent = &self.span,
level = "trace",
skip_all)]
fn abort(&mut self) -> impl Future<Output = Result<()>> + MaybeSend {
self.inner.abort()
}
#[tracing::instrument(
parent = &self.span,
level = "trace",
skip_all)]
fn close(&mut self) -> impl Future<Output = Result<()>> + MaybeSend {
self.inner.close()
}
}
impl<R: oio::BlockingWrite> oio::BlockingWrite for TracingWrapper<R> {
#[tracing::instrument(
parent = &self.span,
level = "trace",
skip_all)]
fn write(&mut self, bs: Buffer) -> Result<()> {
self.inner.write(bs)
}
#[tracing::instrument(
parent = &self.span,
level = "trace",
skip_all)]
fn close(&mut self) -> Result<()> {
self.inner.close()
}
}
impl<R: oio::List> oio::List for TracingWrapper<R> {
#[tracing::instrument(parent = &self.span, level = "debug", skip_all)]
async fn next(&mut self) -> Result<Option<oio::Entry>> {
self.inner.next().await
}
}
impl<R: oio::BlockingList> oio::BlockingList for TracingWrapper<R> {
#[tracing::instrument(parent = &self.span, level = "debug", skip_all)]
fn next(&mut self) -> Result<Option<oio::Entry>> {
self.inner.next()
}
}
impl<R: oio::Delete> oio::Delete for TracingWrapper<R> {
#[tracing::instrument(parent = &self.span, level = "debug", skip_all)]
fn delete(&mut self, path: &str, args: OpDelete) -> Result<()> {
self.inner.delete(path, args)
}
#[tracing::instrument(parent = &self.span, level = "debug", skip_all)]
async fn flush(&mut self) -> Result<usize> {
self.inner.flush().await
}
}
impl<R: oio::BlockingDelete> oio::BlockingDelete for TracingWrapper<R> {
#[tracing::instrument(parent = &self.span, level = "debug", skip_all)]
fn delete(&mut self, path: &str, args: OpDelete) -> Result<()> {
self.inner.delete(path, args)
}
#[tracing::instrument(parent = &self.span, level = "debug", skip_all)]
fn flush(&mut self) -> Result<usize> {
self.inner.flush()
}
}