diff --git a/.gitignore b/.gitignore index 2c96eb1..d8a0347 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ -target/ +*.swp Cargo.lock + +target/ diff --git a/opentracing-api/src/context.rs b/opentracing-api/src/context.rs new file mode 100644 index 0000000..655c142 --- /dev/null +++ b/opentracing-api/src/context.rs @@ -0,0 +1,46 @@ +/// SpanContext represents Span state that must propagate to +/// descendant Spans and across process boundaries. +/// +/// SpanContext is logically divided into two pieces: (1) the user-level "Baggage" that +/// propagates across Span boundaries and (2) any Tracer-implementation-specific fields +/// that are needed to identify or otherwise contextualize the associated Span instance +/// (e.g., a `(trace_id, span_id, sampled)` tuple). +pub trait SpanContext<'a> { + /// Associated type defining how to iterate over baggage items. + type Iter: Iterator; + + /// Iterate over baggage items. + /// + /// Baggage items are key/value pairs that are propagated from + /// the associated `Span` throught the trace. + fn baggage_items(&'a self) -> Self::Iter; +} + +#[cfg(test)] +mod tests { + use std::collections::HashMap; + use std::collections::hash_map::Iter as HashMapIter; + use super::SpanContext; + + struct TestContext { + items: HashMap, + } + impl<'a> SpanContext<'a> for TestContext { + type Iter = HashMapIter<'a, String, String>; + + fn baggage_items(&'a self) -> Self::Iter { + self.items.iter() + } + } + + #[test] + fn get_items() { + let mut items = HashMap::new(); + items.insert("key".into(), "value".into()); + let context = TestContext { + items: items + }; + let items: Vec<(&String, &String)> = context.baggage_items().collect(); + assert_eq!(items, [(&"key".into(), &"value".into())]) + } +} diff --git a/opentracing-api/src/lib.rs b/opentracing-api/src/lib.rs index 60f0898..c49bc73 100644 --- a/opentracing-api/src/lib.rs +++ b/opentracing-api/src/lib.rs @@ -1,7 +1,9 @@ #![doc(html_root_url = "https://docs.rs/opentracing-api/0.1.0")] +mod context; mod tag; mod field; +pub use context::SpanContext; pub use tag::{Tags, ParseTagsError}; pub use field::{Fields, ParseFieldsError};