|
3 | 3 | // https://github.com/davidB/tracing-opentelemetry-instrumentation-sdk/blob/d3609ac2cc699d3a24fbf89754053cc8e938e3bf/LICENSE
|
4 | 4 |
|
5 | 5 | use opentelemetry_sdk::{
|
6 |
| - propagation::{ |
7 |
| - BaggagePropagator, TextMapCompositePropagator, TraceContextPropagator, |
8 |
| - }, |
9 | 6 | resource::{OsResourceDetector, ResourceDetector},
|
10 | 7 | Resource,
|
11 | 8 | };
|
12 | 9 | use std::cmp::max;
|
13 | 10 | use tracing::Subscriber;
|
14 | 11 | use tracing_subscriber::fmt::writer::MakeWriterExt;
|
15 |
| - |
16 |
| -use opentelemetry::{propagation::TextMapPropagator, trace::TraceError}; |
17 | 12 | use tracing_subscriber::{
|
18 | 13 | filter::LevelFilter, fmt::format::FmtSpan, layer::SubscriberExt,
|
19 | 14 | registry::LookupSpan, Layer,
|
@@ -144,7 +139,10 @@ pub fn init_tracing_with_fallbacks(
|
144 | 139 | DetectResource::new(fallback_service_name, fallback_service_version).build();
|
145 | 140 | let otel_tracer =
|
146 | 141 | otlp::init_tracer(otel_rsrc, otlp::identity).expect("setup of Tracer");
|
147 |
| - init_propagator().expect("setup of propagator"); |
| 142 | + |
| 143 | + opentelemetry::global::set_text_map_propagator( |
| 144 | + propagation::TextMapSplitPropagator::default(), |
| 145 | + ); |
148 | 146 |
|
149 | 147 | let level_filter: LevelFilter = max(log_level, tracing::Level::INFO).into();
|
150 | 148 | let subscriber = tracing_subscriber::registry()
|
@@ -177,99 +175,3 @@ macro_rules! init_tracing {
|
177 | 175 | pub fn shutdown_signal() {
|
178 | 176 | opentelemetry::global::shutdown_tracer_provider();
|
179 | 177 | }
|
180 |
| - |
181 |
| -/// 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) |
182 |
| -/// Specifies Propagators to be used in a comma-separated list. |
183 |
| -/// Default value: `"tracecontext,baggage"` |
184 |
| -/// Example: `export OTEL_PROPAGATORS="b3"` |
185 |
| -/// Accepted values for `OTEL_PROPAGATORS` are: |
186 |
| -/// |
187 |
| -/// - "tracecontext": W3C Trace Context |
188 |
| -/// - "baggage": W3C Baggage |
189 |
| -/// - "b3": B3 Single (require feature "zipkin") |
190 |
| -/// - "b3multi": B3 Multi (require feature "zipkin") |
191 |
| -/// - "xray": AWS X-Ray (require feature "xray") |
192 |
| -/// - "ottrace": OT Trace (third party) (not supported) |
193 |
| -/// - "none": No automatically configured propagator. |
194 |
| -/// |
195 |
| -/// # Errors |
196 |
| -/// |
197 |
| -/// Will return `TraceError` if issue in reading or instanciate propagator. |
198 |
| -pub fn init_propagator() -> Result<(), TraceError> { |
199 |
| - let value_from_env = std::env::var("OTEL_PROPAGATORS") |
200 |
| - .unwrap_or_else(|_| "tracecontext,baggage".to_string()); |
201 |
| - let propagators: Vec<(Box<dyn TextMapPropagator + Send + Sync>, String)> = |
202 |
| - value_from_env |
203 |
| - .split(',') |
204 |
| - .map(|s| { |
205 |
| - let name = s.trim().to_lowercase(); |
206 |
| - propagator_from_string(&name).map(|o| o.map(|b| (b, name))) |
207 |
| - }) |
208 |
| - .collect::<Result<Vec<_>, _>>()? |
209 |
| - .into_iter() |
210 |
| - .flatten() |
211 |
| - .collect(); |
212 |
| - if !propagators.is_empty() { |
213 |
| - let (propagators_impl, propagators_name): (Vec<_>, Vec<_>) = |
214 |
| - propagators.into_iter().unzip(); |
215 |
| - tracing::debug!(target: "otel::setup", OTEL_PROPAGATORS = propagators_name.join(",")); |
216 |
| - let composite_propagator = TextMapCompositePropagator::new(propagators_impl); |
217 |
| - opentelemetry::global::set_text_map_propagator(composite_propagator); |
218 |
| - } |
219 |
| - Ok(()) |
220 |
| -} |
221 |
| - |
222 |
| -#[allow(clippy::box_default)] |
223 |
| -fn propagator_from_string( |
224 |
| - v: &str, |
225 |
| -) -> Result<Option<Box<dyn TextMapPropagator + Send + Sync>>, TraceError> { |
226 |
| - match v { |
227 |
| - "tracecontext" => Ok(Some(Box::new(TraceContextPropagator::new()))), |
228 |
| - "baggage" => Ok(Some(Box::new(BaggagePropagator::new()))), |
229 |
| - #[cfg(feature = "zipkin")] |
230 |
| - "b3" => Ok(Some(Box::new( |
231 |
| - opentelemetry_zipkin::Propagator::with_encoding( |
232 |
| - opentelemetry_zipkin::B3Encoding::SingleHeader, |
233 |
| - ), |
234 |
| - ))), |
235 |
| - #[cfg(not(feature = "zipkin"))] |
236 |
| - "b3" => Err(TraceError::from( |
237 |
| - "unsupported propagators form env OTEL_PROPAGATORS: 'b3', try to enable compile feature 'zipkin'" |
238 |
| - )), |
239 |
| - #[cfg(feature = "zipkin")] |
240 |
| - "b3multi" => Ok(Some(Box::new( |
241 |
| - opentelemetry_zipkin::Propagator::with_encoding( |
242 |
| - opentelemetry_zipkin::B3Encoding::MultipleHeader, |
243 |
| - ), |
244 |
| - ))), |
245 |
| - #[cfg(not(feature = "zipkin"))] |
246 |
| - "b3multi" => Err(TraceError::from( |
247 |
| - "unsupported propagators form env OTEL_PROPAGATORS: 'b3multi', try to enable compile feature 'zipkin'" |
248 |
| - )), |
249 |
| - "jaeger" => Err(TraceError::from( |
250 |
| - "unsupported propagators form env OTEL_PROPAGATORS: 'jaeger', try to enable compile feature 'jaeger'" |
251 |
| - )), |
252 |
| - "xray" => Err(TraceError::from( |
253 |
| - "unsupported propagators form env OTEL_PROPAGATORS: 'xray', try to enable compile feature 'xray'" |
254 |
| - )), |
255 |
| - "none" => Ok(None), |
256 |
| - unknown => Err(TraceError::from(format!( |
257 |
| - "unsupported propagators form env OTEL_PROPAGATORS: '{unknown}'" |
258 |
| - ))), |
259 |
| - } |
260 |
| -} |
261 |
| - |
262 |
| -#[cfg(test)] |
263 |
| -#[cfg(feature = "tracer")] |
264 |
| -mod tests { |
265 |
| - use assert2::let_assert; |
266 |
| - |
267 |
| - #[test] |
268 |
| - fn init_tracing_failed_on_invalid_propagator() { |
269 |
| - let_assert!(Err(_) = super::propagator_from_string("xxxxxx")); |
270 |
| - |
271 |
| - // std::env::set_var("OTEL_PROPAGATORS", "xxxxxx"); |
272 |
| - // dbg!(std::env::var("OTEL_PROPAGATORS")); |
273 |
| - // let_assert!(Err(_) = init_tracing()); |
274 |
| - } |
275 |
| -} |
0 commit comments