tycho-util 0.3.7

Shared utilities for node components.
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
use std::borrow::Cow;
use std::io::IsTerminal;
use std::num::NonZeroUsize;
use std::ops::Not;
use std::path::{Path, PathBuf};
use std::sync::Once;
use std::time::Duration;

use anyhow::{Context, Result};
use serde::de::Visitor;
use serde::{Deserialize, Deserializer, Serialize};
use tracing::Subscriber;
use tracing_appender::rolling::Rotation;
use tracing_subscriber::filter::Directive;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{EnvFilter, Layer, fmt};

pub struct LoggerTargets {
    directives: Vec<Directive>,
}

impl LoggerTargets {
    pub fn load_from<P: AsRef<Path>>(path: P) -> Result<Self> {
        crate::serde_helpers::load_json_from_file(path)
    }

    pub fn build_subscriber(&self) -> EnvFilter {
        let mut builder = EnvFilter::default();
        for item in &self.directives {
            builder = builder.add_directive(item.clone());
        }
        builder
    }
}

impl<'de> Deserialize<'de> for LoggerTargets {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct LoggerVisitor;

        impl<'de> Visitor<'de> for LoggerVisitor {
            type Value = LoggerTargets;

            fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.write_str("a list of targets")
            }

            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
            where
                A: serde::de::MapAccess<'de>,
            {
                let mut directives = Vec::new();

                while let Some((target, level)) = map.next_entry::<String, String>()? {
                    let directive = format!("{target}={level}")
                        .parse::<Directive>()
                        .map_err(serde::de::Error::custom)?;

                    directives.push(directive);
                }

                Ok(LoggerTargets { directives })
            }
        }

        deserializer.deserialize_map(LoggerVisitor)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggerConfig {
    pub outputs: Vec<LoggerOutput>,
}

impl Default for LoggerConfig {
    fn default() -> Self {
        Self {
            outputs: vec![LoggerOutput::Stderr(STDERR)],
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum LogFormat {
    #[default]
    Auto,
    Human,
    Json,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum LoggerOutput {
    Stderr(LoggerStderrOutput),
    File(LoggerFileOutput),
}

impl LoggerOutput {
    pub fn as_layer<S>(&self) -> Result<Box<dyn Layer<S> + Send + Sync + 'static>>
    where
        S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
    {
        match self {
            Self::Stderr(stderr) => Ok(stderr.as_layer()),
            Self::File(file) => file.as_layer::<S>(),
        }
    }
}

pub const STDERR: LoggerStderrOutput = LoggerStderrOutput {
    format: LogFormat::Auto,
};

#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub struct LoggerStderrOutput {
    #[serde(default)]
    pub format: LogFormat,
}

impl LoggerStderrOutput {
    pub fn as_layer<S>(&self) -> Box<dyn Layer<S> + Send + Sync + 'static>
    where
        S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
    {
        match self.format {
            LogFormat::Human | LogFormat::Auto => human_layer(),
            LogFormat::Json => tracing_stackdriver::layer()
                .with_writer(std::io::stderr)
                .boxed(),
        }
    }
}

fn human_layer<S>() -> Box<dyn Layer<S> + Send + Sync + 'static>
where
    S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
{
    if is_systemd_child() {
        fmt::layer().without_time().with_ansi(false).boxed()
    } else if !std::io::stdout().is_terminal() {
        fmt::layer().with_ansi(false).boxed()
    } else {
        fmt::layer().boxed()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggerFileOutput {
    pub dir: PathBuf,
    #[serde(default, skip_serializing_if = "<&bool>::not")]
    pub human_readable: bool,
    #[serde(default)]
    pub format: Option<LogFormat>,
    #[serde(default = "log_file_prefix")]
    pub file_prefix: String,
    #[serde(default = "max_log_files")]
    pub max_files: NonZeroUsize,
}

impl LoggerFileOutput {
    fn resolved_format(&self) -> LogFormat {
        let format = self.format.unwrap_or_default();

        match format {
            LogFormat::Human => LogFormat::Human,
            LogFormat::Auto if self.human_readable => LogFormat::Human,
            LogFormat::Json | LogFormat::Auto => LogFormat::Json,
        }
    }

    pub fn as_layer<S>(&self) -> Result<Box<dyn Layer<S> + Send + Sync + 'static>>
    where
        S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
    {
        let writer = tracing_appender::rolling::Builder::new()
            .rotation(Rotation::HOURLY)
            .filename_prefix(&self.file_prefix)
            .max_log_files(self.max_files.get())
            .build(&self.dir)?;

        Ok(match self.resolved_format() {
            LogFormat::Human => fmt::layer()
                .without_time()
                .with_ansi(false)
                .with_writer(writer)
                .boxed(),
            LogFormat::Json | LogFormat::Auto => {
                tracing_stackdriver::layer().with_writer(writer).boxed()
            }
        })
    }
}

fn log_file_prefix() -> String {
    "tycho.log".to_owned()
}

fn max_log_files() -> NonZeroUsize {
    NonZeroUsize::new(25).unwrap()
}

pub fn is_systemd_child() -> bool {
    #[cfg(target_os = "linux")]
    unsafe {
        libc::getppid() == 1
            || std::env::var("SYSTEMD_EXEC_PID")
                .ok()
                .and_then(|s| s.parse::<i64>().ok())
                .is_some()
    }

    #[cfg(not(target_os = "linux"))]
    {
        false
    }
}

pub fn init_logger_simple(default_filter: &str) {
    use tracing_subscriber::layer::SubscriberExt;

    let mut filter = Cow::Borrowed(default_filter);
    if let Ok(env) = std::env::var(EnvFilter::DEFAULT_ENV) {
        filter = Cow::Owned(env);
    }

    tracing_subscriber::registry()
        .with(EnvFilter::try_new(filter).expect("tracing directives"))
        .with(STDERR.as_layer())
        .init();
}

/// Initializes logger once.
///
/// All invocations after the successfull initialization will fail.
///
/// If `logger_targets` file path is provided, spawns a new thread which
/// will poll the file metadata and reload logger when file changes.
pub fn init_logger(config: &LoggerConfig, logger_targets: Option<PathBuf>) -> Result<()> {
    use tracing_subscriber::layer::SubscriberExt;
    use tracing_subscriber::reload;

    let try_make_filter = {
        let logger_targets = logger_targets.clone();
        move || {
            Ok::<_, anyhow::Error>(match &logger_targets {
                None => EnvFilter::builder()
                    .with_default_directive(tracing::Level::INFO.into())
                    .from_env_lossy(),
                Some(path) => LoggerTargets::load_from(path)
                    .context("failed to load logger config")?
                    .build_subscriber(),
            })
        }
    };

    static ONCE: Once = Once::new();

    let mut result = None;
    ONCE.call_once(|| {
        result = Some((|| {
            let (layer, handle) = reload::Layer::new(try_make_filter()?);

            let subscriber = tracing_subscriber::registry().with(layer).with(
                config
                    .outputs
                    .iter()
                    .map(|o| o.as_layer())
                    .collect::<Result<Vec<_>>>()?,
            );
            tracing::subscriber::set_global_default(subscriber)?;

            Ok::<_, anyhow::Error>(handle)
        })());
    });

    let handle = match result {
        Some(res) => res?,
        None => anyhow::bail!("logger was already initialized"),
    };

    if let Some(logger_config) = logger_targets {
        const INTERVAL: Duration = Duration::from_secs(10);

        // NOTE: We are using a simple thread there instead of tokio
        // so that there is no surprise when using it during the app start.
        std::thread::Builder::new()
            .name("watch_logger_config".to_owned())
            .spawn(move || {
                tracing::info!(
                    logger_config = %logger_config.display(),
                    "started watching for changes in logger config"
                );

                let get_metadata = move || {
                    std::fs::metadata(&logger_config)
                        .ok()
                        .and_then(|m| m.modified().ok())
                };

                let mut last_modified = get_metadata();

                loop {
                    std::thread::sleep(INTERVAL);

                    let modified = get_metadata();
                    if last_modified == modified {
                        continue;
                    }
                    last_modified = modified;

                    match try_make_filter() {
                        Ok(filter) => {
                            if handle.reload(filter).is_err() {
                                break;
                            }
                            tracing::info!("reloaded logger config");
                        }
                        Err(e) => tracing::error!(%e, "failed to reload logger config"),
                    }
                }

                tracing::info!("stopped watching for changes in logger config");
            })?;
    }

    Ok(())
}

pub fn set_abort_with_tracing() {
    std::panic::set_hook(Box::new(|info| {
        use std::io::Write;

        let backtrace = std::backtrace::Backtrace::force_capture();
        tracing::error!("panic: {info}\n{backtrace}");

        std::io::stderr().flush().ok();
        std::io::stdout().flush().ok();

        #[allow(clippy::exit)]
        std::process::exit(1);
    }));
}

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

    #[test]
    fn deserializes_stderr_default_format() {
        let output: LoggerOutput = serde_json::from_str(r#"{ "type": "Stderr" }"#).unwrap();
        match output {
            LoggerOutput::Stderr(stderr) => assert_eq!(stderr.format, LogFormat::Auto),
            LoggerOutput::File(_) => panic!("expected stderr output"),
        }
    }

    #[test]
    fn resolves_file_format_from_legacy() {
        let output: LoggerOutput = serde_json::from_str(
            r#"{
                "type": "File",
                "dir": "/var/log/tycho",
                "human_readable": true
            }"#,
        )
        .unwrap();
        match output {
            LoggerOutput::File(file) => assert_eq!(file.resolved_format(), LogFormat::Human),
            LoggerOutput::Stderr(_) => panic!("expected file"),
        }
    }

    #[test]
    fn format_has_more_prio_than_human_readable() {
        let output: LoggerOutput = serde_json::from_str(
            r#"{
                "type": "File",
                "dir": "/var/log/tycho",
                "human_readable": true,
                "format": "json"
            }"#,
        )
        .unwrap();
        match output {
            LoggerOutput::File(file) => assert_eq!(file.resolved_format(), LogFormat::Json),
            LoggerOutput::Stderr(_) => panic!("expected file"),
        }
    }

    #[test]
    fn resolves_auto_to_boolean_default() {
        let output: LoggerOutput = serde_json::from_str(
            r#"{
                "type": "File",
                "dir": "/var/log/tycho",
                "format": "auto"
            }"#,
        )
        .unwrap();
        match output {
            LoggerOutput::File(file) => assert_eq!(file.resolved_format(), LogFormat::Json),
            LoggerOutput::Stderr(_) => panic!("expected file"),
        }
    }
}