Skip to content

Commit 8b0f42f

Browse files
authored
enforce documentation on all public structures (#10)
* enforce documentation on all public structures * lint fixes
1 parent 7e8a6bf commit 8b0f42f

File tree

6 files changed

+386
-161
lines changed

6 files changed

+386
-161
lines changed

Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ export-grpc = ["opentelemetry-otlp/grpc-tonic", "opentelemetry-otlp/tls"]
3939
export-http-protobuf = ["opentelemetry-otlp/http-proto", "opentelemetry-otlp/reqwest-blocking-client", "opentelemetry-otlp/reqwest-rustls"]
4040
export-http-json = ["opentelemetry-otlp/http-json", "opentelemetry-otlp/reqwest-blocking-client", "opentelemetry-otlp/reqwest-rustls"]
4141

42+
[lints.rust]
43+
missing_docs = "warn"
44+
4245
[lints.clippy]
4346
dbg_macro = "deny"
4447
unwrap_used = "deny"
4548
# in general we lint against the pedantic group, but we will whitelist
4649
# certain lints which we don't want to enforce (for now)
4750
pedantic = { level = "deny", priority = -1 }
48-
module_name_repetitions = "allow"
49-
struct_field_names = "allow"
50-
large_futures = "allow" # TODO remove?
51-
doc_markdown = "allow"

src/config.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ use crate::ConfigureError;
77
/// Whether to send logs to Logfire.
88
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
99
pub enum SendToLogfire {
10+
/// Send logs to Logfire.
1011
#[default]
1112
Yes,
13+
/// Do not send logs to Logfire (potentially just print them).
1214
No,
15+
/// Send logs to Logfire only if a token is present.
1316
IfTokenPresent,
1417
}
1518

@@ -42,6 +45,7 @@ impl FromStr for SendToLogfire {
4245
/// Options for controlling console output.
4346
#[expect(clippy::struct_excessive_bools)] // Config options, bools make sense here.
4447
pub struct ConsoleOptions {
48+
/// Whether to show colors in the console.
4549
pub colors: ConsoleColors,
4650
/// How spans are shown in the console.
4751
pub span_style: SpanStyle,
@@ -76,18 +80,23 @@ impl Default for ConsoleOptions {
7680
/// Whether to show colors in the console.
7781
#[derive(Default)]
7882
pub enum ConsoleColors {
83+
/// Decide based on the terminal.
7984
#[default]
8085
Auto,
86+
/// Always show colors.
8187
Always,
88+
/// Never show colors.
8289
Never,
8390
}
8491

8592
/// Style for rendering spans in the console.
8693
#[derive(Default)]
8794
pub enum SpanStyle {
95+
/// Show spans in a simple format.
8896
Simple,
97+
/// Show spans in a detailed format.
8998
Indented,
99+
/// Show parent span ids when printing spans.
90100
#[default]
91101
ShowParents,
92-
HideChildren,
93102
}

src/lib.rs

Lines changed: 77 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ use ulid_id_generator::UlidIdGenerator;
9393

9494
mod internal;
9595

96+
/// An error which may arise when configuring Logfire.
9697
#[derive(Debug, Error)]
9798
#[non_exhaustive]
9899
pub enum ConfigureError {
@@ -116,26 +117,34 @@ pub enum ConfigureError {
116117
#[error("Error configuring the OpenTelemetry tracer: {0}")]
117118
TracingAlreadySetup(#[from] tracing::subscriber::SetGlobalDefaultError),
118119

119-
/// Error parsing the RUST_LOG environment variable.
120+
/// Error parsing the `RUST_LOG` environment variable.
120121
#[error("Error configuring the OpenTelemetry tracer: {0}")]
121122
RustLogInvalid(#[from] tracing_subscriber::filter::FromEnvError),
122123

124+
/// A Rust feature needs to be enabled in the `Cargo.toml`.
123125
#[error("Rust feature required: `{feature_name}` feature must be enabled for {functionality}")]
124126
LogfireFeatureRequired {
127+
/// The feature which is required.
125128
feature_name: &'static str,
129+
/// The functionality which was attempted to be used.
126130
functionality: String,
127131
},
128132

133+
/// A configuration value (from environment) was invalid.
129134
#[error("Invalid configuration value for {parameter}: {value}")]
130135
InvalidConfigurationValue {
136+
/// The name of the configuration parameter.
131137
parameter: &'static str,
138+
/// The invalid value passed for the parameter.
132139
value: String,
133140
},
134141

142+
/// Any other error.
135143
#[error(transparent)]
136144
Other(#[from] Box<dyn std::error::Error + Send + Sync>),
137145
}
138146

147+
/// Whether to print to the console.
139148
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
140149
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
141150
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
@@ -159,15 +168,28 @@ impl FromStr for ConsoleMode {
159168
}
160169
}
161170

162-
pub struct LogfireConfigBuilder {
163-
send_to_logfire: Option<SendToLogfire>,
164-
console_mode: ConsoleMode,
165-
install_panic_handler: bool,
166-
default_level_filter: Option<LevelFilter>,
167-
tracer_provider: Option<SdkTracerProvider>,
168-
}
169-
170-
#[must_use]
171+
/// Main entry point to configure logfire.
172+
///
173+
/// This should be called once at the start of the program.
174+
///
175+
/// See [`LogfireConfigBuilder`] for the full set of configuration options.
176+
///
177+
/// # Example
178+
///
179+
/// ```rust
180+
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
181+
/// let shutdown_handler = logfire::configure()
182+
/// .install_panic_handler()
183+
/// # .send_to_logfire(logfire::SendToLogfire::IfTokenPresent)
184+
/// .finish()?;
185+
///
186+
/// logfire::info!("Hello world");
187+
///
188+
/// shutdown_handler.shutdown()?;
189+
/// Ok(())
190+
/// }
191+
/// ```
192+
#[must_use = "call `.finish()` to complete logfire configuration."]
171193
pub fn configure() -> LogfireConfigBuilder {
172194
LogfireConfigBuilder {
173195
send_to_logfire: None,
@@ -178,7 +200,19 @@ pub fn configure() -> LogfireConfigBuilder {
178200
}
179201
}
180202

203+
/// Builder for logfire configuration, returned from [`logfire::configure()`][configure].
204+
pub struct LogfireConfigBuilder {
205+
send_to_logfire: Option<SendToLogfire>,
206+
console_mode: ConsoleMode,
207+
install_panic_handler: bool,
208+
default_level_filter: Option<LevelFilter>,
209+
tracer_provider: Option<SdkTracerProvider>,
210+
}
211+
181212
impl LogfireConfigBuilder {
213+
/// Call to install a hook to log panics.
214+
///
215+
/// Any existing panic hook will be preserved and called after the logfire panic hook.
182216
pub fn install_panic_handler(&mut self) -> &mut Self {
183217
self.install_panic_handler = true;
184218
self
@@ -191,21 +225,35 @@ impl LogfireConfigBuilder {
191225
self.send_to_logfire = Some(send_to_logfire);
192226
self
193227
}
228+
229+
/// Whether to log to the console.
194230
pub fn console_mode(&mut self, console_mode: ConsoleMode) -> &mut Self {
195231
self.console_mode = console_mode;
196232
self
197233
}
198-
pub fn with_tracer_provider(&mut self, tracer_provider: SdkTracerProvider) -> &mut Self {
199-
self.tracer_provider = Some(tracer_provider);
200-
self
201-
}
234+
235+
/// Override the filter used for traces and logs.
236+
///
237+
/// By default this is set to `LevelFilter::TRACE` if sending to logfire, or `LevelFilter::INFO` if not.
238+
///
239+
/// The `RUST_LOG` environment variable will override this.
202240
pub fn with_defalt_level_filter(&mut self, default_level_filter: LevelFilter) -> &mut Self {
203241
self.default_level_filter = Some(default_level_filter);
204242
self
205243
}
206244

245+
/// Override the tracer provider to use to export opentelemetry data. This will cause
246+
/// `send_to_logfire` to be ignored for spans.
247+
#[cfg(test)]
248+
fn with_tracer_provider(&mut self, tracer_provider: SdkTracerProvider) -> &mut Self {
249+
self.tracer_provider = Some(tracer_provider);
250+
self
251+
}
252+
207253
/// Finish configuring Logfire.
208254
///
255+
/// Because this configures global state for the opentelemetry SDK, this can typically only ever be called once per program.
256+
///
209257
/// # Errors
210258
///
211259
/// See [`ConfigureError`] for possible errors.
@@ -346,7 +394,12 @@ impl LogfireConfigBuilder {
346394
}
347395
}
348396

397+
/// A handler to shutdown the Logfire configuration.
398+
///
399+
/// Calling `.shutdown()` will flush the logfire exporters and make further
400+
/// logfire calls into no-ops.
349401
#[derive(Debug, Clone)]
402+
#[must_use = "this should be kept alive until logging should be stopped"]
350403
pub struct ShutdownHandler {
351404
tracer_provider: SdkTracerProvider,
352405
meter_provider: Option<SdkMeterProvider>,
@@ -598,7 +651,7 @@ const OTEL_EXPORTER_OTLP_PROTOCOL_GRPC: &str = "grpc";
598651
const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF: &str = "http/protobuf";
599652
const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON: &str = "http/json";
600653

601-
/// Temporary workaround for lack of https://github.com/open-telemetry/opentelemetry-rust/pull/2758
654+
/// Temporary workaround for lack of <https://github.com/open-telemetry/opentelemetry-rust/pull/2758>
602655
fn protocol_from_str(value: &str) -> Result<Protocol, ConfigureError> {
603656
match value {
604657
OTEL_EXPORTER_OTLP_PROTOCOL_GRPC => Ok(Protocol::Grpc),
@@ -1024,7 +1077,7 @@ mod tests {
10241077
"code.lineno",
10251078
),
10261079
value: I64(
1027-
964,
1080+
1017,
10281081
),
10291082
},
10301083
KeyValue {
@@ -1150,7 +1203,7 @@ mod tests {
11501203
"code.lineno",
11511204
),
11521205
value: I64(
1153-
965,
1206+
1018,
11541207
),
11551208
},
11561209
KeyValue {
@@ -1286,7 +1339,7 @@ mod tests {
12861339
"code.lineno",
12871340
),
12881341
value: I64(
1289-
965,
1342+
1018,
12901343
),
12911344
},
12921345
KeyValue {
@@ -1428,7 +1481,7 @@ mod tests {
14281481
"code.lineno",
14291482
),
14301483
value: I64(
1431-
966,
1484+
1019,
14321485
),
14331486
},
14341487
KeyValue {
@@ -1570,7 +1623,7 @@ mod tests {
15701623
"code.lineno",
15711624
),
15721625
value: I64(
1573-
968,
1626+
1021,
15741627
),
15751628
},
15761629
KeyValue {
@@ -1740,7 +1793,7 @@ mod tests {
17401793
"code.lineno",
17411794
),
17421795
value: I64(
1743-
969,
1796+
1022,
17441797
),
17451798
},
17461799
KeyValue {
@@ -1819,7 +1872,7 @@ mod tests {
18191872
),
18201873
value: String(
18211874
Owned(
1822-
"src/lib.rs:970:17",
1875+
"src/lib.rs:1023:17",
18231876
),
18241877
),
18251878
},
@@ -1886,7 +1939,7 @@ mod tests {
18861939
"code.lineno",
18871940
),
18881941
value: I64(
1889-
401,
1942+
454,
18901943
),
18911944
},
18921945
KeyValue {
@@ -1984,7 +2037,7 @@ mod tests {
19842037
"code.lineno",
19852038
),
19862039
value: I64(
1987-
964,
2040+
1017,
19882041
),
19892042
},
19902043
KeyValue {

0 commit comments

Comments
 (0)