Best way to add tracing to tokio::spawn method call

I am adding tokio-tracing to my code. I have following method that creates a new tokio async task.

      #[instrument]
      fn start_listener(mut listener: MyListener, should_stop: Receiver<()>) -> JoinHandle<()> {
          tokio::spawn(async move {
              tokio::select! {
                  result = listener.start() => {
                      match result {
                          Ok(()) => { },
                          Err(err) => {
                              println!("{}", err);
                          }
                      }
                  },
              _ = should_stop => {
                      debug!("server stopped");
                  }
              }
          })
      }

What's the best way to do it for this method. What I want is to receive tracing from the Listener.

I believe its something like this if I understand your question correctly.

use tracing::info_span;
use tracing_futures::Instrument;
#[instrument]
fn start_listener(mut listener: MyListener, should_stop: Receiver<()>) -> JoinHandle<()> {
    tokio::spawn(async move {
        tokio::select! {
            result = listener.start().instrument(info_span!("listener")) => {
                match result {
                    Ok(()) => { },
                    Err(err) => {
                        println!("{}", err);
                    }
                }
            },
            _ = should_stop => {
                debug!("server stopped");
            }
        }
    })
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.