-
Notifications
You must be signed in to change notification settings - Fork 384
/
client.rs
69 lines (57 loc) · 2.12 KB
/
client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use hello_world::greeter_client::GreeterClient;
use hello_world::HelloRequest;
use opentelemetry::sdk::propagation::TraceContextPropagator;
use opentelemetry::{global, propagation::Injector};
use tracing::*;
use tracing_futures::Instrument;
use tracing_opentelemetry::OpenTelemetrySpanExt;
use tracing_subscriber::prelude::*;
struct MetadataMap<'a>(&'a mut tonic::metadata::MetadataMap);
impl<'a> Injector for MetadataMap<'a> {
/// Set a key and value in the MetadataMap. Does nothing if the key or value are not valid inputs
fn set(&mut self, key: &str, value: String) {
if let Ok(key) = tonic::metadata::MetadataKey::from_bytes(key.as_bytes()) {
if let Ok(val) = tonic::metadata::MetadataValue::from_str(&value) {
self.0.insert(key, val);
}
}
}
}
pub mod hello_world {
tonic::include_proto!("helloworld");
}
#[instrument]
async fn greet() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let mut client = GreeterClient::connect("http://[::1]:50051")
.instrument(info_span!("client connect"))
.await?;
let mut request = tonic::Request::new(HelloRequest {
name: "Tonic".into(),
});
global::get_text_map_propagator(|propagator| {
propagator.inject_context(
&tracing::Span::current().context(),
&mut MetadataMap(request.metadata_mut()),
)
});
let response = client
.say_hello(request)
.instrument(info_span!("say_hello"))
.await?;
info!("Response received: {:?}", response);
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
global::set_text_map_propagator(TraceContextPropagator::new());
let tracer = opentelemetry_jaeger::new_pipeline()
.with_service_name("grpc-client")
.install_simple()?;
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new("INFO"))
.with(tracing_opentelemetry::layer().with_tracer(tracer))
.try_init()?;
greet().await?;
opentelemetry::global::shutdown_tracer_provider();
Ok(())
}