tembo-cli 0.21.3

The CLI for Tembo
Documentation
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
use crate::cli::context::{get_current_context, Target};
use crate::cmd::apply::{get_instance_id, get_instance_settings};
use anyhow::anyhow;
use anyhow::{Context, Result};
use base64::{engine::general_purpose, Engine as _};
use chrono::DateTime;
use chrono::{LocalResult, TimeZone, Utc};
use clap::Args;
use futures_util::StreamExt;
use rand::Rng;
use reqwest::header::HeaderMap;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::process::{Command, Output};
use tembo_api_client::apis::configuration::Configuration;
use tokio_tungstenite::tungstenite::protocol::Message;
use tungstenite::http::header::{
    HeaderName as TungsteniteHeaderName, HeaderValue as TungsteniteHeaderValue,
};
use tungstenite::http::Request;
use url::Url;

/// View logs for your instance
#[derive(Args)]
pub struct LogsCommand {
    /// Tail your logs
    #[clap(long, action = clap::ArgAction::SetTrue)]
    tail: bool,

    /// Fetch logs for specific apps
    #[clap(long)]
    app: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
struct LogStream {
    app: String,
    container: String,
    pod: String,
    stream: String,
    tembo_instance_id: String,
    tembo_organization_id: String,
}

#[derive(Serialize, Deserialize, Debug)]
struct Value2 {
    level: String,
    ts: String,
    logger: String,
    msg: String,
    logging_pod: String,
    record: Record,
}

#[derive(Serialize, Deserialize, Debug)]
struct Record {
    log_time: String,
    message: String,
}

#[derive(Serialize, Deserialize, Debug)]
struct LogEntry {
    stream: LogStream,
    values: Vec<Vec<String>>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct LogResult {
    result_type: String,
    result: Vec<LogEntry>,
}

#[derive(Serialize, Deserialize, Debug)]
struct LogData {
    status: String,
    data: LogResult,
}

#[derive(Serialize, Deserialize, Debug)]
struct IndividualLogEntry {
    info: String,
}

#[tokio::main]
pub async fn execute(args: LogsCommand) -> Result<(), anyhow::Error> {
    let env = match get_current_context() {
        Ok(env) => env,
        Err(e) => return Err(anyhow!(e)),
    };

    if env.target == Target::Docker.to_string() {
        let instance_settings = get_instance_settings(None, None)?;
        for (_instance_name, _settings) in instance_settings {
            docker_logs(&_settings.instance_name, args.tail)?;
        }
    } else if env.target == Target::TemboCloud.to_string() {
        cloud_logs(args.tail, args.app).await?;
    }
    Ok(())
}

async fn cloud_logs(tail: bool, app: Option<String>) -> Result<(), anyhow::Error> {
    let env_result = get_current_context()?;
    let org_id = env_result.org_id.clone().unwrap_or_default();
    let profile = env_result.selected_profile.clone().unwrap();
    let tembo_data_host = profile.get_tembo_data_host();
    let tembo_access_token = profile.tembo_access_token.clone();

    let config = Configuration {
        base_path: profile.get_tembo_host(),
        bearer_access_token: Some(tembo_access_token.clone()),
        ..Default::default()
    };

    let instance_settings_result = get_instance_settings(None, None)?;

    for (_key, value) in instance_settings_result.iter() {
        let headers = build_headers(&org_id, &tembo_access_token)?;
        let result_clone = env_result.clone();
        let instance_name = value.instance_name.clone();
        let config_clone = config.clone();

        let instance_id_option = tokio::task::spawn_blocking(move || {
            get_instance_id(&instance_name, &config_clone, &result_clone)
        })
        .await
        .context("Failed to get instance ID")?
        .context("Failed to get instance ID")?;

        if let Some(instance_id) = instance_id_option {
            if tail {
                fetch_logs_websocket(&headers, instance_id).await?;
            } else {
                fetch_logs_query(&tembo_data_host, &headers, instance_id, app.clone()).await?;
            }
        } else {
            eprintln!("Instance ID not found for {}", value.instance_name);
        }
    }

    Ok(())
}

fn build_headers(org_id: &str, tembo_access_token: &str) -> Result<HeaderMap, anyhow::Error> {
    let mut headers = HeaderMap::new();
    headers.insert(
        "X-Scope-OrgID",
        reqwest::header::HeaderValue::from_str(org_id)?,
    );
    headers.insert(
        reqwest::header::AUTHORIZATION,
        reqwest::header::HeaderValue::from_str(&format!("Bearer {}", tembo_access_token))?,
    );
    Ok(headers)
}

async fn fetch_logs_websocket(
    headers: &reqwest::header::HeaderMap,
    instance_id: String,
) -> Result<(), anyhow::Error> {
    let query = format!("{{tembo_instance_id=\"{}\"}}", instance_id);
    let url_encoded_query = urlencoding::encode(&query);
    let ws_url = format!(
        "wss://api.data-1.use1.tembo.io/loki/api/v1/tail?query={}",
        url_encoded_query
    );
    let mut key = [0u8; 16];
    rand::thread_rng().fill(&mut key);
    let sec_websocket_key = general_purpose::STANDARD.encode(key);

    let url = Url::parse(&ws_url)?;
    let host = url
        .host_str()
        .ok_or_else(|| anyhow!("Invalid URL: missing host"))?;
    let port = url
        .port_or_known_default()
        .ok_or_else(|| anyhow!("Invalid URL: missing port"))?;

    let mut request_builder = Request::builder()
        .uri(url.to_string())
        .method("GET")
        .header("Connection", "Upgrade")
        .header("Upgrade", "websocket")
        .header("Sec-WebSocket-Version", "13")
        .header("Sec-WebSocket-Key", sec_websocket_key)
        .header("Host", format!("{}:{}", host, port));

    for (name, value) in headers.iter() {
        let header_name = TungsteniteHeaderName::from_bytes(name.as_str().as_bytes())
            .map_err(|_| anyhow!("Invalid header name: {}", name))?;
        let header_value = TungsteniteHeaderValue::from_bytes(value.as_bytes())
            .map_err(|_| anyhow!("Invalid header value for {}", name.as_str()))?;
        request_builder = request_builder.header(header_name, header_value);
    }

    let request = request_builder
        .body(())
        .map_err(|e| anyhow!("Failed to build request: {}", e))?;

    let (mut ws_stream, _) = tokio_tungstenite::connect_async(request).await?;

    while let Some(message) = ws_stream.next().await {
        match message? {
            Message::Text(text) => {
                beautify_logs(&text, None)?;
            }
            Message::Close(_) => {
                println!("WebSocket connection closed by server");
                break;
            }
            _ => {}
        }
    }

    Ok(())
}

async fn fetch_logs_query(
    tembo_data_host: &str,
    headers: &HeaderMap,
    instance_id: String,
    app_name: Option<String>,
) -> Result<(), anyhow::Error> {
    let client = reqwest::Client::new();
    let query = format!("{{tembo_instance_id=\"{}\"}}", instance_id);
    let url = format!("{}/loki/api/v1/query_range", tembo_data_host);

    let response = client
        .get(url)
        .headers(headers.clone())
        .query(&[("query", &query)])
        .send()
        .await
        .context("Failed to send query request")?;

    if response.status().is_success() {
        let response_body = response
            .text()
            .await
            .context("Failed to read response body")?;
        beautify_logs(&response_body, app_name)?;
    } else {
        eprintln!("Error: {:?}", response.status());
    }

    Ok(())
}

pub fn docker_logs(instance_name: &str, tail: bool) -> Result<()> {
    if tail {
        println!("\nFetching logs for instance: {}\n", instance_name);
        let output = Command::new("docker")
            .args(["logs", "--follow", instance_name])
            .output()
            .with_context(|| {
                format!(
                    "Failed to fetch logs for Docker container '{}'",
                    instance_name
                )
            })?;

        if !output.status.success() {
            eprintln!("Error fetching logs for instance '{}'", instance_name);
            return Ok(());
        }

        print_docker_logs(output)?;
    } else {
        println!("\nFetching logs for instance: {}\n", instance_name);
        let output = Command::new("docker")
            .args(["logs", instance_name])
            .output()
            .with_context(|| {
                format!(
                    "Failed to fetch logs for Docker container '{}'",
                    instance_name
                )
            })?;

        if !output.status.success() {
            eprintln!("Error fetching logs for instance '{}'", instance_name);
            return Ok(());
        }
        print_docker_logs(output)?;
    }

    Ok(())
}

fn beautify_logs(json_data: &str, app_name: Option<String>) -> Result<()> {
    let log_data: LogData = serde_json::from_str(json_data)?;
    let mut entries: BTreeMap<DateTime<Utc>, Vec<String>> = BTreeMap::new();

    for entry in &log_data.data.result {
        if app_name
            .as_ref()
            .is_none_or(|app| entry.stream.container == *app)
        {
            for value in &entry.values {
                match value[0].parse::<i64>() {
                    Ok(unix_timestamp_ns) => {
                        let unix_timestamp = unix_timestamp_ns / 1_000_000_000;
                        match Utc.timestamp_opt(unix_timestamp, 0) {
                            LocalResult::Single(date_time) => {
                                let log_detail = match serde_json::from_str::<Value2>(&value[1]) {
                                    Ok(log_details) => format!(
                                        "{} {}: ({}) {}",
                                        date_time.format("%Y-%m-%d %H:%M:%S"),
                                        log_details.level,
                                        log_details.msg,
                                        log_details.record.message
                                    ),
                                    Err(_) => format!(
                                        "{} {}",
                                        date_time.format("%Y-%m-%d %H:%M:%S"),
                                        &value[1]
                                    ),
                                };
                                entries.entry(date_time).or_default().push(log_detail);
                            }
                            _ => eprintln!("Invalid or ambiguous timestamp: {}", unix_timestamp),
                        }
                    }
                    Err(e) => eprintln!("Error parsing string to i64: {}", e),
                }
            }
        }
    }

    for logs in entries.values() {
        for log in logs {
            println!("{}", log);
        }
    }

    if app_name.is_some() && entries.is_empty() {
        return Err(anyhow!("Couldn't find logs with the specified app"));
    }

    Ok(())
}

fn format_log_line(line: &str) -> Option<String> {
    if line.trim().is_empty() {
        None
    } else if line.contains("LOG:") {
        Some(line.to_string())
    } else {
        Some(format!("System Message: {}", line))
    }
}

fn print_docker_logs(output: Output) -> Result<(), anyhow::Error> {
    let logs_stdout = String::from_utf8_lossy(&output.stdout);
    let logs_stderr = String::from_utf8_lossy(&output.stderr);

    let all_logs = format!("{}{}", logs_stdout, logs_stderr);

    all_logs
        .lines()
        .filter_map(format_log_line)
        .for_each(|line| println!("{}", line));

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::anyhow;
    use assert_cmd::prelude::*;
    use std::env;
    use std::error::Error;
    use std::path::PathBuf;

    const ROOT_DIR: &str = env!("CARGO_MANIFEST_DIR");
    const CARGO_BIN: &str = "tembo";

    #[tokio::test]
    async fn docker_logs() -> Result<(), Box<dyn Error>> {
        let test_dir = PathBuf::from(ROOT_DIR).join("examples").join("set");

        env::set_current_dir(test_dir)?;

        // tembo init
        let mut cmd = Command::cargo_bin(CARGO_BIN)?;
        cmd.arg("init");
        cmd.assert().success();

        // tembo context set --name local
        let mut cmd = Command::cargo_bin(CARGO_BIN)?;
        cmd.arg("context");
        cmd.arg("set");
        cmd.arg("--name");
        cmd.arg("local");
        cmd.assert().success();

        // tembo apply
        let mut cmd = Command::cargo_bin(CARGO_BIN)?;
        cmd.arg("--verbose");
        cmd.arg("apply");
        cmd.assert().success();

        // tembo logs
        let mut cmd = Command::cargo_bin(CARGO_BIN)?;
        cmd.arg("logs");
        cmd.assert().success();

        // tembo delete
        let mut cmd = Command::cargo_bin(CARGO_BIN)?;
        cmd.arg("delete");
        let _ = cmd.ok();

        let mut cmd = Command::new("sh");
        cmd.arg("-c");
        cmd.arg("docker volume rm $(docker volume ls -q)");
        cmd.assert().success();

        Ok(())
    }

    fn mock_query(query: &str) -> Result<String> {
        match query {
            "valid_json" => Ok(r#"{
                "status": "success",
                "data": {
                    "resultType": "matrix",
                    "result": [
                        {
                            "stream": {
                                "app": "test_app",
                                "container": "test_container",
                                "pod": "test_pod",
                                "stream": "test_stream",
                                "tembo_instance_id": "test_id",
                                "tembo_organization_id": "test_org_id"
                            },
                            "values": [
                                ["1234567890", "{\"ts\":\"2024-01-24T21:37:53Z\",\"msg\":\"Valid JSON log entry\"}"]
                            ]
                        },
                        {
                            "stream": {
                                "app": "test_app",
                                "container": "test_container",
                                "pod": "test_pod",
                                "stream": "test_stream",
                                "tembo_instance_id": "test_id",
                                "tembo_organization_id": "test_org_id"
                            },
                            "values": [
                                ["1234567890", "Non-JSON log entry"]
                            ]
                        }
                    ]
                }
            }"#.to_string()),
            _ => Err(anyhow!("Invalid query")),
        }
    }

    #[tokio::test]
    async fn cloud_logs() {
        let valid_json_log = mock_query("valid_json").unwrap();
        beautify_logs(&valid_json_log, None).unwrap();
    }
}