1use std::fmt::Debug;
19use std::future::Future;
20use std::sync::Arc;
21
22use tracing::Span;
23
24use crate::raw::*;
25use crate::*;
26
27pub struct TracingLayer;
139
140impl<A: Access> Layer<A> for TracingLayer {
141 type LayeredAccess = TracingAccessor<A>;
142
143 fn layer(&self, inner: A) -> Self::LayeredAccess {
144 TracingAccessor { inner }
145 }
146}
147
148#[derive(Debug)]
149pub struct TracingAccessor<A> {
150 inner: A,
151}
152
153impl<A: Access> LayeredAccess for TracingAccessor<A> {
154 type Inner = A;
155 type Reader = TracingWrapper<A::Reader>;
156 type Writer = TracingWrapper<A::Writer>;
157 type Lister = TracingWrapper<A::Lister>;
158 type Deleter = TracingWrapper<A::Deleter>;
159 type BlockingReader = TracingWrapper<A::BlockingReader>;
160 type BlockingWriter = TracingWrapper<A::BlockingWriter>;
161 type BlockingLister = TracingWrapper<A::BlockingLister>;
162 type BlockingDeleter = TracingWrapper<A::BlockingDeleter>;
163
164 fn inner(&self) -> &Self::Inner {
165 &self.inner
166 }
167
168 #[tracing::instrument(level = "debug")]
169 fn info(&self) -> Arc<AccessorInfo> {
170 self.inner.info()
171 }
172
173 #[tracing::instrument(level = "debug", skip(self))]
174 async fn create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> {
175 self.inner.create_dir(path, args).await
176 }
177
178 #[tracing::instrument(level = "debug", skip(self))]
179 async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
180 self.inner
181 .read(path, args)
182 .await
183 .map(|(rp, r)| (rp, TracingWrapper::new(Span::current(), r)))
184 }
185
186 #[tracing::instrument(level = "debug", skip(self))]
187 async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
188 self.inner
189 .write(path, args)
190 .await
191 .map(|(rp, r)| (rp, TracingWrapper::new(Span::current(), r)))
192 }
193
194 #[tracing::instrument(level = "debug", skip(self))]
195 async fn copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> {
196 self.inner().copy(from, to, args).await
197 }
198
199 #[tracing::instrument(level = "debug", skip(self))]
200 async fn rename(&self, from: &str, to: &str, args: OpRename) -> Result<RpRename> {
201 self.inner().rename(from, to, args).await
202 }
203
204 #[tracing::instrument(level = "debug", skip(self))]
205 async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
206 self.inner.stat(path, args).await
207 }
208
209 #[tracing::instrument(level = "debug", skip(self))]
210 async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
211 self.inner
212 .delete()
213 .await
214 .map(|(rp, r)| (rp, TracingWrapper::new(Span::current(), r)))
215 }
216
217 #[tracing::instrument(level = "debug", skip(self))]
218 async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
219 self.inner
220 .list(path, args)
221 .await
222 .map(|(rp, s)| (rp, TracingWrapper::new(Span::current(), s)))
223 }
224
225 #[tracing::instrument(level = "debug", skip(self))]
226 async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
227 self.inner.presign(path, args).await
228 }
229
230 #[tracing::instrument(level = "debug", skip(self))]
231 fn blocking_create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> {
232 self.inner.blocking_create_dir(path, args)
233 }
234
235 #[tracing::instrument(level = "debug", skip(self))]
236 fn blocking_read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::BlockingReader)> {
237 self.inner
238 .blocking_read(path, args)
239 .map(|(rp, r)| (rp, TracingWrapper::new(Span::current(), r)))
240 }
241
242 #[tracing::instrument(level = "debug", skip(self))]
243 fn blocking_write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::BlockingWriter)> {
244 self.inner
245 .blocking_write(path, args)
246 .map(|(rp, r)| (rp, TracingWrapper::new(Span::current(), r)))
247 }
248
249 #[tracing::instrument(level = "debug", skip(self))]
250 fn blocking_copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> {
251 self.inner().blocking_copy(from, to, args)
252 }
253
254 #[tracing::instrument(level = "debug", skip(self))]
255 fn blocking_rename(&self, from: &str, to: &str, args: OpRename) -> Result<RpRename> {
256 self.inner().blocking_rename(from, to, args)
257 }
258
259 #[tracing::instrument(level = "debug", skip(self))]
260 fn blocking_stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
261 self.inner.blocking_stat(path, args)
262 }
263
264 #[tracing::instrument(level = "debug", skip(self))]
265 fn blocking_delete(&self) -> Result<(RpDelete, Self::BlockingDeleter)> {
266 self.inner
267 .blocking_delete()
268 .map(|(rp, r)| (rp, TracingWrapper::new(Span::current(), r)))
269 }
270
271 #[tracing::instrument(level = "debug", skip(self))]
272 fn blocking_list(&self, path: &str, args: OpList) -> Result<(RpList, Self::BlockingLister)> {
273 self.inner
274 .blocking_list(path, args)
275 .map(|(rp, it)| (rp, TracingWrapper::new(Span::current(), it)))
276 }
277}
278
279pub struct TracingWrapper<R> {
280 span: Span,
281 inner: R,
282}
283
284impl<R> TracingWrapper<R> {
285 fn new(span: Span, inner: R) -> Self {
286 Self { span, inner }
287 }
288}
289
290impl<R: oio::Read> oio::Read for TracingWrapper<R> {
291 #[tracing::instrument(
292 parent = &self.span,
293 level = "trace",
294 skip_all)]
295 async fn read(&mut self) -> Result<Buffer> {
296 self.inner.read().await
297 }
298}
299
300impl<R: oio::BlockingRead> oio::BlockingRead for TracingWrapper<R> {
301 #[tracing::instrument(
302 parent = &self.span,
303 level = "trace",
304 skip_all)]
305 fn read(&mut self) -> Result<Buffer> {
306 self.inner.read()
307 }
308}
309
310impl<R: oio::Write> oio::Write for TracingWrapper<R> {
311 #[tracing::instrument(
312 parent = &self.span,
313 level = "trace",
314 skip_all)]
315 fn write(&mut self, bs: Buffer) -> impl Future<Output = Result<()>> + MaybeSend {
316 self.inner.write(bs)
317 }
318
319 #[tracing::instrument(
320 parent = &self.span,
321 level = "trace",
322 skip_all)]
323 fn abort(&mut self) -> impl Future<Output = Result<()>> + MaybeSend {
324 self.inner.abort()
325 }
326
327 #[tracing::instrument(
328 parent = &self.span,
329 level = "trace",
330 skip_all)]
331 fn close(&mut self) -> impl Future<Output = Result<Metadata>> + MaybeSend {
332 self.inner.close()
333 }
334}
335
336impl<R: oio::BlockingWrite> oio::BlockingWrite for TracingWrapper<R> {
337 #[tracing::instrument(
338 parent = &self.span,
339 level = "trace",
340 skip_all)]
341 fn write(&mut self, bs: Buffer) -> Result<()> {
342 self.inner.write(bs)
343 }
344
345 #[tracing::instrument(
346 parent = &self.span,
347 level = "trace",
348 skip_all)]
349 fn close(&mut self) -> Result<Metadata> {
350 self.inner.close()
351 }
352}
353
354impl<R: oio::List> oio::List for TracingWrapper<R> {
355 #[tracing::instrument(parent = &self.span, level = "debug", skip_all)]
356 async fn next(&mut self) -> Result<Option<oio::Entry>> {
357 self.inner.next().await
358 }
359}
360
361impl<R: oio::BlockingList> oio::BlockingList for TracingWrapper<R> {
362 #[tracing::instrument(parent = &self.span, level = "debug", skip_all)]
363 fn next(&mut self) -> Result<Option<oio::Entry>> {
364 self.inner.next()
365 }
366}
367
368impl<R: oio::Delete> oio::Delete for TracingWrapper<R> {
369 #[tracing::instrument(parent = &self.span, level = "debug", skip_all)]
370 fn delete(&mut self, path: &str, args: OpDelete) -> Result<()> {
371 self.inner.delete(path, args)
372 }
373
374 #[tracing::instrument(parent = &self.span, level = "debug", skip_all)]
375 async fn flush(&mut self) -> Result<usize> {
376 self.inner.flush().await
377 }
378}
379
380impl<R: oio::BlockingDelete> oio::BlockingDelete for TracingWrapper<R> {
381 #[tracing::instrument(parent = &self.span, level = "debug", skip_all)]
382 fn delete(&mut self, path: &str, args: OpDelete) -> Result<()> {
383 self.inner.delete(path, args)
384 }
385
386 #[tracing::instrument(parent = &self.span, level = "debug", skip_all)]
387 fn flush(&mut self) -> Result<usize> {
388 self.inner.flush()
389 }
390}