From 21f6d7c9c6685202555a181d1606caa665b2e3aa Mon Sep 17 00:00:00 2001 From: Raphael Amorim Date: Tue, 16 Jan 2024 14:10:34 +0100 Subject: [PATCH] remove generic propagator --- src/lib.rs | 106 ++--------------------------------------------------- 1 file changed, 4 insertions(+), 102 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 885e5a0..9b9dacf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,17 +3,12 @@ // https://github.com/davidB/tracing-opentelemetry-instrumentation-sdk/blob/d3609ac2cc699d3a24fbf89754053cc8e938e3bf/LICENSE use opentelemetry_sdk::{ - propagation::{ - BaggagePropagator, TextMapCompositePropagator, TraceContextPropagator, - }, resource::{OsResourceDetector, ResourceDetector}, Resource, }; use std::cmp::max; use tracing::Subscriber; use tracing_subscriber::fmt::writer::MakeWriterExt; - -use opentelemetry::{propagation::TextMapPropagator, trace::TraceError}; use tracing_subscriber::{ filter::LevelFilter, fmt::format::FmtSpan, layer::SubscriberExt, registry::LookupSpan, Layer, @@ -144,7 +139,10 @@ pub fn init_tracing_with_fallbacks( DetectResource::new(fallback_service_name, fallback_service_version).build(); let otel_tracer = otlp::init_tracer(otel_rsrc, otlp::identity).expect("setup of Tracer"); - init_propagator().expect("setup of propagator"); + + opentelemetry::global::set_text_map_propagator( + propagation::TextMapSplitPropagator::default(), + ); let level_filter: LevelFilter = max(log_level, tracing::Level::INFO).into(); let subscriber = tracing_subscriber::registry() @@ -177,99 +175,3 @@ macro_rules! init_tracing { pub fn shutdown_signal() { opentelemetry::global::shutdown_tracer_provider(); } - -/// Configure the global propagator based on content of the env variable [OTEL_PROPAGATORS](https://opentelemetry.io/docs/concepts/sdk-configuration/general-sdk-configuration/#otel_propagators) -/// Specifies Propagators to be used in a comma-separated list. -/// Default value: `"tracecontext,baggage"` -/// Example: `export OTEL_PROPAGATORS="b3"` -/// Accepted values for `OTEL_PROPAGATORS` are: -/// -/// - "tracecontext": W3C Trace Context -/// - "baggage": W3C Baggage -/// - "b3": B3 Single (require feature "zipkin") -/// - "b3multi": B3 Multi (require feature "zipkin") -/// - "xray": AWS X-Ray (require feature "xray") -/// - "ottrace": OT Trace (third party) (not supported) -/// - "none": No automatically configured propagator. -/// -/// # Errors -/// -/// Will return `TraceError` if issue in reading or instanciate propagator. -pub fn init_propagator() -> Result<(), TraceError> { - let value_from_env = std::env::var("OTEL_PROPAGATORS") - .unwrap_or_else(|_| "tracecontext,baggage".to_string()); - let propagators: Vec<(Box, String)> = - value_from_env - .split(',') - .map(|s| { - let name = s.trim().to_lowercase(); - propagator_from_string(&name).map(|o| o.map(|b| (b, name))) - }) - .collect::, _>>()? - .into_iter() - .flatten() - .collect(); - if !propagators.is_empty() { - let (propagators_impl, propagators_name): (Vec<_>, Vec<_>) = - propagators.into_iter().unzip(); - tracing::debug!(target: "otel::setup", OTEL_PROPAGATORS = propagators_name.join(",")); - let composite_propagator = TextMapCompositePropagator::new(propagators_impl); - opentelemetry::global::set_text_map_propagator(composite_propagator); - } - Ok(()) -} - -#[allow(clippy::box_default)] -fn propagator_from_string( - v: &str, -) -> Result>, TraceError> { - match v { - "tracecontext" => Ok(Some(Box::new(TraceContextPropagator::new()))), - "baggage" => Ok(Some(Box::new(BaggagePropagator::new()))), - #[cfg(feature = "zipkin")] - "b3" => Ok(Some(Box::new( - opentelemetry_zipkin::Propagator::with_encoding( - opentelemetry_zipkin::B3Encoding::SingleHeader, - ), - ))), - #[cfg(not(feature = "zipkin"))] - "b3" => Err(TraceError::from( - "unsupported propagators form env OTEL_PROPAGATORS: 'b3', try to enable compile feature 'zipkin'" - )), - #[cfg(feature = "zipkin")] - "b3multi" => Ok(Some(Box::new( - opentelemetry_zipkin::Propagator::with_encoding( - opentelemetry_zipkin::B3Encoding::MultipleHeader, - ), - ))), - #[cfg(not(feature = "zipkin"))] - "b3multi" => Err(TraceError::from( - "unsupported propagators form env OTEL_PROPAGATORS: 'b3multi', try to enable compile feature 'zipkin'" - )), - "jaeger" => Err(TraceError::from( - "unsupported propagators form env OTEL_PROPAGATORS: 'jaeger', try to enable compile feature 'jaeger'" - )), - "xray" => Err(TraceError::from( - "unsupported propagators form env OTEL_PROPAGATORS: 'xray', try to enable compile feature 'xray'" - )), - "none" => Ok(None), - unknown => Err(TraceError::from(format!( - "unsupported propagators form env OTEL_PROPAGATORS: '{unknown}'" - ))), - } -} - -#[cfg(test)] -#[cfg(feature = "tracer")] -mod tests { - use assert2::let_assert; - - #[test] - fn init_tracing_failed_on_invalid_propagator() { - let_assert!(Err(_) = super::propagator_from_string("xxxxxx")); - - // std::env::set_var("OTEL_PROPAGATORS", "xxxxxx"); - // dbg!(std::env::var("OTEL_PROPAGATORS")); - // let_assert!(Err(_) = init_tracing()); - } -}