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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use std::fmt::Debug;

use async_trait::async_trait;
use http::Request;
use opentelemetry::propagation::{Extractor, Injector};
use opentelemetry::trace::TraceError;

pub struct HeaderInjector<'a>(pub &'a mut http::HeaderMap);

impl<'a> Injector for HeaderInjector<'a> {
    /// Set a key and value in the HeaderMap.  Does nothing if the key or value are not valid inputs.
    fn set(&mut self, key: &str, value: String) {
        if let Ok(name) = http::header::HeaderName::from_bytes(key.as_bytes()) {
            if let Ok(val) = http::header::HeaderValue::from_str(&value) {
                self.0.insert(name, val);
            }
        }
    }
}

pub struct HeaderExtractor<'a>(pub &'a http::HeaderMap);

impl<'a> Extractor for HeaderExtractor<'a> {
    /// Get a value for a key from the HeaderMap.  If the value is not valid ASCII, returns None.
    fn get(&self, key: &str) -> Option<&str> {
        self.0.get(key).and_then(|value| value.to_str().ok())
    }

    /// Collect all the keys from the HeaderMap.
    fn keys(&self) -> Vec<&str> {
        self.0
            .keys()
            .map(|value| value.as_str())
            .collect::<Vec<_>>()
    }
}

/// A minimal interface necessary for export spans over HTTP.
///
/// Users sometime choose http clients that relay on certain runtime. This trait
/// allows users to bring their choice of http clients.
#[async_trait]
pub trait HttpClient: Debug + Send + Sync {
    /// Send a batch of spans to collectors
    async fn send(&self, request: Request<Vec<u8>>) -> Result<(), TraceError>;
}

#[cfg(feature = "reqwest")]
mod reqwest {
    use super::{async_trait, HttpClient, Request, TraceError};
    use opentelemetry::sdk::export::ExportError;
    use std::convert::TryInto;
    use thiserror::Error;

    #[async_trait]
    impl HttpClient for reqwest::Client {
        async fn send(&self, request: Request<Vec<u8>>) -> Result<(), TraceError> {
            let request = request.try_into().map_err(ReqwestError::from)?;
            let _ = self
                .execute(request)
                .await
                .and_then(|rsp| rsp.error_for_status())
                .map_err(ReqwestError::from)?;
            Ok(())
        }
    }

    #[async_trait]
    impl HttpClient for reqwest::blocking::Client {
        async fn send(&self, request: Request<Vec<u8>>) -> Result<(), TraceError> {
            let _ = request
                .try_into()
                .and_then(|req| self.execute(req))
                .and_then(|rsp| rsp.error_for_status())
                .map_err(ReqwestError::from)?;
            Ok(())
        }
    }

    #[derive(Debug, Error)]
    #[error(transparent)]
    struct ReqwestError(#[from] reqwest::Error);

    impl ExportError for ReqwestError {
        fn exporter_name(&self) -> &'static str {
            "reqwest"
        }
    }
}

#[cfg(feature = "surf")]
mod surf {
    use super::{async_trait, HttpClient, Request, TraceError};
    use opentelemetry::sdk::export::ExportError;
    use std::fmt::{Display, Formatter};

    #[async_trait]
    impl HttpClient for surf::Client {
        async fn send(&self, request: Request<Vec<u8>>) -> Result<(), TraceError> {
            let (parts, body) = request.into_parts();
            let uri = parts
                .uri
                .to_string()
                .parse()
                .map_err(|_err: surf::http::url::ParseError| TraceError::from("error parse url"))?;

            let req = surf::Request::builder(surf::http::Method::Post, uri)
                .content_type("application/json")
                .body(body);
            let result = self.send(req).await.map_err::<SurfError, _>(Into::into)?;

            if result.status().is_success() {
                Ok(())
            } else {
                Err(SurfError(surf::Error::from_str(
                    result.status(),
                    result.status().canonical_reason(),
                ))
                .into())
            }
        }
    }

    #[derive(Debug)]
    struct SurfError(surf::Error);

    impl ExportError for SurfError {
        fn exporter_name(&self) -> &'static str {
            "surf"
        }
    }

    impl From<surf::Error> for SurfError {
        fn from(err: surf::Error) -> Self {
            SurfError(err)
        }
    }

    impl std::error::Error for SurfError {}

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

#[cfg(feature = "isahc")]
mod isahc {
    use super::{async_trait, HttpClient, Request, TraceError};
    use opentelemetry::sdk::export::ExportError;
    use thiserror::Error;

    #[async_trait]
    impl HttpClient for isahc::HttpClient {
        async fn send(&self, request: Request<Vec<u8>>) -> Result<(), TraceError> {
            let res = self.send_async(request).await.map_err(IsahcError::from)?;

            if !res.status().is_success() {
                return Err(TraceError::from(format!(
                    "Expected success response, got {:?}",
                    res.status()
                )));
            }

            Ok(())
        }
    }

    #[derive(Debug, Error)]
    #[error(transparent)]
    struct IsahcError(#[from] isahc::Error);

    impl ExportError for IsahcError {
        fn exporter_name(&self) -> &'static str {
            "isahc"
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn http_headers_get() {
        let mut carrier = http::HeaderMap::new();
        HeaderInjector(&mut carrier).set("headerName", "value".to_string());

        assert_eq!(
            HeaderExtractor(&carrier).get("HEADERNAME"),
            Some("value"),
            "case insensitive extraction"
        )
    }

    #[test]
    fn http_headers_keys() {
        let mut carrier = http::HeaderMap::new();
        HeaderInjector(&mut carrier).set("headerName1", "value1".to_string());
        HeaderInjector(&mut carrier).set("headerName2", "value2".to_string());

        let extractor = HeaderExtractor(&carrier);
        let got = extractor.keys();
        assert_eq!(got.len(), 2);
        assert!(got.contains(&"headername1"));
        assert!(got.contains(&"headername2"));
    }
}