-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy path11_observability.rs
More file actions
131 lines (117 loc) · 4.66 KB
/
Copy path11_observability.rs
File metadata and controls
131 lines (117 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//! Observability example: tracing spans and structured logging
//!
//! Tonbo uses the `tracing` crate for observability. This example shows how to
//! configure different subscribers for development, production, and distributed
//! tracing with OpenTelemetry.
//!
//! Run: cargo run --example 11_observability
//!
//! With debug output:
//! RUST_LOG=tonbo=debug cargo run --example 11_observability
use tonbo::prelude::*;
use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};
#[derive(Record)]
struct Event {
#[metadata(k = "tonbo.key", v = "true")]
id: String,
kind: String,
payload: Option<String>,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// =========================================================================
// Option 1: Development - human-readable output with colors
// =========================================================================
// Uncomment to use:
//
// tracing_subscriber::fmt()
// .with_env_filter("info,tonbo=debug")
// .init();
// =========================================================================
// Option 2: Production - JSON output for log aggregation
// =========================================================================
// Uncomment to use:
//
// tracing_subscriber::fmt()
// .json()
// .with_env_filter("info,tonbo=info")
// .init();
// =========================================================================
// Option 3: Production with file output (non-blocking)
// =========================================================================
// Uncomment to use (requires tracing-appender):
//
// let file_appender = tracing_appender::rolling::daily("logs", "app.log");
// let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
// tracing_subscriber::fmt()
// .json()
// .with_writer(non_blocking)
// .with_env_filter("info,tonbo=debug")
// .init();
// =========================================================================
// Option 4: OpenTelemetry for distributed tracing
// =========================================================================
// Uncomment to use (requires opentelemetry crates):
//
// use opentelemetry::trace::TracerProvider;
// use opentelemetry_otlp::WithExportConfig;
// use opentelemetry_sdk::runtime::Tokio;
//
// let exporter = opentelemetry_otlp::SpanExporter::builder()
// .with_tonic()
// .with_endpoint("http://localhost:4317")
// .build()?;
//
// let provider = opentelemetry_sdk::trace::SdkTracerProvider::builder()
// .with_batch_exporter(exporter, Tokio)
// .build();
//
// let tracer = provider.tracer("tonbo-app");
//
// tracing_subscriber::registry()
// .with(tracing_opentelemetry::layer().with_tracer(tracer))
// .with(fmt::layer().with_filter(EnvFilter::from_default_env()))
// .init();
// =========================================================================
// For this example: simple fmt subscriber with env filter
// =========================================================================
tracing_subscriber::registry()
.with(fmt::layer())
.with(
EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("info,tonbo=debug")),
)
.init();
println!("=== Tonbo Observability Example ===\n");
println!("Tonbo emits tracing spans for async operations like WAL replay,");
println!("compaction, and scans. Configure any tracing subscriber to capture them.\n");
// Create database - this will emit spans for WAL operations
let db = DbBuilder::from_schema(Event::schema())?
.on_disk("/tmp/tonbo_observability")?
.open()
.await?;
// Insert some data
let events = vec![
Event {
id: "evt1".into(),
kind: "click".into(),
payload: Some("button_ok".into()),
},
Event {
id: "evt2".into(),
kind: "view".into(),
payload: None,
},
];
let mut builders = Event::new_builders(events.len());
builders.append_rows(events);
db.ingest(builders.finish().into_record_batch()).await?;
// Query - this will emit spans for scan operations
let batches = db.scan().collect().await?;
println!(
"Inserted and queried {} events",
batches.iter().map(|b| b.num_rows()).sum::<usize>()
);
println!("\nCheck your terminal output above for tracing spans (with RUST_LOG=tonbo=debug)");
Ok(())
}