Skip to content

Commit 37eb0d5

Browse files
committed
feat: example of using the DefaultHttpClient in libdatadog
1 parent f17efaa commit 37eb0d5

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libdd-data-pipeline/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ libdd-common = { version = "1.1.0", path = "../libdd-common", default-features =
3636
libdd-telemetry = { version = "2.0.0", path = "../libdd-telemetry", default-features = false }
3737
libdd-trace-protobuf = { version = "1.0.0", path = "../libdd-trace-protobuf" }
3838
libdd-trace-stats = { version = "1.0.0", path = "../libdd-trace-stats" }
39+
libdd-provider = { version = "0.1.0", path = "../libdd-provider" }
3940
libdd-trace-utils = { version = "1.0.0", path = "../libdd-trace-utils", default-features = false }
4041
libdd-ddsketch = { version = "1.0.0", path = "../libdd-ddsketch" }
4142
libdd-dogstatsd-client = { version = "1.0.0", path = "../libdd-dogstatsd-client", default-features = false }

libdd-data-pipeline/src/agent_info/fetcher.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
66
use super::{schema::AgentInfo, AGENT_INFO_CACHE};
77
use anyhow::{anyhow, Result};
8-
use http_body_util::BodyExt;
9-
use hyper::{self, header::HeaderName};
10-
use libdd_common::{hyper_migration, worker::Worker, Endpoint};
8+
use hyper::header::HeaderName;
9+
use libdd_common::{entity_id, worker::Worker, Endpoint};
10+
use libdd_provider::{DefaultHttpClient, HttpClientTrait, HttpRequest};
1111
use sha2::{Digest, Sha256};
1212
use std::sync::Arc;
1313
use std::time::Duration;
@@ -81,15 +81,25 @@ pub async fn fetch_info(info_endpoint: &Endpoint) -> Result<Box<AgentInfo>> {
8181
/// Returns a tuple of (state_hash, response_body_bytes).
8282
/// The hash is calculated using SHA256 to match the agent's calculation method.
8383
async fn fetch_and_hash_response(info_endpoint: &Endpoint) -> Result<(String, bytes::Bytes)> {
84-
let req = info_endpoint
85-
.to_request_builder(concat!("Libdatadog/", env!("CARGO_PKG_VERSION")))?
86-
.method(hyper::Method::GET)
87-
.body(hyper_migration::Body::empty());
88-
let client = hyper_migration::new_default_client();
89-
let res = client.request(req?).await?;
90-
91-
let body_bytes = res.into_body().collect().await?;
92-
let body_data = body_bytes.to_bytes();
84+
let user_agent = concat!("Libdatadog/", env!("CARGO_PKG_VERSION"));
85+
let mut req =
86+
HttpRequest::get(info_endpoint.url.to_string()).with_header("user-agent", user_agent);
87+
88+
// Add optional endpoint headers (api-key, test-token)
89+
for (name, value) in info_endpoint.get_optional_headers() {
90+
req = req.with_header(name, value);
91+
}
92+
93+
// Add entity-related headers (container-id, entity-id, external-env)
94+
for (name, value) in entity_id::get_entity_headers() {
95+
req = req.with_header(name, value);
96+
}
97+
98+
let res = DefaultHttpClient::request(req)
99+
.await
100+
.map_err(|e| anyhow!("{}", e))?;
101+
102+
let body_data = bytes::Bytes::from(res.body);
93103
let hash = format!("{:x}", Sha256::digest(&body_data));
94104

95105
Ok((hash, body_data))

libdd-provider/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! compile target. Downstream code uses these types directly, avoiding
88
//! generic contagion.
99
10-
pub use libdd_capabilities::HttpClientTrait;
10+
pub use libdd_capabilities::{HttpClientTrait, HttpError, HttpRequest, HttpResponse};
1111

1212
#[cfg(not(target_arch = "wasm32"))]
1313
pub use libdd_common::capabilities::HyperHttpClient as DefaultHttpClient;

0 commit comments

Comments
 (0)