Skip to content

Commit

Permalink
Move propagators to SDK
Browse files Browse the repository at this point in the history
Move third party propagators from contrib to SDK.
  • Loading branch information
Andreas Wiede committed Oct 5, 2020
1 parent 7b914dc commit 07b3349
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 66 deletions.
4 changes: 2 additions & 2 deletions examples/aws-xray/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use hyper::{body::Body, Client};
use opentelemetry::api::{Context, TraceContextExt, Tracer};
use opentelemetry::{
api, exporter::trace::stdout, global, sdk, sdk::id_generator::aws::XrayIdGenerator,
sdk::propagator::aws::XrayPropagator,
};
use opentelemetry_contrib::XrayTraceContextPropagator;

fn init_tracer() -> (sdk::Tracer, stdout::Uninstall) {
global::set_text_map_propagator(XrayTraceContextPropagator::new());
global::set_text_map_propagator(XrayPropagator::new());

// Install stdout exporter pipeline to be able to retrieve the collected spans.
// For the demonstration, use `Sampler::AlwaysOn` sampler to sample all traces. In a production
Expand Down
4 changes: 2 additions & 2 deletions examples/aws-xray/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use opentelemetry::{
exporter::trace::stdout,
global, sdk,
sdk::id_generator::aws::XrayIdGenerator,
sdk::propagator::aws::XrayPropagator,
};
use opentelemetry_contrib::XrayTraceContextPropagator;
use std::{convert::Infallible, net::SocketAddr};

async fn handle(req: Request<Body>) -> Result<Response<Body>, Infallible> {
Expand All @@ -29,7 +29,7 @@ async fn handle(req: Request<Body>) -> Result<Response<Body>, Infallible> {
}

fn init_tracer() -> (sdk::Tracer, stdout::Uninstall) {
global::set_text_map_propagator(XrayTraceContextPropagator::new());
global::set_text_map_propagator(XrayPropagator::new());

// Install stdout exporter pipeline to be able to retrieve the collected spans.
// For the demonstration, use `Sampler::AlwaysOn` sampler to sample all traces. In a production
Expand Down
Empty file.
9 changes: 0 additions & 9 deletions opentelemetry-contrib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,5 @@
//! some users.
//!
//! Typically, those include vendor specific propagators.
mod trace_propagator;

#[cfg(feature = "datadog")]
pub mod datadog;

pub use trace_propagator::{
aws_xray_propagator::XrayTraceContextPropagator,
b3_propagator::{B3Encoding, B3Propagator},
jaeger_propagator::JaegerPropagator,
};
7 changes: 0 additions & 7 deletions opentelemetry-contrib/src/trace_propagator/mod.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
use opentelemetry::api::trace::span_context::TraceState;
use opentelemetry::api::{
Context, Extractor, FieldIter, Injector, SpanContext, SpanId, TextMapFormat, TraceContextExt,
TraceId, TRACE_FLAG_DEFERRED, TRACE_FLAG_NOT_SAMPLED, TRACE_FLAG_SAMPLED,
//! # AWS X-Ray Trace Propagator
//!
//! Extracts and injects values to/from the `x-amzn-trace-id` header. Converting between
//! OpenTelemetry [SpanContext][otel-spec] and [X-Ray Trace format][xray-trace-id].
//!
//! For details on the [`x-amzn-trace-id` header][xray-header] see the AWS X-Ray Docs.
//!
//! ## Example
//!
//! ```
//! use opentelemetry::global;
//! use opentelemetry::sdk::propagator::aws::XrayPropagator;
//!
//! global::set_text_map_propagator(XrayPropagator::default());
//! ```
//!
//! [otel-spec]: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/api.md#spancontext
//! [xray-trace-id]: https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids
//! [xray-header]: https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader
use crate::api::{
trace::span_context::TraceState, Context, Extractor, FieldIter, Injector, SpanContext, SpanId,
TextMapFormat, TraceContextExt, TraceId, TRACE_FLAG_DEFERRED, TRACE_FLAG_NOT_SAMPLED,
TRACE_FLAG_SAMPLED,
};

const AWS_XRAY_TRACE_HEADER: &str = "x-amzn-trace-id";
Expand All @@ -18,34 +37,16 @@ lazy_static::lazy_static! {
static ref AWS_XRAY_HEADER_FIELD: [String; 1] = [AWS_XRAY_TRACE_HEADER.to_string()];
}

/// # AWS X-Ray Trace Propagator
///
/// Extracts and injects values to/from the `x-amzn-trace-id` header. Converting between
/// OpenTelemetry [SpanContext][otel-spec] and [X-Ray Trace format][xray-trace-id].
///
/// For details on the [`x-amzn-trace-id` header][xray-header] see the AWS X-Ray Docs.
///
/// ## Example
///
/// ```
/// extern crate opentelemetry;
/// use opentelemetry::global;
/// use opentelemetry_contrib::XrayTraceContextPropagator;
///
/// global::set_text_map_propagator(XrayTraceContextPropagator::default());
/// ```
///
/// [otel-spec]: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/api.md#spancontext
/// [xray-trace-id]: https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids
/// [xray-header]: https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader
/// Extracts and injects `SpanContext`s into `Extractor`s or `Injector`s using AWS X-Ray header format.
#[derive(Clone, Debug, Default)]
pub struct XrayTraceContextPropagator {
pub struct XrayPropagator {
_private: (),
}

impl XrayTraceContextPropagator {
impl XrayPropagator {
/// Creates a new `XrayTraceContextPropagator`.
pub fn new() -> Self {
XrayTraceContextPropagator::default()
XrayPropagator::default()
}

fn extract_span_context(&self, extractor: &dyn Extractor) -> Result<SpanContext, ()> {
Expand Down Expand Up @@ -102,7 +103,7 @@ impl XrayTraceContextPropagator {
}
}

impl TextMapFormat for XrayTraceContextPropagator {
impl TextMapFormat for XrayPropagator {
fn inject_context(&self, cx: &Context, injector: &mut dyn Injector) {
let span_context: SpanContext = cx.span().span_context();
if span_context.is_valid() {
Expand Down Expand Up @@ -233,8 +234,8 @@ fn title_case(s: &str) -> String {
#[cfg(test)]
mod tests {
use super::*;
use opentelemetry::api;
use opentelemetry::api::trace::span_context::TraceState;
use crate::api;
use crate::api::trace::span_context::TraceState;
use std::collections::HashMap;
use std::str::FromStr;
use std::time::SystemTime;
Expand Down Expand Up @@ -278,7 +279,7 @@ mod tests {
.into_iter()
.collect();

let propagator = XrayTraceContextPropagator::default();
let propagator = XrayPropagator::default();
let context = propagator.extract(&map);
assert_eq!(context.remote_span_context(), Some(&expected));
}
Expand All @@ -287,7 +288,7 @@ mod tests {
#[test]
fn test_extract_empty() {
let map: HashMap<String, String> = HashMap::new();
let propagator = XrayTraceContextPropagator::default();
let propagator = XrayPropagator::default();
let context = propagator.extract(&map);
assert_eq!(
context.remote_span_context(),
Expand Down Expand Up @@ -320,7 +321,7 @@ mod tests {

#[test]
fn test_inject() {
let propagator = XrayTraceContextPropagator::default();
let propagator = XrayPropagator::default();
for (header_value, span_context) in inject_test_data() {
let mut injector: HashMap<String, String> = HashMap::new();
propagator.inject_context(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
//!
//! If `inject_encoding` is set to `B3Encoding::SingleHeader` then `b3` header is used to inject
//! and extract. Otherwise, separate headers are used to inject and extract.
use opentelemetry::api::trace::span_context::TraceState;
use opentelemetry::{
use crate::{
api,
api::{context::propagation::text_propagator::FieldIter, TraceContextExt, TRACE_FLAG_DEFERRED},
api::{
context::propagation::text_propagator::FieldIter, trace::span_context::TraceState,
TraceContextExt, TRACE_FLAG_DEFERRED,
},
};

static B3_SINGLE_HEADER: &str = "b3";
Expand Down Expand Up @@ -310,10 +312,8 @@ impl api::TextMapFormat for B3Propagator {
#[cfg(test)]
mod tests {
use super::*;
use opentelemetry::api::trace::span_context::{SpanId, TraceId, TRACE_FLAG_NOT_SAMPLED};
use opentelemetry::api::{
TextMapFormat, TRACE_FLAG_DEBUG, TRACE_FLAG_DEFERRED, TRACE_FLAG_SAMPLED,
};
use crate::api::trace::span_context::{SpanId, TraceId, TRACE_FLAG_NOT_SAMPLED};
use crate::api::{TextMapFormat, TRACE_FLAG_DEBUG, TRACE_FLAG_DEFERRED, TRACE_FLAG_SAMPLED};
use std::collections::HashMap;

const TRACE_ID_STR: &str = "4bf92f3577b34da6a3ce929d0e0e4736";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//!
//! [`Jaeger documentation`]: https://www.jaegertracing.io/docs/1.18/client-libraries/#propagation-format
use opentelemetry::api::trace::span_context::TraceState;
use opentelemetry::api::{
use crate::api::trace::span_context::TraceState;
use crate::api::{
Context, Extractor, FieldIter, Injector, SpanContext, SpanId, TextMapFormat, TraceContextExt,
TraceId, TRACE_FLAG_DEBUG, TRACE_FLAG_NOT_SAMPLED, TRACE_FLAG_SAMPLED,
};
Expand Down Expand Up @@ -171,13 +171,13 @@ impl TextMapFormat for JaegerPropagator {

#[cfg(test)]
mod tests {
use crate::trace_propagator::jaeger_propagator::{JaegerPropagator, JAEGER_HEADER};
use opentelemetry::api;
use opentelemetry::api::trace::span_context::TraceState;
use opentelemetry::api::{
use crate::api;
use crate::api::trace::span_context::TraceState;
use crate::api::{
Context, Injector, Span, SpanContext, SpanId, TextMapFormat, TraceContextExt, TraceId,
TRACE_FLAG_DEBUG, TRACE_FLAG_NOT_SAMPLED, TRACE_FLAG_SAMPLED,
};
use crate::sdk::propagator::jaeger::{JaegerPropagator, JAEGER_HEADER};
use std::collections::HashMap;
use std::time::SystemTime;

Expand Down
3 changes: 3 additions & 0 deletions src/sdk/trace/propagator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
//! more information see the [OpenTelemetry Spec]['spec'].
//!
//! ['spec']: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/context/api-propagators.md#overview
pub mod aws;
pub mod b3;
pub mod jaeger;
pub mod w3;

0 comments on commit 07b3349

Please sign in to comment.