Compare commits

..

No commits in common. "7187638c10fd77928378a94a15f677308d1e5ad4" and "2d2349a06b7eda1584dd015fdbcd832b0c5616f3" have entirely different histories.

5 changed files with 18 additions and 1013 deletions

View File

@ -15,7 +15,7 @@ use args::Args;
async fn main() -> anyhow::Result<()> {
let cfg = AppConfig::load();
let log_level = cfg.log_level().unwrap_or_else(|_| "info".to_string());
observability::init_tracing_subscriber(&log_level, false);
observability::init_tracing_subscriber(&log_level);
let args = Args::parse();
let grpc_addr: SocketAddr = args

View File

@ -40,8 +40,7 @@ fn build_session_key(cfg: &AppConfig) -> anyhow::Result<Key> {
async fn main() -> anyhow::Result<()> {
let cfg = AppConfig::load();
let log_level = cfg.log_level().unwrap_or_else(|_| "info".to_string());
let otel_enabled = cfg.otel_enabled().unwrap_or(false);
init_tracing_subscriber(&log_level, otel_enabled);
init_tracing_subscriber(&log_level);
tracing::info!(
app_name = %cfg.app_name().unwrap_or_default(),
app_version = %cfg.app_version().unwrap_or_default(),
@ -67,7 +66,7 @@ async fn main() -> anyhow::Result<()> {
let worker_handle =
tokio::spawn(async move { worker_service.start_room_workers(shutdown_rx).await });
let _otel_guard = if otel_enabled {
let _otel_guard = if cfg.otel_enabled().unwrap_or(false) {
let endpoint = cfg
.otel_endpoint()
.unwrap_or_else(|_| "http://localhost:4317".to_string());

View File

@ -9,6 +9,7 @@
//! OTLP tracing layer on top.
use opentelemetry::trace::TracerProvider;
use opentelemetry::KeyValue;
use opentelemetry_otlp::{SpanExporter, WithExportConfig};
use opentelemetry_sdk::trace as sdktrace;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
@ -33,15 +34,10 @@ impl OtelGuard {
/// Uses HTTP/proto transport to the given endpoint.
/// Returns `Ok(Some(guard))` on success; the caller should store the guard and
/// call `guard.shutdown()` during app shutdown for a clean flush.
///
/// The `fmt_registry` parameter should be the value returned by
/// `init_tracing_subscriber(level, true)` — i.e. a registry that was built but
/// not yet installed. This function extends that registry with the OTLP tracing
/// layer and calls `try_init()` once, avoiding the "global default already set" error.
pub fn init_otlp(
endpoint: &str,
service_name: &str,
_service_version: &str,
service_version: &str,
log_level: &str,
) -> Result<Option<OtelGuard>, InitOtlError> {
if endpoint.is_empty() {
@ -67,19 +63,25 @@ pub fn init_otlp(
.with_line_number(true)
.flatten_event(true);
let resource = opentelemetry_sdk::Resource::builder()
.with_service_name(service_name.to_string())
.with_attribute(KeyValue::new("service.version", service_version.to_string()))
.build();
let tracer_provider = sdktrace::SdkTracerProvider::builder()
.with_batch_exporter(exporter)
.with_resource(resource)
.build();
let tracer = tracer_provider.tracer(service_name.to_string());
let otel_layer = tracing_opentelemetry::layer().with_tracer(tracer);
let layered = tracing_subscriber::registry()
let registry = tracing_subscriber::registry()
.with(env_filter)
.with(fmt_layer)
.with(otel_layer);
tracing::Dispatch::new(layered)
registry
.try_init()
.map_err(|e| InitOtlError::SubscriberInit(e.to_string()))?;

View File

@ -37,11 +37,7 @@ pub fn instance_id() -> String {
/// Each JSON line includes `ts`, `level`, `target` (module), `fields` (structured kv),
/// `line`, `file`, and `instance_id`.
/// `RUST_LOG` env var controls the log level filter.
///
/// Pass `defer = true` when OTLP will be initialized afterwards via `init_otlp()`;
/// in that case this function only builds the subscriber without calling `try_init()`,
/// and the combined (fmt + OTLP) subscriber is installed by `init_otlp()` instead.
pub fn init_tracing_subscriber(level: &str, defer: bool) {
pub fn init_tracing_subscriber(level: &str) {
let env_filter = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::from_str(level))
.expect("invalid log level");
@ -59,11 +55,9 @@ pub fn init_tracing_subscriber(level: &str, defer: bool) {
.with(env_filter)
.with(fmt_layer);
if defer {
// Caller will invoke init_otlp() which builds the full subscriber
// including the OTLP layer, then calls try_init() once.
return;
}
// try_init only fails if a global is already set — this is safe when
// init_otlp() is also called (it rebuilds the subscriber with OTLP layers).
let _ = registry.try_init();
}

File diff suppressed because it is too large Load Diff