Skip to content

Commit 7e8a6bf

Browse files
authored
refactor send_to_logfire setting to match Python, start adding docs (#9)
* refactor `send_to_logfire` setting to match Python, start adding docs * lint fixes
1 parent ea98f24 commit 7e8a6bf

File tree

6 files changed

+271
-41
lines changed

6 files changed

+271
-41
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
>
1414
> This is an initial release of the Logfire Rust SDK. We've been using it internally to build Logfire for some time, and it is serving us well. As we're using it ourselves in production, we figured it's ready for everyone else also using Logfire.
1515
>
16-
> We are continually iterating to make this SDK better. We'd love your feedback on all aspects of the SDK and are keen to make the design as idiomatic and performant as possible.
16+
> We are continually iterating to make this SDK better. We'd love your feedback on all aspects of the SDK and are keen to make the design as idiomatic and performant as possible. There are also many features currently supported by the Python SDK which are not yet supported by this SDK; please open issues to help us prioritize these to close this gap.
1717
>
1818
> In particular, the current coupling to `tracing` is an open design point. By building on top of tracing we get widest compatibility and a relatively simple SDK, however to make Logfire-specific adjustments we might prefer in future to move `tracing` to be an optional integration.
1919

examples/basic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! A basic example of using Logfire to instrument Rust code.
2+
13
use std::sync::LazyLock;
24

35
use opentelemetry::{KeyValue, metrics::Counter};
@@ -12,7 +14,6 @@ static BASIC_COUNTER: LazyLock<Counter<u64>> = LazyLock::new(|| {
1214
fn main() -> Result<(), Box<dyn std::error::Error>> {
1315
let shutdown_handler = logfire::configure()
1416
.install_panic_handler()
15-
.send_to_logfire(true)
1617
.console_mode(logfire::ConsoleMode::Fallback)
1718
.finish()?;
1819

src/bridges/log.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ mod tests {
9595
.line(Some(42))
9696
.build();
9797

98-
let _ = crate::configure().send_to_logfire(false).finish();
98+
let _ = crate::configure()
99+
.send_to_logfire(crate::SendToLogfire::No)
100+
.finish();
99101
crate::span!("root span",).in_scope(|| logger.log(&record));
100102
},
101103
);

src/config.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use std::{fmt::Display, str::FromStr};
2+
3+
use tracing::Level;
4+
5+
use crate::ConfigureError;
6+
7+
/// Whether to send logs to Logfire.
8+
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
9+
pub enum SendToLogfire {
10+
#[default]
11+
Yes,
12+
No,
13+
IfTokenPresent,
14+
}
15+
16+
impl Display for SendToLogfire {
17+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18+
match self {
19+
SendToLogfire::Yes => write!(f, "yes"),
20+
SendToLogfire::No => write!(f, "no"),
21+
SendToLogfire::IfTokenPresent => write!(f, "if-token-present"),
22+
}
23+
}
24+
}
25+
26+
impl FromStr for SendToLogfire {
27+
type Err = ConfigureError;
28+
29+
fn from_str(s: &str) -> Result<Self, Self::Err> {
30+
match s {
31+
"yes" => Ok(SendToLogfire::Yes),
32+
"no" => Ok(SendToLogfire::No),
33+
"if-token-present" => Ok(SendToLogfire::IfTokenPresent),
34+
_ => Err(ConfigureError::InvalidConfigurationValue {
35+
parameter: "LOGFIRE_SEND_TO_LOGFIRE",
36+
value: s.to_owned(),
37+
}),
38+
}
39+
}
40+
}
41+
42+
/// Options for controlling console output.
43+
#[expect(clippy::struct_excessive_bools)] // Config options, bools make sense here.
44+
pub struct ConsoleOptions {
45+
pub colors: ConsoleColors,
46+
/// How spans are shown in the console.
47+
pub span_style: SpanStyle,
48+
/// Whether to include timestamps in the console output.
49+
pub include_timestamps: bool,
50+
/// Whether to include tags in the console output.
51+
pub include_tags: bool,
52+
/// Whether to show verbose output.
53+
///
54+
/// It includes the filename, log level, and line number.
55+
pub verbose: bool,
56+
/// The minimum log level to show in the console.
57+
pub min_log_level: Level,
58+
/// Whether to print the URL of the Logfire project after initialization.
59+
pub show_project_link: bool,
60+
}
61+
62+
impl Default for ConsoleOptions {
63+
fn default() -> Self {
64+
ConsoleOptions {
65+
colors: ConsoleColors::default(),
66+
span_style: SpanStyle::default(),
67+
include_timestamps: true,
68+
include_tags: true,
69+
verbose: false,
70+
min_log_level: Level::INFO,
71+
show_project_link: true,
72+
}
73+
}
74+
}
75+
76+
/// Whether to show colors in the console.
77+
#[derive(Default)]
78+
pub enum ConsoleColors {
79+
#[default]
80+
Auto,
81+
Always,
82+
Never,
83+
}
84+
85+
/// Style for rendering spans in the console.
86+
#[derive(Default)]
87+
pub enum SpanStyle {
88+
Simple,
89+
Indented,
90+
#[default]
91+
ShowParents,
92+
HideChildren,
93+
}

src/internal/exporters/remove_pending.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl<Inner: SpanExporter> SpanExporter for RemovePendingSpansExporter<Inner> {
5252
mod tests {
5353
use std::time::Duration;
5454

55+
use crate::config::SendToLogfire;
5556
use crate::set_local_logfire;
5657
use crate::tests::DeterministicExporter;
5758
use crate::tests::DeterministicIdGenerator;
@@ -87,7 +88,7 @@ mod tests {
8788

8889
let mut config = crate::configure();
8990
config
90-
.send_to_logfire(false)
91+
.send_to_logfire(SendToLogfire::No)
9192
.install_panic_handler()
9293
.with_tracer_provider(provider)
9394
.with_defalt_level_filter(LevelFilter::TRACE);

0 commit comments

Comments
 (0)