|
1 | | -use std::path::PathBuf; |
2 | | - |
3 | | -use tracing; |
4 | | -use tracing_appender::rolling::{RollingFileAppender, Rotation}; |
5 | | -use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry}; |
6 | | - |
7 | 1 | pub mod controller; |
8 | 2 | mod k8s_events; |
9 | | - |
10 | | -#[derive(Debug, Clone, clap::ValueEnum, PartialEq, Eq)] |
11 | | -pub enum TracingTarget { |
12 | | - None, |
13 | | - Jaeger, |
14 | | -} |
15 | | - |
16 | | -impl Default for TracingTarget { |
17 | | - fn default() -> Self { |
18 | | - Self::None |
19 | | - } |
20 | | -} |
21 | | - |
22 | | -/// Initializes `tracing` logging with options from the environment variable |
23 | | -/// given in the `env` parameter. |
24 | | -/// |
25 | | -/// We force users to provide a variable name so it can be different per product. |
26 | | -/// We encourage it to be the product name plus `_LOG`, e.g. `FOOBAR_OPERATOR_LOG`. |
27 | | -/// If no environment variable is provided, the maximum log level is set to INFO. |
28 | | -/// |
29 | | -/// Log output can be copied to a file by setting `{env}_DIRECTORY` (e.g. `FOOBAR_OPERATOR_DIRECTORY`) |
30 | | -/// to a directory path. This file will be rotated regularly. |
31 | | -pub fn initialize_logging(env: &str, app_name: &str, tracing_target: TracingTarget) { |
32 | | - let filter = match EnvFilter::try_from_env(env) { |
33 | | - Ok(env_filter) => env_filter, |
34 | | - _ => EnvFilter::try_new(tracing::Level::INFO.to_string()) |
35 | | - .expect("Failed to initialize default tracing level to INFO"), |
36 | | - }; |
37 | | - |
38 | | - let terminal_fmt = tracing_subscriber::fmt::layer(); |
39 | | - |
40 | | - let file_appender_directory = std::env::var_os(format!("{env}_DIRECTORY")).map(PathBuf::from); |
41 | | - let file_fmt = file_appender_directory.as_deref().map(|log_dir| { |
42 | | - let file_appender = RollingFileAppender::builder() |
43 | | - .rotation(Rotation::HOURLY) |
44 | | - .filename_prefix(app_name.to_string()) |
45 | | - .filename_suffix("tracing-rs.json") |
46 | | - .max_log_files(6) |
47 | | - .build(log_dir) |
48 | | - .expect("failed to initialize rolling file appender"); |
49 | | - tracing_subscriber::fmt::layer() |
50 | | - .json() |
51 | | - .with_writer(file_appender) |
52 | | - }); |
53 | | - |
54 | | - let jaeger = match tracing_target { |
55 | | - TracingTarget::Jaeger => { |
56 | | - // FIXME (@Techassi): Replace with opentelemetry_otlp |
57 | | - #[allow(deprecated)] |
58 | | - let jaeger = opentelemetry_jaeger::new_agent_pipeline() |
59 | | - .with_service_name(app_name) |
60 | | - .install_batch(opentelemetry_sdk::runtime::Tokio) |
61 | | - .expect("Failed to initialize Jaeger pipeline"); |
62 | | - let opentelemetry = tracing_opentelemetry::layer().with_tracer(jaeger); |
63 | | - Some(opentelemetry) |
64 | | - } |
65 | | - TracingTarget::None => None, |
66 | | - }; |
67 | | - |
68 | | - Registry::default() |
69 | | - .with(filter) |
70 | | - .with(terminal_fmt) |
71 | | - .with(file_fmt) |
72 | | - .with(jaeger) |
73 | | - .init(); |
74 | | - |
75 | | - // need to delay logging until after tracing is initialized |
76 | | - match file_appender_directory { |
77 | | - Some(dir) => tracing::info!(directory = %dir.display(), "file logging enabled"), |
78 | | - None => tracing::debug!("file logging disabled, because no log directory set"), |
79 | | - } |
80 | | -} |
81 | | - |
82 | | -#[cfg(test)] |
83 | | -mod tests { |
84 | | - |
85 | | - use tracing::{debug, error, info}; |
86 | | - |
87 | | - use crate::logging::TracingTarget; |
88 | | - |
89 | | - // If there is a proper way to programmatically inspect the global max level than we should use that. |
90 | | - // Until then, this is mostly a sanity check for the implementation above. |
91 | | - // Either run |
92 | | - // cargo test default_tracing -- --nocapture |
93 | | - // to see the ERROR and INFO messages, or |
94 | | - // NOT_SET=debug cargo test default_tracing -- --nocapture |
95 | | - // to see them all. |
96 | | - #[test] |
97 | | - fn default_tracing_level_is_set_to_info() { |
98 | | - super::initialize_logging("NOT_SET", "test", TracingTarget::None); |
99 | | - |
100 | | - error!("ERROR level messages should be seen."); |
101 | | - info!("INFO level messages should also be seen by default."); |
102 | | - debug!("DEBUG level messages should be seen only if you set the NOT_SET env var."); |
103 | | - } |
104 | | -} |
0 commit comments