|
| 1 | +//! Configuration options for the Logfire SDK. |
| 2 | +//! |
| 3 | +//! See [`LogfireConfigBuilder`][crate::LogfireConfigBuilder] for documentation of all these options. |
| 4 | +
|
1 | 5 | use std::{fmt::Display, str::FromStr};
|
2 | 6 |
|
| 7 | +use opentelemetry_sdk::trace::{IdGenerator, SpanProcessor}; |
3 | 8 | use tracing::Level;
|
4 | 9 |
|
5 | 10 | use crate::ConfigureError;
|
@@ -42,6 +47,16 @@ impl FromStr for SendToLogfire {
|
42 | 47 | }
|
43 | 48 | }
|
44 | 49 |
|
| 50 | +impl From<bool> for SendToLogfire { |
| 51 | + fn from(b: bool) -> Self { |
| 52 | + if b { |
| 53 | + SendToLogfire::Yes |
| 54 | + } else { |
| 55 | + SendToLogfire::No |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
45 | 60 | /// Options for controlling console output.
|
46 | 61 | #[expect(clippy::struct_excessive_bools)] // Config options, bools make sense here.
|
47 | 62 | pub struct ConsoleOptions {
|
@@ -100,3 +115,107 @@ pub enum SpanStyle {
|
100 | 115 | #[default]
|
101 | 116 | ShowParents,
|
102 | 117 | }
|
| 118 | + |
| 119 | +/// Options primarily used for testing by Logfire developers. |
| 120 | +pub struct AdvancedOptions { |
| 121 | + /// Root URL for the Logfire API. |
| 122 | + pub(crate) base_url: String, |
| 123 | + /// Generator for trace and span IDs. |
| 124 | + pub(crate) id_generator: Option<BoxedIdGenerator>, |
| 125 | + // |
| 126 | + // |
| 127 | + // TODO: arguments below supported by Python |
| 128 | + |
| 129 | + // /// Generator for nanosecond start and end timestamps of spans. |
| 130 | + // pub ns_timestamp_generator: Option, |
| 131 | + |
| 132 | + // /// Configuration for OpenTelemetry logging. This is experimental and may be removed. |
| 133 | + // pub log_record_processors: Vec<Box<dyn LogRecordProcessor>>, |
| 134 | +} |
| 135 | + |
| 136 | +impl Default for AdvancedOptions { |
| 137 | + fn default() -> Self { |
| 138 | + AdvancedOptions { |
| 139 | + base_url: "https://logfire-api.pydantic.dev".to_string(), |
| 140 | + id_generator: None, |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +impl AdvancedOptions { |
| 146 | + /// Set the base URL for the Logfire API. |
| 147 | + #[must_use] |
| 148 | + pub fn with_base_url<T: AsRef<str>>(mut self, base_url: T) -> Self { |
| 149 | + self.base_url = base_url.as_ref().into(); |
| 150 | + self |
| 151 | + } |
| 152 | + |
| 153 | + /// Set the ID generator for trace and span IDs. |
| 154 | + #[must_use] |
| 155 | + pub fn with_id_generator<T: IdGenerator + Send + Sync + 'static>( |
| 156 | + mut self, |
| 157 | + generator: T, |
| 158 | + ) -> Self { |
| 159 | + self.id_generator = Some(BoxedIdGenerator::new(Box::new(generator))); |
| 160 | + self |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +/// Wrapper around a `SpanProcessor` to use in `additional_span_processors`. |
| 165 | +#[derive(Debug)] |
| 166 | +pub(crate) struct BoxedSpanProcessor(Box<dyn SpanProcessor>); |
| 167 | + |
| 168 | +impl BoxedSpanProcessor { |
| 169 | + pub fn new(processor: Box<dyn SpanProcessor + Send + Sync>) -> Self { |
| 170 | + BoxedSpanProcessor(processor) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +impl SpanProcessor for BoxedSpanProcessor { |
| 175 | + fn on_start(&self, span: &mut opentelemetry_sdk::trace::Span, cx: &opentelemetry::Context) { |
| 176 | + self.0.on_start(span, cx); |
| 177 | + } |
| 178 | + |
| 179 | + fn on_end(&self, span: opentelemetry_sdk::trace::SpanData) { |
| 180 | + self.0.on_end(span); |
| 181 | + } |
| 182 | + |
| 183 | + fn force_flush(&self) -> opentelemetry_sdk::error::OTelSdkResult { |
| 184 | + self.0.force_flush() |
| 185 | + } |
| 186 | + |
| 187 | + fn shutdown(&self) -> opentelemetry_sdk::error::OTelSdkResult { |
| 188 | + self.0.shutdown() |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +/// Wrapper around an `IdGenerator` to use in `id_generator`. |
| 193 | +#[derive(Debug)] |
| 194 | +pub(crate) struct BoxedIdGenerator(Box<dyn IdGenerator>); |
| 195 | + |
| 196 | +impl BoxedIdGenerator { |
| 197 | + pub fn new(generator: Box<dyn IdGenerator>) -> Self { |
| 198 | + BoxedIdGenerator(generator) |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +impl IdGenerator for BoxedIdGenerator { |
| 203 | + fn new_trace_id(&self) -> opentelemetry::trace::TraceId { |
| 204 | + self.0.new_trace_id() |
| 205 | + } |
| 206 | + |
| 207 | + fn new_span_id(&self) -> opentelemetry::trace::SpanId { |
| 208 | + self.0.new_span_id() |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +#[cfg(test)] |
| 213 | +mod tests { |
| 214 | + use crate::config::SendToLogfire; |
| 215 | + |
| 216 | + #[test] |
| 217 | + fn test_send_to_logfire_from_bool() { |
| 218 | + assert_eq!(SendToLogfire::from(true), SendToLogfire::Yes); |
| 219 | + assert_eq!(SendToLogfire::from(false), SendToLogfire::No); |
| 220 | + } |
| 221 | +} |
0 commit comments