99 lines
3.4 KiB
Rust
99 lines
3.4 KiB
Rust
//! `leleka logs` and `leleka report`: reading the service's own logs back, and bundling the facts a person needs to
|
|
//! attach to feedback.
|
|
//!
|
|
//! The logs are the files `STATE/logs/{leleka,engine,gateway,api,requests}.log` (+ the rotated generations `.1`..`.N`),
|
|
//! written by the processes as one JSON object per line (`leleka-obs`). A line that is not JSON (an engine start-up
|
|
//! line, a library message, a line of an older build) is kept as free text of its source. Nothing here writes to the
|
|
//! logs, contacts a network or sends anything: `report` produces one local file, and only when asked to with `--yes`.
|
|
use crate::cli::UpOptions;
|
|
use leleka_obs::{Level, format_utc_ms, now_ms, parse_duration_ms, parse_utc_ms, redact};
|
|
use serde_json::{Map, Value, json};
|
|
use std::{
|
|
collections::VecDeque,
|
|
io::{BufRead, Read, Seek, SeekFrom, Write},
|
|
path::{Path, PathBuf},
|
|
};
|
|
|
|
/// The log sources, in the order `leleka logs` lists them.
|
|
pub const SOURCES: [&str; 5] = ["leleka", "engine", "gateway", "api", "requests"];
|
|
/// Never read more than this from the end of one file for a plain tail (a request search reads the whole file).
|
|
const TAIL_SCAN_BYTES: u64 = 8 << 20;
|
|
/// A request record is ~0.6 KB: this many bytes of `requests.log` hold several thousand of them.
|
|
const RECORDS_SCAN_BYTES: u64 = 6 << 20;
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct Entry {
|
|
pub ts_ms: Option<u64>,
|
|
pub level: Level,
|
|
pub comp: String,
|
|
pub ev: String,
|
|
pub fields: Map<String, Value>,
|
|
pub raw: String,
|
|
pub source: String,
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct Filter {
|
|
pub since_ms: Option<u64>,
|
|
pub min_level: Level,
|
|
pub component: Option<String>,
|
|
pub source: Option<String>,
|
|
/// A request id or a prefix of one (at least 6 characters).
|
|
pub request: Option<String>,
|
|
pub tail: usize,
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------
|
|
// leleka logs
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
pub struct LogsArgs {
|
|
pub tail: Option<usize>,
|
|
pub since: Option<String>,
|
|
pub level: Option<String>,
|
|
pub component: Option<String>,
|
|
pub source: Option<String>,
|
|
pub request: Option<String>,
|
|
pub follow: bool,
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------
|
|
// leleka report
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct ReportArgs {
|
|
/// Where to write; `-` for standard output. Default: `STATE/reports/leleka-report-<UTC>.json`.
|
|
pub out: Option<String>,
|
|
/// Request records to include.
|
|
pub last: usize,
|
|
/// The explicit opt-in. Without it `report` only shows what it would contain.
|
|
pub yes: bool,
|
|
}
|
|
|
|
/// Everything that makes a report, gathered without contacting any network service.
|
|
pub struct ReportInputs {
|
|
pub now_ms: u64,
|
|
pub version: String,
|
|
pub config: Option<Value>,
|
|
pub expert_tier: Option<Value>,
|
|
pub host: Value,
|
|
pub service: Option<Value>,
|
|
pub recent: Vec<Value>,
|
|
pub warnings: Vec<Entry>,
|
|
pub log_files: Vec<(String, u64)>,
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|
|
|
|
mod collection;
|
|
mod entries;
|
|
mod logs_command;
|
|
mod report;
|
|
pub use collection::{
|
|
collect, join_requests, parse_since, recent_requests, recent_requests_line, summarize,
|
|
};
|
|
pub use entries::{for_each_line, generations, parse_line, render_entry, sources_in};
|
|
pub use logs_command::{build_report, filter_from, run_logs, sanitize};
|
|
pub use report::{gather_report, run_report};
|