-
Notifications
You must be signed in to change notification settings - Fork 385
/
client.rs
46 lines (40 loc) · 1.56 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
use hyper::{body::Body, Client};
use opentelemetry::global;
use opentelemetry::sdk::export::trace::stdout;
use opentelemetry::sdk::{
propagation::TraceContextPropagator,
trace::{Config, Sampler},
};
use opentelemetry::{
trace::{TraceContextExt, Tracer},
Context, KeyValue,
};
fn init_tracer() -> (impl Tracer, stdout::Uninstall) {
global::set_text_map_propagator(TraceContextPropagator::new());
// Install stdout exporter pipeline to be able to retrieve the collected spans.
// For the demonstration, use `Sampler::AlwaysOn` sampler to sample all traces. In a production
// application, use `Sampler::ParentBased` or `Sampler::TraceIdRatioBased` with a desired ratio.
stdout::new_pipeline()
.with_trace_config(Config {
default_sampler: Box::new(Sampler::AlwaysOn),
..Default::default()
})
.install()
}
#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let _guard = init_tracer();
let client = Client::new();
let span = global::tracer("example/client").start("say hello");
let cx = Context::current_with_span(span);
let mut req = hyper::Request::builder().uri("http://127.0.0.1:3000");
global::get_text_map_propagator(|propagator| {
propagator.inject_context(&cx, req.headers_mut().unwrap())
});
let res = client.request(req.body(Body::from("Hallo!"))?).await?;
cx.span().add_event(
"Got response!".to_string(),
vec![KeyValue::new("status", res.status().to_string())],
);
Ok(())
}