Skip to content

Commit e0156e6

Browse files
committed
Use bytes::Bytes as the HTTP request body in HttpClient.
This will allow pooling of buffers on caller side compared to usage of Vec<u8>.
1 parent 42b4f2f commit e0156e6

File tree

8 files changed

+28
-16
lines changed

8 files changed

+28
-16
lines changed

opentelemetry-http/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Bump msrv to 1.75.0.
66
- Add "internal-logs" feature flag (enabled by default), and emit internal logs.
7+
- Add `HttpClient::send_bytes` with `bytes::Bytes` request payload and deprecate old `HttpClient::send` function.
78

89
## 0.27.0
910

opentelemetry-http/src/lib.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,24 @@ pub type HttpError = Box<dyn std::error::Error + Send + Sync + 'static>;
5555
/// users to bring their choice of HTTP client.
5656
#[async_trait]
5757
pub trait HttpClient: Debug + Send + Sync {
58-
/// Send the specified HTTP request
58+
/// Send the specified HTTP request with `Vec<u8>` payload
5959
///
6060
/// Returns the HTTP response including the status code and body.
6161
///
6262
/// Returns an error if it can't connect to the server or the request could not be completed,
6363
/// e.g. because of a timeout, infinite redirects, or a loss of connection.
64-
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError>;
64+
#[deprecated(note = "Use `send_bytes` with `Bytes` payload instead.")]
65+
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
66+
self.send_bytes(request.map(Into::into)).await
67+
}
68+
69+
/// Send the specified HTTP request with `Bytes` payload.
70+
///
71+
/// Returns the HTTP response including the status code and body.
72+
///
73+
/// Returns an error if it can't connect to the server or the request could not be completed,
74+
/// e.g. because of a timeout, infinite redirects, or a loss of connection.
75+
async fn send_bytes(&self, request: Request<Bytes>) -> Result<Response<Bytes>, HttpError>;
6576
}
6677

6778
#[cfg(feature = "reqwest")]
@@ -72,7 +83,7 @@ mod reqwest {
7283

7384
#[async_trait]
7485
impl HttpClient for reqwest::Client {
75-
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
86+
async fn send_bytes(&self, request: Request<Bytes>) -> Result<Response<Bytes>, HttpError> {
7687
otel_debug!(name: "ReqwestClient.Send");
7788
let request = request.try_into()?;
7889
let mut response = self.execute(request).await?.error_for_status()?;
@@ -89,7 +100,7 @@ mod reqwest {
89100
#[cfg(not(target_arch = "wasm32"))]
90101
#[async_trait]
91102
impl HttpClient for reqwest::blocking::Client {
92-
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
103+
async fn send_bytes(&self, request: Request<Bytes>) -> Result<Response<Bytes>, HttpError> {
93104
otel_debug!(name: "ReqwestBlockingClient.Send");
94105
let request = request.try_into()?;
95106
let mut response = self.execute(request)?.error_for_status()?;
@@ -159,7 +170,7 @@ pub mod hyper {
159170

160171
#[async_trait]
161172
impl HttpClient for HyperClient {
162-
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
173+
async fn send_bytes(&self, request: Request<Bytes>) -> Result<Response<Bytes>, HttpError> {
163174
otel_debug!(name: "HyperClient.Send");
164175
let (parts, body) = request.into_parts();
165176
let mut request = Request::from_parts(parts, Body(Full::from(body)));

opentelemetry-otlp/src/exporter/http/logs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl LogExporter for OtlpHttpClient {
2828
.method(Method::POST)
2929
.uri(&self.collector_endpoint)
3030
.header(CONTENT_TYPE, content_type)
31-
.body(body)
31+
.body(body.into())
3232
.map_err(|e| crate::Error::RequestFailed(Box::new(e)))?;
3333

3434
for (k, v) in &self.headers {
@@ -37,7 +37,7 @@ impl LogExporter for OtlpHttpClient {
3737

3838
let request_uri = request.uri().to_string();
3939
otel_debug!(name: "HttpLogsClient.CallingExport");
40-
let response = client.send(request).await?;
40+
let response = client.send_bytes(request).await?;
4141

4242
if !response.status().is_success() {
4343
let error = format!(

opentelemetry-otlp/src/exporter/http/metrics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl MetricsClient for OtlpHttpClient {
2727
.method(Method::POST)
2828
.uri(&self.collector_endpoint)
2929
.header(CONTENT_TYPE, content_type)
30-
.body(body)
30+
.body(body.into())
3131
.map_err(|e| crate::Error::RequestFailed(Box::new(e)))?;
3232

3333
for (k, v) in &self.headers {
@@ -36,7 +36,7 @@ impl MetricsClient for OtlpHttpClient {
3636

3737
otel_debug!(name: "HttpMetricsClient.CallingExport");
3838
client
39-
.send(request)
39+
.send_bytes(request)
4040
.await
4141
.map_err(|e| MetricError::ExportErr(Box::new(Error::RequestFailed(e))))?;
4242

opentelemetry-otlp/src/exporter/http/trace.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl SpanExporter for OtlpHttpClient {
3030
.method(Method::POST)
3131
.uri(&self.collector_endpoint)
3232
.header(CONTENT_TYPE, content_type)
33-
.body(body)
33+
.body(body.into())
3434
{
3535
Ok(req) => req,
3636
Err(e) => {
@@ -48,7 +48,7 @@ impl SpanExporter for OtlpHttpClient {
4848
Box::pin(async move {
4949
let request_uri = request.uri().to_string();
5050
otel_debug!(name: "HttpTracesClient.CallingExport");
51-
let response = client.send(request).await?;
51+
let response = client.send_bytes(request).await?;
5252

5353
if !response.status().is_success() {
5454
let error = format!(

opentelemetry-sdk/src/trace/sampler/jaeger_remote/sampler.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl JaegerRemoteSampler {
228228
{
229229
let request = http::Request::get(endpoint)
230230
.header("Content-Type", "application/json")
231-
.body(Vec::new())
231+
.body(Default::default())
232232
.unwrap();
233233

234234
let resp = client

opentelemetry-zipkin/src/exporter/uploader.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ impl JsonV2Client {
4141
.method(Method::POST)
4242
.uri(self.collector_endpoint.clone())
4343
.header(CONTENT_TYPE, "application/json")
44-
.body(serde_json::to_vec(&spans).unwrap_or_default())
44+
.body(serde_json::to_vec(&spans).unwrap_or_default().into())
4545
.map_err::<Error, _>(Into::into)?;
46-
let _ = self.client.send(req).await?.error_for_status()?;
46+
let _ = self.client.send_bytes(req).await?.error_for_status()?;
4747
Ok(())
4848
}
4949
}

opentelemetry-zipkin/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@
109109
//!
110110
//! #[async_trait]
111111
//! impl HttpClient for HyperClient {
112-
//! async fn send(&self, req: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
112+
//! async fn send_bytes(&self, req: Request<Bytes>) -> Result<Response<Bytes>, HttpError> {
113113
//! let resp = self
114114
//! .0
115-
//! .request(req.map(|v| Full::new(Bytes::from(v))))
115+
//! .request(req.map(|v| Full::new(v)))
116116
//! .await?;
117117
//!
118118
//! let response = Response::builder()

0 commit comments

Comments
 (0)