Skip to content

Commit

Permalink
WIP: Span
Browse files Browse the repository at this point in the history
  • Loading branch information
daschl committed Mar 15, 2018
1 parent 1f5ab04 commit bf4359a
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 2 deletions.
4 changes: 3 additions & 1 deletion opentracing-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
mod context;
mod tag;
mod field;
mod span;

pub use context::SpanContext;
pub use tag::{ParseTagsError, Tags};
pub use tag::{ParseTagsError, TagValue, Tags};
pub use field::{Fields, ParseFieldsError};
pub use span::{FinishedSpan, Span};
61 changes: 61 additions & 0 deletions opentracing-api/src/span.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use SpanContext;
use TagValue;

/// The `Span` represents the OpenTracing specification's Span contract.
pub trait Span<'a> {
/// The associated `SpanContext`.
type Context: SpanContext<'a>;

/// Retrieve the associated `SpanContext`.
///
/// This may be called any time, including after `finish`.
fn context(&self) -> &Self::Context;

/// Sets a key:value tag on the `Span`.
fn set_tag(&mut self, key: &str, value: TagValue);

/// Returns a tag value if set, none otherwise
fn tag(&self, key: &str) -> Option<&'a TagValue>;

/// Record an event at the current walltime timestamp.
fn log(&mut self, event: String);

/// Record an event at the given walltime timestamp.
fn log_at(&mut self, timestamp: u64, event: String);

/// Sets a baggage item in the Span (and its SpanContext) as a key/value pair.
fn set_baggage_item(&mut self, key: &str, value: String);

/// the value of the baggage item identified by the given key, or None if no such item
/// could be found.
fn baggage_item(&self, key: &str) -> Option<&'a String>;

/// Sets the string name for the logical operation this span represents.
fn set_operation_name(&mut self, name: &str);

/// Returns the operation name if set, None otherwise.
fn operation_name(&self) -> Option<&'a str>;

/// Sets the end timestamp to now and finishes (records) the span.
fn finish(self) -> FinishedSpan<Self::Context>;

/// Sets an explicit end timestamp and finishes (records) the span.
fn finish_at(self, timestamp: u64) -> FinishedSpan<Self::Context>;
}

pub struct FinishedSpan<C> {
context: C,
}

impl<'a, C> FinishedSpan<C>
where
C: SpanContext<'a>,
{
pub fn new(context: C) -> Self {
FinishedSpan { context }
}

pub fn context(&self) -> &C {
&self.context
}
}
6 changes: 6 additions & 0 deletions opentracing-api/src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ impl fmt::Display for ParseTagsError {
}
}

pub enum TagValue {
String(String),
Boolean(bool),
Number(f64),
}

#[cfg(test)]
mod tests {

Expand Down
52 changes: 51 additions & 1 deletion opentracing-noop/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

extern crate opentracing_api;

use opentracing_api::SpanContext;
use opentracing_api::{FinishedSpan, Span, SpanContext, TagValue};
use std::iter::{empty, Empty};

/// The `NoopSpanContext` just returns an empty iterator on
Expand All @@ -25,6 +25,56 @@ impl<'a> SpanContext<'a> for NoopSpanContext {
}
}

pub struct NoopSpan {
ctx: NoopSpanContext,
}

impl NoopSpan {
pub fn new() -> Self {
NoopSpan {
ctx: NoopSpanContext::default(),
}
}
}

impl<'a> Span<'a> for NoopSpan {
type Context = NoopSpanContext;

fn context(&self) -> &Self::Context {
&self.ctx
}

fn set_tag(&mut self, _key: &str, _value: TagValue) {}

fn tag(&self, _key: &str) -> Option<&'a TagValue> {
None
}

fn log(&mut self, _event: String) {}

fn log_at(&mut self, _timestamp: u64, _event: String) {}

fn set_baggage_item(&mut self, _key: &str, _value: String) {}

fn baggage_item(&self, _key: &str) -> Option<&'a String> {
None
}

fn set_operation_name(&mut self, _name: &str) {}

fn operation_name(&self) -> Option<&'a str> {
None
}

fn finish(self) -> FinishedSpan<Self::Context> {
FinishedSpan::new(self.ctx)
}

fn finish_at(self, _timestamp: u64) -> FinishedSpan<Self::Context> {
FinishedSpan::new(self.ctx)
}
}

#[cfg(test)]
mod tests {

Expand Down

0 comments on commit bf4359a

Please sign in to comment.