mirror of
https://github.com/tokio-rs/tracing.git
synced 2025-10-02 15:24:47 +00:00

## Motivation As discussed in #308, there are a large number of crates in this repository, which can be confusing for users and can increase the maintainance burden for maintainers. Also, the `tracing-fmt` and `tracing-subscriber` crates both contain filtering implementations with similar behaviour and APIs, but `tracing-subscriber`'s filter module offers more advanced features (filtering on field values), and is usable with any subscriber implementation. Two separate filter implementations also has the potential to be confusing for users. ## Solution This branch moves most of the code from `tracing-fmt` into a module in `tracing-subscriber`, and changes the `tracing-fmt` builder APIs to use the `Filter` type in `tracing-subscriber`. The `tracing-subscriber/fmt` feature flag can be used to disable the formatting subscriber when it is not used. The `tracing-fmt` crate has been updated to re-export the APIs from `tracing-subscriber`, and marked as deprecated. Once we've published a new version of `tracing-subscriber` with the format APIs, we can publish a final release of `tracing-fmt` that will update the documentation & mark all APIs as deprecated, so that users know to move to the `tracing-subscriber` crate. Refs: #308 Signed-off-by: Eliza Weisman <eliza@buoyant.io>
60 lines
1.6 KiB
Rust
60 lines
1.6 KiB
Rust
//! Demonstrates using the `trace` attribute macro to instrument `async`
|
|
//! functions.
|
|
//!
|
|
//! This is based on the [`hello_world`] example from `tokio`. and implements a
|
|
//! simple client that opens a TCP stream, writes "hello world\n", and closes
|
|
//! the connection.
|
|
//!
|
|
//! You can test this out by running:
|
|
//!
|
|
//! ncat -l 6142
|
|
//!
|
|
//! And then in another terminal run:
|
|
//!
|
|
//! cargo +nightly run --example async_fn
|
|
//!
|
|
//! [`hello_world`]: https://github.com/tokio-rs/tokio/blob/132e9f1da5965530b63554d7a1c59824c3de4e30/tokio/examples/hello_world.rs
|
|
#![deny(rust_2018_idioms)]
|
|
|
|
use tokio;
|
|
use tokio::io::AsyncWriteExt;
|
|
use tokio::net::TcpStream;
|
|
|
|
use tracing::info;
|
|
use tracing_attributes::instrument;
|
|
|
|
use std::{error::Error, io, net::SocketAddr};
|
|
|
|
#[instrument]
|
|
async fn connect(addr: &SocketAddr) -> io::Result<TcpStream> {
|
|
let stream = TcpStream::connect(&addr).await;
|
|
tracing::info!("created stream");
|
|
stream
|
|
}
|
|
|
|
#[instrument]
|
|
async fn write(stream: &mut TcpStream) -> io::Result<usize> {
|
|
let result = stream.write(b"hello world\n").await;
|
|
info!("wrote to stream; success={:?}", result.is_ok());
|
|
result
|
|
}
|
|
|
|
#[tokio::main]
|
|
pub async fn main() -> Result<(), Box<dyn Error>> {
|
|
let addr = "127.0.0.1:6142".parse()?;
|
|
|
|
let subscriber = tracing_subscriber::fmt::Subscriber::builder()
|
|
.with_filter("async_fn=trace")
|
|
.finish();
|
|
tracing::subscriber::set_global_default(subscriber).unwrap();
|
|
|
|
// Open a TCP stream to the socket address.
|
|
//
|
|
// Note that this is the Tokio TcpStream, which is fully async.
|
|
let mut stream = connect(&addr).await?;
|
|
|
|
write(&mut stream).await?;
|
|
|
|
Ok(())
|
|
}
|