Skip to content

Commit adc7f5d

Browse files
committed
wip: Initial design of pre-configured Tracing instance
1 parent cd73728 commit adc7f5d

File tree

1 file changed

+37
-1
lines changed
  • crates/stackable-telemetry/src/tracing

1 file changed

+37
-1
lines changed

crates/stackable-telemetry/src/tracing/mod.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//!
77
//! To get started, see [`Tracing`].
88
9+
use std::path::PathBuf;
10+
911
use opentelemetry::trace::TracerProvider;
1012
use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
1113
use opentelemetry_otlp::{LogExporter, SpanExporter};
@@ -14,7 +16,7 @@ use opentelemetry_sdk::{
1416
trace::SdkTracerProvider,
1517
};
1618
use snafu::{ResultExt as _, Snafu};
17-
use tracing::subscriber::SetGlobalDefaultError;
19+
use tracing::{level_filters::LevelFilter, subscriber::SetGlobalDefaultError};
1820
use tracing_appender::rolling::{InitError, RollingFileAppender};
1921
use tracing_subscriber::{EnvFilter, Layer, Registry, filter::Directive, layer::SubscriberExt};
2022

@@ -244,11 +246,45 @@ pub struct Tracing {
244246
}
245247

246248
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+
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

Comments
 (0)