-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This changeset adds the SpanContext trait with support for baggage item iteration. Completes #6
- Loading branch information
1 parent
87fb663
commit 528570c
Showing
3 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
target/ | ||
*.swp | ||
Cargo.lock | ||
|
||
target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |