|
| 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