-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedded.rs
More file actions
67 lines (57 loc) · 2.66 KB
/
Copy pathembedded.rs
File metadata and controls
67 lines (57 loc) · 2.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
//! Embed tael inside another application: an in-process server, programmatic
//! queries through the typed client, CLI-style command dispatch, and the
//! interactive `tael live` TUI — all from one host binary.
//!
//! Run with:
//!
//! ```sh
//! cargo run -p tael-cli --example embedded # server + queries
//! cargo run -p tael-cli --example embedded -- --live # also open the live TUI
//! ```
use std::time::Duration;
use tael_cli::tael_server::{ServerConfig, run_embedded};
use tael_cli::{Commands, GlobalOpts, TaelClient};
const REST_ADDR: &str = "127.0.0.1:17701";
const OTLP_ADDR: &str = "127.0.0.1:14317";
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// 1. Start the full tael server (OTLP ingest, storage, REST API) on a
// background task inside this process. `run_embedded` is quiet: no
// startup banner, and it leaves the host's tracing subscriber alone.
let mut config = ServerConfig::from_env();
config.otlp_grpc_addr = OTLP_ADDR.to_string();
config.rest_api_addr = REST_ADDR.to_string();
config.rest_api_socket = None;
let data_root = std::env::temp_dir().join("tael-embedded-example");
config.data_dir = data_root.join("data").to_string_lossy().into_owned();
config.wal_dir = data_root.join("wal").to_string_lossy().into_owned();
tokio::spawn(run_embedded(config));
let server_url = format!("http://{REST_ADDR}");
let client = TaelClient::new(&server_url);
// Wait until the REST API accepts connections.
for attempt in 0..50 {
match client.healthz().await {
Ok(_) => break,
Err(e) if attempt == 49 => return Err(e.context("embedded server never became ready")),
Err(_) => tokio::time::sleep(Duration::from_millis(100)).await,
}
}
println!("embedded tael server ready on {server_url} (OTLP gRPC on {OTLP_ADDR})");
// 2. Query it programmatically with the typed client — structured JSON in,
// no subprocess, no stdout parsing.
let services = client.list_services().await?;
println!("services: {services}");
// 3. Or dispatch any `tael` CLI command in-process, output rendering
// included — the same code path as the real binary.
let opts = GlobalOpts {
server: server_url.clone(),
..GlobalOpts::default()
};
tael_cli::run_command(Commands::Services, &opts).await?;
// 4. Hand the terminal to the `tael live` TUI. It restores the terminal
// when the user quits, so the host application can carry on after.
if std::env::args().any(|a| a == "--live") {
tael_cli::tui::run_with_options(&server_url, tael_cli::tui::LiveOptions::default()).await?;
}
Ok(())
}