otelite 0.1.98

Otelite: OTLP receiver, dashboard, and CLI for local OpenTelemetry observability
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
//! Configuration for the Otelite CLI

use std::path::PathBuf;
use std::time::Duration;

use serde::Deserialize;

/// Output format for CLI commands
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OutputFormat {
    /// Pretty-printed table format (default)
    #[default]
    Pretty,
    /// JSON format for machine parsing
    Json,
    /// Compact JSON format (single-line, for piping to jq)
    JsonCompact,
}

impl std::str::FromStr for OutputFormat {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "pretty" => Ok(Self::Pretty),
            "json" => Ok(Self::Json),
            "json-compact" => Ok(Self::JsonCompact),
            _ => Err(format!(
                "Invalid output format: '{}'. Use 'pretty', 'json', or 'json-compact'",
                s
            )),
        }
    }
}

/// CLI configuration
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
    /// Otelite backend endpoint URL
    pub endpoint: String,
    /// Request timeout duration
    pub timeout: Duration,
    /// Output format (pretty or json)
    pub format: OutputFormat,
    /// Disable color output
    pub no_color: bool,
    /// Disable table headers
    pub no_header: bool,
    /// Disable automatic paging
    pub no_pager: bool,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            endpoint: "http://localhost:3000".to_string(),
            timeout: Duration::from_secs(30),
            format: OutputFormat::Pretty,
            no_color: false,
            no_header: false,
            no_pager: false,
        }
    }
}

impl Config {
    /// Create a new configuration with custom values
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        endpoint: String,
        timeout: Duration,
        format: OutputFormat,
        no_color: bool,
        no_header: bool,
        no_pager: bool,
    ) -> Self {
        Self {
            endpoint,
            timeout,
            format,
            no_color,
            no_header,
            no_pager,
        }
    }

    /// Get endpoint from environment variable or use default
    pub fn endpoint_from_env() -> String {
        std::env::var("OTELITE_ENDPOINT").unwrap_or_else(|_| "http://localhost:3000".to_string())
    }

    /// Get the config directory path
    /// ($XDG_CONFIG_HOME/otelite when set, else ~/.config/otelite)
    pub fn config_dir() -> PathBuf {
        if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
            if !xdg.is_empty() {
                return PathBuf::from(xdg).join("otelite");
            }
        }
        let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
        PathBuf::from(home).join(".config").join("otelite")
    }

    /// Get the config file path (~/.config/otelite/config.toml)
    pub fn config_file() -> PathBuf {
        Self::config_dir().join("config.toml")
    }

    /// Load configuration from the config file, if one exists.
    ///
    /// Precedence (highest first): CLI flags, the `OTELITE_ENDPOINT`
    /// environment variable, this file, built-in defaults. This function
    /// returns only the file layer (falling back to built-in defaults for
    /// keys the file does not set); callers apply flags on top.
    ///
    /// A missing file is not an error — built-in defaults are returned.
    /// A present-but-unparseable file is an error, because silently
    /// ignoring a configuration the user edited would hide the typo.
    pub fn load_file() -> Result<Config, crate::error::Error> {
        let path = Self::config_file();
        if !path.exists() {
            return Ok(Self::default());
        }
        let content = std::fs::read_to_string(&path).map_err(|e| {
            crate::error::Error::ConfigError(format!(
                "Failed to read config file {}: {} — check the file permissions,                  or remove the file to fall back to defaults",
                path.display(),
                e
            ))
        })?;
        let parsed: FileConfig = toml::from_str(&content).map_err(|e| {
            crate::error::Error::ConfigError(format!(
                "Failed to parse config file {}: {} — fix the syntax,                  or remove the file to fall back to defaults",
                path.display(),
                e
            ))
        })?;
        let defaults = Self::default();
        Ok(Config {
            endpoint: parsed.endpoint.unwrap_or(defaults.endpoint),
            timeout: parsed
                .timeout
                .map(Duration::from_secs)
                .unwrap_or(defaults.timeout),
            format: match parsed.format {
                Some(f) => f.parse().map_err(crate::error::Error::ConfigError)?,
                None => defaults.format,
            },
            no_color: parsed.no_color.unwrap_or(defaults.no_color),
            no_header: parsed.no_header.unwrap_or(defaults.no_header),
            no_pager: parsed.no_pager.unwrap_or(defaults.no_pager),
        })
    }

    /// Check if this is the first run (config file doesn't exist)
    pub fn is_first_run() -> bool {
        !Self::config_file().exists()
    }

    /// Create the config directory and file with default settings
    pub fn create_default_config() -> std::io::Result<()> {
        let config_dir = Self::config_dir();
        std::fs::create_dir_all(&config_dir)?;

        let config_file = Self::config_file();
        let default_config = r#"# Otelite Configuration
# This file was automatically generated on first run.
#
# Values here are CLI defaults. Precedence: command-line flags >
# OTELITE_ENDPOINT > this file > built-in defaults.
#
# Server behaviour (bind address, OTLP ports, storage location,
# retention) is configured with flags and environment variables,
# not this file: `otelite serve --help`, OTELITE_DATA_DIR,
# OTELITE_OTLP_GRPC_PORT, OTELITE_OTLP_HTTP_PORT, OTELITE_RETENTION_DAYS.

# Backend endpoint URL (default: http://localhost:3000)
# endpoint = "http://localhost:3000"

# Request timeout in seconds (default: 30)
# timeout = 30

# Output format: "pretty", "json" or "json-compact" (default: pretty)
# format = "pretty"

# no_color = false
# no_header = false
# no_pager = false
"#;

        std::fs::write(config_file, default_config)?;
        Ok(())
    }
}

/// Keys accepted in config.toml. Unknown keys are ignored so the file
/// can carry settings for other tools or future releases.
#[derive(Debug, Default, Deserialize)]
struct FileConfig {
    endpoint: Option<String>,
    timeout: Option<u64>,
    format: Option<String>,
    no_color: Option<bool>,
    no_header: Option<bool>,
    no_pager: Option<bool>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Mutex;

    /// Serialise tests that mutate HOME / XDG_CONFIG_HOME, and point them
    /// at a TempDir so the real user config is never read or written.
    static ENV_MUTEX: Mutex<()> = Mutex::new(());

    /// Restores HOME on drop. Declared so it outlives the TempDir (fields
    /// drop in declaration order): the environment must be restored before
    /// the isolated home disappears, or later tests inherit a dangling
    /// HOME.
    struct RestoreHome {
        old: Option<String>,
    }

    impl Drop for RestoreHome {
        fn drop(&mut self) {
            match &self.old {
                Some(value) => std::env::set_var("HOME", value),
                None => std::env::remove_var("HOME"),
            }
        }
    }

    struct IsolatedHome {
        _restore: RestoreHome,
        _tmp: tempfile::TempDir,
        _guard: std::sync::MutexGuard<'static, ()>,
    }

    fn isolated_home() -> IsolatedHome {
        let guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
        let old = std::env::var("HOME").ok();
        let tmp = tempfile::TempDir::new().expect("temp dir");
        std::env::set_var("HOME", tmp.path());
        std::env::remove_var("XDG_CONFIG_HOME");
        IsolatedHome {
            _restore: RestoreHome { old },
            _tmp: tmp,
            _guard: guard,
        }
    }

    fn write_config(tmp: &tempfile::TempDir, contents: &str) -> std::path::PathBuf {
        let dir = tmp.path().join(".config").join("otelite");
        std::fs::create_dir_all(&dir).unwrap();
        let file = dir.join("config.toml");
        std::fs::write(&file, contents).unwrap();
        file
    }

    #[test]
    fn test_output_format_from_str() {
        assert_eq!(
            "pretty".parse::<OutputFormat>().unwrap(),
            OutputFormat::Pretty
        );
        assert_eq!("json".parse::<OutputFormat>().unwrap(), OutputFormat::Json);
        assert_eq!(
            "json-compact".parse::<OutputFormat>().unwrap(),
            OutputFormat::JsonCompact
        );
        assert_eq!(
            "PRETTY".parse::<OutputFormat>().unwrap(),
            OutputFormat::Pretty
        );
        assert_eq!("JSON".parse::<OutputFormat>().unwrap(), OutputFormat::Json);
        assert_eq!(
            "JSON-COMPACT".parse::<OutputFormat>().unwrap(),
            OutputFormat::JsonCompact
        );
        assert!("invalid".parse::<OutputFormat>().is_err());
    }

    #[test]
    fn test_config_default() {
        let config = Config::default();
        assert_eq!(config.endpoint, "http://localhost:3000");
        assert_eq!(config.timeout, Duration::from_secs(30));
        assert_eq!(config.format, OutputFormat::Pretty);
        assert!(!config.no_color);
        assert!(!config.no_header);
    }

    #[test]
    fn test_config_new() {
        let config = Config::new(
            "http://example.com:9090".to_string(),
            Duration::from_secs(60),
            OutputFormat::Json,
            true,
            true,
            true,
        );
        assert_eq!(config.endpoint, "http://example.com:9090");
        assert_eq!(config.timeout, Duration::from_secs(60));
        assert_eq!(config.format, OutputFormat::Json);
        assert!(config.no_color);
        assert!(config.no_header);
        assert!(config.no_pager);
    }

    #[test]
    fn test_load_file_returns_defaults_when_absent() {
        let _iso = isolated_home();
        let config = Config::load_file().expect("no file means defaults, not an error");
        assert_eq!(config.endpoint, "http://localhost:3000");
        assert_eq!(config.timeout, Duration::from_secs(30));
        assert_eq!(config.format, OutputFormat::Pretty);
        assert!(!config.no_color);
    }

    #[test]
    fn test_load_file_reads_values() {
        let iso = isolated_home();
        write_config(
            &iso._tmp,
            r#"
endpoint = "http://127.0.0.1:9999"
timeout = 5
format = "json"
no_color = true
"#,
        );

        let config = Config::load_file().expect("valid file parses");
        assert_eq!(config.endpoint, "http://127.0.0.1:9999");
        assert_eq!(config.timeout, Duration::from_secs(5));
        assert_eq!(config.format, OutputFormat::Json);
        assert!(config.no_color);
        // unset keys keep their defaults
        assert!(!config.no_header);
        assert!(!config.no_pager);
    }

    #[test]
    fn test_load_file_malformed_is_config_error() {
        let iso = isolated_home();
        let path = write_config(&iso._tmp, "endpoint = ");

        let err = Config::load_file().expect_err("broken TOML must be an error");
        let msg = format!("{err}");
        assert!(
            msg.contains("Failed to parse config file"),
            "error names the failure: {msg}"
        );
        assert!(msg.contains("config.toml"), "error names the file: {msg}");
        assert!(path.to_string_lossy().as_ref().contains("config.toml"));
    }

    #[test]
    fn test_load_file_invalid_format_is_config_error() {
        let iso = isolated_home();
        write_config(&iso._tmp, "format = \"bogus\"");

        let err = Config::load_file().expect_err("unknown format must be an error");
        let msg = format!("{err}");
        assert!(
            msg.contains("Invalid output format"),
            "error explains the valid values: {msg}"
        );
    }

    #[test]
    fn test_load_file_ignores_unknown_keys() {
        let iso = isolated_home();
        write_config(
            &iso._tmp,
            r#"
endpoint = "http://127.0.0.1:4000"

[server]
addr = "127.0.0.1:3000"
"#,
        );

        let config = Config::load_file().expect("unknown keys are ignored");
        assert_eq!(config.endpoint, "http://127.0.0.1:4000");
    }

    #[test]
    fn test_config_dir_respects_xdg_config_home() {
        let _iso = isolated_home();
        let xdg = PathBuf::from(std::env::var("HOME").unwrap()).join("xdg-config");
        std::env::set_var("XDG_CONFIG_HOME", &xdg);

        assert_eq!(Config::config_dir(), xdg.join("otelite"));

        std::env::remove_var("XDG_CONFIG_HOME");
    }

    #[test]
    fn test_created_default_config_round_trips_to_defaults() {
        let _iso = isolated_home();

        Config::create_default_config().expect("first-run write succeeds");
        let file = Config::config_file();
        assert!(file.exists(), "template written");

        // The generated template is all commented-out keys, so loading it
        // must yield exactly the built-in defaults.
        let config = Config::load_file().expect("generated template parses");
        assert_eq!(config, Config::default());
    }
}