Skip to content

Commit 02f48fd

Browse files
mox692Motoyuki Kimura
andauthored
Add simple example for local execution (#306)
Co-authored-by: Motoyuki Kimura <motoyuki.kimura@woven-planet.global>
1 parent b519349 commit 02f48fd

4 files changed

Lines changed: 98 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ members = [
88
"dial9-viewer",
99
"perf-self-profile",
1010
"examples/metrics-service",
11+
"examples/simple-local",
1112
]
1213
exclude = [
1314
"dial9-trace-format/fuzz",

examples/simple-local/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "simple-local"
3+
edition = "2024"
4+
version = "0.1.0"
5+
publish = false
6+
7+
[dependencies]
8+
tokio = { version = "1.51.0", default-features = false, features = ["time"] }
9+
dial9-tokio-telemetry = { path = "../../dial9-tokio-telemetry", features = ["tracing-layer"] }

examples/simple-local/src/main.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use dial9_tokio_telemetry::telemetry::{RotatingWriter, TelemetryHandle, TracedRuntime};
2+
use std::time::Duration;
3+
use tokio::runtime::Builder;
4+
5+
const TRACE_DIR: &str = "/tmp/simple-local-traces";
6+
7+
fn fibonacci_recursive(n: u32) -> u32 {
8+
match n {
9+
0 => 0,
10+
1 => 1,
11+
_ => fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2),
12+
}
13+
}
14+
15+
async fn do_some_work() {
16+
// do some work here
17+
fibonacci_recursive(25);
18+
}
19+
20+
fn main() -> std::io::Result<()> {
21+
// Configure the trace writer
22+
let trace_path = format!("{}/trace.bin", TRACE_DIR);
23+
let writer = RotatingWriter::builder()
24+
.base_path(&trace_path)
25+
.max_file_size(10_000_000) // 10MB per file
26+
.max_total_size(50_000_000) // 50MB total
27+
.segment_metadata(vec![
28+
("service".into(), "simple-local".into()),
29+
("example".into(), "basic".into()),
30+
])
31+
.build()?;
32+
33+
// Build the traced runtime
34+
let mut builder = Builder::new_multi_thread();
35+
builder.worker_threads(2).enable_all();
36+
37+
let traced_builder = TracedRuntime::builder()
38+
.with_trace_path(&trace_path)
39+
.with_task_tracking(true);
40+
41+
let (runtime, guard) = traced_builder.build(builder, writer)?;
42+
guard.enable();
43+
let handle = guard.handle();
44+
45+
// Run the async code
46+
runtime.block_on(async {
47+
handle
48+
.spawn(async move {
49+
let telemetry_handle = TelemetryHandle::current();
50+
let mut handles = vec![];
51+
52+
// Run some concurrent work
53+
for _ in 0..100 {
54+
handles.push(telemetry_handle.spawn(do_some_work()));
55+
tokio::time::sleep(Duration::from_millis(1)).await;
56+
}
57+
58+
// Wait for all tasks to complete
59+
for handle in handles {
60+
handle.await.unwrap()
61+
}
62+
})
63+
.await
64+
.unwrap();
65+
});
66+
67+
// Clean shutdown
68+
drop(runtime);
69+
70+
guard.graceful_shutdown(Duration::from_secs(1))?;
71+
72+
println!("\n✓ Trace files written to: {}", trace_path);
73+
println!(
74+
" You can view them with: cargo run --package dial9-viewer -- --local-dir {}",
75+
TRACE_DIR
76+
);
77+
println!(" Then open http://localhost:3000 in your browser");
78+
79+
Ok(())
80+
}

0 commit comments

Comments
 (0)