1#![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#[derive(Clone, Debug, Default)]
108#[non_exhaustive]
109pub struct FastraceLayer {}
110
111impl FastraceLayer {
112 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 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)]
262pub 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}