use std::time::Duration;
use tokio_util::sync::CancellationToken;
use zelos_trace::TraceRouter;
use zelos_trace::TraceSource;
use zelos_trace_grpc::publish::{TracePublishClient, TracePublishClientConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let url = std::env::var("ZELOS_URL").unwrap_or_else(|_| "grpc://127.0.0.1:2300".to_string());
println!("Connecting to Zelos agent at: {}", url);
let cancellation_token = CancellationToken::new();
let (router, router_task) = TraceRouter::new(cancellation_token.clone());
tokio::spawn(router_task);
let config = TracePublishClientConfig {
url: url.clone(),
..Default::default()
};
let (client, client_task) = TracePublishClient::new(router.clone(), config);
tokio::spawn(client_task);
client
.wait_until_connected(Duration::from_secs(5))
.await
.expect("Failed to connect to agent");
println!("Connected to agent at {}", url);
let source = TraceSource::new("hello-world-example", router.sender());
let hello_event = source
.build_event("hello")
.add_u64_field("count", None)
.add_u64_field("timestamp", Some("ns".to_string()))
.build()
.expect("Failed to register hello event");
println!("Publishing hello message...");
if let Err(e) = hello_event
.build()
.try_insert_u64("count", 1)
.and_then(|b| b.try_insert_u64("timestamp", zelos_trace::time::now_time_ns() as u64))
.and_then(|b| b.emit())
{
eprintln!("Failed to emit hello event: {e}");
return Err(e.into());
}
println!("Successfully published hello message!");
println!("Check your Zelos agent/collector to see the data.");
tokio::time::sleep(Duration::from_millis(1000)).await;
Ok(())
}