Skip to content

Commit

Permalink
opentelemetry-http: move isahc client impl from opentelemetry-jaeger
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Jan 5, 2021
1 parent ee813bc commit f8b9ca1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 41 deletions.
1 change: 1 addition & 0 deletions opentelemetry-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition = "2018"
[dependencies]
async-trait = "0.1.42"
http = "0.2.2"
isahc = { version = "0.9", default-features = false, optional = true }
opentelemetry = { version = "0.11.2", path = "../opentelemetry", features = ["trace"] }
reqwest = { version = "0.10", default-features = false, features = ["blocking"], optional = true }
surf = { version = "2.0", default-features = false, optional = true }
48 changes: 48 additions & 0 deletions opentelemetry-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,54 @@ impl HttpClient for surf::Client {
}
}

#[cfg(feature = "isahc")]
impl ExportError for IsahcError {
fn exporter_name(&self) -> &'static str {
"isahc"
}
}

#[cfg(feature = "isahc")]
#[derive(Debug)]
struct IsahcError(isahc::Error);

#[cfg(feature = "isahc")]
impl Display for IsahcError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.to_string())
}
}

#[cfg(feature = "isahc")]
impl std::error::Error for IsahcError {}

#[cfg(feature = "isahc")]
impl From<isahc::Error> for IsahcError {
fn from(err: isahc::Error) -> Self {
IsahcError(err)
}
}

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

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

Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-jaeger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ optional = true
[features]
default = []
collector_client = ["http", "opentelemetry-http"]
isahc_collector_client = ["isahc", "collector_client"]
isahc_collector_client = ["isahc", "opentelemetry-http/isahc"]
reqwest_blocking_collector_client = ["reqwest/blocking", "collector_client", "headers", "opentelemetry-http/reqwest"]
reqwest_collector_client = ["reqwest", "collector_client", "headers", "opentelemetry-http/reqwest"]
surf_collector_client = ["surf", "collector_client", "opentelemetry-http/surf"]
Expand Down
42 changes: 2 additions & 40 deletions opentelemetry-jaeger/src/exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,12 @@ impl PipelineBuilder {
.credentials(isahc::auth::Credentials::new(username, password));
}

Box::new(IsahcHttpClient(builder.build().map_err(|err| {
Box::new(builder.build().map_err(|err| {
crate::Error::ThriftAgentError(::thrift::Error::from(std::io::Error::new(
std::io::ErrorKind::Other,
err.to_string(),
)))
})?))
})?)
});

#[cfg(all(
Expand Down Expand Up @@ -435,44 +435,6 @@ impl surf::middleware::Middleware for BasicAuthMiddleware {
}
}

#[derive(Debug)]
#[cfg(feature = "isahc_collector_client")]
struct IsahcHttpClient(isahc::HttpClient);

#[async_trait]
#[cfg(feature = "isahc_collector_client")]
impl HttpClient for IsahcHttpClient {
async fn send(
&self,
request: http::Request<Vec<u8>>,
) -> opentelemetry::sdk::export::trace::ExportResult {
let res = self
.0
.send_async(request)
.await
.map_err::<crate::Error, _>(|err| {
::thrift::Error::from(std::io::Error::new(
std::io::ErrorKind::Other,
err.to_string(),
))
.into()
})
.map_err::<TraceError, _>(TraceError::from)?;

if !res.status().is_success() {
return Err(crate::Error::ThriftAgentError(::thrift::Error::from(
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Expected success response, got {:?}", res.status()),
),
))
.into());
}

Ok(())
}
}

#[rustfmt::skip]
impl Into<jaeger::Tag> for KeyValue {
fn into(self) -> jaeger::Tag {
Expand Down

0 comments on commit f8b9ca1

Please sign in to comment.