Skip to content

Commit

Permalink
remove generic propagator
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Jan 16, 2024
1 parent 741e17e commit 21f6d7c
Showing 1 changed file with 4 additions and 102 deletions.
106 changes: 4 additions & 102 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<dyn TextMapPropagator + Send + Sync>, 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::<Result<Vec<_>, _>>()?
.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<Option<Box<dyn TextMapPropagator + Send + Sync>>, 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());
}
}

0 comments on commit 21f6d7c

Please sign in to comment.