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
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)]
248pub 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}