From ef5dd29030113887b8b41ec2b104562fee56ee83 Mon Sep 17 00:00:00 2001 From: Jan Kuehle Date: Mon, 13 Jan 2025 22:04:03 +0100 Subject: [PATCH] Log exception in tracing example And add the same logging in the equivalent opentelemetry example. --- examples/opentelemetry.rs | 27 +++++++++++++++++++++------ examples/tracing/src/main.rs | 29 +++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/examples/opentelemetry.rs b/examples/opentelemetry.rs index 8e506ac..2f6faf4 100644 --- a/examples/opentelemetry.rs +++ b/examples/opentelemetry.rs @@ -20,20 +20,21 @@ async fn spawn_children(n: u32, process_name: String) { KeyValue::new("process_name", process_name.clone()), ]); let cx = Context::current_with_span(span); - for _ in 0..n { - spawn_child_process(&process_name) + for child_no in 0..n { + spawn_child_process(&process_name, child_no) .with_context(cx.clone()) .await; } } -async fn spawn_child_process(process_name: &str) { +async fn spawn_child_process(process_name: &str, child_no: u32) { let tracer = global::tracer("spawn_child_process"); let mut span = tracer .span_builder("spawn_child_process") .with_kind(SpanKind::Client) .start(&tracer); span.set_attribute(KeyValue::new("process_name", process_name.to_string())); + span.set_attribute(KeyValue::new("child_no", child_no as i64)); let cx = Context::current_with_span(span); let mut injector = HashMap::new(); @@ -45,6 +46,7 @@ async fn spawn_child_process(process_name: &str) { .remove("traceparent") .expect("propagator should inject traceparent"), ) + .arg(child_no.to_string()) .spawn() .expect("failed to spawn"); child @@ -54,11 +56,16 @@ async fn spawn_child_process(process_name: &str) { .expect("awaiting process failed"); } -async fn run_in_child_process() { +async fn run_in_child_process(child_no: u32) { let tracer = global::tracer("run_in_child_process"); let mut span = tracer.start("run_in_child_process"); + span.set_attribute(KeyValue::new("child_no", child_no as i64)); span.add_event("leaf fn", vec![]); - sleep(Duration::from_millis(50)).await + sleep(Duration::from_millis(50)).await; + if (child_no + 1) % 4 == 0 { + let error: Box = "An error".into(); + span.record_error(error.as_ref()); + } } #[tokio::main] @@ -68,6 +75,7 @@ async fn main() -> Result<(), Box> { let mut iter = env::args(); let process_name = iter.next().expect("0th argument should exist"); let traceparent = iter.next(); + let child_no = iter.next(); let tracer = opentelemetry_application_insights::new_pipeline_from_env() .expect("env var APPLICATIONINSIGHTS_CONNECTION_STRING should exist") @@ -85,7 +93,14 @@ async fn main() -> Result<(), Box> { .with_kind(SpanKind::Server) .start(&tracer); let cx = Context::current_with_span(span); - run_in_child_process().with_context(cx).await; + run_in_child_process( + child_no + .expect("child process has child_no arg") + .parse() + .expect("child_no arg is u32"), + ) + .with_context(cx) + .await; } _ => { let span = tracer.start("root"); diff --git a/examples/tracing/src/main.rs b/examples/tracing/src/main.rs index f422b30..747bd35 100644 --- a/examples/tracing/src/main.rs +++ b/examples/tracing/src/main.rs @@ -13,13 +13,13 @@ use tracing_subscriber::{layer::SubscriberExt, Registry}; #[instrument] async fn spawn_children(n: u32, process_name: String) { - for _ in 0..n { - spawn_child_process(&process_name).await; + for child_no in 0..n { + spawn_child_process(&process_name, child_no).await; } } #[instrument(fields(otel.kind = "client"))] -async fn spawn_child_process(process_name: &str) { +async fn spawn_child_process(process_name: &str, child_no: u32) { let mut injector = HashMap::new(); let propagator = TraceContextPropagator::new(); propagator.inject_context(&Span::current().context(), &mut injector); @@ -29,15 +29,21 @@ async fn spawn_child_process(process_name: &str) { .remove("traceparent") .expect("propagator should inject traceparent"), ) + .arg(child_no.to_string()) .spawn() .expect("failed to spawn"); child.wait().await.expect("awaiting process failed"); } -#[instrument(fields(test.in_attr="in attr"))] -async fn run_in_child_process() { - tracing::info!(test.in_event="in event", "leaf fn"); - sleep(Duration::from_millis(50)).await +#[instrument] +async fn run_in_child_process(child_no: u32) { + tracing::info!("leaf fn"); + sleep(Duration::from_millis(50)).await; + if (child_no + 1) % 4 == 0 { + let error: Box = "An error".into(); + tracing::error!(error = error, "exception"); + // or: tracing::error!(error = "An error"); + } } #[tokio::main] @@ -45,6 +51,7 @@ async fn main() -> Result<(), Box> { let mut iter = env::args(); let process_name = iter.next().expect("0th argument should exist"); let traceparent = iter.next(); + let child_no = iter.next(); let provider = opentelemetry_application_insights::new_pipeline_from_env() .expect("env var APPLICATIONINSIGHTS_CONNECTION_STRING should exist") @@ -63,7 +70,13 @@ async fn main() -> Result<(), Box> { let span = tracing::info_span!("child", otel.kind = "server"); span.set_parent(cx); let _guard = span.enter(); - run_in_child_process().await; + run_in_child_process( + child_no + .expect("child process has child_no arg") + .parse() + .expect("child_no arg is u32"), + ) + .await; } _ => { let span = tracing::info_span!("root");