Skip to content

Commit

Permalink
Add SpanContext trait. (#10)
Browse files Browse the repository at this point in the history
This changeset adds the SpanContext trait with support for
baggage item iteration.

Completes #6
  • Loading branch information
stefano-pogliani authored and daschl committed Mar 13, 2018
1 parent 87fb663 commit 528570c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
target/
*.swp
Cargo.lock

target/
46 changes: 46 additions & 0 deletions opentracing-api/src/context.rs
Original file line number Diff line number Diff line change
@@ -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<Item = (&'a String, &'a String)>;

/// 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<String, String>,
}
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())])
}
}
2 changes: 2 additions & 0 deletions opentracing-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};

0 comments on commit 528570c

Please sign in to comment.