|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +/// Configuration for the tracing. |
| 4 | +#[derive(Debug, Clone, Default)] |
| 5 | +pub struct TracingConfig { |
| 6 | + /// Loki configuration. Enables loki logging if provided. If not - no loki |
| 7 | + /// logging is enabled. |
| 8 | + pub loki: Option<LokiConfig>, |
| 9 | + |
| 10 | + /// Console configuration. Enables console logging if provided. If not - no |
| 11 | + /// console logging is enabled. |
| 12 | + pub console: Option<ConsoleConfig>, |
| 13 | + |
| 14 | + /// Enables metrics logging. If not - no metrics logging is enabled. |
| 15 | + pub metrics: bool, |
| 16 | + |
| 17 | + /// Overrides the environment filter. If not - the environment filter is |
| 18 | + /// used. |
| 19 | + pub override_env_filter: Option<String>, |
| 20 | +} |
| 21 | + |
| 22 | +/// Configuration for the loki logging. |
| 23 | +#[derive(Debug, Clone)] |
| 24 | +pub struct LokiConfig { |
| 25 | + /// URL of the Loki instance. |
| 26 | + pub loki_url: String, |
| 27 | + |
| 28 | + /// Labels to add to the Loki logs. |
| 29 | + pub labels: HashMap<String, String>, |
| 30 | + |
| 31 | + /// Extra fields to add to the Loki logs. |
| 32 | + pub extra_fields: HashMap<String, String>, |
| 33 | +} |
| 34 | + |
| 35 | +/// Configuration for the console logging. |
| 36 | +#[derive(Debug, Clone)] |
| 37 | +pub struct ConsoleConfig { |
| 38 | + /// Whether to include the target module in logs. |
| 39 | + pub with_target: bool, |
| 40 | + |
| 41 | + /// Whether to include the log level in logs. |
| 42 | + pub with_level: bool, |
| 43 | + |
| 44 | + /// Whether to include thread IDs in logs. |
| 45 | + pub with_thread_ids: bool, |
| 46 | + |
| 47 | + /// Whether to include the source file name in logs. |
| 48 | + pub with_file: bool, |
| 49 | + |
| 50 | + /// Whether to include line numbers in logs. |
| 51 | + pub with_line_number: bool, |
| 52 | + |
| 53 | + /// Whether to use ANSI colors in logs. |
| 54 | + pub with_ansi: bool, |
| 55 | +} |
| 56 | + |
| 57 | +impl Default for ConsoleConfig { |
| 58 | + fn default() -> Self { |
| 59 | + Self { |
| 60 | + with_target: true, |
| 61 | + with_level: true, |
| 62 | + with_thread_ids: false, |
| 63 | + with_file: false, |
| 64 | + with_line_number: false, |
| 65 | + with_ansi: true, |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/// Builder for [`TracingConfig`]. |
| 71 | +#[derive(Debug, Clone, Default)] |
| 72 | +pub struct TracingConfigBuilder { |
| 73 | + tracing_config: TracingConfig, |
| 74 | +} |
| 75 | + |
| 76 | +impl TracingConfigBuilder { |
| 77 | + /// Creates a new builder with default values. |
| 78 | + pub fn new() -> Self { |
| 79 | + Self::default() |
| 80 | + } |
| 81 | + |
| 82 | + /// Sets the Loki configuration. |
| 83 | + pub fn loki(mut self, config: LokiConfig) -> Self { |
| 84 | + self.tracing_config.loki = Some(config); |
| 85 | + self |
| 86 | + } |
| 87 | + |
| 88 | + /// Sets the console configuration. |
| 89 | + pub fn console(mut self, config: ConsoleConfig) -> Self { |
| 90 | + self.tracing_config.console = Some(config); |
| 91 | + self |
| 92 | + } |
| 93 | + |
| 94 | + /// Enables console logging with default configuration. |
| 95 | + pub fn with_default_console(mut self) -> Self { |
| 96 | + self.tracing_config.console = Some(ConsoleConfig::default()); |
| 97 | + self |
| 98 | + } |
| 99 | + |
| 100 | + /// Enables console logging and configures whether to include the target |
| 101 | + /// module. |
| 102 | + pub fn console_with_target(mut self, with_target: bool) -> Self { |
| 103 | + self.tracing_config |
| 104 | + .console |
| 105 | + .get_or_insert_with(ConsoleConfig::default) |
| 106 | + .with_target = with_target; |
| 107 | + self |
| 108 | + } |
| 109 | + |
| 110 | + /// Enables console logging and configures whether to include the log level. |
| 111 | + pub fn console_with_level(mut self, with_level: bool) -> Self { |
| 112 | + self.tracing_config |
| 113 | + .console |
| 114 | + .get_or_insert_with(ConsoleConfig::default) |
| 115 | + .with_level = with_level; |
| 116 | + self |
| 117 | + } |
| 118 | + |
| 119 | + /// Enables console logging and configures whether to include thread IDs. |
| 120 | + pub fn console_with_thread_ids(mut self, with_thread_ids: bool) -> Self { |
| 121 | + self.tracing_config |
| 122 | + .console |
| 123 | + .get_or_insert_with(ConsoleConfig::default) |
| 124 | + .with_thread_ids = with_thread_ids; |
| 125 | + self |
| 126 | + } |
| 127 | + |
| 128 | + /// Enables console logging and configures whether to include the source |
| 129 | + /// file name. |
| 130 | + pub fn console_with_file(mut self, with_file: bool) -> Self { |
| 131 | + self.tracing_config |
| 132 | + .console |
| 133 | + .get_or_insert_with(ConsoleConfig::default) |
| 134 | + .with_file = with_file; |
| 135 | + self |
| 136 | + } |
| 137 | + |
| 138 | + /// Enables console logging and configures whether to include line numbers. |
| 139 | + pub fn console_with_line_number(mut self, with_line_number: bool) -> Self { |
| 140 | + self.tracing_config |
| 141 | + .console |
| 142 | + .get_or_insert_with(ConsoleConfig::default) |
| 143 | + .with_line_number = with_line_number; |
| 144 | + self |
| 145 | + } |
| 146 | + |
| 147 | + /// Enables console logging and configures whether to use ANSI colors. |
| 148 | + pub fn console_with_ansi(mut self, with_ansi: bool) -> Self { |
| 149 | + self.tracing_config |
| 150 | + .console |
| 151 | + .get_or_insert_with(ConsoleConfig::default) |
| 152 | + .with_ansi = with_ansi; |
| 153 | + self |
| 154 | + } |
| 155 | + |
| 156 | + /// Enables metrics logging. |
| 157 | + pub fn with_metrics(mut self, enabled: bool) -> Self { |
| 158 | + self.tracing_config.metrics = enabled; |
| 159 | + self |
| 160 | + } |
| 161 | + |
| 162 | + /// Sets whether metrics logging is enabled. |
| 163 | + pub fn metrics(mut self, enabled: bool) -> Self { |
| 164 | + self.tracing_config.metrics = enabled; |
| 165 | + self |
| 166 | + } |
| 167 | + |
| 168 | + /// Sets the environment filter override. |
| 169 | + pub fn override_env_filter(mut self, filter: impl Into<String>) -> Self { |
| 170 | + self.tracing_config.override_env_filter = Some(filter.into()); |
| 171 | + self |
| 172 | + } |
| 173 | + |
| 174 | + /// Builds the [`TracingConfig`]. |
| 175 | + pub fn build(self) -> TracingConfig { |
| 176 | + self.tracing_config |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +impl TracingConfig { |
| 181 | + /// Creates a new builder for [`TracingConfig`]. |
| 182 | + pub fn builder() -> TracingConfigBuilder { |
| 183 | + TracingConfigBuilder::new() |
| 184 | + } |
| 185 | +} |
0 commit comments