Skip to main content

opendal_layer_fastrace/
lib.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#![doc = include_str!("../README.md")]
19#![cfg_attr(docsrs, feature(doc_cfg))]
20#![cfg_attr(docsrs, doc(auto_cfg))]
21#![deny(missing_docs)]
22use std::future::Future;
23use std::sync::Arc;
24
25use fastrace::prelude::*;
26use opendal_core::raw::*;
27use opendal_core::*;
28
29/// `FastraceLayer` traces every operation with
30/// [fastrace](https://docs.rs/fastrace/).
31///
32/// It creates spans for service calls and deferred operation bodies
33/// such as readers, writers, listers, deleters, and copiers.
34///
35/// # Examples
36///
37/// ## Basic Setup
38///
39/// ```no_run
40/// # use opendal_core::services;
41/// # use opendal_core::Operator;
42/// # use opendal_core::Result;
43/// # use opendal_layer_fastrace::FastraceLayer;
44/// #
45/// # fn main() -> Result<()> {
46/// let _ = Operator::new(services::Memory::default())?
47///     .layer(FastraceLayer::new());
48/// # Ok(())
49/// # }
50/// ```
51///
52/// ## Real usage
53///
54/// ```no_run
55/// # use anyhow::Result;
56/// # use fastrace::prelude::*;
57/// # use opendal_core::services;
58/// # use opendal_core::Operator;
59/// # use opendal_layer_fastrace::FastraceLayer;
60/// #
61/// # fn main() -> Result<()> {
62/// let reporter = fastrace_jaeger::JaegerReporter::new("127.0.0.1:6831".parse()?, "opendal").unwrap();
63/// fastrace::set_reporter(reporter, fastrace::collector::Config::default());
64///
65/// {
66///     let root = Span::root("op", SpanContext::random());
67///     let runtime = tokio::runtime::Runtime::new()?;
68///     runtime.block_on(
69///         async {
70///             let _ = dotenvy::dotenv();
71///             let op = Operator::new(services::Memory::default())?
72///                 .layer(FastraceLayer::new());
73///             op.write("test", "0".repeat(16 * 1024 * 1024).into_bytes())
74///                 .await?;
75///             op.stat("test").await?;
76///             op.read("test").await?;
77///             Ok::<(), opendal_core::Error>(())
78///         }
79///         .in_span(Span::enter_with_parent("test", &root)),
80///     )?;
81/// }
82///
83/// fastrace::flush();
84/// # Ok(())
85/// # }
86/// ```
87///
88/// # Output
89///
90/// OpenDAL is using [`fastrace`](https://docs.rs/fastrace/latest/fastrace/) for tracing internally.
91///
92/// To enable fastrace output, initialize a reporter supported by `fastrace`.
93///
94/// For example:
95///
96/// ```no_run
97/// # use anyhow::Result;
98/// #
99/// # fn main() -> Result<()> {
100/// let reporter = fastrace_jaeger::JaegerReporter::new("127.0.0.1:6831".parse()?, "opendal").unwrap();
101/// fastrace::set_reporter(reporter, fastrace::collector::Config::default());
102/// # Ok(())
103/// # }
104/// ```
105///
106/// For real-world usage, take a look at [`fastrace-datadog`](https://crates.io/crates/fastrace-datadog) or [`fastrace-jaeger`](https://crates.io/crates/fastrace-jaeger).
107#[derive(Clone, Debug, Default)]
108#[non_exhaustive]
109pub struct FastraceLayer {}
110
111impl FastraceLayer {
112    /// Create a new [`FastraceLayer`].
113    pub fn new() -> Self {
114        Self::default()
115    }
116}
117
118impl Layer for FastraceLayer {
119    fn apply_service(&self, inner: Servicer) -> Servicer {
120        Arc::new(self.layer(inner))
121    }
122}
123
124impl FastraceLayer {
125    fn layer(&self, inner: Servicer) -> FastraceAccessor {
126        FastraceAccessor { inner }
127    }
128}
129
130#[doc(hidden)]
131#[derive(Debug)]
132pub struct FastraceAccessor {
133    inner: Servicer,
134}
135
136impl Service for FastraceAccessor {
137    // Operations with returned bodies continue after the service call returns,
138    // so wrap those bodies to trace deferred IO as well.
139    type Reader = FastraceWrapper<oio::Reader>;
140    type Writer = FastraceWrapper<oio::Writer>;
141    type Lister = FastraceWrapper<oio::Lister>;
142    type Deleter = FastraceWrapper<oio::Deleter>;
143    type Copier = FastraceWrapper<oio::Copier>;
144    type Composer = oio::Composer;
145
146    fn info(&self) -> ServiceInfo {
147        self.inner.info()
148    }
149
150    fn capability(&self) -> Capability {
151        self.inner.capability()
152    }
153
154    fn compose(&self, ctx: &OperationContext, to: &str, args: OpCompose) -> Result<Self::Composer> {
155        self.inner.compose(ctx, to, args)
156    }
157
158    async fn create_dir(
159        &self,
160        ctx: &OperationContext,
161        path: &str,
162        args: OpCreateDir,
163    ) -> Result<RpCreateDir> {
164        let _guard = Span::enter_with_local_parent(Operation::CreateDir.into_static());
165        self.inner.create_dir(ctx, path, args).await
166    }
167
168    fn read(&self, ctx: &OperationContext, path: &str, args: OpRead) -> Result<Self::Reader> {
169        let _guard = Span::enter_with_local_parent(Operation::Read.into_static());
170        self.inner.read(ctx, path, args).map(|r| {
171            FastraceWrapper::new(
172                Span::enter_with_local_parent(Operation::Read.into_static()),
173                r,
174            )
175        })
176    }
177
178    fn write(&self, ctx: &OperationContext, path: &str, args: OpWrite) -> Result<Self::Writer> {
179        let _guard = Span::enter_with_local_parent(Operation::Write.into_static());
180        self.inner.write(ctx, path, args).map(|r| {
181            FastraceWrapper::new(
182                Span::enter_with_local_parent(Operation::Write.into_static()),
183                r,
184            )
185        })
186    }
187
188    fn copy(
189        &self,
190        ctx: &OperationContext,
191        from: &str,
192        to: &str,
193        args: OpCopy,
194    ) -> Result<Self::Copier> {
195        let _guard = Span::enter_with_local_parent(Operation::Copy.into_static());
196        self.inner.copy(ctx, from, to, args).map(|c| {
197            FastraceWrapper::new(
198                Span::enter_with_local_parent(Operation::Copy.into_static()),
199                c,
200            )
201        })
202    }
203
204    async fn rename(
205        &self,
206        ctx: &OperationContext,
207        from: &str,
208        to: &str,
209        args: OpRename,
210    ) -> Result<RpRename> {
211        let _guard = Span::enter_with_local_parent(Operation::Rename.into_static());
212        self.inner.rename(ctx, from, to, args).await
213    }
214
215    async fn restore(
216        &self,
217        ctx: &OperationContext,
218        path: &str,
219        args: OpRestore,
220    ) -> Result<RpRestore> {
221        let _guard = Span::enter_with_local_parent(Operation::Restore.into_static());
222        self.inner.restore(ctx, path, args).await
223    }
224
225    async fn stat(&self, ctx: &OperationContext, path: &str, args: OpStat) -> Result<RpStat> {
226        let _guard = Span::enter_with_local_parent(Operation::Stat.into_static());
227        self.inner.stat(ctx, path, args).await
228    }
229
230    fn delete(&self, ctx: &OperationContext) -> Result<Self::Deleter> {
231        let _guard = Span::enter_with_local_parent(Operation::Delete.into_static());
232        self.inner.delete(ctx).map(|r| {
233            FastraceWrapper::new(
234                Span::enter_with_local_parent(Operation::Delete.into_static()),
235                r,
236            )
237        })
238    }
239
240    fn list(&self, ctx: &OperationContext, path: &str, args: OpList) -> Result<Self::Lister> {
241        let _guard = Span::enter_with_local_parent(Operation::List.into_static());
242        self.inner.list(ctx, path, args).map(|s| {
243            FastraceWrapper::new(
244                Span::enter_with_local_parent(Operation::List.into_static()),
245                s,
246            )
247        })
248    }
249
250    async fn presign(
251        &self,
252        ctx: &OperationContext,
253        path: &str,
254        args: OpPresign,
255    ) -> Result<RpPresign> {
256        let _guard = Span::enter_with_local_parent(Operation::Presign.into_static());
257        self.inner.presign(ctx, path, args).await
258    }
259}
260
261#[doc(hidden)]
262// Keep the operation span with the returned body so later body methods can
263// attach child spans without relying on local span state.
264pub struct FastraceWrapper<R> {
265    span: Arc<Span>,
266    inner: R,
267}
268
269impl<R> FastraceWrapper<R> {
270    fn new(span: Span, inner: R) -> Self {
271        Self {
272            span: Arc::new(span),
273            inner,
274        }
275    }
276
277    fn with_span(span: Arc<Span>, inner: R) -> Self {
278        Self { span, inner }
279    }
280}
281
282impl<R: oio::ReadStream> oio::ReadStream for FastraceWrapper<R> {
283    fn read(&mut self) -> impl Future<Output = Result<Buffer>> + MaybeSend {
284        let _guard = self.span.set_local_parent();
285        let _span = LocalSpan::enter_with_local_parent(Operation::Read.into_static());
286        self.inner.read()
287    }
288}
289
290impl<R: oio::Read> oio::Read for FastraceWrapper<R> {
291    fn open(
292        &self,
293        range: BytesRange,
294    ) -> impl Future<Output = Result<(RpRead, Box<dyn oio::ReadStreamDyn>)>> + MaybeSend {
295        let _guard = self.span.set_local_parent();
296        let span = self.span.clone();
297        let fut = self.inner.open(range);
298        async move {
299            let (rp, stream) = fut.await?;
300            Ok((
301                rp,
302                Box::new(FastraceWrapper::with_span(span, stream)) as Box<dyn oio::ReadStreamDyn>,
303            ))
304        }
305    }
306
307    fn read(
308        &self,
309        range: BytesRange,
310    ) -> impl Future<Output = Result<(RpRead, Buffer)>> + MaybeSend {
311        let _guard = self.span.set_local_parent();
312        let _span = LocalSpan::enter_with_local_parent(Operation::Read.into_static());
313        self.inner.read(range)
314    }
315}
316
317impl<R: oio::Write> oio::Write for FastraceWrapper<R> {
318    fn write(&mut self, bs: Buffer) -> impl Future<Output = Result<()>> + MaybeSend {
319        let _guard = self.span.set_local_parent();
320        let _span = LocalSpan::enter_with_local_parent(Operation::Write.into_static());
321        self.inner.write(bs)
322    }
323
324    fn copy_from(
325        &mut self,
326        path: &str,
327        args: OpRead,
328        range: BytesRange,
329    ) -> impl Future<Output = Result<()>> + MaybeSend {
330        let _guard = self.span.set_local_parent();
331        let _span = LocalSpan::enter_with_local_parent(Operation::Write.into_static());
332        let path = path.to_string();
333        async move { self.inner.copy_from(&path, args, range).await }
334    }
335
336    fn abort(&mut self) -> impl Future<Output = Result<()>> + MaybeSend {
337        let _guard = self.span.set_local_parent();
338        let _span = LocalSpan::enter_with_local_parent(Operation::Write.into_static());
339        self.inner.abort()
340    }
341
342    fn close(&mut self) -> impl Future<Output = Result<Metadata>> + MaybeSend {
343        let _guard = self.span.set_local_parent();
344        let _span = LocalSpan::enter_with_local_parent(Operation::Write.into_static());
345        self.inner.close()
346    }
347}
348
349impl<R: oio::List> oio::List for FastraceWrapper<R> {
350    fn next(&mut self) -> impl Future<Output = Result<Option<oio::Entry>>> + MaybeSend {
351        let _guard = self.span.set_local_parent();
352        let _span = LocalSpan::enter_with_local_parent(Operation::List.into_static());
353        self.inner.next()
354    }
355}
356
357impl<R: oio::Delete> oio::Delete for FastraceWrapper<R> {
358    fn delete<'a>(
359        &'a mut self,
360        path: &'a str,
361        args: OpDelete,
362    ) -> impl Future<Output = Result<()>> + MaybeSend + 'a {
363        let _guard = self.span.set_local_parent();
364        let _span = LocalSpan::enter_with_local_parent(Operation::Delete.into_static());
365        self.inner.delete(path, args)
366    }
367
368    fn close(&mut self) -> impl Future<Output = Result<()>> + MaybeSend {
369        let _guard = self.span.set_local_parent();
370        let _span = LocalSpan::enter_with_local_parent(Operation::Delete.into_static());
371        self.inner.close()
372    }
373}
374
375impl<C: oio::Copy> oio::Copy for FastraceWrapper<C> {
376    fn next(&mut self) -> impl Future<Output = Result<Option<usize>>> + MaybeSend {
377        let _guard = self.span.set_local_parent();
378        let _span = LocalSpan::enter_with_local_parent(Operation::Copy.into_static());
379        self.inner.next()
380    }
381
382    fn close(&mut self) -> impl Future<Output = Result<Metadata>> + MaybeSend {
383        let _guard = self.span.set_local_parent();
384        let _span = LocalSpan::enter_with_local_parent(Operation::Copy.into_static());
385        self.inner.close()
386    }
387
388    fn abort(&mut self) -> impl Future<Output = Result<()>> + MaybeSend {
389        let _guard = self.span.set_local_parent();
390        let _span = LocalSpan::enter_with_local_parent(Operation::Copy.into_static());
391        self.inner.abort()
392    }
393}