-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transport): Add tracing support to server (#175)
* feat(transport): Add tracing to server * Add tracing example * Fix duplicate versions of tracing-subscriber
- Loading branch information
1 parent
1626c2e
commit f46a454
Showing
8 changed files
with
197 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
pub mod hello_world { | ||
tonic::include_proto!("helloworld"); | ||
} | ||
|
||
use hello_world::{greeter_client::GreeterClient, HelloRequest}; | ||
use tracing_attributes::instrument; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
tracing_subscriber::FmtSubscriber::builder() | ||
.with_max_level(tracing::Level::DEBUG) | ||
.init(); | ||
|
||
say_hi("Bob".into()).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[instrument] | ||
async fn say_hi(name: String) -> Result<(), Box<dyn std::error::Error>> { | ||
let mut client = GreeterClient::connect("http://[::1]:50051").await?; | ||
|
||
let request = tonic::Request::new(HelloRequest { name }); | ||
|
||
tracing::info!( | ||
message = "Sending request.", | ||
request = %request.get_ref().name | ||
); | ||
|
||
let response = client.say_hello(request).await?; | ||
|
||
tracing::info!( | ||
message = "Got a response.", | ||
response = %response.get_ref().message | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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(Default)] | ||
pub struct MyGreeter {} | ||
|
||
#[tonic::async_trait] | ||
impl Greeter for MyGreeter { | ||
async fn say_hello( | ||
&self, | ||
request: Request<HelloRequest>, | ||
) -> Result<Response<HelloReply>, Status> { | ||
tracing::info!(message = "Inbound request.", metadata = ?request.metadata()); | ||
|
||
let reply = hello_world::HelloReply { | ||
message: format!("Hello {}!", request.into_inner().name).into(), | ||
}; | ||
|
||
tracing::debug!(message = "Sending reply.", response = %reply.message); | ||
|
||
Ok(Response::new(reply)) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
tracing_subscriber::FmtSubscriber::builder() | ||
.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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use pin_project::pin_project; | ||
use std::{ | ||
future::Future, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
#[pin_project] | ||
pub struct ResponseFuture<F> { | ||
inner: F, | ||
span: Option<Span>, | ||
} | ||
|
||
impl<F: Future> Future for ResponseFuture<F> { | ||
type Output = F::Output; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let me = self.project(); | ||
|
||
if let Some(span) = me.span.clone().take() { | ||
let _enter = span.enter(); | ||
// me.poll(cx).map_err(Into::into) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use pin_project::pin_project; | ||
use std::{ | ||
future::Future, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
#[pin_project] | ||
pub struct ResponseFuture<F> { | ||
inner: F, | ||
span: Option<Span>, | ||
} | ||
|
||
impl<F: Future> Future for ResponseFuture<F> { | ||
type Output = F::Output; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let me = self.project(); | ||
|
||
if let Some(span) = me.span.clone().take() { | ||
let _enter = span.enter(); | ||
// me.poll(cx).map_err(Into::into) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters