mod common;
use std::time::Duration;
use scopedb_client::AppendDeliveryOutcome;
use scopedb_client::AppendFailurePolicy;
use scopedb_client::AppendStream;
use uuid::Uuid;
fn track(stream: &AppendStream, request: u64) -> bool {
match stream.try_send(&serde_json::json!({
"event_id": Uuid::now_v7(),
"occurred_at": jiff::Timestamp::now(),
"name": "request.completed",
"attributes": {"request": request, "status": 200},
})) {
Ok(()) => true,
Err(error) => {
eprintln!("telemetry row dropped locally: {error}");
false
}
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = common::client()?;
let table = common::write_table(&client)?;
let telemetry = table
.append_stream()
.failure_policy(AppendFailurePolicy::Continue)
.batch_bytes(1024 * 1024)
.flush_interval(Duration::from_secs(1))
.max_pending_bytes(32 * 1024 * 1024)
.max_in_flight_requests(2)
.attempt_timeout(Duration::from_secs(10))
.on_batch_failure(|event| {
eprintln!("telemetry append failure: {event:?}");
})
.build()?;
let mut dropped_locally = 0_u64;
for request in 1..=100 {
if !track(&telemetry, request) {
dropped_locally += 1;
}
}
println!("locally dropped: {dropped_locally}");
let report = telemetry.shutdown().await?;
if report.outcome != AppendDeliveryOutcome::Ok {
eprintln!("telemetry loss or ambiguity: {report:?}");
}
println!("final stream stats: {:?}", telemetry.stats());
Ok(())
}