Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stackable-telemetry): Add method for pre-configured Tracing instance #1001

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion crates/stackable-telemetry/src/tracing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//!
//! To get started, see [`Tracing`].

use std::path::PathBuf;

use opentelemetry::trace::TracerProvider;
use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
use opentelemetry_otlp::{LogExporter, SpanExporter};
Expand All @@ -14,7 +16,7 @@ use opentelemetry_sdk::{
trace::SdkTracerProvider,
};
use snafu::{ResultExt as _, Snafu};
use tracing::subscriber::SetGlobalDefaultError;
use tracing::{level_filters::LevelFilter, subscriber::SetGlobalDefaultError};
use tracing_appender::rolling::{InitError, RollingFileAppender};
use tracing_subscriber::{EnvFilter, Layer, Registry, filter::Directive, layer::SubscriberExt};

Expand Down Expand Up @@ -244,11 +246,45 @@ pub struct Tracing {
}

impl Tracing {
pub const CONSOLE_LOG_ENV_VAR: &str = "CONSOLE_LOG";
pub const FILE_LOG_ENV_VAR: &str = "FILE_LOG";
pub const OTLP_LOG_ENV_VAR: &str = "OTLP_LOG";
pub const OTLP_TRACE_ENV_VAR: &str = "OTLP_TRACE";

/// Creates and returns a [`TracingBuilder`].
pub fn builder() -> TracingBuilder<builder_state::PreServiceName> {
TracingBuilder::default()
}

/// Creates an returns a pre-configured [`Tracing`] instance.
pub fn pre_configured(
service_name: &'static str,
no_console_output: bool,
log_directory: Option<PathBuf>,
rotation_period: Rotation,
otlp_logs: bool,
otlp_traces: bool,
) -> Self {
Self::builder()
.service_name(service_name)
.with_console_output((
Self::CONSOLE_LOG_ENV_VAR,
LevelFilter::INFO,
!no_console_output,
))
.with_file_output(log_directory.map(|log_directory| {
Settings::builder()
.with_environment_variable(Self::FILE_LOG_ENV_VAR)
.with_default_level(LevelFilter::INFO)
.file_log_settings_builder(log_directory, "tracing-rs.log")
.with_rotation_period(rotation_period)
.build()
}))
.with_otlp_log_exporter((Self::OTLP_LOG_ENV_VAR, LevelFilter::DEBUG, otlp_logs))
.with_otlp_trace_exporter((Self::OTLP_TRACE_ENV_VAR, LevelFilter::DEBUG, otlp_traces))
.build()
}

/// Initialise the configured tracing subscribers, returning a guard that
/// will shutdown the subscribers when dropped.
///
Expand Down
Loading