|
| 1 | +use hello_world::greeter_server::{Greeter, GreeterServer}; |
| 2 | +use hello_world::{HelloReply, HelloRequest}; |
| 3 | +use opentelemetry::api::{self, HttpTextFormat, KeyValue, Provider}; |
| 4 | +use opentelemetry::sdk::{self, Sampler}; |
| 5 | +use tonic::{transport::Server, Request, Response, Status}; |
| 6 | +use tracing::*; |
| 7 | +use tracing_opentelemetry::OpenTelemetrySpanExt; |
| 8 | +use tracing_subscriber::prelude::*; |
| 9 | + |
| 10 | +pub mod hello_world { |
| 11 | + tonic::include_proto!("helloworld"); // The string specified here must match the proto package name |
| 12 | +} |
| 13 | + |
| 14 | +#[instrument] |
| 15 | +fn expensive_fn(to_print: String) { |
| 16 | + for _ in 0..5 { |
| 17 | + std::thread::sleep(std::time::Duration::from_secs(1)); |
| 18 | + info!("{}", to_print); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Debug, Default)] |
| 23 | +pub struct MyGreeter {} |
| 24 | + |
| 25 | +#[tonic::async_trait] |
| 26 | +impl Greeter for MyGreeter { |
| 27 | + #[instrument] |
| 28 | + async fn say_hello( |
| 29 | + &self, |
| 30 | + request: Request<HelloRequest>, // Accept request of type HelloRequest |
| 31 | + ) -> Result<Response<HelloReply>, Status> { |
| 32 | + let propagator = api::TraceContextPropagator::new(); |
| 33 | + let parent_cx = propagator.extract(&HttpHeaderMapCarrier(request.metadata())); |
| 34 | + let span = tracing::Span::current(); |
| 35 | + span.set_parent(&parent_cx); |
| 36 | + let name = request.into_inner().name; |
| 37 | + expensive_fn(format!("Got name: {:?}", name)); |
| 38 | + |
| 39 | + // Return an instance of type HelloReply |
| 40 | + let reply = hello_world::HelloReply { |
| 41 | + message: format!("Hello {}!", name), // We must use .into_inner() as the fields of gRPC requests and responses are private |
| 42 | + }; |
| 43 | + |
| 44 | + Ok(Response::new(reply)) // Send back our formatted greeting |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +fn tracing_init() -> Result<(), Box<dyn std::error::Error>> { |
| 49 | + let builder = opentelemetry_jaeger::Exporter::builder() |
| 50 | + .with_agent_endpoint("127.0.0.1:6831".parse().unwrap()); |
| 51 | + |
| 52 | + let exporter = builder |
| 53 | + .with_process(opentelemetry_jaeger::Process { |
| 54 | + service_name: "grpc-server".to_string(), |
| 55 | + tags: vec![KeyValue::new("version", "0.1.0")], |
| 56 | + }) |
| 57 | + .init()?; |
| 58 | + |
| 59 | + // For the demonstration, use `Sampler::Always` sampler to sample all traces. In a production |
| 60 | + // application, use `Sampler::Parent` or `Sampler::Probability` with a desired probability. |
| 61 | + let provider = sdk::Provider::builder() |
| 62 | + .with_simple_exporter(exporter) |
| 63 | + .with_config(sdk::Config { |
| 64 | + default_sampler: Box::new(Sampler::Always), |
| 65 | + ..Default::default() |
| 66 | + }) |
| 67 | + .build(); |
| 68 | + let tracer = provider.get_tracer("grpc-server"); |
| 69 | + |
| 70 | + let opentelemetry = tracing_opentelemetry::layer().with_tracer(tracer); |
| 71 | + tracing_subscriber::registry() |
| 72 | + .with(opentelemetry) |
| 73 | + .try_init()?; |
| 74 | + |
| 75 | + Ok(()) |
| 76 | +} |
| 77 | + |
| 78 | +struct HttpHeaderMapCarrier<'a>(&'a tonic::metadata::MetadataMap); |
| 79 | +impl<'a> api::Carrier for HttpHeaderMapCarrier<'a> { |
| 80 | + fn get(&self, key: &'static str) -> Option<&str> { |
| 81 | + self.0 |
| 82 | + .get(key.to_lowercase().as_str()) |
| 83 | + .and_then(|value| value.to_str().ok()) |
| 84 | + } |
| 85 | + |
| 86 | + fn set(&mut self, _key: &'static str, _value: String) { |
| 87 | + unimplemented!() |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +#[tokio::main] |
| 92 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 93 | + tracing_init()?; |
| 94 | + let addr = "[::1]:50051".parse()?; |
| 95 | + let greeter = MyGreeter::default(); |
| 96 | + |
| 97 | + Server::builder() |
| 98 | + .add_service(GreeterServer::new(greeter)) |
| 99 | + .serve(addr) |
| 100 | + .await?; |
| 101 | + |
| 102 | + Ok(()) |
| 103 | +} |
0 commit comments