@@ -93,6 +93,7 @@ use ulid_id_generator::UlidIdGenerator;
93
93
94
94
mod internal;
95
95
96
+ /// An error which may arise when configuring Logfire.
96
97
#[ derive( Debug , Error ) ]
97
98
#[ non_exhaustive]
98
99
pub enum ConfigureError {
@@ -116,26 +117,34 @@ pub enum ConfigureError {
116
117
#[ error( "Error configuring the OpenTelemetry tracer: {0}" ) ]
117
118
TracingAlreadySetup ( #[ from] tracing:: subscriber:: SetGlobalDefaultError ) ,
118
119
119
- /// Error parsing the RUST_LOG environment variable.
120
+ /// Error parsing the ` RUST_LOG` environment variable.
120
121
#[ error( "Error configuring the OpenTelemetry tracer: {0}" ) ]
121
122
RustLogInvalid ( #[ from] tracing_subscriber:: filter:: FromEnvError ) ,
122
123
124
+ /// A Rust feature needs to be enabled in the `Cargo.toml`.
123
125
#[ error( "Rust feature required: `{feature_name}` feature must be enabled for {functionality}" ) ]
124
126
LogfireFeatureRequired {
127
+ /// The feature which is required.
125
128
feature_name : & ' static str ,
129
+ /// The functionality which was attempted to be used.
126
130
functionality : String ,
127
131
} ,
128
132
133
+ /// A configuration value (from environment) was invalid.
129
134
#[ error( "Invalid configuration value for {parameter}: {value}" ) ]
130
135
InvalidConfigurationValue {
136
+ /// The name of the configuration parameter.
131
137
parameter : & ' static str ,
138
+ /// The invalid value passed for the parameter.
132
139
value : String ,
133
140
} ,
134
141
142
+ /// Any other error.
135
143
#[ error( transparent) ]
136
144
Other ( #[ from] Box < dyn std:: error:: Error + Send + Sync > ) ,
137
145
}
138
146
147
+ /// Whether to print to the console.
139
148
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Default ) ]
140
149
#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
141
150
#[ cfg_attr( feature = "serde" , serde( rename_all = "lowercase" ) ) ]
@@ -159,15 +168,28 @@ impl FromStr for ConsoleMode {
159
168
}
160
169
}
161
170
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." ]
171
193
pub fn configure ( ) -> LogfireConfigBuilder {
172
194
LogfireConfigBuilder {
173
195
send_to_logfire : None ,
@@ -178,7 +200,19 @@ pub fn configure() -> LogfireConfigBuilder {
178
200
}
179
201
}
180
202
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
+
181
212
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.
182
216
pub fn install_panic_handler ( & mut self ) -> & mut Self {
183
217
self . install_panic_handler = true ;
184
218
self
@@ -191,21 +225,35 @@ impl LogfireConfigBuilder {
191
225
self . send_to_logfire = Some ( send_to_logfire) ;
192
226
self
193
227
}
228
+
229
+ /// Whether to log to the console.
194
230
pub fn console_mode ( & mut self , console_mode : ConsoleMode ) -> & mut Self {
195
231
self . console_mode = console_mode;
196
232
self
197
233
}
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.
202
240
pub fn with_defalt_level_filter ( & mut self , default_level_filter : LevelFilter ) -> & mut Self {
203
241
self . default_level_filter = Some ( default_level_filter) ;
204
242
self
205
243
}
206
244
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
+
207
253
/// Finish configuring Logfire.
208
254
///
255
+ /// Because this configures global state for the opentelemetry SDK, this can typically only ever be called once per program.
256
+ ///
209
257
/// # Errors
210
258
///
211
259
/// See [`ConfigureError`] for possible errors.
@@ -346,7 +394,12 @@ impl LogfireConfigBuilder {
346
394
}
347
395
}
348
396
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.
349
401
#[ derive( Debug , Clone ) ]
402
+ #[ must_use = "this should be kept alive until logging should be stopped" ]
350
403
pub struct ShutdownHandler {
351
404
tracer_provider : SdkTracerProvider ,
352
405
meter_provider : Option < SdkMeterProvider > ,
@@ -598,7 +651,7 @@ const OTEL_EXPORTER_OTLP_PROTOCOL_GRPC: &str = "grpc";
598
651
const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF : & str = "http/protobuf" ;
599
652
const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON : & str = "http/json" ;
600
653
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>
602
655
fn protocol_from_str ( value : & str ) -> Result < Protocol , ConfigureError > {
603
656
match value {
604
657
OTEL_EXPORTER_OTLP_PROTOCOL_GRPC => Ok ( Protocol :: Grpc ) ,
@@ -1024,7 +1077,7 @@ mod tests {
1024
1077
"code.lineno",
1025
1078
),
1026
1079
value: I64(
1027
- 964 ,
1080
+ 1017 ,
1028
1081
),
1029
1082
},
1030
1083
KeyValue {
@@ -1150,7 +1203,7 @@ mod tests {
1150
1203
"code.lineno",
1151
1204
),
1152
1205
value: I64(
1153
- 965 ,
1206
+ 1018 ,
1154
1207
),
1155
1208
},
1156
1209
KeyValue {
@@ -1286,7 +1339,7 @@ mod tests {
1286
1339
"code.lineno",
1287
1340
),
1288
1341
value: I64(
1289
- 965 ,
1342
+ 1018 ,
1290
1343
),
1291
1344
},
1292
1345
KeyValue {
@@ -1428,7 +1481,7 @@ mod tests {
1428
1481
"code.lineno",
1429
1482
),
1430
1483
value: I64(
1431
- 966 ,
1484
+ 1019 ,
1432
1485
),
1433
1486
},
1434
1487
KeyValue {
@@ -1570,7 +1623,7 @@ mod tests {
1570
1623
"code.lineno",
1571
1624
),
1572
1625
value: I64(
1573
- 968 ,
1626
+ 1021 ,
1574
1627
),
1575
1628
},
1576
1629
KeyValue {
@@ -1740,7 +1793,7 @@ mod tests {
1740
1793
"code.lineno",
1741
1794
),
1742
1795
value: I64(
1743
- 969 ,
1796
+ 1022 ,
1744
1797
),
1745
1798
},
1746
1799
KeyValue {
@@ -1819,7 +1872,7 @@ mod tests {
1819
1872
),
1820
1873
value: String(
1821
1874
Owned(
1822
- "src/lib.rs:970 :17",
1875
+ "src/lib.rs:1023 :17",
1823
1876
),
1824
1877
),
1825
1878
},
@@ -1886,7 +1939,7 @@ mod tests {
1886
1939
"code.lineno",
1887
1940
),
1888
1941
value: I64(
1889
- 401 ,
1942
+ 454 ,
1890
1943
),
1891
1944
},
1892
1945
KeyValue {
@@ -1984,7 +2037,7 @@ mod tests {
1984
2037
"code.lineno",
1985
2038
),
1986
2039
value: I64(
1987
- 964 ,
2040
+ 1017 ,
1988
2041
),
1989
2042
},
1990
2043
KeyValue {
0 commit comments