-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathopentelemetry.rs
48 lines (39 loc) · 1.68 KB
/
opentelemetry.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
use opentelemetry::global;
use std::{error::Error, thread, time::Duration};
use tracing::{instrument, span, trace, warn};
use tracing_subscriber::prelude::*;
#[instrument]
#[inline]
fn expensive_work() -> &'static str {
span!(tracing::Level::INFO, "expensive_step_1")
.in_scope(|| thread::sleep(Duration::from_millis(25)));
span!(tracing::Level::INFO, "expensive_step_2")
.in_scope(|| thread::sleep(Duration::from_millis(25)));
"success"
}
fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// Install an otel pipeline with a simple span processor that exports data one at a time when
// spans end. See the `install_batch` option on each exporter's pipeline builder to see how to
// export in batches.
let tracer = opentelemetry_jaeger::new_agent_pipeline()
.with_service_name("report_example")
.install_simple()?;
let opentelemetry = tracing_opentelemetry::layer().with_tracer(tracer);
tracing_subscriber::registry()
.with(opentelemetry)
.try_init()?;
{
let root = span!(tracing::Level::INFO, "app_start", work_units = 2);
let _enter = root.enter();
let work_result = expensive_work();
span!(tracing::Level::INFO, "faster_work")
.in_scope(|| thread::sleep(Duration::from_millis(10)));
warn!("About to exit!");
trace!("status: {}", work_result);
} // Once this scope is closed, all spans inside are closed as well
// Shut down the current tracer provider. This will invoke the shutdown
// method on all span processors. span processors should export remaining
// spans before return.
global::shutdown_tracer_provider();
Ok(())
}