6
6
//!
7
7
//! To get started, see [`Tracing`].
8
8
9
+ use std:: path:: PathBuf ;
10
+
9
11
use opentelemetry:: trace:: TracerProvider ;
10
12
use opentelemetry_appender_tracing:: layer:: OpenTelemetryTracingBridge ;
11
13
use opentelemetry_otlp:: { LogExporter , SpanExporter } ;
@@ -14,7 +16,7 @@ use opentelemetry_sdk::{
14
16
trace:: SdkTracerProvider ,
15
17
} ;
16
18
use snafu:: { ResultExt as _, Snafu } ;
17
- use tracing:: subscriber:: SetGlobalDefaultError ;
19
+ use tracing:: { level_filters :: LevelFilter , subscriber:: SetGlobalDefaultError } ;
18
20
use tracing_appender:: rolling:: { InitError , RollingFileAppender } ;
19
21
use tracing_subscriber:: { EnvFilter , Layer , Registry , filter:: Directive , layer:: SubscriberExt } ;
20
22
@@ -244,11 +246,45 @@ pub struct Tracing {
244
246
}
245
247
246
248
impl Tracing {
249
+ pub const CONSOLE_LOG_ENV_VAR : & str = "CONSOLE_LOG" ;
250
+ pub const FILE_LOG_ENV_VAR : & str = "FILE_LOG" ;
251
+ pub const OTLP_LOG_ENV_VAR : & str = "OTLP_LOG" ;
252
+ pub const OTLP_TRACE_ENV_VAR : & str = "OTLP_TRACE" ;
253
+
247
254
/// Creates and returns a [`TracingBuilder`].
248
255
pub fn builder ( ) -> TracingBuilder < builder_state:: PreServiceName > {
249
256
TracingBuilder :: default ( )
250
257
}
251
258
259
+ /// Creates an returns a pre-configured [`Tracing`] instance.
260
+ pub fn pre_configured (
261
+ service_name : & ' static str ,
262
+ no_console_output : bool ,
263
+ log_directory : Option < PathBuf > ,
264
+ rotation_period : Rotation ,
265
+ otlp_logs : bool ,
266
+ otlp_traces : bool ,
267
+ ) -> Self {
268
+ Self :: builder ( )
269
+ . service_name ( service_name)
270
+ . with_console_output ( (
271
+ Self :: CONSOLE_LOG_ENV_VAR ,
272
+ LevelFilter :: INFO ,
273
+ !no_console_output,
274
+ ) )
275
+ . with_file_output ( log_directory. map ( |log_directory| {
276
+ Settings :: builder ( )
277
+ . with_environment_variable ( Self :: FILE_LOG_ENV_VAR )
278
+ . with_default_level ( LevelFilter :: INFO )
279
+ . file_log_settings_builder ( log_directory, "tracing-rs.log" )
280
+ . with_rotation_period ( rotation_period)
281
+ . build ( )
282
+ } ) )
283
+ . with_otlp_log_exporter ( ( Self :: OTLP_LOG_ENV_VAR , LevelFilter :: DEBUG , otlp_logs) )
284
+ . with_otlp_trace_exporter ( ( Self :: OTLP_TRACE_ENV_VAR , LevelFilter :: DEBUG , otlp_traces) )
285
+ . build ( )
286
+ }
287
+
252
288
/// Initialise the configured tracing subscribers, returning a guard that
253
289
/// will shutdown the subscribers when dropped.
254
290
///
0 commit comments