xref: /tonic/examples/src/tracing/client.rs (revision 61555ff2)
1 pub mod hello_world {
2     tonic::include_proto!("helloworld");
3 }
4 
5 use hello_world::{greeter_client::GreeterClient, HelloRequest};
6 use tracing_attributes::instrument;
7 
8 #[tokio::main]
9 async fn main() -> Result<(), Box<dyn std::error::Error>> {
10     tracing_subscriber::FmtSubscriber::builder()
11         .with_max_level(tracing::Level::DEBUG)
12         .init();
13 
14     say_hi("Bob".into()).await?;
15 
16     Ok(())
17 }
18 
19 #[instrument]
20 async fn say_hi(name: String) -> Result<(), Box<dyn std::error::Error>> {
21     let mut client = GreeterClient::connect("http://[::1]:50051").await?;
22 
23     let request = tonic::Request::new(HelloRequest { name });
24 
25     tracing::info!(
26         message = "Sending request.",
27         request = %request.get_ref().name
28     );
29 
30     let response = client.say_hello(request).await?;
31 
32     tracing::info!(
33         message = "Got a response.",
34         response = %response.get_ref().message
35     );
36 
37     Ok(())
38 }
39