1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::api;
use pin_project::pin_project;
use std::{pin::Pin, task::Context};
#[pin_project]
#[derive(Debug, Clone)]
pub struct Instrumented<F, S: api::Span> {
#[pin]
inner: F,
span: S,
}
impl<F: Sized> Instrument for F {}
impl<F: std::future::Future, S: api::Span> std::future::Future for Instrumented<F, S> {
type Output = F::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> std::task::Poll<Self::Output> {
let this = self.project();
this.span.mark_as_active();
let res = this.inner.poll(cx);
this.span.mark_as_inactive();
res
}
}
pub trait Instrument: Sized {
fn instrument<S: api::Span>(self, span: S) -> Instrumented<Self, S> {
Instrumented { inner: self, span }
}
fn in_active_span<T: api::Tracer>(self, tracer: T) -> Instrumented<Self, T::Span> {
let span = tracer.get_active_span();
self.instrument(span)
}
}