Skip to content

Commit

Permalink
Merge pull request #95 from frigus02/log-exception
Browse files Browse the repository at this point in the history
Log exception in tracing example
  • Loading branch information
frigus02 authored Jan 13, 2025
2 parents 92a11f4 + ef5dd29 commit 958d86f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
27 changes: 21 additions & 6 deletions examples/opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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<dyn std::error::Error> = "An error".into();
span.record_error(error.as_ref());
}
}

#[tokio::main]
Expand All @@ -68,6 +75,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
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")
Expand All @@ -85,7 +93,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
.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");
Expand Down
29 changes: 21 additions & 8 deletions examples/tracing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -29,22 +29,29 @@ 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<dyn std::error::Error> = "An error".into();
tracing::error!(error = error, "exception");
// or: tracing::error!(error = "An error");
}
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
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")
Expand All @@ -63,7 +70,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
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");
Expand Down

0 comments on commit 958d86f

Please sign in to comment.