66//!
77//! To get started, see [`Tracing`].
88
9+ use std:: path:: PathBuf ;
10+
911use opentelemetry:: trace:: TracerProvider ;
1012use opentelemetry_appender_tracing:: layer:: OpenTelemetryTracingBridge ;
1113use opentelemetry_otlp:: { LogExporter , SpanExporter } ;
@@ -14,7 +16,7 @@ use opentelemetry_sdk::{
1416 trace:: SdkTracerProvider ,
1517} ;
1618use snafu:: { ResultExt as _, Snafu } ;
17- use tracing:: subscriber:: SetGlobalDefaultError ;
19+ use tracing:: { level_filters :: LevelFilter , subscriber:: SetGlobalDefaultError } ;
1820use tracing_appender:: rolling:: { InitError , RollingFileAppender } ;
1921use tracing_subscriber:: { EnvFilter , Layer , Registry , filter:: Directive , layer:: SubscriberExt } ;
2022
@@ -244,11 +246,45 @@ pub struct Tracing {
244246}
245247
246248impl 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+
247254 /// Creates and returns a [`TracingBuilder`].
248255 pub fn builder ( ) -> TracingBuilder < builder_state:: PreServiceName > {
249256 TracingBuilder :: default ( )
250257 }
251258
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+
252288 /// Initialise the configured tracing subscribers, returning a guard that
253289 /// will shutdown the subscribers when dropped.
254290 ///
0 commit comments