* examples: update to `tracing` 0.1.14, use `#[instrument]` Now that `tracing-attributes`'s `#[instrument]` macro plays nicely with `async-trait`, we can update the tracing example to use `instrument`. This lets us simplify the events emitted in the example. Signed-off-by: Eliza Weisman <[email protected]> * feat(transport): Dynamic load balancing (#341) * Fix typo (#356) Co-authored-by: Dawid Nowak <[email protected]> Co-authored-by: Paulo Duarte <[email protected]>
53 lines
1.2 KiB
Rust
53 lines
1.2 KiB
Rust
use tonic::{transport::Server, Request, Response, Status};
|
|
|
|
pub mod hello_world {
|
|
tonic::include_proto!("helloworld");
|
|
}
|
|
|
|
use hello_world::{
|
|
greeter_server::{Greeter, GreeterServer},
|
|
HelloReply, HelloRequest,
|
|
};
|
|
|
|
#[derive(Debug, Default)]
|
|
pub struct MyGreeter {}
|
|
|
|
#[tonic::async_trait]
|
|
impl Greeter for MyGreeter {
|
|
#[tracing::instrument]
|
|
async fn say_hello(
|
|
&self,
|
|
request: Request<HelloRequest>,
|
|
) -> Result<Response<HelloReply>, Status> {
|
|
tracing::info!("received request");
|
|
|
|
let reply = hello_world::HelloReply {
|
|
message: format!("Hello {}!", request.into_inner().name),
|
|
};
|
|
|
|
tracing::debug!("sending response");
|
|
|
|
Ok(Response::new(reply))
|
|
}
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
tracing_subscriber::fmt()
|
|
.with_max_level(tracing::Level::DEBUG)
|
|
.init();
|
|
|
|
let addr = "[::1]:50051".parse().unwrap();
|
|
let greeter = MyGreeter::default();
|
|
|
|
tracing::info!(message = "Starting server.", %addr);
|
|
|
|
Server::builder()
|
|
.trace_fn(|_| tracing::info_span!("helloworld_server"))
|
|
.add_service(GreeterServer::new(greeter))
|
|
.serve(addr)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|