1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#[derive(Debug)]
pub struct Duration(pub std::time::Duration);

impl serde::Serialize for Duration {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_u64(self.0.as_nanos() as u64)
    }
}

#[derive(Debug, Serialize)]
#[serde(tag = "type")]
pub enum SpanItem {
    Log {
        message: &'static str,
    },
    Field {
        name: &'static str,
        value: serde_json::Value,
    },
    TransientField {
        name: &'static str,
        value: serde_json::Value,
    },
    Query {
        query: String,
        bind: Option<String>,
        result: Result<usize, String>,
    },
    Frame(Span),
}

#[derive(Serialize)]
pub struct Span {
    pub id: String,
    key: String,
    pub items: Vec<(Duration, SpanItem)>,
    pub success: Option<bool>,
    pub result: Option<serde_json::Value>,
    pub err: Option<String>,
    #[serde(skip_serializing)]
    pub created_on: std::time::Instant,
    pub duration: Option<Duration>,
}

impl Clone for Span {
    fn clone(&self) -> Self {
        Span::new(&self.id)
    }
}

impl std::fmt::Debug for Span {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_struct("Span")
            .field("id", &self.id)
            .field("key", &self.key)
            .field("items", &self.items)
            .field("success", &self.success)
            .field("result", &self.result)
            .field("err", &self.err)
            .field("start_time", &self.created_on)
            .field("duration", &self.duration)
            .finish()
    }
}

impl Span {
    pub fn duration(&self) -> std::time::Duration {
        self.duration
            .as_ref()
            .map(|v| v.0)
            .unwrap_or_else(|| std::time::Instant::now().duration_since(self.created_on))
    }

    pub fn new(id: &str) -> Span {
        Span {
            id: id.to_owned(),
            key: uuid::Uuid::new_v4().to_string(),
            items: Vec::new(),
            success: None,
            result: None,
            err: None,
            created_on: std::time::Instant::now(),
            duration: None,
        }
    }
    pub(crate) fn set_id(&mut self, id: &str) {
        self.id = id.to_string();
    }

    pub fn end(&mut self) -> &mut Self {
        // TODO: assert .ended_on is null
        self.duration = Some(Duration(
            std::time::Instant::now().duration_since(self.created_on),
        ));
        self
    }

    pub fn set_result(&mut self, result: impl serde::Serialize) -> &mut Self {
        // TODO: assert .result is null
        self.result = Some(json!(result));
        self
    }

    pub fn set_success(&mut self, is_success: bool) -> &mut Self {
        // TODO: assert .success is null
        self.success = Some(is_success);
        self
    }

    pub fn set_err(&mut self, err: Option<String>) -> &mut Self {
        // TODO: assert .err is null
        self.err = err;
        self
    }

    pub fn add_sub_frame(&mut self, created_on: std::time::Instant, frame: Span) {
        self.items.push((
            Duration(created_on.duration_since(self.created_on)),
            SpanItem::Frame(frame),
        ));
    }

    pub fn get_key(&self) -> String {
        self.key.clone()
    }

    pub fn add_log(&mut self, log: &'static str) {
        self.items.push((
            Duration(std::time::Instant::now().duration_since(self.created_on)),
            SpanItem::Log { message: log },
        ))
    }

    //adding breadcrumbs
    pub fn add_breadcrumbs(&mut self, name: &'static str, value: serde_json::Value) {
        self.items.push((
            Duration(std::time::Instant::now().duration_since(self.created_on)),
            SpanItem::Field { name, value },
        ))
    }

    pub fn add_transient_field(&mut self, name: &'static str, value: serde_json::Value) {
        self.items.push((
            Duration(std::time::Instant::now().duration_since(self.created_on)),
            SpanItem::TransientField { name, value },
        ))
    }

    pub fn add_query(
        &mut self,
        query: String,
        bind: Option<String>,
        result: Result<usize, String>,
    ) {
        self.items.push((
            Duration(std::time::Instant::now().duration_since(self.created_on)),
            SpanItem::Query {
                query,
                bind,
                result,
            },
        ))
    }
}