// use tracing_subscriber::prelude::*; use opentelemetry::global; use opentelemetry::propagation::TextMapPropagator; use opentelemetry::sdk::propagation::{BaggagePropagator, TraceContextPropagator}; use pi_logger::tracing::{create_baggage, init_sls}; use std::collections::HashMap; use std::{error::Error, thread, time::Duration}; use tracing::{self, instrument, Instrument}; use tracing_opentelemetry::OpenTelemetrySpanExt; use tracing_subscriber::prelude::*; // use opentelemetry::util::tokio_interval_stream; static ENDPOINT: &str = "https://gcwl-logs.cn-chengdu.log.aliyuncs.com:10010"; static PROJECT: &str = "gcwl-logs"; static INSTANCE_ID: &str = "pidemo"; static AK_ID: &str = "LTAI5t6nowJdSqViaou6WAUo"; static AK_SECRET: &str = "chY7jOgrt1C0BQuxPN2DhB9kGvz3n4"; static SERVICE_VERSION: &str = "v0.1.0"; static SERVICE_NAME: &str = "pi_demo"; static SERVICE_NAMESPACE: &str = "pi_demo_namespace"; static HOST_NAME: &str = "localhost"; #[instrument] #[inline] async fn expensive_work() -> &'static str { tracing::span!(tracing::Level::INFO, "expensive_step_1") .in_scope(|| thread::sleep(Duration::from_millis(25))); tracing::span!(tracing::Level::INFO, "expensive_step_2") .in_scope(|| thread::sleep(Duration::from_millis(25))); "success" } #[tokio::test(flavor = "multi_thread")] async fn test_baggage() -> Result<(), Box> { let tracer = init_sls( ENDPOINT.to_string(), PROJECT.to_string(), INSTANCE_ID.to_string(), AK_ID.to_string(), AK_SECRET.to_string(), SERVICE_VERSION.to_string(), SERVICE_NAME.to_string(), SERVICE_NAMESPACE.to_string(), HOST_NAME.to_string(), ); // let metrics = init_sls_metrics(); tracing_subscriber::Registry::default() .with( tracing_subscriber::filter::FilterFn::new(|_| true) .with_max_level_hint(tracing_subscriber::filter::LevelFilter::INFO), ) .with(tracing_opentelemetry::layer().with_tracer(tracer)) // .with(tracing_opentelemetry::MetricsLayer::new(metrics)) // .with(tracing_subscriber::fmt::layer()) .init(); { let propagator = TraceContextPropagator::new(); let mut carrier = HashMap::new(); carrier.insert( "traceparent".to_string(), "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01".to_string(), ); let parent_context = propagator.extract(&carrier); // let baggage_propagator = BaggagePropagator::new(); // let parent_context = baggage_propagator.extract(carrier); // let parent_context = // global::get_text_map_propagator(|propagator| propagator.extract(&carrier)); let propagator = create_baggage(); // {"traceparent": "00-277656cdf4562c5892da9925ce98c697-0fa197db81558780-01", "tracestate": ""} let mut outgoing_req_carrier = HashMap::new(); propagator.inject_context(&parent_context, &mut outgoing_req_carrier); println!("traceparent:{:?}", outgoing_req_carrier.get("traceparent")); let app_root = tracing::info_span!("app_start"); // println!("parentContext:{:?}", parent_context); app_root.set_parent(parent_context); } // Once this scope is closed, all spans inside are closed as well opentelemetry::global::shutdown_tracer_provider(); // thread::sleep(Duration::from_millis(10000)); Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test() -> Result<(), Box> { let tracer = init_sls( ENDPOINT.to_string(), PROJECT.to_string(), INSTANCE_ID.to_string(), AK_ID.to_string(), AK_SECRET.to_string(), SERVICE_VERSION.to_string(), SERVICE_NAME.to_string(), SERVICE_NAMESPACE.to_string(), HOST_NAME.to_string(), ); // let metrics = init_sls_metrics(); tracing_subscriber::Registry::default() .with( tracing_subscriber::filter::FilterFn::new(|_| true) .with_max_level_hint(tracing_subscriber::filter::LevelFilter::INFO), ) .with(tracing_opentelemetry::layer().with_tracer(tracer)) // .with(tracing_opentelemetry::MetricsLayer::new(metrics)) // .with(tracing_subscriber::fmt::layer()) .init(); { let propagator = create_baggage(); // {"traceparent": "00-277656cdf4562c5892da9925ce98c697-0fa197db81558780-01", "tracestate": ""} let mut outgoing_req_carrier = HashMap::new(); let root = tracing::span!(tracing::Level::INFO, "app_start112"); let _enter = root.enter(); println!("context:{:?}", &root.context()); println!("outgoing_req_carrier1:{:?}", outgoing_req_carrier); propagator.inject_context(&root.context(), &mut outgoing_req_carrier); println!("outgoing_req_carrier2:{:?}", outgoing_req_carrier); println!("traceparent:{:?}", outgoing_req_carrier.get("traceparent")); let work_result = expensive_work(); let parent_context = global::get_text_map_propagator(|propagator| propagator.extract(&outgoing_req_carrier)); println!("parent_context:{:?}", parent_context); println!("parent_context2:{:?}", &root.context()); tracing::span!(tracing::Level::INFO, "faster_work") .in_scope(|| thread::sleep(Duration::from_millis(10))); tracing::span!(tracing::Level::INFO, "test").in_scope(|| { tracing::trace!("status: {}", "ok"); tracing::info!("log1111111"); thread::sleep(Duration::from_millis(10)) }); tracing::info!(monotonic_counter.foo = 1); tracing::info!(monotonic_counter.bar = 1.1); tracing::info!(counter.baz = 1); tracing::info!(counter.baz = -1); tracing::info!(counter.xyz = 1.1); tracing::info!(value.qux = 1); tracing::info!(value.abc = -1); tracing::info!(value.def = 1.1); tracing::warn!("About to exit!"); } // Once this scope is closed, all spans inside are closed as well opentelemetry::global::shutdown_tracer_provider(); // thread::sleep(Duration::from_millis(10000)); Ok(()) } #[test] fn test2() { let r = tokio::runtime::Builder::new_multi_thread() .worker_threads(4) .enable_all() .build() .unwrap(); r.spawn(async { let tracer = init_sls( ENDPOINT.to_string(), PROJECT.to_string(), INSTANCE_ID.to_string(), AK_ID.to_string(), AK_SECRET.to_string(), SERVICE_VERSION.to_string(), SERVICE_NAME.to_string(), SERVICE_NAMESPACE.to_string(), HOST_NAME.to_string(), ); // let metrics = init_sls_metrics(); tracing_subscriber::Registry::default() .with( tracing_subscriber::filter::FilterFn::new(|_| true) .with_max_level_hint(tracing_subscriber::filter::LevelFilter::INFO), ) .with(tracing_opentelemetry::layer().with_tracer(tracer)) // .with(tracing_opentelemetry::MetricsLayer::new(metrics)) // .with(tracing_subscriber::fmt::layer()) .init(); { let root = tracing::span!(tracing::Level::INFO, "app_start", work_units = 2); let _enter = root.enter(); let work_result = expensive_work(); tracing::span!(tracing::Level::INFO, "faster_work") .in_scope(|| thread::sleep(Duration::from_millis(10))); tracing::span!(tracing::Level::INFO, "test").in_scope(|| { tracing::trace!("status: {}", "ok"); tracing::info!("log1111111"); thread::sleep(Duration::from_millis(10)) }); tracing::info!(monotonic_counter.foo = 1); tracing::info!(monotonic_counter.bar = 1.1); tracing::info!(counter.baz = 1); tracing::info!(counter.baz = -1); tracing::info!(counter.xyz = 1.1); tracing::info!(value.qux = 1); tracing::info!(value.abc = -1); tracing::info!(value.def = 1.1); tracing::warn!("About to exit!"); } // Once this scope is closed, all spans inside are closed as well // opentelemetry::global::shutdown_tracer_provider(); // thread::sleep(Duration::from_millis(10000)); }); // opentelemetry::global::shutdown_tracer_provider(); thread::sleep(Duration::from_millis(1000)); } /// 同步方法 #[tokio::test(flavor = "multi_thread")] async fn test3() -> Result<(), Box> { let tracer = init_sls( ENDPOINT.to_string(), PROJECT.to_string(), INSTANCE_ID.to_string(), AK_ID.to_string(), AK_SECRET.to_string(), SERVICE_VERSION.to_string(), SERVICE_NAME.to_string(), SERVICE_NAMESPACE.to_string(), HOST_NAME.to_string(), ); // let metrics = init_sls_metrics(); tracing_subscriber::Registry::default() .with( tracing_subscriber::filter::FilterFn::new(|_| true) .with_max_level_hint(tracing_subscriber::filter::LevelFilter::INFO), ) .with(tracing_opentelemetry::layer().with_tracer(tracer)) // .with(tracing_opentelemetry::MetricsLayer::new(metrics)) // .with(tracing_subscriber::fmt::layer()) .init(); { let root = tracing::info_span!("app_start", aaa = 111, bbb = 222); let _enter = root.enter(); tracing::info!("log test"); // 同步调用 work(); // 异步调用 work_async() .instrument(tracing::info_span!("child work async root")) .await; // 自动创建span方法 let _ = expensive_work().await; } // Once this scope is closed, all spans inside are closed as well opentelemetry::global::shutdown_tracer_provider(); // thread::sleep(Duration::from_millis(10000)); Ok(()) } fn work() { let root = tracing::info_span!("child work"); let _enter = root.enter(); } async fn work_async() { let root = tracing::info_span!("child work async"); let _enter = root.enter(); }