Skip to content

Commit

Permalink
update: simplified otel logic (#143)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
heemankv authored Oct 9, 2024
1 parent 705b119 commit f65e11c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 38 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,3 @@ STARKNET_ACCOUNT_ADDRESS=
##### Instrumentation #####
OTEL_SERVICE_NAME=
OTEL_COLLECTOR_ENDPOINT=
TRACING_LEVEL=
2 changes: 0 additions & 2 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ MADARA_BINARY_PATH="/path/to/madara"

## Instrumentation
OTEL_SERVICE_NAME="madara_orchestrator"
OTEL_COLLECTOR_ENDPOINT=""
TRACING_LEVEL="info"

##### Tests #####

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 0 additions & 8 deletions crates/orchestrator/src/main.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
49 changes: 22 additions & 27 deletions crates/orchestrator/src/telemetry.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::str::FromStr as _;
use std::str::FromStr;
use std::time::Duration;

use lazy_static::lazy_static;
Expand All @@ -13,33 +13,31 @@ 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)]
pub static ref OTEL_SERVICE_NAME: String = get_env_var_or_panic("OTEL_SERVICE_NAME");
}

pub fn setup_analytics() -> Option<SdkMeterProvider> {
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<SdkMeterProvider>) {
Expand Down Expand Up @@ -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());

Expand All @@ -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()
Expand Down Expand Up @@ -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");
Expand All @@ -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");

Expand All @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit f65e11c

Please sign in to comment.