      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
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194

    use crate::stdlib::pin::Pin;
    use crate::stdlib::task::{Context, Poll};
    use crate::stdlib::{future::Future, marker::Sized};
    use crate::{dispatcher, span::Span, Dispatch};
    use pin_project_lite::pin_project;

    /// Attaches spans to a `std::future::Future`.
    ///
    /// Extension trait allowing futures to be
    /// instrumented with a `tracing` [span].
    ///
    /// [span]:  ../struct.Span.html
    pub trait Instrument: Sized {
        /// Instruments this type with the provided `Span`, returning an
        /// `Instrumented` wrapper.
        ///
        /// The attached `Span` will be [entered] every time the instrumented `Future` is polled.
        ///
        /// # Examples
        ///
        /// Instrumenting a future:
        ///
        /// ```rust
        /// use tracing::Instrument;
        ///
        /// # async fn doc() {
        /// let my_future = async {
        ///     // ...
        /// };
        ///
        /// my_future
        ///     .instrument(tracing::info_span!("my_future"))
        ///     .await
        /// # }
        /// ```
        ///
        /// [entered]: ../struct.Span.html#method.enter
        fn instrument(self, span: Span) -> Instrumented<Self> {
            Instrumented { inner: self, span }
        }

        /// Instruments this type with the [current] `Span`, returning an
        /// `Instrumented` wrapper.
        ///
        /// If the instrumented type is a future, stream, or sink, the attached `Span`
        /// will be [entered] every time it is polled. If the instrumented type
        /// is a future executor, every future spawned on that executor will be
        /// instrumented by the attached `Span`.
        ///
        /// This can be used to propagate the current span when spawning a new future.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use tracing::Instrument;
        ///
        /// # async fn doc() {
        /// let span = tracing::info_span!("my_span");
        /// let _enter = span.enter();
        ///
        /// // ...
        ///
        /// let future = async {
        ///     tracing::debug!("this event will occur inside `my_span`");
        ///     // ...
        /// };
        /// tokio::spawn(future.in_current_span());
        /// # }
        /// ```
        ///
        /// [current]: ../struct.Span.html#method.current
        /// [entered]: ../struct.Span.html#method.enter
        #[inline]
        fn in_current_span(self) -> Instrumented<Self> {
            self.instrument(Span::current())
        }
    }

    /// Extension trait allowing futures to be instrumented with
    /// a `tracing` [`Subscriber`].
    ///
    /// [`Subscriber`]: ../trait.Subscriber.html
    pub trait WithSubscriber: Sized {
        /// Attaches the provided [`Subscriber`] to this type, returning a
        /// `WithDispatch` wrapper.
        ///
        /// The attached subscriber will be set as the [default] when the returned `Future` is polled.
        ///
        /// [`Subscriber`]: ../trait.Subscriber.html
        /// [default]: https://docs.rs/tracing/latest/tracing/dispatcher/index.html#setting-the-default-subscriber
        fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
        where
            S: Into<Dispatch>,
        {
            WithDispatch {
                inner: self,
                dispatch: subscriber.into(),
            }
        }

        /// Attaches the current [default] [`Subscriber`] to this type, returning a
        /// `WithDispatch` wrapper.
        ///
        /// When the wrapped type is a future, stream, or sink, the attached
        /// subscriber will be set as the [default] while it is being polled.
        /// When the wrapped type is an executor, the subscriber will be set as the
        /// default for any futures spawned on that executor.
        ///
        /// This can be used to propagate the current dispatcher context when
        /// spawning a new future.
        ///
        /// [`Subscriber`]: ../trait.Subscriber.html
        /// [default]: https://docs.rs/tracing/latest/tracing/dispatcher/index.html#setting-the-default-subscriber
        #[inline]
        fn with_current_subscriber(self) -> WithDispatch<Self> {
            WithDispatch {
                inner: self,
                dispatch: dispatcher::get_default(|default| default.clone()),
            }
        }
    }

    pin_project! {
        /// A future that has been instrumented with a `tracing` subscriber.
        #[derive(Clone, Debug)]
        #[must_use = "futures do nothing unless you `.await` or poll them"]
        pub struct WithDispatch<T> {
            #[pin]
            inner: T,
            dispatch: Dispatch,
        }
    }

    pin_project! {
        /// A future that has been instrumented with a `tracing` span.
        #[derive(Debug, Clone)]
        #[must_use = "futures do nothing unless you `.await` or poll them"]
        pub struct Instrumented<T> {
            #[pin]
            inner: T,
            span: Span,
        }
    }

    impl<T: Future> Future for Instrumented<T> {
        type Output = T::Output;

        fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
            let this = self.project();
            let _enter = this.span.enter();
            this.inner.poll(cx)
        }
    }

    impl<T: Sized> Instrument for T {}

    impl<T> Instrumented<T> {
        /// Borrows the `Span` that this type is instrumented by.
        pub fn span(&self) -> &Span {
            &self.span
        }

        /// Mutably borrows the `Span` that this type is instrumented by.
        pub fn span_mut(&mut self) -> &mut Span {
            &mut self.span
        }

        /// Borrows the wrapped type.
        pub fn inner(&self) -> &T {
            &self.inner
        }

        /// Mutably borrows the wrapped type.
        pub fn inner_mut(&mut self) -> &mut T {
            &mut self.inner
        }

        /// Get a pinned reference to the wrapped type.
        pub fn inner_pin_ref(self: Pin<&Self>) -> Pin<&T> {
            self.project_ref().inner
        }

        /// Get a pinned mutable reference to the wrapped type.
        pub fn inner_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T> {
            self.project().inner
        }

        /// Consumes the `Instrumented`, returning the wrapped type.
        ///
        /// Note that this drops the span.
        pub fn into_inner(self) -> T {
            self.inner
        }
    }