Skip to content

Commit b863364

Browse files
Aaalibaba42dubloom
andauthored
feat(capabilities)!: trait architecture http (#1555)
# What does this PR do? Add capability trait architecture, and few native implementations. # Motivation Ongoing effort to make libdatadog wasm compatible. # Additional Notes In the long run, several other parts of the codebase ought to use these capabilities via the provided public structure, so that it can automagically choose the correct underlying implementation when compiling or not to wasm. # How to test the change? I should add some tests... Co-authored-by: dubloom <75956635+dubloom@users.noreply.github.com> Co-authored-by: jules.wiriath <jules.wiriath@datadoghq.com>
1 parent 660b8a8 commit b863364

54 files changed

Lines changed: 1656 additions & 929 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ docker-bake.hcl @DataDog/apm-common-components-core
3939
docs @DataDog/libdatadog
4040
examples @DataDog/libdatadog
4141
libdd-alloc/ @DataDog/libdatadog-profiling
42+
libdd-capabilities*/ @DataDog/apm-common-components-core
4243
libdd-common*/ @DataDog/libdatadog
4344
libdd-crashtracker*/ @DataDog/libdatadog-profiling
4445
libdd-data-pipeline*/ @DataDog/libdatadog-apm

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ members = [
2929
"libdd-trace-utils",
3030
"libdd-trace-stats",
3131
"datadog-tracer-flare",
32+
"libdd-capabilities",
33+
"libdd-capabilities-impl",
3234
"libdd-common",
3335
"libdd-common-ffi",
3436
"libdd-telemetry",

LICENSE-3rdparty.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
root_name: builder, build_common, tools, libdd-alloc, libdd-crashtracker, libdd-common, libdd-telemetry, libdd-ddsketch, libdd-libunwind-sys, libdd-crashtracker-ffi, libdd-common-ffi, datadog-ffe, datadog-ffe-ffi, datadog-ipc, datadog-ipc-macros, libdd-tinybytes, spawn_worker, cc_utils, libdd-library-config, libdd-trace-protobuf, libdd-library-config-ffi, datadog-live-debugger, libdd-data-pipeline, libdd-dogstatsd-client, libdd-trace-stats, libdd-trace-utils, libdd-trace-normalization, libdd-log, datadog-live-debugger-ffi, libdd-profiling, libdd-profiling-protobuf, libdd-profiling-ffi, libdd-data-pipeline-ffi, libdd-ddsketch-ffi, libdd-log-ffi, libdd-telemetry-ffi, symbolizer-ffi, datadog-profiling-replayer, datadog-remote-config, datadog-sidecar, datadog-sidecar-macros, datadog-sidecar-ffi, libdd-trace-obfuscation, datadog-tracer-flare, sidecar_mockgen, test_spawn_from_lib, bin_tests
1+
root_name: builder, build_common, tools, libdd-alloc, libdd-crashtracker, libdd-common, libdd-capabilities, libdd-telemetry, libdd-ddsketch, libdd-libunwind-sys, libdd-crashtracker-ffi, libdd-common-ffi, datadog-ffe, datadog-ffe-ffi, datadog-ipc, datadog-ipc-macros, libdd-tinybytes, spawn_worker, cc_utils, libdd-library-config, libdd-trace-protobuf, libdd-library-config-ffi, datadog-live-debugger, libdd-data-pipeline, libdd-dogstatsd-client, libdd-trace-stats, libdd-trace-utils, libdd-trace-normalization, libdd-capabilities-impl, libdd-log, datadog-live-debugger-ffi, libdd-profiling, libdd-profiling-protobuf, libdd-profiling-ffi, libdd-data-pipeline-ffi, libdd-ddsketch-ffi, libdd-log-ffi, libdd-telemetry-ffi, symbolizer-ffi, datadog-profiling-replayer, datadog-remote-config, datadog-sidecar, datadog-sidecar-macros, datadog-sidecar-ffi, libdd-trace-obfuscation, datadog-tracer-flare, sidecar_mockgen, test_spawn_from_lib, bin_tests
22
third_party_libraries:
33
- package_name: addr2line
44
package_version: 0.24.2

datadog-sidecar/src/service/agent_info.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use datadog_ipc::platform::NamedShmHandle;
1616
use futures::future::Shared;
1717
use futures::FutureExt;
1818
use http::uri::PathAndQuery;
19+
use libdd_common::DefaultHttpClient;
1920
use libdd_common::{Endpoint, MutexExt};
2021
use libdd_data_pipeline::agent_info::schema::AgentInfoStruct;
2122
use libdd_data_pipeline::agent_info::{fetch_info_with_state, FetchInfoStatus};
@@ -101,7 +102,9 @@ impl AgentInfoFetcher {
101102
parts.path_and_query = Some(PathAndQuery::from_static("/info"));
102103
fetch_endpoint.url = http::Uri::from_parts(parts).unwrap();
103104
loop {
104-
let fetched = fetch_info_with_state(&fetch_endpoint, state.as_deref()).await;
105+
let fetched =
106+
fetch_info_with_state::<DefaultHttpClient>(&fetch_endpoint, state.as_deref())
107+
.await;
105108
let mut complete_fut = None;
106109
{
107110
let mut infos_guard = agent_infos.0.lock_or_panic();

datadog-sidecar/src/service/tracing/trace_flusher.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ use super::TraceSendData;
55
use crate::agent_remote_config::AgentRemoteConfigWriter;
66
use datadog_ipc::platform::NamedShmHandle;
77
use futures::future::join_all;
8-
use http_body_util::BodyExt;
9-
use libdd_common::http_common::new_default_client;
10-
use libdd_common::{Endpoint, HttpClient, MutexExt};
8+
use libdd_common::capabilities::HttpClientTrait;
9+
use libdd_common::{DefaultHttpClient, Endpoint, MutexExt};
1110
use libdd_trace_utils::trace_utils;
1211
use libdd_trace_utils::trace_utils::SendData;
1312
use libdd_trace_utils::trace_utils::SendDataResult;
@@ -96,7 +95,7 @@ pub(crate) struct TraceFlusher {
9695
pub(crate) min_force_drop_size_bytes: AtomicU32, // put a limit on memory usage
9796
remote_config: Mutex<AgentRemoteConfigs>,
9897
pub metrics: Mutex<TraceFlusherMetrics>,
99-
client: HttpClient,
98+
client: DefaultHttpClient,
10099
}
101100
impl Default for TraceFlusher {
102101
fn default() -> Self {
@@ -107,7 +106,7 @@ impl Default for TraceFlusher {
107106
min_force_drop_size_bytes: AtomicU32::new(trace_utils::MAX_PAYLOAD_SIZE as u32),
108107
remote_config: Mutex::new(Default::default()),
109108
metrics: Mutex::new(Default::default()),
110-
client: new_default_client(),
109+
client: DefaultHttpClient::new_client(),
111110
}
112111
}
113112
}
@@ -255,12 +254,7 @@ impl TraceFlusher {
255254
Ok(response) => {
256255
if endpoint.api_key.is_none() {
257256
// not when intake
258-
match response.into_body().collect().await {
259-
Ok(body) => {
260-
self.write_remote_configs(endpoint.clone(), body.to_bytes().to_vec())
261-
}
262-
Err(e) => error!("Error receiving agent configuration: {e:?}"),
263-
}
257+
self.write_remote_configs(endpoint.clone(), response.into_body().to_vec());
264258
}
265259
info!("Successfully flushed traces to {endpoint:?}");
266260
}

libdd-capabilities-impl/Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
[package]
5+
name = "libdd-capabilities-impl"
6+
version = "0.1.0"
7+
description = "Native implementations of libdd-capabilities traits"
8+
homepage = "https://github.com/DataDog/libdatadog/tree/main/libdd-capabilities-impl"
9+
repository = "https://github.com/DataDog/libdatadog/tree/main/libdd-capabilities-impl"
10+
edition.workspace = true
11+
rust-version.workspace = true
12+
license.workspace = true
13+
14+
[lib]
15+
crate-type = ["lib"]
16+
bench = false
17+
18+
[dependencies]
19+
bytes = "1"
20+
http = "1"
21+
libdd-capabilities = { path = "../libdd-capabilities", version = "0.1.0" }
22+
libdd-common = { path = "../libdd-common", version = "3.0.2" }

libdd-capabilities-impl/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# libdd-capabilities-impl
2+
3+
Native implementations of `libdd-capabilities` traits.
4+
5+
## Overview
6+
7+
`libdd-capabilities-impl` provides platform-native backends for the capability traits defined in `libdd-capabilities`. It is the concrete implementation used by native leaf crates (FFI bindings, benchmarks, tests) and should **not** be depended on by core library crates.
8+
9+
## Capabilities
10+
11+
- **`DefaultHttpClient`**: HTTP client backed by hyper and the `libdd-common` connector infrastructure (supports Unix sockets, HTTPS with rustls, Windows named pipes).
12+
13+
## Types
14+
15+
- **`NativeCapabilities`**: Bundle type alias that implements all capability traits using native backends. Currently delegates to `DefaultHttpClient`; as more capability traits are added (spawn, sleep, etc.), this type will implement all of them.
16+
17+
## Usage
18+
19+
Native leaf crates pin the concrete capability type:
20+
21+
```rust
22+
use libdd_capabilities_impl::NativeCapabilities;
23+
use libdd_data_pipeline::TraceExporter;
24+
25+
let exporter: TraceExporter<NativeCapabilities> = TraceExporterBuilder::new(/* ... */).build();
26+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! Re-exports `DefaultHttpClient` from `libdd-common`, where it lives alongside
5+
//! the hyper infrastructure it wraps.
6+
7+
pub use libdd_common::DefaultHttpClient;

libdd-capabilities-impl/src/lib.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! Native capability implementations for libdatadog.
5+
//!
6+
//! `NativeCapabilities` is the bundle struct that implements all capability
7+
//! traits using platform-native backends (hyper for HTTP, tokio for spawn,
8+
//! etc.). Leaf crates (FFI, benchmarks) pin this type as the generic parameter.
9+
10+
mod http;
11+
12+
use core::future::Future;
13+
14+
pub use http::DefaultHttpClient;
15+
use libdd_capabilities::http::HttpError;
16+
pub use libdd_capabilities::HttpClientTrait;
17+
use libdd_capabilities::MaybeSend;
18+
19+
/// Bundle struct for native platform capabilities.
20+
///
21+
/// Delegates to [`DefaultHttpClient`] for HTTP. As more capability traits are
22+
/// added (spawn, sleep, etc.), additional fields and impls are added here
23+
/// without changing the type identity — consumers see the same
24+
/// `NativeCapabilities` throughout.
25+
///
26+
/// Individual capability traits keep minimal per-function bounds (e.g.
27+
/// functions that only need HTTP require just `H: HttpClientTrait`, not the
28+
/// full bundle) so that native callers like the sidecar can use
29+
/// `DefaultHttpClient` directly without pulling in this bundle.
30+
#[derive(Clone, Debug)]
31+
pub struct NativeCapabilities {
32+
http: DefaultHttpClient,
33+
}
34+
35+
impl HttpClientTrait for NativeCapabilities {
36+
fn new_client() -> Self {
37+
Self {
38+
http: DefaultHttpClient::new_client(),
39+
}
40+
}
41+
42+
fn request(
43+
&self,
44+
req: ::http::Request<bytes::Bytes>,
45+
) -> impl Future<Output = Result<::http::Response<bytes::Bytes>, HttpError>> + MaybeSend {
46+
self.http.request(req)
47+
}
48+
}

0 commit comments

Comments
 (0)