From 4f113e6ec97691d90e6618a035e9f2fe98a3bb41 Mon Sep 17 00:00:00 2001 From: jgrusewski Date: Sat, 21 Feb 2026 19:54:04 +0100 Subject: [PATCH] fix(common): re-enable observability module and fix type errors Fix three compilation errors in the observability module that had been commented out due to type errors with tracing_subscriber: - Fix lifetime issue in set_correlation_id by cloning Arc before async - Fix Option vs CorrelationId type mismatch in get_correlation_id - Update deprecated opentelemetry_sdk::trace::Config API to builder methods - Remove unused imports across all observability submodules Co-Authored-By: Claude Opus 4.6 --- Cargo.lock | 1 + common/src/lib.rs | 22 +++++++------- common/src/observability/correlation.rs | 35 ++++++++++------------ common/src/observability/logger.rs | 8 ++--- common/src/observability/mod.rs | 2 +- common/src/observability/tracing_config.rs | 17 ++++------- 6 files changed, 35 insertions(+), 50 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 71f9bcc48..70079951c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11690,6 +11690,7 @@ dependencies = [ "lazy_static", "nom", "oid-registry", + "ring", "rusticata-macros", "thiserror 1.0.69", "time", diff --git a/common/src/lib.rs b/common/src/lib.rs index b3371afe9..363882861 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -31,8 +31,7 @@ pub mod features; // Wave D: Shared feature extraction (225 features) pub mod market_data; pub mod metrics; // Wave 5: Prometheus metrics infrastructure (W5-3) pub mod ml_strategy; -// TODO: Fix observability module - type errors with tracing_subscriber -// pub mod observability; // Wave 5: Structured logging and observability +pub mod observability; // Wave 5: Structured logging and observability pub mod regime_persistence; pub mod resilience; // Wave 5: Circuit breaker and retry patterns pub mod thresholds; @@ -103,17 +102,16 @@ pub use resilience::{ RetryError, }; -// TODO: Re-enable when observability module is fixed // Re-export observability infrastructure (Wave 5: Structured logging and observability) -// pub use crate::observability::{ -// init_observability, -// CorrelationId, -// CorrelationIdExt, -// JsonLoggerConfig, -// LogLevel, -// TracingConfig, -// CORRELATION_ID_HEADER, -// }; +pub use crate::observability::{ + init_observability, + CorrelationId, + CorrelationIdExt, + JsonLoggerConfig, + LogLevel, + TracingConfig, + CORRELATION_ID_HEADER, +}; // Test utilities module (available for all tests) #[cfg(test)] diff --git a/common/src/observability/correlation.rs b/common/src/observability/correlation.rs index a2f934a4f..d4d91ae5e 100644 --- a/common/src/observability/correlation.rs +++ b/common/src/observability/correlation.rs @@ -33,10 +33,10 @@ use uuid::Uuid; /// The gRPC metadata header key for correlation IDs. pub const CORRELATION_ID_HEADER: &str = "x-correlation-id"; -/// Thread-local storage for the current correlation ID. -/// -/// This allows correlation IDs to be automatically available in any function -/// within the current async task context without explicit parameter passing. +// Thread-local storage for the current correlation ID. +// +// This allows correlation IDs to be automatically available in any function +// within the current async task context without explicit parameter passing. tokio::task_local! { pub static CURRENT_CORRELATION_ID: Arc>>; } @@ -231,14 +231,13 @@ impl CorrelationIdExt for tonic::metadata::MetadataMap { /// } /// ``` pub async fn set_correlation_id(correlation_id: CorrelationId) { - CURRENT_CORRELATION_ID - .try_with(|id| -> std::pin::Pin + '_>> { - Box::pin(async move { - let mut guard = id.write().await; - *guard = Some(correlation_id); - }) - }) + let maybe_lock = CURRENT_CORRELATION_ID + .try_with(|id| Arc::clone(id)) .ok(); + if let Some(lock) = maybe_lock { + let mut guard = lock.write().await; + *guard = Some(correlation_id); + } } /// Get the current correlation ID from the async task context. @@ -261,15 +260,11 @@ pub async fn set_correlation_id(correlation_id: CorrelationId) { /// } /// ``` pub async fn get_correlation_id() -> Option { - CURRENT_CORRELATION_ID - .try_with(|id| -> std::pin::Pin + Send + '_>> { - Box::pin(async move { - let guard = id.read().await; - guard.clone() - }) - }) - .ok()? - .await + let lock = CURRENT_CORRELATION_ID + .try_with(|id| Arc::clone(id)) + .ok()?; + let guard = lock.read().await; + guard.clone() } /// Create a new metadata map with the correlation ID header set. diff --git a/common/src/observability/logger.rs b/common/src/observability/logger.rs index 77b5d15bd..6a98c21b3 100644 --- a/common/src/observability/logger.rs +++ b/common/src/observability/logger.rs @@ -30,15 +30,11 @@ //! ``` use crate::error::{CommonError, CommonResult}; -use serde_json::json; -use std::io::Write; use std::path::PathBuf; -use tracing::{Level, Subscriber}; +use tracing::Level; use tracing_subscriber::{ - fmt::Layer as FmtLayer, layer::SubscriberExt, - registry::LookupSpan, - EnvFilter, Layer, Registry, + EnvFilter, Registry, }; /// Log rotation strategy. diff --git a/common/src/observability/mod.rs b/common/src/observability/mod.rs index 838c31d3b..1839f899e 100644 --- a/common/src/observability/mod.rs +++ b/common/src/observability/mod.rs @@ -75,7 +75,7 @@ pub use correlation::{CorrelationId, CorrelationIdExt, CORRELATION_ID_HEADER}; pub use logger::{init_json_logger, JsonLoggerConfig, LogLevel}; pub use tracing_config::{init_tracing, TracingConfig}; -use crate::error::{CommonError, CommonResult}; +use crate::error::CommonResult; /// Initialize the complete observability stack for a service. /// diff --git a/common/src/observability/tracing_config.rs b/common/src/observability/tracing_config.rs index 5f5cd63fd..0dda02004 100644 --- a/common/src/observability/tracing_config.rs +++ b/common/src/observability/tracing_config.rs @@ -19,14 +19,12 @@ //! across multiple services. Trace context is propagated via gRPC metadata //! using the W3C Trace Context specification. -use crate::error::{CommonError, CommonResult}; +use crate::error::CommonResult; use opentelemetry::{global, KeyValue}; use opentelemetry_sdk::{ trace::{Sampler, TracerProvider}, Resource, }; -use tracing::Subscriber; -use tracing_subscriber::{layer::SubscriberExt, registry::LookupSpan, EnvFilter, Layer, Registry}; /// Configuration for OpenTelemetry tracing. /// @@ -99,14 +97,11 @@ pub async fn init_tracing(config: TracingConfig) -> CommonResult<()> { // Create a basic tracer provider with console export for development let provider = TracerProvider::builder() - .with_config( - opentelemetry_sdk::trace::Config::default() - .with_sampler(Sampler::AlwaysOn) - .with_resource(Resource::new(vec![ - KeyValue::new("service.name", config.service_name.clone()), - KeyValue::new("service.version", env!("CARGO_PKG_VERSION")), - ])), - ) + .with_sampler(Sampler::AlwaysOn) + .with_resource(Resource::new(vec![ + KeyValue::new("service.name", config.service_name.clone()), + KeyValue::new("service.version", env!("CARGO_PKG_VERSION")), + ])) .build(); // Set as global tracer provider