From f65e11c6402b37b5ee6c2a0b73ac1c9a19ec23e1 Mon Sep 17 00:00:00 2001 From: Heemank Verma Date: Wed, 9 Oct 2024 15:08:01 +0530 Subject: [PATCH] update: simplified otel logic (#143) * update: simplified OTEL logic * update: simplified otel and tracing logic * removed TRACING_LEVEL from tests * update: removed TRACING_LEVEL from env * update: fixed e2e otel issue * update: re-moved dotenv from telemetry to metric --- .env.example | 1 - .env.test | 2 -- CHANGELOG.md | 1 + crates/orchestrator/src/main.rs | 8 ----- crates/orchestrator/src/telemetry.rs | 49 +++++++++++++--------------- 5 files changed, 23 insertions(+), 38 deletions(-) diff --git a/.env.example b/.env.example index 6c874c28..b4f888ad 100644 --- a/.env.example +++ b/.env.example @@ -56,4 +56,3 @@ STARKNET_ACCOUNT_ADDRESS= ##### Instrumentation ##### OTEL_SERVICE_NAME= OTEL_COLLECTOR_ENDPOINT= -TRACING_LEVEL= diff --git a/.env.test b/.env.test index 6b3a32f9..99815845 100644 --- a/.env.test +++ b/.env.test @@ -71,8 +71,6 @@ MADARA_BINARY_PATH="/path/to/madara" ## Instrumentation OTEL_SERVICE_NAME="madara_orchestrator" -OTEL_COLLECTOR_ENDPOINT="" -TRACING_LEVEL="info" ##### Tests ##### diff --git a/CHANGELOG.md b/CHANGELOG.md index 882b3b49..cf144782 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ## Fixed +- Simplified otel setup. - Added new_with_settings to SharpClient. - Calculate root hash logic and added a simple test for it. - Cargo.toml dependency reorg. diff --git a/crates/orchestrator/src/main.rs b/crates/orchestrator/src/main.rs index e9575fed..c0ba0e3d 100644 --- a/crates/orchestrator/src/main.rs +++ b/crates/orchestrator/src/main.rs @@ -1,11 +1,8 @@ -use std::str::FromStr; - use dotenvy::dotenv; use orchestrator::config::init_config; use orchestrator::queue::init_consumers; use orchestrator::routes::app_router; use orchestrator::telemetry::{setup_analytics, shutdown_analytics}; -use tracing::Level; use utils::env_utils::get_env_var_or_default; /// Start the server @@ -15,13 +12,8 @@ use utils::env_utils::get_env_var_or_default; #[allow(clippy::needless_return)] async fn main() { dotenv().ok(); - let log_level = get_env_var_or_default("RUST_LOG", "INFO"); - let level = Level::from_str(&log_level).unwrap_or(Level::INFO); - - tracing_subscriber::fmt().with_max_level(level).with_target(false).init(); // Analytics Setup - let meter_provider = setup_analytics(); // initial config setup diff --git a/crates/orchestrator/src/telemetry.rs b/crates/orchestrator/src/telemetry.rs index d2fbc84c..b8152d42 100644 --- a/crates/orchestrator/src/telemetry.rs +++ b/crates/orchestrator/src/telemetry.rs @@ -1,4 +1,4 @@ -use std::str::FromStr as _; +use std::str::FromStr; use std::time::Duration; use lazy_static::lazy_static; @@ -13,7 +13,7 @@ use tracing::Level; use tracing_opentelemetry::OpenTelemetryLayer; use tracing_subscriber::layer::SubscriberExt as _; use tracing_subscriber::util::SubscriberInitExt as _; -use utils::env_utils::get_env_var_or_panic; +use utils::env_utils::{get_env_var_optional, get_env_var_or_default, get_env_var_or_panic}; lazy_static! { #[derive(Debug)] @@ -21,25 +21,23 @@ lazy_static! { } pub fn setup_analytics() -> Option { - let otel_endpoint = get_env_var_or_panic("OTEL_COLLECTOR_ENDPOINT"); - let tracing_level = Level::from_str(&get_env_var_or_panic("TRACING_LEVEL")) - .expect("Could not obtain tracing level from environment variable."); - - // guard clause if otel is disabled - if otel_endpoint.is_empty() { - return None; + let otel_endpoint = get_env_var_optional("OTEL_COLLECTOR_ENDPOINT").expect("Failed to get OTEL_COLLECTOR_ENDPOINT"); + let log_level = get_env_var_or_default("RUST_LOG", "INFO"); + let level = Level::from_str(&log_level).unwrap_or(Level::INFO); + + let tracing_subscriber = tracing_subscriber::registry() + .with(tracing_subscriber::filter::LevelFilter::from_level(level)) + .with(tracing_subscriber::fmt::layer().with_target(false)); + + if let Some(otel_endpoint) = otel_endpoint { + let meter_provider = init_metric_provider(&otel_endpoint); + let tracer = init_tracer_provider(&otel_endpoint); + tracing_subscriber.with(OpenTelemetryLayer::new(tracer)).init(); + Some(meter_provider) + } else { + tracing_subscriber.init(); + None } - - let meter_provider = init_metric_provider(otel_endpoint.as_str()); - let tracer = init_tracer_provider(otel_endpoint.as_str()); - - tracing_subscriber::registry() - .with(tracing_subscriber::filter::LevelFilter::from_level(tracing_level)) - .with(tracing_subscriber::fmt::layer()) - .with(OpenTelemetryLayer::new(tracer)) - .init(); - - Some(meter_provider) } pub fn shutdown_analytics(meter_provider: Option) { @@ -70,7 +68,7 @@ pub fn init_tracer_provider(otel_endpoint: &str) -> Tracer { )]))) .with_batch_config(batch_config) .install_batch(runtime::Tokio) - .unwrap(); + .expect("Failed to install tracer provider"); global::set_tracer_provider(provider.clone()); @@ -89,8 +87,9 @@ pub fn init_metric_provider(otel_endpoint: &str) -> SdkMeterProvider { ); // Creates a periodic reader that exports every 5 seconds - let reader = - PeriodicReader::builder(exporter.unwrap(), runtime::Tokio).with_interval(Duration::from_secs(5)).build(); + let reader = PeriodicReader::builder(exporter.expect("Failed to build metrics exporter"), runtime::Tokio) + .with_interval(Duration::from_secs(5)) + .build(); // Builds a meter provider with the periodic reader let provider = SdkMeterProvider::builder() @@ -120,7 +119,6 @@ mod tests { async fn test_init_metric_provider() { // Set up necessary environment variables env::set_var("OTEL_COLLECTOR_ENDPOINT", "http://localhost:4317"); - env::set_var("TRACING_LEVEL", "info"); env::set_var("OTEL_SERVICE_NAME", "test_service"); let otel_endpoint = get_env_var_or_panic("OTEL_COLLECTOR_ENDPOINT"); @@ -140,7 +138,6 @@ mod tests { async fn test_init_tracer_provider() { // Set up necessary environment variables env::set_var("OTEL_COLLECTOR_ENDPOINT", "http://localhost:4317"); - env::set_var("TRACING_LEVEL", "info"); env::set_var("OTEL_SERVICE_NAME", "test_service"); let otel_endpoint = get_env_var_or_panic("OTEL_COLLECTOR_ENDPOINT"); @@ -158,7 +155,6 @@ mod tests { // This test just ensures that the function doesn't panic env::set_var("OTEL_COLLECTOR_ENDPOINT", "http://localhost:4317"); - env::set_var("TRACING_LEVEL", "info"); env::set_var("OTEL_SERVICE_NAME", "test_service"); let analytics = setup_analytics(); @@ -172,7 +168,6 @@ mod tests { // This test just ensures that the function doesn't panic env::set_var("OTEL_COLLECTOR_ENDPOINT", "http://localhost:4317"); - env::set_var("TRACING_LEVEL", "info"); env::set_var("OTEL_SERVICE_NAME", "test_service"); setup_analytics();