Skip to content

Commit 422fae9

Browse files
feat(trace_utils): support generic connector in send_data (#1340)
feat(trace_utils): support generic connector in send_data define ddcommon connect trait fix import Co-authored-by: vianney.ruhlmann <vianney.ruhlmann@datadoghq.com>
1 parent 77e6bbe commit 422fae9

5 files changed

Lines changed: 37 additions & 20 deletions

File tree

libdd-common/src/connector/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,14 @@ mod tests {
211211
/// Verify that the Connector type implements the correct bound Connect + Clone
212212
/// to be able to use the hyper::Client
213213
fn test_hyper_client_from_connector() {
214-
let _: hyper_migration::HttpClient = hyper_migration::new_default_client();
214+
let _ = hyper_migration::new_default_client();
215215
}
216216

217217
#[test]
218218
#[cfg_attr(miri, ignore)]
219219
#[cfg(feature = "use_webpki_roots")]
220220
fn test_hyper_client_from_connector_with_webpki_roots() {
221-
let _: hyper_migration::HttpClient = hyper_migration::new_default_client();
221+
let _ = hyper_migration::new_default_client();
222222
}
223223

224224
#[tokio::test]

libdd-common/src/hyper_migration.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use core::fmt;
55
use std::{convert::Infallible, task::Poll};
66

7-
use crate::connector::{self, Connector};
7+
use crate::connector::Connector;
88
use http_body_util::BodyExt;
99
use hyper::body::Incoming;
1010
use pin_project::pin_project;
@@ -17,7 +17,7 @@ use hyper::Request as HyperRequest;
1717
/// every second connection because of low keep alive in the agent.
1818
///
1919
/// This is on general not a problem if we use the client once every tens of seconds.
20-
pub fn new_client_periodic() -> HttpClient {
20+
pub fn new_client_periodic() -> GenericHttpClient<Connector> {
2121
hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::default())
2222
.pool_max_idle_per_host(0)
2323
.build(Connector::default())
@@ -26,7 +26,7 @@ pub fn new_client_periodic() -> HttpClient {
2626
/// Create a new default configuration hyper client.
2727
///
2828
/// It will keep connections open for a longer time and reuse them.
29-
pub fn new_default_client() -> HttpClient {
29+
pub fn new_default_client() -> GenericHttpClient<Connector> {
3030
hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::default())
3131
.build(Connector::default())
3232
}
@@ -209,7 +209,7 @@ impl hyper::body::Body for Body {
209209
}
210210
}
211211

212-
pub type HttpClient = hyper_util::client::legacy::Client<connector::Connector, Body>;
212+
pub type GenericHttpClient<C> = hyper_util::client::legacy::Client<C, Body>;
213213

214214
pub fn client_builder() -> hyper_util::client::legacy::Builder {
215215
hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::default())

libdd-common/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,18 @@ pub mod header {
108108
HeaderName::from_static("x-datadog-test-session-token");
109109
}
110110

111-
pub type HttpClient = hyper_migration::HttpClient;
111+
pub type HttpClient = hyper_migration::GenericHttpClient<connector::Connector>;
112+
pub type GenericHttpClient<C> = hyper_migration::GenericHttpClient<C>;
112113
pub type HttpResponse = hyper_migration::HttpResponse;
113114
pub type HttpRequestBuilder = hyper::http::request::Builder;
115+
pub trait Connect:
116+
hyper_util::client::legacy::connect::Connect + Clone + Send + Sync + 'static
117+
{
118+
}
119+
impl<C: hyper_util::client::legacy::connect::Connect + Clone + Send + Sync + 'static> Connect
120+
for C
121+
{
122+
}
114123

115124
// Used by tag! macro
116125
use crate::entity_id::DD_EXTERNAL_ENV;

libdd-trace-utils/src/send_data/mod.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ use anyhow::{anyhow, Context};
1111
use futures::stream::FuturesUnordered;
1212
use futures::StreamExt;
1313
use hyper::header::CONTENT_TYPE;
14-
use libdd_common::HttpClient;
1514
use libdd_common::{
1615
header::{
1716
APPLICATION_MSGPACK_STR, APPLICATION_PROTOBUF_STR, DATADOG_SEND_REAL_HTTP_STATUS_STR,
1817
DATADOG_TRACE_COUNT_STR,
1918
},
20-
Endpoint,
19+
Connect, Endpoint, GenericHttpClient,
2120
};
2221
use libdd_trace_protobuf::pb::{AgentPayload, TracerPayload};
2322
use send_data_result::SendDataResult;
@@ -237,24 +236,27 @@ impl SendData {
237236
/// # Returns
238237
///
239238
/// A `SendDataResult` instance containing the result of the operation.
240-
pub async fn send(&self, http_client: &HttpClient) -> SendDataResult {
239+
pub async fn send<C: Connect>(&self, http_client: &GenericHttpClient<C>) -> SendDataResult {
241240
self.send_internal(http_client).await
242241
}
243242

244-
async fn send_internal(&self, http_client: &HttpClient) -> SendDataResult {
243+
async fn send_internal<C: Connect>(
244+
&self,
245+
http_client: &GenericHttpClient<C>,
246+
) -> SendDataResult {
245247
if self.use_protobuf() {
246248
self.send_with_protobuf(http_client).await
247249
} else {
248250
self.send_with_msgpack(http_client).await
249251
}
250252
}
251253

252-
async fn send_payload(
254+
async fn send_payload<C: Connect>(
253255
&self,
254256
chunks: u64,
255257
payload: Vec<u8>,
256258
headers: HashMap<&'static str, String>,
257-
http_client: &HttpClient,
259+
http_client: &GenericHttpClient<C>,
258260
) -> (SendWithRetryResult, u64, u64) {
259261
#[allow(clippy::unwrap_used)]
260262
let payload_len = u64::try_from(payload.len()).unwrap();
@@ -298,7 +300,10 @@ impl SendData {
298300
}
299301
}
300302

301-
async fn send_with_protobuf(&self, http_client: &HttpClient) -> SendDataResult {
303+
async fn send_with_protobuf<C: Connect>(
304+
&self,
305+
http_client: &GenericHttpClient<C>,
306+
) -> SendDataResult {
302307
let mut result = SendDataResult::default();
303308

304309
#[allow(clippy::unwrap_used)]
@@ -336,7 +341,10 @@ impl SendData {
336341
}
337342
}
338343

339-
async fn send_with_msgpack(&self, http_client: &HttpClient) -> SendDataResult {
344+
async fn send_with_msgpack<C: Connect>(
345+
&self,
346+
http_client: &GenericHttpClient<C>,
347+
) -> SendDataResult {
340348
let mut result = SendDataResult::default();
341349
let mut futures = FuturesUnordered::new();
342350

libdd-trace-utils/src/send_with_retry/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub use retry_strategy::{RetryBackoffType, RetryStrategy};
99

1010
use bytes::Bytes;
1111
use hyper::Method;
12-
use libdd_common::{hyper_migration, Endpoint, HttpClient, HttpRequestBuilder};
12+
use libdd_common::{hyper_migration, Connect, Endpoint, GenericHttpClient, HttpRequestBuilder};
1313
use std::{collections::HashMap, time::Duration};
1414
use tracing::{debug, error};
1515

@@ -111,8 +111,8 @@ impl std::error::Error for RequestError {}
111111
/// send_with_retry(&client, &target, payload, &headers, &retry_strategy).await
112112
/// # }
113113
/// ```
114-
pub async fn send_with_retry(
115-
client: &HttpClient,
114+
pub async fn send_with_retry<C: Connect>(
115+
client: &GenericHttpClient<C>,
116116
target: &Endpoint,
117117
payload: Vec<u8>,
118118
headers: &HashMap<&'static str, String>,
@@ -222,8 +222,8 @@ pub async fn send_with_retry(
222222
}
223223
}
224224

225-
async fn send_request(
226-
client: &HttpClient,
225+
async fn send_request<C: Connect>(
226+
client: &GenericHttpClient<C>,
227227
timeout: Duration,
228228
req: HttpRequestBuilder,
229229
payload: Bytes,

0 commit comments

Comments
 (0)