refactor(common): make init_observability sync with optional OTLP endpoint
Remove unnecessary async from init_observability -- body was fully sync. Change otlp_endpoint from &str to Option<&str> -- when None, OTLP layer is skipped (fmt-only mode). Update all 8 service callers. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -29,7 +29,7 @@
|
||||
//! #[tokio::main]
|
||||
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
//! // Initialize observability infrastructure
|
||||
//! init_observability("trading_service", "http://localhost:4317").await?;
|
||||
//! init_observability("trading_service", Some("http://localhost:4317"))?;
|
||||
//!
|
||||
//! // Correlation IDs are automatically propagated via gRPC metadata
|
||||
//! Ok(())
|
||||
@@ -87,7 +87,8 @@ use crate::error::CommonResult;
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `service_name` - Name of the service (e.g., "api_gateway", "trading_service")
|
||||
/// * `otlp_endpoint` - OTLP collector endpoint (e.g., "http://localhost:4317")
|
||||
/// * `otlp_endpoint` - Optional OTLP collector endpoint (e.g., `Some("http://localhost:4317")`).
|
||||
/// When `None`, the OTLP tracing layer is disabled and only JSON logging is active.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -100,15 +101,15 @@ use crate::error::CommonResult;
|
||||
///
|
||||
/// #[tokio::main]
|
||||
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// init_observability("trading_service", "http://localhost:4317").await?;
|
||||
/// init_observability("trading_service", Some("http://localhost:4317"))?;
|
||||
///
|
||||
/// // Service logic here...
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
pub async fn init_observability(
|
||||
pub fn init_observability(
|
||||
service_name: &str,
|
||||
otlp_endpoint: &str,
|
||||
otlp_endpoint: Option<&str>,
|
||||
) -> CommonResult<()> {
|
||||
use tracing_subscriber::prelude::*;
|
||||
|
||||
@@ -129,8 +130,8 @@ pub async fn init_observability(
|
||||
// the `S` type parameter be inferred correctly by the compiler.
|
||||
let tracing_config = TracingConfig {
|
||||
service_name: service_name.to_string(),
|
||||
otlp_endpoint: otlp_endpoint.to_string(),
|
||||
enable_export: true,
|
||||
otlp_endpoint: otlp_endpoint.unwrap_or_default().to_string(),
|
||||
enable_export: otlp_endpoint.is_some(),
|
||||
};
|
||||
let otel_tracer = tracing_config::build_otel_tracer(&tracing_config);
|
||||
let otel_layer = otel_tracer
|
||||
|
||||
@@ -24,7 +24,7 @@ async fn main() -> Result<()> {
|
||||
// Initialize observability (JSON logging + OpenTelemetry tracing via OTLP)
|
||||
let otlp_endpoint = std::env::var("OTEL_EXPORTER_OTLP_ENDPOINT")
|
||||
.unwrap_or_else(|_| "http://localhost:4317".to_string());
|
||||
if let Err(e) = common::observability::init_observability("web_gateway", &otlp_endpoint).await {
|
||||
if let Err(e) = common::observability::init_observability("web_gateway", Some(&otlp_endpoint)) {
|
||||
eprintln!("Failed to initialize observability: {}", e);
|
||||
}
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ async fn main() -> Result<()> {
|
||||
// Initialize observability (JSON logging + OpenTelemetry tracing via OTLP)
|
||||
let otlp_endpoint = std::env::var("OTEL_EXPORTER_OTLP_ENDPOINT")
|
||||
.unwrap_or_else(|_| "http://localhost:4317".to_string());
|
||||
if let Err(e) = common::observability::init_observability("api_gateway", &otlp_endpoint).await {
|
||||
if let Err(e) = common::observability::init_observability("api_gateway", Some(&otlp_endpoint)) {
|
||||
eprintln!("Failed to initialize observability: {}", e);
|
||||
}
|
||||
|
||||
|
||||
@@ -38,8 +38,8 @@ async fn main() -> Result<()> {
|
||||
.unwrap_or_else(|_| "http://localhost:4317".to_string());
|
||||
if let Err(e) = common::observability::init_observability(
|
||||
"backtesting_service",
|
||||
&otlp_endpoint,
|
||||
).await {
|
||||
Some(&otlp_endpoint),
|
||||
) {
|
||||
eprintln!("Failed to initialize observability: {}", e);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ async fn main() -> Result<()> {
|
||||
// Initialize observability (JSON logging + OpenTelemetry tracing via OTLP)
|
||||
let otlp_endpoint = std::env::var("OTEL_EXPORTER_OTLP_ENDPOINT")
|
||||
.unwrap_or_else(|_| "http://localhost:4317".to_string());
|
||||
if let Err(e) = common::observability::init_observability("broker_gateway", &otlp_endpoint).await {
|
||||
if let Err(e) = common::observability::init_observability("broker_gateway", Some(&otlp_endpoint)) {
|
||||
eprintln!("Failed to initialize observability: {}", e);
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Initialize observability (JSON logging + OpenTelemetry tracing via OTLP)
|
||||
let otlp_endpoint = std::env::var("OTEL_EXPORTER_OTLP_ENDPOINT")
|
||||
.unwrap_or_else(|_| "http://localhost:4317".to_string());
|
||||
if let Err(e) = common::observability::init_observability("data_acquisition", &otlp_endpoint).await {
|
||||
if let Err(e) = common::observability::init_observability("data_acquisition", Some(&otlp_endpoint)) {
|
||||
eprintln!("Failed to initialize observability: {}", e);
|
||||
}
|
||||
|
||||
|
||||
@@ -134,8 +134,8 @@ async fn serve(args: ServeArgs) -> Result<()> {
|
||||
.unwrap_or_else(|_| "http://localhost:4317".to_string());
|
||||
if let Err(e) = common::observability::init_observability(
|
||||
"ml_training_service",
|
||||
&otlp_endpoint,
|
||||
).await {
|
||||
Some(&otlp_endpoint),
|
||||
) {
|
||||
eprintln!("Failed to initialize observability: {}", e);
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ async fn main() -> Result<()> {
|
||||
// Initialize observability (JSON logging + OpenTelemetry tracing via OTLP)
|
||||
let otlp_endpoint = std::env::var("OTEL_EXPORTER_OTLP_ENDPOINT")
|
||||
.unwrap_or_else(|_| "http://localhost:4317".to_string());
|
||||
if let Err(e) = common::observability::init_observability("trading_agent", &otlp_endpoint).await {
|
||||
if let Err(e) = common::observability::init_observability("trading_agent", Some(&otlp_endpoint)) {
|
||||
eprintln!("Failed to initialize observability: {}", e);
|
||||
}
|
||||
|
||||
|
||||
@@ -56,8 +56,8 @@ async fn main() -> Result<()> {
|
||||
.unwrap_or_else(|_| "http://localhost:4317".to_string());
|
||||
if let Err(e) = common::observability::init_observability(
|
||||
"trading_service",
|
||||
&otlp_endpoint,
|
||||
).await {
|
||||
Some(&otlp_endpoint),
|
||||
) {
|
||||
eprintln!("Failed to initialize observability: {}", e);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user