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
use chrono::{DateTime, Utc};
use hyper::header::HeaderValue;
use std::fmt;
use std::ops::Deref;
use uuid::Uuid;

/// Header - `X-Span-ID` - used to track a request through a chain of microservices.
pub const X_SPAN_ID: &str = "X-Span-ID";

/// Wrapper for a string being used as an X-Span-ID.
#[derive(Debug, Clone)]
pub struct XSpanIdString(pub String);

impl XSpanIdString {
    /// Extract an X-Span-ID from a request header if present, and if not
    /// generate a new one.
    pub fn get_or_generate<T>(req: &hyper::Request<T>) -> Self {
        let x_span_id = req.headers().get(X_SPAN_ID);

        x_span_id
            .map(|x| x.to_str().ok())
            .flatten()
            .map(|x| XSpanIdString(x.to_string()))
            .unwrap_or_default()
    }
}

impl Default for XSpanIdString {
    fn default() -> Self {
        XSpanIdString(Uuid::new_v4().to_string())
    }
}

impl fmt::Display for XSpanIdString {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// A struct to allow homogeneous conversion into a HeaderValue. We can't
/// implement the From/Into trait on HeaderValue because we don't own
/// either of the types.
#[derive(Debug, Clone)]
pub struct IntoHeaderValue<T>(pub T);

// Generic implementations

impl<T> Deref for IntoHeaderValue<T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.0
    }
}

// Derive for each From<T> in hyper::header::HeaderValue

macro_rules! ihv_generate {
    ($t:ident) => {
        impl From<HeaderValue> for IntoHeaderValue<$t> {
            fn from(hdr_value: HeaderValue) -> Self {
                IntoHeaderValue(hdr_value.to_str().unwrap().parse::<$t>().unwrap())
            }
        }

        impl From<IntoHeaderValue<$t>> for HeaderValue {
            fn from(hdr_value: IntoHeaderValue<$t>) -> Self {
                hdr_value.0.into()
            }
        }
    };
}

ihv_generate!(u64);
ihv_generate!(i64);
ihv_generate!(i16);
ihv_generate!(u16);
ihv_generate!(u32);
ihv_generate!(usize);
ihv_generate!(isize);
ihv_generate!(i32);

// Custom derivations

impl From<HeaderValue> for IntoHeaderValue<Vec<String>> {
    fn from(hdr_value: HeaderValue) -> Self {
        IntoHeaderValue(
            hdr_value
                .to_str()
                .unwrap()
                .split(',')
                .filter_map(|x| match x.trim() {
                    "" => None,
                    y => Some(y.to_string()),
                })
                .collect(),
        )
    }
}

impl From<IntoHeaderValue<Vec<String>>> for HeaderValue {
    fn from(hdr_value: IntoHeaderValue<Vec<String>>) -> Self {
        HeaderValue::from_str(&hdr_value.0.join(", ")).unwrap()
    }
}

impl From<HeaderValue> for IntoHeaderValue<String> {
    fn from(hdr_value: HeaderValue) -> Self {
        IntoHeaderValue(hdr_value.to_str().unwrap().to_string())
    }
}

impl From<IntoHeaderValue<String>> for HeaderValue {
    fn from(hdr_value: IntoHeaderValue<String>) -> Self {
        HeaderValue::from_str(&hdr_value.0).unwrap()
    }
}

impl From<HeaderValue> for IntoHeaderValue<DateTime<Utc>> {
    fn from(hdr_value: HeaderValue) -> Self {
        IntoHeaderValue(
            DateTime::parse_from_rfc3339(hdr_value.to_str().unwrap())
                .unwrap()
                .with_timezone(&Utc),
        )
    }
}

impl From<IntoHeaderValue<DateTime<Utc>>> for HeaderValue {
    fn from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Self {
        HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()).unwrap()
    }
}

#[cfg(test)]
mod tests {
    use super::XSpanIdString;
    use hyper::Request;

    /// Create a request with the given X-Span-ID.
    fn create_xspan_id_req(id: &'static str) -> Request<()> {
        Request::builder().header("X-Span-ID", id).body(()).unwrap()
    }

    #[test]
    fn xspanid_test() {
        XSpanIdString::get_or_generate(&create_xspan_id_req("£"));

        let valid_uuid = "123e4567-e89b-12d3-a456-426655440000";

        assert_eq!(
            valid_uuid,
            &XSpanIdString::get_or_generate(&create_xspan_id_req(valid_uuid)).0
        );
    }
}