tusk_data 0.0.1

Data types for monitoring with Tusk
Documentation
use chrono;
use uuid;

use ::logs::types::Message;
use ::metadata;

#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum APMTypes {
    Span(Span),
    //Trace(Trace),
    //Context(Context),
}

/// Each `Span` must have a unique identifier assigned to it
/// in order to keep track of which span belongs to witch.
pub type SpanID = uuid::Uuid;

// Data relating to the length of time that a
// series of tasks took to complete.
/// If the `parent_id` is `None` then this is a root span.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Span {
    parent_id: Option<SpanID>,
    id: SpanID,
    name: Message,
    start: chrono::DateTime<chrono::Utc>,
    end: Option<chrono::DateTime<chrono::Utc>>,
    logs: Vec<Message>,
    labels: metadata::Labels,
    error: bool,
    warn: bool,
}

impl Default for Span {
    fn default() -> Self {
        Self::new("Unknown".to_string())
    }
}

impl Span {
    /// Returns a new instance of a span.
    /// This would only really be used for
    /// creating a parent span.
    /// ```
    /// let span = Span::new();
    /// ```
    pub fn new(name: Message) -> Self {
        Span {
            parent_id: None,
            id: uuid::Uuid::new_v4(),
            name: name,
            start: chrono::Utc::now(),
            end: None,
            logs: Vec::new(),
            labels: metadata::Labels::new(),
            error: false,
            warn: false,
        }
    }

    /// Adds a note to the span that a fatal
    /// error took place.
    pub fn error(&mut self) {
        self.error = true;
    }

    /// Adds a note to the span that a recoverable
    /// error took place.
    pub fn warn(&mut self) {
        self.warn = true;
    }

    /// Closes this span.
    pub fn end(&mut self) {
        self.end = Some(chrono::Utc::now());
    }

    /// Adds some information to this span that might
    /// add context about what happened.
    pub fn log(&mut self, log: &Message) {
        self.logs.push(log.clone());
    }

    /// Creates and returns a span that is a child of
    /// the calling span.
    pub fn child(&self, name: &Message) -> Self {
        let mut child = Self::new(name.clone());
        child.parent_id = Some(self.id);
        child
    }

    /// Creates and returns a span that is a child of
    /// the provided parent ID.
    pub fn child_from_parent_id(name: &Message, parent_id: SpanID) -> Self {
        let mut child = Self::new(name.clone());
        child.parent_id = Some(parent_id);
        child
    }
}

impl APMTypes {}