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
145    fn info(&self) -> ServiceInfo {
146        self.inner.info()
147    }
148
149    fn capability(&self) -> Capability {
150        self.inner.capability()
151    }
152
153    async fn create_dir(
154        &self,
155        ctx: &OperationContext,
156        path: &str,
157        args: OpCreateDir,
158    ) -> Result<RpCreateDir> {
159        let _guard = Span::enter_with_local_parent(Operation::CreateDir.into_static());
160        self.inner.create_dir(ctx, path, args).await
161    }
162
163    fn read(&self, ctx: &OperationContext, path: &str, args: OpRead) -> Result<Self::Reader> {
164        let _guard = Span::enter_with_local_parent(Operation::Read.into_static());
165        self.inner.read(ctx, path, args).map(|r| {
166            FastraceWrapper::new(
167                Span::enter_with_local_parent(Operation::Read.into_static()),
168                r,
169            )
170        })
171    }
172
173    fn write(&self, ctx: &OperationContext, path: &str, args: OpWrite) -> Result<Self::Writer> {
174        let _guard = Span::enter_with_local_parent(Operation::Write.into_static());
175        self.inner.write(ctx, path, args).map(|r| {
176            FastraceWrapper::new(
177                Span::enter_with_local_parent(Operation::Write.into_static()),
178                r,
179            )
180        })
181    }
182
183    fn copy(
184        &self,
185        ctx: &OperationContext,
186        from: &str,
187        to: &str,
188        args: OpCopy,
189        opts: OpCopier,
190    ) -> Result<Self::Copier> {
191        let _guard = Span::enter_with_local_parent(Operation::Copy.into_static());
192        self.inner.copy(ctx, from, to, args, opts).map(|c| {
193            FastraceWrapper::new(
194                Span::enter_with_local_parent(Operation::Copy.into_static()),
195                c,
196            )
197        })
198    }
199
200    async fn rename(
201        &self,
202        ctx: &OperationContext,
203        from: &str,
204        to: &str,
205        args: OpRename,
206    ) -> Result<RpRename> {
207        let _guard = Span::enter_with_local_parent(Operation::Rename.into_static());
208        self.inner.rename(ctx, from, to, args).await
209    }
210
211    async fn stat(&self, ctx: &OperationContext, path: &str, args: OpStat) -> Result<RpStat> {
212        let _guard = Span::enter_with_local_parent(Operation::Stat.into_static());
213        self.inner.stat(ctx, path, args).await
214    }
215
216    fn delete(&self, ctx: &OperationContext) -> Result<Self::Deleter> {
217        let _guard = Span::enter_with_local_parent(Operation::Delete.into_static());
218        self.inner.delete(ctx).map(|r| {
219            FastraceWrapper::new(
220                Span::enter_with_local_parent(Operation::Delete.into_static()),
221                r,
222            )
223        })
224    }
225
226    fn list(&self, ctx: &OperationContext, path: &str, args: OpList) -> Result<Self::Lister> {
227        let _guard = Span::enter_with_local_parent(Operation::List.into_static());
228        self.inner.list(ctx, path, args).map(|s| {
229            FastraceWrapper::new(
230                Span::enter_with_local_parent(Operation::List.into_static()),
231                s,
232            )
233        })
234    }
235
236    async fn presign(
237        &self,
238        ctx: &OperationContext,
239        path: &str,
240        args: OpPresign,
241    ) -> Result<RpPresign> {
242        let _guard = Span::enter_with_local_parent(Operation::Presign.into_static());
243        self.inner.presign(ctx, path, args).await
244    }
245}
246
247#[doc(hidden)]
248// Keep the operation span with the returned body so later body methods can
249// attach child spans without relying on local span state.
250pub struct FastraceWrapper<R> {
251    span: Arc<Span>,
252    inner: R,
253}
254
255impl<R> FastraceWrapper<R> {
256    fn new(span: Span, inner: R) -> Self {
257        Self {
258            span: Arc::new(span),
259            inner,
260        }
261    }
262
263    fn with_span(span: Arc<Span>, inner: R) -> Self {
264        Self { span, inner }
265    }
266}
267
268impl<R: oio::ReadStream> oio::ReadStream for FastraceWrapper<R> {
269    fn read(&mut self) -> impl Future<Output = Result<Buffer>> + MaybeSend {
270        let _guard = self.span.set_local_parent();
271        let _span = LocalSpan::enter_with_local_parent(Operation::Read.into_static());
272        self.inner.read()
273    }
274}
275
276impl<R: oio::Read> oio::Read for FastraceWrapper<R> {
277    fn open(
278        &self,
279        range: BytesRange,
280    ) -> impl Future<Output = Result<(RpRead, Box<dyn oio::ReadStreamDyn>)>> + MaybeSend {
281        let _guard = self.span.set_local_parent();
282        let span = self.span.clone();
283        let fut = self.inner.open(range);
284        async move {
285            let (rp, stream) = fut.await?;
286            Ok((
287                rp,
288                Box::new(FastraceWrapper::with_span(span, stream)) as Box<dyn oio::ReadStreamDyn>,
289            ))
290        }
291    }
292
293    fn read(
294        &self,
295        range: BytesRange,
296    ) -> impl Future<Output = Result<(RpRead, Buffer)>> + MaybeSend {
297        let _guard = self.span.set_local_parent();
298        let _span = LocalSpan::enter_with_local_parent(Operation::Read.into_static());
299        self.inner.read(range)
300    }
301}
302
303impl<R: oio::Write> oio::Write for FastraceWrapper<R> {
304    fn write(&mut self, bs: Buffer) -> impl Future<Output = Result<()>> + MaybeSend {
305        let _guard = self.span.set_local_parent();
306        let _span = LocalSpan::enter_with_local_parent(Operation::Write.into_static());
307        self.inner.write(bs)
308    }
309
310    fn abort(&mut self) -> impl Future<Output = Result<()>> + MaybeSend {
311        let _guard = self.span.set_local_parent();
312        let _span = LocalSpan::enter_with_local_parent(Operation::Write.into_static());
313        self.inner.abort()
314    }
315
316    fn close(&mut self) -> impl Future<Output = Result<Metadata>> + MaybeSend {
317        let _guard = self.span.set_local_parent();
318        let _span = LocalSpan::enter_with_local_parent(Operation::Write.into_static());
319        self.inner.close()
320    }
321}
322
323impl<R: oio::List> oio::List for FastraceWrapper<R> {
324    fn next(&mut self) -> impl Future<Output = Result<Option<oio::Entry>>> + MaybeSend {
325        let _guard = self.span.set_local_parent();
326        let _span = LocalSpan::enter_with_local_parent(Operation::List.into_static());
327        self.inner.next()
328    }
329}
330
331impl<R: oio::Delete> oio::Delete for FastraceWrapper<R> {
332    fn delete<'a>(
333        &'a mut self,
334        path: &'a str,
335        args: OpDelete,
336    ) -> impl Future<Output = Result<()>> + MaybeSend + 'a {
337        let _guard = self.span.set_local_parent();
338        let _span = LocalSpan::enter_with_local_parent(Operation::Delete.into_static());
339        self.inner.delete(path, args)
340    }
341
342    fn close(&mut self) -> impl Future<Output = Result<()>> + MaybeSend {
343        let _guard = self.span.set_local_parent();
344        let _span = LocalSpan::enter_with_local_parent(Operation::Delete.into_static());
345        self.inner.close()
346    }
347}
348
349impl<C: oio::Copy> oio::Copy for FastraceWrapper<C> {
350    fn next(&mut self) -> impl Future<Output = Result<Option<usize>>> + MaybeSend {
351        let _guard = self.span.set_local_parent();
352        let _span = LocalSpan::enter_with_local_parent(Operation::Copy.into_static());
353        self.inner.next()
354    }
355
356    fn close(&mut self) -> impl Future<Output = Result<Metadata>> + MaybeSend {
357        let _guard = self.span.set_local_parent();
358        let _span = LocalSpan::enter_with_local_parent(Operation::Copy.into_static());
359        self.inner.close()
360    }
361
362    fn abort(&mut self) -> impl Future<Output = Result<()>> + MaybeSend {
363        let _guard = self.span.set_local_parent();
364        let _span = LocalSpan::enter_with_local_parent(Operation::Copy.into_static());
365        self.inner.abort()
366    }
367}