简体   繁体   中英

Instrument library in Rust with OpenTelemetry

Im learning tracing in Rust and open-telemetry in Rust . I feel there are too many concepts and too many crates (at least in Rust) to see traces.

I wrote a simple lib app that adds two u32 s.

use std::ops::Add;

pub fn add(f: u32, s: u32) -> u32 {
    let span = tracing::info_span!("Add function", ?f, ?s);
    let _guard = span.enter();
    tracing::info!("Info event");
    f.add(s)
}

And then Im using the lib in my bin app as below.

use TracedLibrary::add;
use tracing_opentelemetry::OpenTelemetryLayer;
use tracing_subscriber::util::SubscriberInitExt;
use opentelemetry::{global, sdk::propagation::TraceContextPropagator};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::Registry;

fn main() {
    setup_global_subscriber();
    let sum = add::add(1, 2);
}

fn setup_global_subscriber() {
    global::set_text_map_propagator(TraceContextPropagator::new());
    let (tracer, _uninstall) = opentelemetry_jaeger::new_pipeline()
        .with_service_name("trace_demo_2")
        .install().expect("Error initializing Jaeger exporter");
    let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);

    Registry::default()
        .with(telemetry).init();
}

The most confusing part is my apps Cargo.toml which looks like

...
tracing-subscriber = { version = "0.2.15" }
tracing-opentelemetry = { version= "0.11.0"}
opentelemetry = "0.12.0"
opentelemetry-jaeger = {version = "0.11.0" }
...

What on earth are those different crates are for? The only crate that makes sense is opentelemetry-jaeger . Are others even required?

And to my main question: Im running Jaeger's all-in-one docker container. But when I visit http://localhost:16686 ...I see no traces. Does anyone know what's happening?

Well..when I create a jaeger pipeline in setup_global_subscriber() ... the _uninstall being returned gets dropped at the end of the method. And when it gets dropped, collector shuts down.

To get traces I had to move contents of setup_global_subscriber() in main() .

##$&@(& me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM