Skip to content

Commit 6c75e7c

Browse files
committed
Fix hyper feature
1 parent e52ba2c commit 6c75e7c

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

opentelemetry-http/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ edition = "2021"
1010
rust-version = "1.65"
1111

1212
[features]
13+
hyper = ["dep:http-body-util", "dep:hyper", "dep:hyper-util", "dep:tokio"]
1314
reqwest-rustls = ["reqwest", "reqwest/rustls-tls-native-roots"]
1415
reqwest-rustls-webpki-roots = ["reqwest", "reqwest/rustls-tls-webpki-roots"]
1516

1617
[dependencies]
1718
async-trait = { workspace = true }
1819
bytes = { workspace = true }
1920
http = { workspace = true }
21+
http-body-util = { workspace = true, optional = true }
2022
hyper = { workspace = true, features = ["http2", "client"], optional = true }
23+
hyper-util = { workspace = true, features = ["client-legacy"], optional = true }
2124
opentelemetry = { version = "0.23", path = "../opentelemetry", features = ["trace"] }
2225
reqwest = { workspace = true, features = ["blocking"], optional = true }
2326
tokio = { workspace = true, features = ["time"], optional = true }

opentelemetry-http/src/lib.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,23 @@ pub mod hyper {
105105

106106
use super::{async_trait, Bytes, HttpClient, HttpError, Request, Response};
107107
use http::HeaderValue;
108-
use hyper::client::connect::Connect;
109-
use hyper::Client;
108+
use hyper::body::Body;
109+
use hyper_util::client::legacy::{connect::Connect, Client};
110+
use http_body_util::BodyExt;
111+
use std::error::Error;
110112
use std::fmt::Debug;
111113
use std::time::Duration;
112114
use tokio::time;
113115

114116
#[derive(Debug, Clone)]
115-
pub struct HyperClient<C> {
116-
inner: Client<C>,
117+
pub struct HyperClient<C, B> {
118+
inner: Client<C, B>,
117119
timeout: Duration,
118120
authorization: Option<HeaderValue>,
119121
}
120122

121-
impl<C> HyperClient<C> {
122-
pub fn new_with_timeout(inner: Client<C>, timeout: Duration) -> Self {
123+
impl<C, B> HyperClient<C, B> {
124+
pub fn new_with_timeout(inner: Client<C, B>, timeout: Duration) -> Self {
123125
Self {
124126
inner,
125127
timeout,
@@ -128,7 +130,7 @@ pub mod hyper {
128130
}
129131

130132
pub fn new_with_timeout_and_authorization_header(
131-
inner: Client<C>,
133+
inner: Client<C, B>,
132134
timeout: Duration,
133135
authorization: HeaderValue,
134136
) -> Self {
@@ -141,23 +143,27 @@ pub mod hyper {
141143
}
142144

143145
#[async_trait]
144-
impl<C> HttpClient for HyperClient<C>
146+
impl<C, B> HttpClient for HyperClient<C, B>
145147
where
146148
C: Connect + Send + Sync + Clone + Debug + 'static,
149+
B: From<Vec<u8>> + Body + Send + Sync + Debug + Unpin + 'static,
150+
<B as Body>::Data: Send,
151+
<B as Body>::Error: Into<Box<dyn Error + Send + Sync>>,
147152
{
148153
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
149154
let (parts, body) = request.into_parts();
150-
let mut request = Request::from_parts(parts, body.into());
155+
let mut request: Request<B> = Request::from_parts(parts, body.into());
151156
if let Some(ref authorization) = self.authorization {
152157
request
153158
.headers_mut()
154159
.insert(http::header::AUTHORIZATION, authorization.clone());
155160
}
156161
let mut response = time::timeout(self.timeout, self.inner.request(request)).await??;
157162
let headers = std::mem::take(response.headers_mut());
163+
158164
let mut http_response = Response::builder()
159165
.status(response.status())
160-
.body(hyper::body::to_bytes(response.into_body()).await?)?;
166+
.body(response.into_body().collect().await?.to_bytes())?;
161167
*http_response.headers_mut() = headers;
162168

163169
Ok(http_response.error_for_status()?)

0 commit comments

Comments
 (0)