From 8246a0b902edf70bf1106a399f32acbadcf776b9 Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Wed, 15 Jul 2026 13:58:10 +0200 Subject: [PATCH 01/16] feat: added env capability --- crates/capabilities/src/env.rs | 49 ++++++++++++++++++++++++ crates/capabilities/src/env_transport.js | 13 +++++++ crates/capabilities/src/lib.rs | 22 +++++++++++ crates/library_config/Cargo.toml | 2 +- crates/process_discovery/Cargo.toml | 7 +--- test/env-transport.js | 37 ++++++++++++++++++ 6 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 crates/capabilities/src/env.rs create mode 100644 crates/capabilities/src/env_transport.js create mode 100644 test/env-transport.js diff --git a/crates/capabilities/src/env.rs b/crates/capabilities/src/env.rs new file mode 100644 index 0000000..a60fa85 --- /dev/null +++ b/crates/capabilities/src/env.rs @@ -0,0 +1,49 @@ +// Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/ +// SPDX-License-Identifier: Apache-2.0 + +//! Wasm implementation of [`EnvCapability`] backed by Node.js `process.env`. + +use wasm_bindgen::prelude::*; + +use libdd_capabilities::env::{EnvCapability, EnvError}; + +#[wasm_bindgen(module = "/src/env_transport.js")] +extern "C" { + #[wasm_bindgen(js_name = "get")] + fn js_env_get(name: &str) -> JsValue; + + #[wasm_bindgen(js_name = "set")] + fn js_env_set(name: &str, value: &str); + + #[wasm_bindgen(js_name = "unset")] + fn js_env_unset(name: &str); +} + +#[derive(Debug, Clone)] +pub struct WasmEnvCapability; + +impl EnvCapability for WasmEnvCapability { + fn new() -> Self { + Self + } + + fn get(&self, name: &str) -> Result, EnvError> { + // Node coerces every process.env value to a string, so NotUnicode is unreachable here. + let value = js_env_get(name); + if value.is_undefined() || value.is_null() { + Ok(None) + } else { + Ok(value.as_string()) + } + } + + fn set(&self, name: &str, value: &str) -> Result<(), EnvError> { + js_env_set(name, value); + Ok(()) + } + + fn unset(&self, name: &str) -> Result<(), EnvError> { + js_env_unset(name); + Ok(()) + } +} diff --git a/crates/capabilities/src/env_transport.js b/crates/capabilities/src/env_transport.js new file mode 100644 index 0000000..da79bc0 --- /dev/null +++ b/crates/capabilities/src/env_transport.js @@ -0,0 +1,13 @@ +'use strict' + +module.exports.get = function (name) { + return process.env[name] +} + +module.exports.set = function (name, value) { + process.env[name] = value +} + +module.exports.unset = function (name) { + delete process.env[name] +} diff --git a/crates/capabilities/src/lib.rs b/crates/capabilities/src/lib.rs index ebd385f..111571c 100644 --- a/crates/capabilities/src/lib.rs +++ b/crates/capabilities/src/lib.rs @@ -12,14 +12,17 @@ use std::future::Future; use std::time::Duration; +use libdd_capabilities::env::{EnvCapability, EnvError}; use libdd_capabilities::file::{FileCapability, FileError, FileMetadata}; use libdd_capabilities::http::HttpError; use libdd_capabilities::{HttpClientCapability, LogWriterCapability, MaybeSend, SleepCapability}; +pub mod env; pub mod file; pub mod http; pub mod sleep; +pub use env::WasmEnvCapability; pub use file::WasmFileCapability; pub use http::WasmHttpClient; pub use sleep::WasmSleepCapability; @@ -38,6 +41,7 @@ pub struct WasmCapabilities { sleep: WasmSleepCapability, /// Filesystem access delegated to the Node.js `fs` transport. file: WasmFileCapability, + env: WasmEnvCapability, } impl Default for WasmCapabilities { @@ -52,6 +56,7 @@ impl WasmCapabilities { http: WasmHttpClient::new_client(), sleep: WasmSleepCapability, file: WasmFileCapability, + env: WasmEnvCapability, } } } @@ -122,3 +127,20 @@ impl FileCapability for WasmCapabilities { } } +impl EnvCapability for WasmCapabilities { + fn new() -> Self { + Self::new() + } + + fn get(&self, name: &str) -> Result, EnvError> { + self.env.get(name) + } + + fn set(&self, name: &str, value: &str) -> Result<(), EnvError> { + self.env.set(name, value) + } + + fn unset(&self, name: &str) -> Result<(), EnvError> { + self.env.unset(name) + } +} diff --git a/crates/library_config/Cargo.toml b/crates/library_config/Cargo.toml index be76bd4..a4f47cc 100644 --- a/crates/library_config/Cargo.toml +++ b/crates/library_config/Cargo.toml @@ -8,7 +8,7 @@ crate-type = ["cdylib", "rlib"] [dependencies] anyhow = "1" -libdd-library-config = { git = "https://github.com/DataDog/libdatadog.git", rev = "353134770b312b7ccd2df6afabc253090b948e5f" } +libdd-library-config = { git = "https://github.com/DataDog/libdatadog.git", branch = "jwiriath/env-caps" } wasm-bindgen = "0.2.100" serde = { version = "1.0", features = ["derive"] } diff --git a/crates/process_discovery/Cargo.toml b/crates/process_discovery/Cargo.toml index 4404228..30c0fa2 100644 --- a/crates/process_discovery/Cargo.toml +++ b/crates/process_discovery/Cargo.toml @@ -8,11 +8,8 @@ crate-type = ["cdylib", "rlib"] [dependencies] anyhow = "1" -# Pointed at the merge commit that introduced ThreadLocalMetadata (caller-supplied -# schema version + extra process-context attributes). Swap back to a tagged release -# once one that includes 7cdeb7896e92d1ba38bde495934e112dac2eda25 is published. -libdd-library-config = { git = "https://github.com/DataDog/libdatadog.git", rev = "7cdeb7896e92d1ba38bde495934e112dac2eda25", features = ["otel-thread-ctx"] } -libdd-trace-protobuf = { git = "https://github.com/DataDog/libdatadog.git", rev = "7cdeb7896e92d1ba38bde495934e112dac2eda25" } +libdd-library-config = { git = "https://github.com/DataDog/libdatadog.git", branch = "jwiriath/env-caps", features = ["otel-thread-ctx"] } +libdd-trace-protobuf = { git = "https://github.com/DataDog/libdatadog.git", branch = "jwiriath/env-caps" } napi = { version = "2" } napi-derive = { version = "2", default-features = false } diff --git a/test/env-transport.js b/test/env-transport.js new file mode 100644 index 0000000..f7a82f6 --- /dev/null +++ b/test/env-transport.js @@ -0,0 +1,37 @@ +'use strict' + +// The transport shim is plain CommonJS, so drive it directly. + +const { describe, it, before, after } = require('node:test') +const assert = require('node:assert') + +const envTransport = require('../crates/capabilities/src/env_transport') + +describe('env_transport', () => { + const NAME = 'LIBDD_CAP_TEST_ENV_TRANSPORT' + let savedValue + + before(() => { + savedValue = process.env[NAME] + }) + after(() => { + if (savedValue === undefined) delete process.env[NAME] + else process.env[NAME] = savedValue + }) + + it('returns undefined for an unset var', () => { + delete process.env[NAME] + assert.strictEqual(envTransport.get(NAME), undefined) + }) + + it('set then get round-trips the value', () => { + envTransport.set(NAME, 'value1') + assert.strictEqual(envTransport.get(NAME), 'value1') + }) + + it('unset then get returns undefined', () => { + envTransport.set(NAME, 'value2') + envTransport.unset(NAME) + assert.strictEqual(envTransport.get(NAME), undefined) + }) +}) From d6240f48ff96a37f1e2d9e9101ecf87fa764de76 Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Thu, 16 Jul 2026 11:52:11 +0200 Subject: [PATCH 02/16] feat: respect envcapabilities method unsafedness --- crates/capabilities/src/env.rs | 6 ++++-- crates/capabilities/src/lib.rs | 10 ++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/crates/capabilities/src/env.rs b/crates/capabilities/src/env.rs index a60fa85..c5c99ae 100644 --- a/crates/capabilities/src/env.rs +++ b/crates/capabilities/src/env.rs @@ -37,12 +37,14 @@ impl EnvCapability for WasmEnvCapability { } } - fn set(&self, name: &str, value: &str) -> Result<(), EnvError> { + unsafe fn set(&self, name: &str, value: &str) -> Result<(), EnvError> { + // SAFETY: Wasm is single-threaded; no concurrent env access is possible. js_env_set(name, value); Ok(()) } - fn unset(&self, name: &str) -> Result<(), EnvError> { + unsafe fn unset(&self, name: &str) -> Result<(), EnvError> { + // SAFETY: Wasm is single-threaded; no concurrent env access is possible. js_env_unset(name); Ok(()) } diff --git a/crates/capabilities/src/lib.rs b/crates/capabilities/src/lib.rs index 111571c..f259559 100644 --- a/crates/capabilities/src/lib.rs +++ b/crates/capabilities/src/lib.rs @@ -136,11 +136,13 @@ impl EnvCapability for WasmCapabilities { self.env.get(name) } - fn set(&self, name: &str, value: &str) -> Result<(), EnvError> { - self.env.set(name, value) + unsafe fn set(&self, name: &str, value: &str) -> Result<(), EnvError> { + // SAFETY: forwarded verbatim; Wasm is single-threaded so the precondition is trivially upheld. + unsafe { self.env.set(name, value) } } - fn unset(&self, name: &str) -> Result<(), EnvError> { - self.env.unset(name) + unsafe fn unset(&self, name: &str) -> Result<(), EnvError> { + // SAFETY: forwarded verbatim; Wasm is single-threaded so the precondition is trivially upheld. + unsafe { self.env.unset(name) } } } From 01e760fe905b0033a0e15c9ebb0c8f7d279b7809 Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Thu, 16 Jul 2026 13:25:20 +0200 Subject: [PATCH 03/16] feat: validate before saying it's ok --- crates/capabilities/src/env.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/capabilities/src/env.rs b/crates/capabilities/src/env.rs index c5c99ae..51bff88 100644 --- a/crates/capabilities/src/env.rs +++ b/crates/capabilities/src/env.rs @@ -5,7 +5,7 @@ use wasm_bindgen::prelude::*; -use libdd_capabilities::env::{EnvCapability, EnvError}; +use libdd_capabilities::env::{validate_name, validate_value, EnvCapability, EnvError}; #[wasm_bindgen(module = "/src/env_transport.js")] extern "C" { @@ -38,12 +38,15 @@ impl EnvCapability for WasmEnvCapability { } unsafe fn set(&self, name: &str, value: &str) -> Result<(), EnvError> { + validate_name(name)?; + validate_value(value)?; // SAFETY: Wasm is single-threaded; no concurrent env access is possible. js_env_set(name, value); Ok(()) } unsafe fn unset(&self, name: &str) -> Result<(), EnvError> { + validate_name(name)?; // SAFETY: Wasm is single-threaded; no concurrent env access is possible. js_env_unset(name); Ok(()) From 381e7eb005aafd3f900351ad971bf1f0648cf4fd Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Thu, 16 Jul 2026 16:24:42 +0200 Subject: [PATCH 04/16] revert: remove set/unset since they would be dangerous APIs in libdatadog (and are most likely not useful in the near/middel future in dd-trace-js) --- crates/capabilities/src/env.rs | 23 +---------------------- crates/capabilities/src/env_transport.js | 8 -------- crates/capabilities/src/lib.rs | 10 ---------- test/env-transport.js | 10 ++-------- 4 files changed, 3 insertions(+), 48 deletions(-) diff --git a/crates/capabilities/src/env.rs b/crates/capabilities/src/env.rs index 51bff88..1cba7db 100644 --- a/crates/capabilities/src/env.rs +++ b/crates/capabilities/src/env.rs @@ -5,18 +5,12 @@ use wasm_bindgen::prelude::*; -use libdd_capabilities::env::{validate_name, validate_value, EnvCapability, EnvError}; +use libdd_capabilities::env::{EnvCapability, EnvError}; #[wasm_bindgen(module = "/src/env_transport.js")] extern "C" { #[wasm_bindgen(js_name = "get")] fn js_env_get(name: &str) -> JsValue; - - #[wasm_bindgen(js_name = "set")] - fn js_env_set(name: &str, value: &str); - - #[wasm_bindgen(js_name = "unset")] - fn js_env_unset(name: &str); } #[derive(Debug, Clone)] @@ -36,19 +30,4 @@ impl EnvCapability for WasmEnvCapability { Ok(value.as_string()) } } - - unsafe fn set(&self, name: &str, value: &str) -> Result<(), EnvError> { - validate_name(name)?; - validate_value(value)?; - // SAFETY: Wasm is single-threaded; no concurrent env access is possible. - js_env_set(name, value); - Ok(()) - } - - unsafe fn unset(&self, name: &str) -> Result<(), EnvError> { - validate_name(name)?; - // SAFETY: Wasm is single-threaded; no concurrent env access is possible. - js_env_unset(name); - Ok(()) - } } diff --git a/crates/capabilities/src/env_transport.js b/crates/capabilities/src/env_transport.js index da79bc0..414fd2a 100644 --- a/crates/capabilities/src/env_transport.js +++ b/crates/capabilities/src/env_transport.js @@ -3,11 +3,3 @@ module.exports.get = function (name) { return process.env[name] } - -module.exports.set = function (name, value) { - process.env[name] = value -} - -module.exports.unset = function (name) { - delete process.env[name] -} diff --git a/crates/capabilities/src/lib.rs b/crates/capabilities/src/lib.rs index f259559..b2288d2 100644 --- a/crates/capabilities/src/lib.rs +++ b/crates/capabilities/src/lib.rs @@ -135,14 +135,4 @@ impl EnvCapability for WasmCapabilities { fn get(&self, name: &str) -> Result, EnvError> { self.env.get(name) } - - unsafe fn set(&self, name: &str, value: &str) -> Result<(), EnvError> { - // SAFETY: forwarded verbatim; Wasm is single-threaded so the precondition is trivially upheld. - unsafe { self.env.set(name, value) } - } - - unsafe fn unset(&self, name: &str) -> Result<(), EnvError> { - // SAFETY: forwarded verbatim; Wasm is single-threaded so the precondition is trivially upheld. - unsafe { self.env.unset(name) } - } } diff --git a/test/env-transport.js b/test/env-transport.js index f7a82f6..f45a377 100644 --- a/test/env-transport.js +++ b/test/env-transport.js @@ -24,14 +24,8 @@ describe('env_transport', () => { assert.strictEqual(envTransport.get(NAME), undefined) }) - it('set then get round-trips the value', () => { - envTransport.set(NAME, 'value1') + it('returns the value when the var is set', () => { + process.env[NAME] = 'value1' assert.strictEqual(envTransport.get(NAME), 'value1') }) - - it('unset then get returns undefined', () => { - envTransport.set(NAME, 'value2') - envTransport.unset(NAME) - assert.strictEqual(envTransport.get(NAME), undefined) - }) }) From 3f3544aee7d18617eb7aab84c3a1f072f427765f Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Fri, 17 Jul 2026 11:48:32 +0200 Subject: [PATCH 05/16] chore: change input to main's commit that has the change --- crates/library_config/Cargo.toml | 2 +- crates/process_discovery/Cargo.toml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/library_config/Cargo.toml b/crates/library_config/Cargo.toml index a4f47cc..b6deab0 100644 --- a/crates/library_config/Cargo.toml +++ b/crates/library_config/Cargo.toml @@ -8,7 +8,7 @@ crate-type = ["cdylib", "rlib"] [dependencies] anyhow = "1" -libdd-library-config = { git = "https://github.com/DataDog/libdatadog.git", branch = "jwiriath/env-caps" } +libdd-library-config = { git = "https://github.com/DataDog/libdatadog.git", rev = "0c6e2a5df2a163d34c4f385353ffc5d7257c72f4" } wasm-bindgen = "0.2.100" serde = { version = "1.0", features = ["derive"] } diff --git a/crates/process_discovery/Cargo.toml b/crates/process_discovery/Cargo.toml index 30c0fa2..32e9449 100644 --- a/crates/process_discovery/Cargo.toml +++ b/crates/process_discovery/Cargo.toml @@ -8,8 +8,8 @@ crate-type = ["cdylib", "rlib"] [dependencies] anyhow = "1" -libdd-library-config = { git = "https://github.com/DataDog/libdatadog.git", branch = "jwiriath/env-caps", features = ["otel-thread-ctx"] } -libdd-trace-protobuf = { git = "https://github.com/DataDog/libdatadog.git", branch = "jwiriath/env-caps" } +libdd-library-config = { git = "https://github.com/DataDog/libdatadog.git", rev = "0c6e2a5df2a163d34c4f385353ffc5d7257c72f4", features = ["otel-thread-ctx"] } +libdd-trace-protobuf = { git = "https://github.com/DataDog/libdatadog.git", rev = "0c6e2a5df2a163d34c4f385353ffc5d7257c72f4" } napi = { version = "2" } napi-derive = { version = "2", default-features = false } From 584465816cf5a10f48d660c1892b25ec6ef4e4f7 Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Thu, 23 Jul 2026 12:03:21 +0200 Subject: [PATCH 06/16] docs: update doc to remove specific references to the capabilities --- crates/capabilities/src/lib.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/capabilities/src/lib.rs b/crates/capabilities/src/lib.rs index b2288d2..e76a381 100644 --- a/crates/capabilities/src/lib.rs +++ b/crates/capabilities/src/lib.rs @@ -4,10 +4,9 @@ //! Wasm capability implementations for libdatadog-nodejs. //! //! [`WasmCapabilities`] is the bundle struct that implements every capability -//! trait `TraceExporter` requires (HTTP, sleep, log output) using wasm_bindgen -//! and JS transports. The wasm binding crate pins this type as the capability -//! generic for libdatadog's `TraceExporter`, mirroring libdatadog's native -//! `NativeCapabilities`. +//! trait `TraceExporter` requires using wasm_bindgen and JS transports. The +//! wasm binding crate pins this type as the capability generic for libdatadog's +//! `TraceExporter`, mirroring libdatadog's native `NativeCapabilities`. use std::future::Future; use std::time::Duration; From a872df95b0414706484ff0e87aa01907310e841f Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Thu, 23 Jul 2026 12:09:54 +0200 Subject: [PATCH 07/16] fix: remove extra checks --- crates/capabilities/src/env.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/crates/capabilities/src/env.rs b/crates/capabilities/src/env.rs index 1cba7db..716d4b4 100644 --- a/crates/capabilities/src/env.rs +++ b/crates/capabilities/src/env.rs @@ -23,11 +23,6 @@ impl EnvCapability for WasmEnvCapability { fn get(&self, name: &str) -> Result, EnvError> { // Node coerces every process.env value to a string, so NotUnicode is unreachable here. - let value = js_env_get(name); - if value.is_undefined() || value.is_null() { - Ok(None) - } else { - Ok(value.as_string()) - } + Ok(js_env_get(name).as_string()) } } From 4c45d0706dc96ab12942a6f6f282151b49c12826 Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Mon, 27 Jul 2026 15:57:58 +0200 Subject: [PATCH 08/16] fix: cache env beforehand --- Cargo.lock | 42 ++++++++---------------- crates/capabilities/src/env_transport.js | 6 ++-- 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d45e097..377fa06 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1072,7 +1072,7 @@ dependencies = [ "libdd-shared-runtime 2.0.0", "libdd-tinybytes", "libdd-trace-normalization", - "libdd-trace-protobuf 4.0.0", + "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f)", "libdd-trace-stats", "libdd-trace-utils", "rmp-serde", @@ -1117,26 +1117,12 @@ dependencies = [ [[package]] name = "libdd-library-config" -version = "1.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=353134770b312b7ccd2df6afabc253090b948e5f#353134770b312b7ccd2df6afabc253090b948e5f" -dependencies = [ - "anyhow", - "memfd", - "rand", - "rmp", - "rmp-serde", - "serde", - "serde_yaml", -] - -[[package]] -name = "libdd-library-config" -version = "2.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=7cdeb7896e92d1ba38bde495934e112dac2eda25#7cdeb7896e92d1ba38bde495934e112dac2eda25" +version = "3.0.0" +source = "git+https://github.com/DataDog/libdatadog.git?rev=0c6e2a5df2a163d34c4f385353ffc5d7257c72f4#0c6e2a5df2a163d34c4f385353ffc5d7257c72f4" dependencies = [ "anyhow", "libc", - "libdd-trace-protobuf 3.0.2", + "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=0c6e2a5df2a163d34c4f385353ffc5d7257c72f4)", "memfd", "prost", "rand", @@ -1232,7 +1218,7 @@ version = "3.0.0" source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" dependencies = [ "anyhow", - "libdd-trace-protobuf 4.0.0", + "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f)", ] [[package]] @@ -1243,7 +1229,7 @@ dependencies = [ "anyhow", "fluent-uri", "libdd-common 5.1.0", - "libdd-trace-protobuf 4.0.0", + "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f)", "libdd-trace-utils", "log", "percent-encoding", @@ -1253,8 +1239,8 @@ dependencies = [ [[package]] name = "libdd-trace-protobuf" -version = "3.0.2" -source = "git+https://github.com/DataDog/libdatadog.git?rev=7cdeb7896e92d1ba38bde495934e112dac2eda25#7cdeb7896e92d1ba38bde495934e112dac2eda25" +version = "4.0.0" +source = "git+https://github.com/DataDog/libdatadog.git?rev=0c6e2a5df2a163d34c4f385353ffc5d7257c72f4#0c6e2a5df2a163d34c4f385353ffc5d7257c72f4" dependencies = [ "prost", "serde", @@ -1289,7 +1275,7 @@ dependencies = [ "libdd-dogstatsd-client", "libdd-shared-runtime 2.0.0", "libdd-trace-obfuscation", - "libdd-trace-protobuf 4.0.0", + "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f)", "libdd-trace-utils", "rmp-serde", "serde", @@ -1320,7 +1306,7 @@ dependencies = [ "libdd-common 5.1.0", "libdd-tinybytes", "libdd-trace-normalization", - "libdd-trace-protobuf 4.0.0", + "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f)", "prost", "rand", "rmp", @@ -1357,7 +1343,7 @@ version = "0.2.0" dependencies = [ "anyhow", "getrandom 0.2.17", - "libdd-library-config 1.0.0", + "libdd-library-config", "serde", "serde-wasm-bindgen", "wasm-bindgen", @@ -1811,7 +1797,7 @@ dependencies = [ "libdd-common 5.1.0", "libdd-data-pipeline", "libdd-shared-runtime 2.0.0", - "libdd-trace-protobuf 4.0.0", + "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f)", "libdd-trace-stats", "libdd-trace-utils", "rmp-serde", @@ -1872,8 +1858,8 @@ name = "process-discovery" version = "0.1.0" dependencies = [ "anyhow", - "libdd-library-config 2.0.0", - "libdd-trace-protobuf 3.0.2", + "libdd-library-config", + "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=0c6e2a5df2a163d34c4f385353ffc5d7257c72f4)", "napi", "napi-derive", ] diff --git a/crates/capabilities/src/env_transport.js b/crates/capabilities/src/env_transport.js index 414fd2a..92d6504 100644 --- a/crates/capabilities/src/env_transport.js +++ b/crates/capabilities/src/env_transport.js @@ -1,5 +1,7 @@ 'use strict' -module.exports.get = function (name) { - return process.env[name] +const { env } = process + +module.exports.get = (name) => { + return env[name] } From a595c9d36ac78351a5f0a0d954a767bb1adf4001 Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Mon, 20 Jul 2026 18:47:27 +0200 Subject: [PATCH 09/16] feat: Adopt TraceExporter's Telemetry stuff --- crates/pipeline/Cargo.toml | 3 ++- crates/pipeline/src/lib.rs | 34 +++++++++++++++++++++++++++++++++- crates/pipeline/src/stats.rs | 8 ++++---- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/crates/pipeline/Cargo.toml b/crates/pipeline/Cargo.toml index 29fc272..4eb8fe3 100644 --- a/crates/pipeline/Cargo.toml +++ b/crates/pipeline/Cargo.toml @@ -16,7 +16,7 @@ serde_json = "1" libdatadog-nodejs-capabilities = { path = "../capabilities" } libdd-capabilities = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f" } libdd-common = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f", default-features = false } -libdd-data-pipeline = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f", default-features = false } +libdd-data-pipeline = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f", default-features = false, features = ["telemetry"] } libdd-trace-utils = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f", default-features = false, features = ["change-buffer"] } libdd-trace-stats = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f", default-features = false } libdd-trace-protobuf = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f", default-features = false } @@ -25,6 +25,7 @@ web-time = "1" rmp-serde = "1" bytes = "1" http = "1" +web-time = "1" console_error_panic_hook = "0.1" [target.'cfg(target_arch = "wasm32")'.dependencies] diff --git a/crates/pipeline/src/lib.rs b/crates/pipeline/src/lib.rs index 31188af..f6bc324 100644 --- a/crates/pipeline/src/lib.rs +++ b/crates/pipeline/src/lib.rs @@ -1,7 +1,7 @@ use libdatadog_nodejs_capabilities::WasmCapabilities; use libdd_data_pipeline::trace_exporter::agent_response::AgentResponse; use libdd_data_pipeline::trace_exporter::{ - TraceExporter, TraceExporterBuilder, TraceExporterOutputFormat, + TelemetryConfig, TraceExporter, TraceExporterBuilder, TraceExporterOutputFormat, }; use libdd_data_pipeline::OtlpProtocol; use libdd_shared_runtime::LocalRuntime; @@ -204,6 +204,11 @@ pub struct WasmSpanState { /// Extra HTTP headers for OTLP export (e.g. collector auth), as key/value /// pairs. Only applied when `otlp_endpoint` is set. otlp_headers: RefCell>, + /// When set, the lazily-built exporter has telemetry enabled with this + /// config (`heartbeat`/`runtime_id`/`debug_enabled` — see libdatadog's + /// `TelemetryConfig`). Only takes effect if set before the first send + /// (when the exporter is built). + telemetry_config: RefCell>, /// Latched message from a failed lazy `build_async`. Building is one-shot and /// a failure is fatal (bad config), so once set every send returns it (as a /// distinguishable error) instead of a misleading "builder already consumed", @@ -338,6 +343,7 @@ impl WasmSpanState { otlp_endpoint: RefCell::new(None), otlp_protocol: Cell::new(None), otlp_headers: RefCell::new(Vec::new()), + telemetry_config: RefCell::new(None), build_error: RefCell::new(None), }) } @@ -374,6 +380,29 @@ impl WasmSpanState { Ok(()) } + /// Enable telemetry on the lazily-built trace exporter. Off by default — + /// dd-trace-js opts in from JS. `heartbeat_ms` sets the metric-flush cadence + /// (0 disables periodic heartbeats and only sends on shutdown), `runtime_id` + /// tags telemetry payloads with the tracer's runtime id when provided, and + /// `debug_enabled` toggles libdd-telemetry's verbose logging. + /// + /// Must be called before the first `sendPreparedChunk` — the exporter is + /// built lazily on first send and telemetry config is fixed at build time, + /// so later calls have no effect. + #[wasm_bindgen(js_name = "enableTelemetry")] + pub fn enable_telemetry( + &self, + heartbeat_ms: u64, + runtime_id: Option, + debug_enabled: bool, + ) { + *self.telemetry_config.borrow_mut() = Some(TelemetryConfig { + heartbeat: heartbeat_ms, + runtime_id, + debug_enabled, + }); + } + /// Set extra HTTP headers for OTLP export as a flat `[key, value, ...]` /// array (the host flattens its key/value map). Only takes effect with an /// OTLP endpoint set, before the first send. A trailing unpaired element on @@ -530,6 +559,9 @@ impl WasmSpanState { builder.set_otlp_headers(headers.clone()); } } + if let Some(cfg) = self.telemetry_config.borrow().clone() { + builder.enable_telemetry(cfg); + } match builder.build_async::().await { Ok(built) => *exporter_slot = Some(built), Err(e) => { diff --git a/crates/pipeline/src/stats.rs b/crates/pipeline/src/stats.rs index ac17a11..3e65cb4 100644 --- a/crates/pipeline/src/stats.rs +++ b/crates/pipeline/src/stats.rs @@ -9,11 +9,11 @@ use web_time::{Duration, SystemTime}; -/// Wall-clock now() for wasm. `std::time::SystemTime::now()` is unimplemented on -/// `wasm32-unknown-unknown` (it panics/traps), so derive the time from JS -/// `Date.now()` (milliseconds since the Unix epoch). +/// Wall-clock now() for wasm. Delegates to `web_time::SystemTime::now()`, +/// which routes to JS `Date.now()` on `wasm32-unknown-unknown` (native +/// `std::time::SystemTime::now()` is unimplemented on that target and traps). fn now() -> SystemTime { - SystemTime::UNIX_EPOCH + Duration::from_millis(js_sys::Date::now() as u64) + SystemTime::now() } use bytes::Bytes; From d2187a8a86ce2902681900f73db8f30db87875b2 Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Tue, 21 Jul 2026 14:13:59 +0200 Subject: [PATCH 10/16] docs: fix libdatadog's behavior when 0 is passed --- crates/pipeline/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/pipeline/src/lib.rs b/crates/pipeline/src/lib.rs index f6bc324..baace39 100644 --- a/crates/pipeline/src/lib.rs +++ b/crates/pipeline/src/lib.rs @@ -382,7 +382,7 @@ impl WasmSpanState { /// Enable telemetry on the lazily-built trace exporter. Off by default — /// dd-trace-js opts in from JS. `heartbeat_ms` sets the metric-flush cadence - /// (0 disables periodic heartbeats and only sends on shutdown), `runtime_id` + /// (0 defers to libdatadog's default interval), `runtime_id` /// tags telemetry payloads with the tracer's runtime id when provided, and /// `debug_enabled` toggles libdd-telemetry's verbose logging. /// From 72c003e0ae9d9d8e959e9c1294c53d51ae2b1ea4 Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Tue, 21 Jul 2026 14:19:38 +0200 Subject: [PATCH 11/16] chore: js tomfoolery --- crates/pipeline/src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/pipeline/src/lib.rs b/crates/pipeline/src/lib.rs index baace39..d913f2a 100644 --- a/crates/pipeline/src/lib.rs +++ b/crates/pipeline/src/lib.rs @@ -381,10 +381,10 @@ impl WasmSpanState { } /// Enable telemetry on the lazily-built trace exporter. Off by default — - /// dd-trace-js opts in from JS. `heartbeat_ms` sets the metric-flush cadence - /// (0 defers to libdatadog's default interval), `runtime_id` - /// tags telemetry payloads with the tracer's runtime id when provided, and - /// `debug_enabled` toggles libdd-telemetry's verbose logging. + /// dd-trace-js opts in from JS. `heartbeat_ms`sets the metric-flush cadence + /// (0 defers to libdatadog's default interval), `runtime_id` tags telemetry + /// payloads with the tracer's runtime id when provided, and `debug_enabled` + /// toggles libdd-telemetry's verbose logging. /// /// Must be called before the first `sendPreparedChunk` — the exporter is /// built lazily on first send and telemetry config is fixed at build time, @@ -392,12 +392,12 @@ impl WasmSpanState { #[wasm_bindgen(js_name = "enableTelemetry")] pub fn enable_telemetry( &self, - heartbeat_ms: u64, + heartbeat_ms: u32, runtime_id: Option, debug_enabled: bool, ) { *self.telemetry_config.borrow_mut() = Some(TelemetryConfig { - heartbeat: heartbeat_ms, + heartbeat: heartbeat_ms as u64, runtime_id, debug_enabled, }); From 4adacd9b1906422b4fd552beca9538a7586cd7c9 Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Mon, 27 Jul 2026 12:13:17 +0200 Subject: [PATCH 12/16] fix: comments --- crates/pipeline/src/lib.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/crates/pipeline/src/lib.rs b/crates/pipeline/src/lib.rs index d913f2a..89106e2 100644 --- a/crates/pipeline/src/lib.rs +++ b/crates/pipeline/src/lib.rs @@ -205,10 +205,9 @@ pub struct WasmSpanState { /// pairs. Only applied when `otlp_endpoint` is set. otlp_headers: RefCell>, /// When set, the lazily-built exporter has telemetry enabled with this - /// config (`heartbeat`/`runtime_id`/`debug_enabled` — see libdatadog's - /// `TelemetryConfig`). Only takes effect if set before the first send + /// config. Only takes effect if set before the first send /// (when the exporter is built). - telemetry_config: RefCell>, + telemetry_config: Cell>, /// Latched message from a failed lazy `build_async`. Building is one-shot and /// a failure is fatal (bad config), so once set every send returns it (as a /// distinguishable error) instead of a misleading "builder already consumed", @@ -343,7 +342,7 @@ impl WasmSpanState { otlp_endpoint: RefCell::new(None), otlp_protocol: Cell::new(None), otlp_headers: RefCell::new(Vec::new()), - telemetry_config: RefCell::new(None), + telemetry_config: Cell::new(None), build_error: RefCell::new(None), }) } @@ -380,15 +379,17 @@ impl WasmSpanState { Ok(()) } - /// Enable telemetry on the lazily-built trace exporter. Off by default — - /// dd-trace-js opts in from JS. `heartbeat_ms`sets the metric-flush cadence - /// (0 defers to libdatadog's default interval), `runtime_id` tags telemetry - /// payloads with the tracer's runtime id when provided, and `debug_enabled` - /// toggles libdd-telemetry's verbose logging. + /// Enable telemetry on the lazily-built trace exporter. /// - /// Must be called before the first `sendPreparedChunk` — the exporter is - /// built lazily on first send and telemetry config is fixed at build time, - /// so later calls have no effect. + /// Must be called before the first `sendPreparedChunk`. Later calls have + /// no effect. + /// + /// # Arguments + /// + /// - `heartbeat_ms`: sets the metric-flush cadence. Set to 0 to defer to + /// libdatadog's default interval. + /// - `runtime_id`: tags telemetry payloads with the tracer's runtime id when provided + /// - `debug_enabled`: toggles libdd-telemetry's verbose logging #[wasm_bindgen(js_name = "enableTelemetry")] pub fn enable_telemetry( &self, @@ -396,11 +397,11 @@ impl WasmSpanState { runtime_id: Option, debug_enabled: bool, ) { - *self.telemetry_config.borrow_mut() = Some(TelemetryConfig { + self.telemetry_config.set(Some(TelemetryConfig { heartbeat: heartbeat_ms as u64, runtime_id, debug_enabled, - }); + })); } /// Set extra HTTP headers for OTLP export as a flat `[key, value, ...]` @@ -559,7 +560,7 @@ impl WasmSpanState { builder.set_otlp_headers(headers.clone()); } } - if let Some(cfg) = self.telemetry_config.borrow().clone() { + if let Some(cfg) = self.telemetry_config.take() { builder.enable_telemetry(cfg); } match builder.build_async::().await { From cf9b3564c302f073634354d404633fa273a22b57 Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Mon, 27 Jul 2026 17:24:15 +0200 Subject: [PATCH 13/16] fix: use Cells uniformaly --- Cargo.lock | 33 ++++++++++++++++++++++++++++++++- crates/pipeline/Cargo.toml | 1 - crates/pipeline/src/lib.rs | 22 +++++++++++----------- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 377fa06..848464c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1031,7 +1031,7 @@ dependencies = [ "libc", "libdd-common 5.0.0", "libdd-libunwind-sys", - "libdd-telemetry", + "libdd-telemetry 5.0.1", "nix 0.29.0", "num-derive", "num-traits", @@ -1070,6 +1070,7 @@ dependencies = [ "libdd-ddsketch 1.1.0", "libdd-dogstatsd-client", "libdd-shared-runtime 2.0.0", + "libdd-telemetry 6.0.0", "libdd-tinybytes", "libdd-trace-normalization", "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f)", @@ -1204,6 +1205,35 @@ dependencies = [ "winver", ] +[[package]] +name = "libdd-telemetry" +version = "6.0.0" +source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" +dependencies = [ + "anyhow", + "async-trait", + "base64", + "bytes", + "futures", + "getrandom 0.2.17", + "hashbrown 0.15.5", + "http", + "libc", + "libdd-capabilities 2.1.0", + "libdd-common 5.1.0", + "libdd-ddsketch 1.1.0", + "libdd-shared-runtime 2.0.0", + "serde", + "serde_json", + "sys-info", + "tokio", + "tokio-util", + "tracing", + "uuid", + "web-time", + "winver", +] + [[package]] name = "libdd-tinybytes" version = "1.1.1" @@ -1274,6 +1304,7 @@ dependencies = [ "libdd-ddsketch 1.1.0", "libdd-dogstatsd-client", "libdd-shared-runtime 2.0.0", + "libdd-telemetry 6.0.0", "libdd-trace-obfuscation", "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f)", "libdd-trace-utils", diff --git a/crates/pipeline/Cargo.toml b/crates/pipeline/Cargo.toml index 4eb8fe3..8056816 100644 --- a/crates/pipeline/Cargo.toml +++ b/crates/pipeline/Cargo.toml @@ -21,7 +21,6 @@ libdd-trace-utils = { git = "https://github.com/DataDog/libdatadog.git", rev = " libdd-trace-stats = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f", default-features = false } libdd-trace-protobuf = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f", default-features = false } libdd-shared-runtime = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f", default-features = false } -web-time = "1" rmp-serde = "1" bytes = "1" http = "1" diff --git a/crates/pipeline/src/lib.rs b/crates/pipeline/src/lib.rs index 89106e2..4ff36bb 100644 --- a/crates/pipeline/src/lib.rs +++ b/crates/pipeline/src/lib.rs @@ -197,13 +197,13 @@ pub struct WasmSpanState { /// Datadog agent. libdatadog maps its internal traces to OTLP, so no /// JS-formatted spans are involved. Like `use_v05`, only takes effect if /// set before the first send (when the exporter is built). - otlp_endpoint: RefCell>, + otlp_endpoint: Cell>, /// OTLP wire protocol (`http/json` default, or `http/protobuf`). Only /// applied when `otlp_endpoint` is set. otlp_protocol: Cell>, /// Extra HTTP headers for OTLP export (e.g. collector auth), as key/value /// pairs. Only applied when `otlp_endpoint` is set. - otlp_headers: RefCell>, + otlp_headers: Cell>, /// When set, the lazily-built exporter has telemetry enabled with this /// config. Only takes effect if set before the first send /// (when the exporter is built). @@ -339,9 +339,9 @@ impl WasmSpanState { prepared_spans: RefCell::new(Vec::new()), sending: Cell::new(false), use_v05: Cell::new(false), - otlp_endpoint: RefCell::new(None), + otlp_endpoint: Cell::new(None), otlp_protocol: Cell::new(None), - otlp_headers: RefCell::new(Vec::new()), + otlp_headers: Cell::new(Vec::new()), telemetry_config: Cell::new(None), build_error: RefCell::new(None), }) @@ -364,7 +364,7 @@ impl WasmSpanState { /// Takes precedence over `setUseV05` (OTLP bypasses the agent entirely). #[wasm_bindgen(js_name = "setOtlpEndpoint")] pub fn set_otlp_endpoint(&self, url: String) { - *self.otlp_endpoint.borrow_mut() = Some(url); + self.otlp_endpoint.set(Some(url)); } /// Select the OTLP wire protocol: `http/json` (default) or `http/protobuf`. @@ -416,7 +416,7 @@ impl WasmSpanState { .chunks_exact(2) .map(|pair| (pair[0].clone(), pair[1].clone())) .collect(); - *self.otlp_headers.borrow_mut() = headers; + self.otlp_headers.set(headers); } #[wasm_bindgen] @@ -550,14 +550,14 @@ impl WasmSpanState { // When an OTLP endpoint is configured, libdatadog exports traces via // OTLP HTTP to that endpoint instead of the Datadog agent (mutually // exclusive with the agent v0.4/v0.5 path). - if let Some(url) = self.otlp_endpoint.borrow().as_deref() { - builder.set_otlp_endpoint(url); - if let Some(protocol) = self.otlp_protocol.get() { + if let Some(url) = self.otlp_endpoint.take() { + builder.set_otlp_endpoint(&url); + if let Some(protocol) = self.otlp_protocol.take() { builder.set_otlp_protocol(protocol); } - let headers = self.otlp_headers.borrow(); + let headers = self.otlp_headers.take(); if !headers.is_empty() { - builder.set_otlp_headers(headers.clone()); + builder.set_otlp_headers(headers); } } if let Some(cfg) = self.telemetry_config.take() { From 10ecfbf352c6f8cd275e0743ac72579dfe0ba26c Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Fri, 24 Jul 2026 15:27:39 +0200 Subject: [PATCH 14/16] feat: use libdatadog's css instead of local one --- Cargo.lock | 108 ++++++++----------- crates/pipeline/Cargo.toml | 18 ++-- crates/pipeline/src/lib.rs | 107 +++---------------- crates/pipeline/src/stats.rs | 201 ----------------------------------- test/pipeline.js | 169 +---------------------------- 5 files changed, 70 insertions(+), 533 deletions(-) delete mode 100644 crates/pipeline/src/stats.rs diff --git a/Cargo.lock b/Cargo.lock index 848464c..673f0b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -917,7 +917,7 @@ dependencies = [ [[package]] name = "libdd-capabilities" version = "2.1.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" +source = "git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec#15899dfe754d12186ce7db72f0ff41c1920d52ec" dependencies = [ "anyhow", "bytes", @@ -941,7 +941,7 @@ dependencies = [ [[package]] name = "libdd-capabilities-impl" version = "3.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" +source = "git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec#15899dfe754d12186ce7db72f0ff41c1920d52ec" dependencies = [ "anyhow", "bytes", @@ -990,7 +990,7 @@ dependencies = [ [[package]] name = "libdd-common" version = "5.1.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" +source = "git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec#15899dfe754d12186ce7db72f0ff41c1920d52ec" dependencies = [ "anyhow", "bytes", @@ -1031,7 +1031,7 @@ dependencies = [ "libc", "libdd-common 5.0.0", "libdd-libunwind-sys", - "libdd-telemetry 5.0.1", + "libdd-telemetry", "nix 0.29.0", "num-derive", "num-traits", @@ -1053,7 +1053,7 @@ dependencies = [ [[package]] name = "libdd-data-pipeline" version = "7.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" +source = "git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec#15899dfe754d12186ce7db72f0ff41c1920d52ec" dependencies = [ "anyhow", "arc-swap", @@ -1070,10 +1070,9 @@ dependencies = [ "libdd-ddsketch 1.1.0", "libdd-dogstatsd-client", "libdd-shared-runtime 2.0.0", - "libdd-telemetry 6.0.0", "libdd-tinybytes", "libdd-trace-normalization", - "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f)", + "libdd-trace-protobuf 4.0.0", "libdd-trace-stats", "libdd-trace-utils", "rmp-serde", @@ -1098,7 +1097,7 @@ dependencies = [ [[package]] name = "libdd-ddsketch" version = "1.1.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" +source = "git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec#15899dfe754d12186ce7db72f0ff41c1920d52ec" dependencies = [ "prost", ] @@ -1106,24 +1105,41 @@ dependencies = [ [[package]] name = "libdd-dogstatsd-client" version = "4.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" +source = "git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec#15899dfe754d12186ce7db72f0ff41c1920d52ec" dependencies = [ "anyhow", + "async-trait", "cadence", "http", "libdd-common 5.1.0", + "libdd-shared-runtime 2.0.0", "serde", + "tokio", "tracing", ] [[package]] name = "libdd-library-config" -version = "3.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=0c6e2a5df2a163d34c4f385353ffc5d7257c72f4#0c6e2a5df2a163d34c4f385353ffc5d7257c72f4" +version = "1.0.0" +source = "git+https://github.com/DataDog/libdatadog.git?rev=353134770b312b7ccd2df6afabc253090b948e5f#353134770b312b7ccd2df6afabc253090b948e5f" +dependencies = [ + "anyhow", + "memfd", + "rand", + "rmp", + "rmp-serde", + "serde", + "serde_yaml", +] + +[[package]] +name = "libdd-library-config" +version = "2.0.0" +source = "git+https://github.com/DataDog/libdatadog.git?rev=7cdeb7896e92d1ba38bde495934e112dac2eda25#7cdeb7896e92d1ba38bde495934e112dac2eda25" dependencies = [ "anyhow", "libc", - "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=0c6e2a5df2a163d34c4f385353ffc5d7257c72f4)", + "libdd-trace-protobuf 3.0.2", "memfd", "prost", "rand", @@ -1164,7 +1180,7 @@ dependencies = [ [[package]] name = "libdd-shared-runtime" version = "2.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" +source = "git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec#15899dfe754d12186ce7db72f0ff41c1920d52ec" dependencies = [ "async-trait", "futures", @@ -1205,39 +1221,10 @@ dependencies = [ "winver", ] -[[package]] -name = "libdd-telemetry" -version = "6.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" -dependencies = [ - "anyhow", - "async-trait", - "base64", - "bytes", - "futures", - "getrandom 0.2.17", - "hashbrown 0.15.5", - "http", - "libc", - "libdd-capabilities 2.1.0", - "libdd-common 5.1.0", - "libdd-ddsketch 1.1.0", - "libdd-shared-runtime 2.0.0", - "serde", - "serde_json", - "sys-info", - "tokio", - "tokio-util", - "tracing", - "uuid", - "web-time", - "winver", -] - [[package]] name = "libdd-tinybytes" version = "1.1.1" -source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" +source = "git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec#15899dfe754d12186ce7db72f0ff41c1920d52ec" dependencies = [ "serde", ] @@ -1245,21 +1232,21 @@ dependencies = [ [[package]] name = "libdd-trace-normalization" version = "3.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" +source = "git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec#15899dfe754d12186ce7db72f0ff41c1920d52ec" dependencies = [ "anyhow", - "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f)", + "libdd-trace-protobuf 4.0.0", ] [[package]] name = "libdd-trace-obfuscation" version = "5.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" +source = "git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec#15899dfe754d12186ce7db72f0ff41c1920d52ec" dependencies = [ "anyhow", "fluent-uri", "libdd-common 5.1.0", - "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f)", + "libdd-trace-protobuf 4.0.0", "libdd-trace-utils", "log", "percent-encoding", @@ -1269,8 +1256,8 @@ dependencies = [ [[package]] name = "libdd-trace-protobuf" -version = "4.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=0c6e2a5df2a163d34c4f385353ffc5d7257c72f4#0c6e2a5df2a163d34c4f385353ffc5d7257c72f4" +version = "3.0.2" +source = "git+https://github.com/DataDog/libdatadog.git?rev=7cdeb7896e92d1ba38bde495934e112dac2eda25#7cdeb7896e92d1ba38bde495934e112dac2eda25" dependencies = [ "prost", "serde", @@ -1280,7 +1267,7 @@ dependencies = [ [[package]] name = "libdd-trace-protobuf" version = "4.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" +source = "git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec#15899dfe754d12186ce7db72f0ff41c1920d52ec" dependencies = [ "prost", "serde", @@ -1290,7 +1277,7 @@ dependencies = [ [[package]] name = "libdd-trace-stats" version = "6.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" +source = "git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec#15899dfe754d12186ce7db72f0ff41c1920d52ec" dependencies = [ "anyhow", "arc-swap", @@ -1304,9 +1291,8 @@ dependencies = [ "libdd-ddsketch 1.1.0", "libdd-dogstatsd-client", "libdd-shared-runtime 2.0.0", - "libdd-telemetry 6.0.0", "libdd-trace-obfuscation", - "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f)", + "libdd-trace-protobuf 4.0.0", "libdd-trace-utils", "rmp-serde", "serde", @@ -1319,7 +1305,7 @@ dependencies = [ [[package]] name = "libdd-trace-utils" version = "9.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f#3081603d3c74f209be4e3be951f78a1a7469397f" +source = "git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec#15899dfe754d12186ce7db72f0ff41c1920d52ec" dependencies = [ "anyhow", "base64", @@ -1337,7 +1323,7 @@ dependencies = [ "libdd-common 5.1.0", "libdd-tinybytes", "libdd-trace-normalization", - "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f)", + "libdd-trace-protobuf 4.0.0", "prost", "rand", "rmp", @@ -1374,7 +1360,7 @@ version = "0.2.0" dependencies = [ "anyhow", "getrandom 0.2.17", - "libdd-library-config", + "libdd-library-config 1.0.0", "serde", "serde-wasm-bindgen", "wasm-bindgen", @@ -1818,27 +1804,21 @@ checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" name = "pipeline" version = "0.1.0" dependencies = [ - "bytes", "console_error_panic_hook", "getrandom 0.2.17", - "http", "js-sys", "libdatadog-nodejs-capabilities", "libdd-capabilities 2.1.0", "libdd-common 5.1.0", "libdd-data-pipeline", "libdd-shared-runtime 2.0.0", - "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=3081603d3c74f209be4e3be951f78a1a7469397f)", - "libdd-trace-stats", "libdd-trace-utils", - "rmp-serde", "serde", "serde_json", "uuid", "wasm-bindgen", "wasm-bindgen-futures", "wasm-bindgen-test", - "web-time", ] [[package]] @@ -1889,8 +1869,8 @@ name = "process-discovery" version = "0.1.0" dependencies = [ "anyhow", - "libdd-library-config", - "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=0c6e2a5df2a163d34c4f385353ffc5d7257c72f4)", + "libdd-library-config 2.0.0", + "libdd-trace-protobuf 3.0.2", "napi", "napi-derive", ] diff --git a/crates/pipeline/Cargo.toml b/crates/pipeline/Cargo.toml index 8056816..81f1c15 100644 --- a/crates/pipeline/Cargo.toml +++ b/crates/pipeline/Cargo.toml @@ -14,17 +14,13 @@ js-sys = "0.3" serde = { version = "1.0", features = ["derive"] } serde_json = "1" libdatadog-nodejs-capabilities = { path = "../capabilities" } -libdd-capabilities = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f" } -libdd-common = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f", default-features = false } -libdd-data-pipeline = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f", default-features = false, features = ["telemetry"] } -libdd-trace-utils = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f", default-features = false, features = ["change-buffer"] } -libdd-trace-stats = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f", default-features = false } -libdd-trace-protobuf = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f", default-features = false } -libdd-shared-runtime = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f", default-features = false } -rmp-serde = "1" -bytes = "1" -http = "1" -web-time = "1" +# TODO: Replace these temporary libdatadog PR revs with the official release/tag +# that contains DataDog/libdatadog#2235, then regenerate Cargo.lock. +libdd-capabilities = { git = "https://github.com/DataDog/libdatadog.git", rev = "15899dfe754d12186ce7db72f0ff41c1920d52ec" } +libdd-common = { git = "https://github.com/DataDog/libdatadog.git", rev = "15899dfe754d12186ce7db72f0ff41c1920d52ec", default-features = false } +libdd-data-pipeline = { git = "https://github.com/DataDog/libdatadog.git", rev = "15899dfe754d12186ce7db72f0ff41c1920d52ec", default-features = false, features = ["telemetry"] } +libdd-trace-utils = { git = "https://github.com/DataDog/libdatadog.git", rev = "15899dfe754d12186ce7db72f0ff41c1920d52ec", default-features = false, features = ["change-buffer"] } +libdd-shared-runtime = { git = "https://github.com/DataDog/libdatadog.git", rev = "15899dfe754d12186ce7db72f0ff41c1920d52ec", default-features = false } console_error_panic_hook = "0.1" [target.'cfg(target_arch = "wasm32")'.dependencies] diff --git a/crates/pipeline/src/lib.rs b/crates/pipeline/src/lib.rs index 4ff36bb..bd296c1 100644 --- a/crates/pipeline/src/lib.rs +++ b/crates/pipeline/src/lib.rs @@ -18,8 +18,6 @@ mod span_bytes; mod trace_data; use trace_data::*; -mod stats; - use libdd_trace_utils::change_buffer::{ChangeBuffer, ChangeBufferState}; use libdd_trace_utils::span::v04::{AttributeAnyValue, AttributeArrayValue, SpanEvent}; use span_string::SpanString; @@ -170,7 +168,6 @@ pub struct WasmSpanState { exporter: UnsafeCell>>, builder: UnsafeCell>>, cbs: RefCell>, - stats_collector: RefCell>, /// Chunks staged by `prepareChunk`, one per trace (segment), sent together by /// `sendPreparedChunk` as a single multi-trace request. The exporter groups a /// flush batch by trace and calls `prepareChunk` once per trace so each chunk @@ -224,21 +221,6 @@ impl Drop for InFlightGuard<'_> { } } -fn stats_flush_result(sent: bool, collapsed_spans: u64) -> Result { - let result = js_sys::Object::new(); - js_sys::Reflect::set( - &result, - &JsValue::from_str("sent"), - &JsValue::from_bool(sent), - )?; - js_sys::Reflect::set( - &result, - &JsValue::from_str("collapsedSpans"), - &JsValue::from_f64(collapsed_spans as f64), - )?; - Ok(result.into()) -} - #[wasm_bindgen] impl WasmSpanState { #[wasm_bindgen(constructor)] @@ -268,8 +250,7 @@ impl WasmSpanState { .set_language_interpreter(lang_interpreter) .set_otlp_instrumentation_scope("dd-trace-js", tracer_version) // Populate the payload-level TracerMetadata (service/env/hostname/ - // app_version) the agent receives. These values are already passed - // in for the stats collector; without these calls the trace + // app_version) the agent receives. Without these calls the trace // payload's tracer metadata is sent empty. .set_service(tracer_service) .set_env(env) @@ -278,16 +259,18 @@ impl WasmSpanState { .set_runtime_id(runtime_id) .enable_agent_rates_payload_version(); - // Advertise `Datadog-Client-Computed-Stats` so the agent skips its own - // APM stats/sampling for these traces. This is required in two cases: - // - `stats_enabled`: we build a StatsCollector and send client-side - // stats, so the agent MUST NOT also compute them (double counting); - // - `client_computed_stats`: set independently for APM-standalone - // (apmTracingEnabled=false), where the agent should skip APM stats - // even though we don't compute them client-side. - // Enabling stats therefore always implies the header, so OR the flags - // rather than relying on the caller to keep them in sync. - if client_computed_stats || stats_enabled { + // Client-side stats. Two disjoint modes: + // - `stats_enabled`: libdatadog runs the concentrator + /v0.6/stats + // worker natively (LocalRuntime::spawn_worker), gates activation on + // the agent's /info (`client_drop_p0s` + `/v0.6/stats`), and stamps + // `Datadog-Client-Computed-Stats` per trace request when stats + // actually run. + // - `client_computed_stats` (without `stats_enabled`): APM-standalone + // (apmTracingEnabled=false). We advertise the header so the agent + // skips its own APM stats, even though we don't compute any here. + if stats_enabled { + builder.enable_stats(Duration::from_secs(10)); + } else if client_computed_stats { builder.set_client_computed_stats(); } @@ -311,31 +294,12 @@ impl WasmSpanState { pid, ); - let stats_collector = if stats_enabled { - Some(stats::StatsCollector::new( - Duration::from_secs(10), - url.to_string(), - stats::StatsMeta { - hostname: hostname.to_string(), - env: env.to_string(), - version: app_version.to_string(), - lang: lang.to_string(), - tracer_version: tracer_version.to_string(), - runtime_id: runtime_id.to_string(), - service: tracer_service.to_string(), - }, - )) - } else { - None - }; - Ok(WasmSpanState { change_queue, string_table_input: vec![0u8; string_table_input_size as usize], exporter: UnsafeCell::new(None), builder: UnsafeCell::new(Some(builder)), cbs: RefCell::new(change_buffer_state), - stats_collector: RefCell::new(stats_collector), prepared_spans: RefCell::new(Vec::new()), sending: Cell::new(false), use_v05: Cell::new(false), @@ -439,8 +403,8 @@ impl WasmSpanState { self.string_table_input.len() as u32 } - /// Prepare a chunk of spans for sending. Flushes the change buffer, - /// extracts spans, feeds stats. Returns `true` if a chunk was prepared + /// Prepare a chunk of spans for sending. Flushes the change buffer and + /// extracts spans. Returns `true` if a chunk was prepared /// (there are spans to send) and `false` if there was nothing to send. /// Must be followed by `sendPreparedChunk()` to actually send. #[wasm_bindgen(js_name = "prepareChunk")] @@ -486,10 +450,6 @@ impl WasmSpanState { .flush_chunk(&span_ids, first_is_local_root) .map_err(|e| JsValue::from_str(&e.to_string()))?; - if let Some(collector) = self.stats_collector.borrow_mut().as_mut() { - collector.add_spans(&spans_vec); - } - // Stage this trace's chunk for the subsequent sendPreparedChunk call. // Multiple prepareChunk calls (one per trace) accumulate here and are // sent together as one multi-trace request. An empty result (e.g. every @@ -590,43 +550,6 @@ impl WasmSpanState { .map_err(|e| JsValue::from_str(&format!("{:?}", e))) } - /// Flush aggregated stats to the agent's /v0.6/stats endpoint. - /// - /// Should be called periodically (e.g. every 10s) from JS, and with - /// `force=true` on shutdown. Returns `{ sent, collapsedSpans }` so JS can - /// emit the same collapsed-span health metric as libdd-trace-stats's native - /// exporter. - #[wasm_bindgen(js_name = "flushStats")] - pub async fn flush_stats(&self, force: bool) -> Result { - // Build the stats request under a brief *synchronous* borrow, then drop - // the borrow BEFORE the async send. The collector therefore stays in - // `stats_collector`, so a concurrent `prepareChunk` during the in-flight - // send still reaches `add_spans` and those spans are counted. (Taking - // the collector out for the whole await would silently drop them from - // client-side stats.) No borrow is held across the await, so there is - // no double-borrow hazard from overlapping calls. - let prepared = { - let mut guard = self.stats_collector.borrow_mut(); - match guard.as_mut() { - Some(collector) => collector - .prepare_request(force) - .map_err(|e| JsValue::from_str(&e))?, - None => return stats_flush_result(false, 0), - } - }; - let sent = match prepared.request { - Some(req) => { - stats::StatsCollector::send_request(req) - .await - .map_err(|e| JsValue::from_str(&e))?; - true - } - None => false, - }; - - stats_flush_result(sent, prepared.collapsed_spans) - } - /// Flush the queued change-buffer operations. On success always returns /// `true` (the bool exists only for signature symmetry with the other /// flush methods); failures surface as a thrown error. diff --git a/crates/pipeline/src/stats.rs b/crates/pipeline/src/stats.rs deleted file mode 100644 index 3e65cb4..0000000 --- a/crates/pipeline/src/stats.rs +++ /dev/null @@ -1,201 +0,0 @@ -// Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/ -// SPDX-License-Identifier: Apache-2.0 - -//! Native stats collection for the pipeline WASM module. -//! -//! Wraps `SpanConcentrator` from `libdd-trace-stats` and provides encoding + -//! HTTP transport for flushing stats to the Datadog agent's `/v0.6/stats` -//! endpoint. - -use web_time::{Duration, SystemTime}; - -/// Wall-clock now() for wasm. Delegates to `web_time::SystemTime::now()`, -/// which routes to JS `Date.now()` on `wasm32-unknown-unknown` (native -/// `std::time::SystemTime::now()` is unimplemented on that target and traps). -fn now() -> SystemTime { - SystemTime::now() -} - -use bytes::Bytes; -use libdatadog_nodejs_capabilities::WasmHttpClient; -use libdd_capabilities::http::HttpClientCapability; -use libdd_common::parse_uri; -use libdd_trace_protobuf::pb; -use libdd_trace_stats::span_concentrator::SpanConcentrator; - -use crate::trace_data::WasmTraceData; - -const STATS_ENDPOINT_PATH: &str = "/v0.6/stats"; - -/// Metadata for the stats payload envelope. -pub struct StatsMeta { - pub hostname: String, - pub env: String, - pub version: String, - pub lang: String, - pub tracer_version: String, - pub runtime_id: String, - pub service: String, -} - -/// Stats data prepared by a synchronous concentrator flush. -pub struct PreparedStatsFlush { - pub request: Option>, - pub collapsed_spans: u64, -} - -/// Manages stats aggregation and flushing. -pub struct StatsCollector { - concentrator: SpanConcentrator, - meta: StatsMeta, - agent_url: String, - sequence: u64, -} - -impl StatsCollector { - /// Create a new stats collector. - pub fn new(bucket_size: Duration, agent_url: String, meta: StatsMeta) -> Self { - StatsCollector { - concentrator: SpanConcentrator::new( - bucket_size, - now(), - vec![ - "client".to_string(), - "server".to_string(), - "producer".to_string(), - "consumer".to_string(), - ], - Vec::new(), - None, - Vec::new(), - ), - meta, - agent_url, - sequence: 0, - } - } - - /// Add spans to the concentrator for stats aggregation. - /// - /// The spans should already have `_dd.top_level` and `_dd.measured` metrics - /// set (done by `ChangeBufferState::flush_chunk`). - pub fn add_spans(&mut self, spans: &[libdd_trace_utils::span::v04::Span]) { - for span in spans { - self.concentrator.add_span(span); - } - } - - /// Drain aggregated stats into a ready-to-send request plus flush metadata, - /// **synchronously**. - /// - /// Returns `request: None` when there is no stats payload to send. The - /// concentrator is drained and the sequence advanced as part of this call, - /// so a returned request must be sent (see `send_request`). Kept - /// synchronous and separate from the send so a caller can build the request - /// under a brief borrow and release the collector *before* the async send — - /// leaving it available for `add_spans` while the stats request is in - /// flight. - pub fn prepare_request(&mut self, force: bool) -> Result { - let mut flush = self.concentrator.flush(now(), force); - let collapsed_spans = flush.collapsed_spans; - if !flush.obfuscated_buckets.is_empty() { - // TODO: stats obfuscation is currently disabled. Obfuscated stats - // require the datadog-obfuscation-version header, which - // prepare_request doesn't emit yet. Add that header before enabling - // stats-obfuscation. - return Err( - "stats flush produced obfuscated buckets without obfuscation header support" - .to_string(), - ); - } - if flush.unobfuscated_buckets.is_empty() { - return Ok(PreparedStatsFlush { - request: None, - collapsed_spans, - }); - } - - self.sequence += 1; - let buckets = std::mem::take(&mut flush.unobfuscated_buckets); - let payload = encode_stats_payload(&buckets, &self.meta, self.sequence); - - let body = rmp_serde::encode::to_vec_named(&payload) - .map_err(|e| format!("stats msgpack encode error: {e}"))?; - - // Build the base agent URI exactly like the trace exporter does, via - // libdatadog's `parse_uri`. For a `unix://` / `windows:` agent URL that - // hex-encodes the socket path into the URI *authority* (there is no - // standard URL form for socket paths), which the WASM HTTP client's - // `decode_socket_path` reverses to route over the socket. A raw parse - // instead leaves the socket path in the URI *path* with an empty/invalid - // authority, so the stats request never reaches the socket — client stats - // silently never arrive over UDS (dd-trace-js #9139, uds-express4). - let base = parse_uri(&self.agent_url).map_err(|e| format!("invalid agent URL: {e}"))?; - // Append `/v0.6/stats` to the base path while preserving the (hex) - // authority, mirroring libdd-data-pipeline's `add_path`. For `unix://` - // the base path is "/" and the authority holds the hex socket path; for - // TCP it's `http://host:port/`. Trim a trailing slash so the path is - // exactly `/v0.6/stats` (a double slash makes the agent miss the request). - let base_path = base.path().strip_suffix('/').unwrap_or_else(|| base.path()); - let new_path_and_query = format!("{base_path}{STATS_ENDPOINT_PATH}"); - let mut parts = base.into_parts(); - parts.path_and_query = Some( - new_path_and_query - .parse() - .map_err(|e| format!("invalid stats path: {e}"))?, - ); - let uri = http::Uri::from_parts(parts).map_err(|e| format!("invalid stats URL: {e}"))?; - - let req = http::Request::builder() - .method(http::Method::PUT) - .uri(uri) - .header("Content-Type", "application/msgpack") - .header("Datadog-Meta-Lang", &self.meta.lang) - .header("Datadog-Meta-Tracer-Version", &self.meta.tracer_version) - .body(Bytes::from(body)) - .map_err(|e| format!("failed to build stats request: {e}"))?; - - Ok(PreparedStatsFlush { - request: Some(req), - collapsed_spans, - }) - } - - /// Send a prepared stats request to the agent. Does **not** borrow the - /// collector, so trace export (`add_spans`) can proceed during the await. - pub async fn send_request(req: http::Request) -> Result<(), String> { - let client = WasmHttpClient::new_client(); - client - .request(req) - .await - .map_err(|e| format!("stats send error: {e:?}"))?; - Ok(()) - } -} - -/// Encode flushed stats buckets into a `ClientStatsPayload` for msgpack -/// serialization. -fn encode_stats_payload( - buckets: &[pb::ClientStatsBucket], - meta: &StatsMeta, - sequence: u64, -) -> pb::ClientStatsPayload { - pb::ClientStatsPayload { - hostname: meta.hostname.clone(), - env: meta.env.clone(), - version: meta.version.clone(), - lang: meta.lang.clone(), - tracer_version: meta.tracer_version.clone(), - runtime_id: meta.runtime_id.clone(), - sequence, - stats: buckets.to_vec(), - service: meta.service.clone(), - container_id: String::new(), - tags: Vec::new(), - agent_aggregation: String::new(), - git_commit_sha: String::new(), - image_tag: String::new(), - process_tags: String::new(), - process_tags_hash: 0, - } -} diff --git a/test/pipeline.js b/test/pipeline.js index 59511fe..7089e81 100644 --- a/test/pipeline.js +++ b/test/pipeline.js @@ -1081,171 +1081,10 @@ describe('pipeline', { skip }, () => { assert.strictEqual(header, undefined) }) - it('sends the header when stats are enabled (client-side stats imply it)', async () => { - // Enabling client-side stats without clientComputedStats must still send - // the header, otherwise the agent double-counts APM stats. - const { header, sawTraces } = await captureTraceHeader({ statsEnabled: true, clientComputedStats: false }) - assert.ok(sawTraces, 'expected a POST to /v0.4/traces') - assert.strictEqual(header, 'true') - }) - }) - - describe('client-side stats', () => { - it('aggregates and flushes stats to /v0.6/stats', async () => { - const http = require('node:http') - const seen = [] - const server = http.createServer((req, res) => { - const chunks = [] - req.on('data', c => chunks.push(c)) - req.on('end', () => { - seen.push({ method: req.method, url: req.url, len: Buffer.concat(chunks).length }) - res.writeHead(200, { 'content-type': 'application/json' }) - res.end('{}') - }) - }) - await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)) - const { port } = server.address() - - // statsEnabled:true builds the StatsCollector; prepareChunk feeds spans - // into it, and flushStats(true) force-flushes to /v0.6/stats. - const ns = new NativeSpansInterface({ agentUrl: `http://127.0.0.1:${port}`, statsEnabled: true }) - const span = ns.createSpan() - span.name = 'stats-span' - span.service = 'stats-svc' - span.resource = '/stats' - span.type = 'web' - span.duration = 5_000_000n - - try { - await ns.flushSpans(span) - const result = await ns.state.flushStats(true) - assert.deepStrictEqual(result, { sent: true, collapsedSpans: 0 }, 'flushStats reported a send') - const statsReq = seen.find(r => r.url === '/v0.6/stats') - assert.ok(statsReq, 'agent received a /v0.6/stats request') - assert.strictEqual(statsReq.method, 'PUT') - assert.ok(statsReq.len > 0, 'stats payload is non-empty') - - // Nothing new aggregated -> a second forced flush is a no-op. - assert.deepStrictEqual(await ns.state.flushStats(true), { sent: false, collapsedSpans: 0 }, 'second flush has nothing to send') - } finally { - server.closeAllConnections?.() - server.close() - } - }) - - it('returns collapsed span count when stats cardinality overflows', async () => { - const http = require('node:http') - const seen = [] - const server = http.createServer((req, res) => { - const chunks = [] - req.on('data', c => chunks.push(c)) - req.on('end', () => { - seen.push({ method: req.method, url: req.url, len: Buffer.concat(chunks).length }) - res.writeHead(200, { 'content-type': 'application/json' }) - res.end('{}') - }) - }) - await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)) - const { port } = server.address() - - const ns = new NativeSpansInterface({ agentUrl: `http://127.0.0.1:${port}`, statsEnabled: true }) - let batch = [] - - try { - for (let i = 0; i < 15_000; i++) { - const span = ns.createSpan() - span.name = 'stats-span' - span.service = 'stats-svc' - span.resource = `/stats/${i}` - span.type = 'web' - span.setTag('span.kind', 'server') - span.duration = 5_000_000n - ns.flushChangeQueue() - batch.push(span) - if (batch.length === 500) { - await ns.flushSpans(...batch) - batch = [] - } - } - if (batch.length > 0) { - await ns.flushSpans(...batch) - } - - const result = await ns.state.flushStats(true) - assert.strictEqual(result.sent, true) - assert.ok(result.collapsedSpans > 0, 'stats cardinality overflow reported collapsed spans') - assert.ok(seen.some(r => r.url === '/v0.6/stats'), 'agent received a /v0.6/stats request') - } finally { - server.closeAllConnections?.() - server.close() - } - }) - - it('flushStats reports no send when stats are disabled', async () => { - const ns = new NativeSpansInterface({ statsEnabled: false }) - assert.deepStrictEqual(await ns.state.flushStats(true), { sent: false, collapsedSpans: 0 }) - }) - - it('flushes stats to /v0.6/stats over a Unix domain socket', { skip: process.platform === 'win32' }, async () => { - // A `unix://` agent URL must route /v0.6/stats over the socket, like - // traces do. parse_uri hex-encodes the socket path into the URI authority - // (which the transport's decode_socket_path reverses); a raw parse would - // leave the path in the URI path and never reach the socket. - const http = require('node:http') - const os = require('node:os') - const fs = require('node:fs') - const nodePath = require('node:path') - // Keep the path short — AF_UNIX paths are capped (~104 bytes on macOS). - const sockPath = nodePath.join(os.tmpdir(), `dd-st-${process.pid}.sock`) - try { - fs.unlinkSync(sockPath) - } catch { - // not present - } - const seen = [] - const server = http.createServer((req, res) => { - const chunks = [] - req.on('data', c => chunks.push(c)) - req.on('end', () => { - seen.push({ method: req.method, url: req.url, len: Buffer.concat(chunks).length }) - res.writeHead(200, { 'content-type': 'application/json' }) - res.end('{}') - }) - }) - await new Promise((resolve, reject) => { - server.once('error', reject) - server.listen(sockPath, resolve) - }) - - const ns = new NativeSpansInterface({ agentUrl: `unix://${sockPath}`, statsEnabled: true }) - const span = ns.createSpan() - span.name = 'stats-span' - span.service = 'stats-svc' - span.resource = '/stats' - span.type = 'web' - span.duration = 5_000_000n - - try { - await ns.flushSpans(span) - const result = await ns.state.flushStats(true) - assert.deepStrictEqual( - result, - { sent: true, collapsedSpans: 0 }, - 'flushStats reported a send over the socket', - ) - const statsReq = seen.find(r => r.url === '/v0.6/stats') - assert.ok(statsReq, 'agent received a /v0.6/stats request over the socket') - assert.ok(statsReq.len > 0, 'stats payload is non-empty') - } finally { - server.closeAllConnections?.() - server.close() - try { - fs.unlinkSync(sockPath) - } catch { - // already gone - } - } - }) + // When statsEnabled=true, libdatadog stamps the header dynamically only + // after the agent /info advertises `client_drop_p0s` + `/v0.6/stats`. + // That's inherently race-y in a unit test, so it's covered end-to-end + // instead of asserted here. }) describe('send re-entrancy', () => { From b0d12dc5241181febdfe32f2e7565beeb053a9d1 Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Fri, 24 Jul 2026 16:48:01 +0200 Subject: [PATCH 15/16] feat: added force shutdown binding, and trimmed out verbose comments --- crates/pipeline/src/lib.rs | 49 ++++++++++++++++++++++++++++---------- test/pipeline.js | 5 ---- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/crates/pipeline/src/lib.rs b/crates/pipeline/src/lib.rs index bd296c1..e5fa612 100644 --- a/crates/pipeline/src/lib.rs +++ b/crates/pipeline/src/lib.rs @@ -249,9 +249,7 @@ impl WasmSpanState { .set_language_version(lang_version) .set_language_interpreter(lang_interpreter) .set_otlp_instrumentation_scope("dd-trace-js", tracer_version) - // Populate the payload-level TracerMetadata (service/env/hostname/ - // app_version) the agent receives. Without these calls the trace - // payload's tracer metadata is sent empty. + // Without these setters the payload-level TracerMetadata is empty. .set_service(tracer_service) .set_env(env) .set_hostname(hostname) @@ -259,15 +257,11 @@ impl WasmSpanState { .set_runtime_id(runtime_id) .enable_agent_rates_payload_version(); - // Client-side stats. Two disjoint modes: - // - `stats_enabled`: libdatadog runs the concentrator + /v0.6/stats - // worker natively (LocalRuntime::spawn_worker), gates activation on - // the agent's /info (`client_drop_p0s` + `/v0.6/stats`), and stamps - // `Datadog-Client-Computed-Stats` per trace request when stats - // actually run. - // - `client_computed_stats` (without `stats_enabled`): APM-standalone - // (apmTracingEnabled=false). We advertise the header so the agent - // skips its own APM stats, even though we don't compute any here. + // `enable_stats` makes libdatadog stamp the client-computed-stats + // header itself when stats run, so the two flags are disjoint: + // `set_client_computed_stats` is only for APM-standalone + // (`apmTracingEnabled=false`), where the header is advertised + // without any stats actually being computed. if stats_enabled { builder.enable_stats(Duration::from_secs(10)); } else if client_computed_stats { @@ -562,6 +556,37 @@ impl WasmSpanState { Ok(true) } + /// Force-flush the current stats bucket and stop the background workers. + /// The stats worker only flushes on its bucket interval, so spans + /// recorded between the last tick and process exit are lost unless this + /// is awaited during tracer shutdown. Consumes the exporter — subsequent + /// sends will error. + /// + /// `timeoutMs` bounds the wait; on timeout returns an error and workers + /// may still be finishing. `None` waits indefinitely. + #[wasm_bindgen(js_name = "shutdown")] + pub async fn shutdown(&self, timeout_ms: Option) -> Result<(), JsValue> { + // `sendPreparedChunk` holds an `&mut` on the exporter across awaits; + // taking it out from under an in-flight send would alias. + if self.sending.get() { + return Err(JsValue::from_str("shutdown: sendPreparedChunk in flight")); + } + self.sending.set(true); + let _in_flight = InFlightGuard(&self.sending); + + // SAFETY: `sending` guard prevents overlapping access; WASM is single-threaded. + let exporter_slot = unsafe { &mut *self.exporter.get() }; + // Idempotent: never-built or already-shut-down state is not an error. + let Some(exporter) = exporter_slot.take() else { + return Ok(()); + }; + let timeout = timeout_ms.map(|ms| Duration::from_millis(u64::from(ms))); + exporter + .shutdown_async(timeout) + .await + .map_err(|e| JsValue::from_str(&format!("shutdown: {e:?}"))) + } + /// Set default meta tags applied to every new span. /// Takes a flat array of key-value pairs: [key1, val1, key2, val2, ...] #[wasm_bindgen(js_name = "setDefaultMeta")] diff --git a/test/pipeline.js b/test/pipeline.js index 7089e81..08032c0 100644 --- a/test/pipeline.js +++ b/test/pipeline.js @@ -1080,11 +1080,6 @@ describe('pipeline', { skip }, () => { assert.ok(sawTraces, 'expected a POST to /v0.4/traces') assert.strictEqual(header, undefined) }) - - // When statsEnabled=true, libdatadog stamps the header dynamically only - // after the agent /info advertises `client_drop_p0s` + `/v0.6/stats`. - // That's inherently race-y in a unit test, so it's covered end-to-end - // instead of asserted here. }) describe('send re-entrancy', () => { From 49db4d0443533067da3f284b6930867f3ec499b1 Mon Sep 17 00:00:00 2001 From: Jules Wiriath Date: Mon, 27 Jul 2026 12:16:12 +0200 Subject: [PATCH 16/16] docs: fix --- Cargo.lock | 73 +++++++++++++++++++++------------- crates/capabilities/Cargo.toml | 2 +- crates/pipeline/src/lib.rs | 8 ++-- 3 files changed, 49 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 673f0b4..d77d03c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1031,7 +1031,7 @@ dependencies = [ "libc", "libdd-common 5.0.0", "libdd-libunwind-sys", - "libdd-telemetry", + "libdd-telemetry 5.0.1", "nix 0.29.0", "num-derive", "num-traits", @@ -1070,9 +1070,10 @@ dependencies = [ "libdd-ddsketch 1.1.0", "libdd-dogstatsd-client", "libdd-shared-runtime 2.0.0", + "libdd-telemetry 6.0.0", "libdd-tinybytes", "libdd-trace-normalization", - "libdd-trace-protobuf 4.0.0", + "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec)", "libdd-trace-stats", "libdd-trace-utils", "rmp-serde", @@ -1120,26 +1121,12 @@ dependencies = [ [[package]] name = "libdd-library-config" -version = "1.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=353134770b312b7ccd2df6afabc253090b948e5f#353134770b312b7ccd2df6afabc253090b948e5f" -dependencies = [ - "anyhow", - "memfd", - "rand", - "rmp", - "rmp-serde", - "serde", - "serde_yaml", -] - -[[package]] -name = "libdd-library-config" -version = "2.0.0" -source = "git+https://github.com/DataDog/libdatadog.git?rev=7cdeb7896e92d1ba38bde495934e112dac2eda25#7cdeb7896e92d1ba38bde495934e112dac2eda25" +version = "3.0.0" +source = "git+https://github.com/DataDog/libdatadog.git?rev=0c6e2a5df2a163d34c4f385353ffc5d7257c72f4#0c6e2a5df2a163d34c4f385353ffc5d7257c72f4" dependencies = [ "anyhow", "libc", - "libdd-trace-protobuf 3.0.2", + "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=0c6e2a5df2a163d34c4f385353ffc5d7257c72f4)", "memfd", "prost", "rand", @@ -1221,6 +1208,35 @@ dependencies = [ "winver", ] +[[package]] +name = "libdd-telemetry" +version = "6.0.0" +source = "git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec#15899dfe754d12186ce7db72f0ff41c1920d52ec" +dependencies = [ + "anyhow", + "async-trait", + "base64", + "bytes", + "futures", + "getrandom 0.2.17", + "hashbrown 0.15.5", + "http", + "libc", + "libdd-capabilities 2.1.0", + "libdd-common 5.1.0", + "libdd-ddsketch 1.1.0", + "libdd-shared-runtime 2.0.0", + "serde", + "serde_json", + "sys-info", + "tokio", + "tokio-util", + "tracing", + "uuid", + "web-time", + "winver", +] + [[package]] name = "libdd-tinybytes" version = "1.1.1" @@ -1235,7 +1251,7 @@ version = "3.0.0" source = "git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec#15899dfe754d12186ce7db72f0ff41c1920d52ec" dependencies = [ "anyhow", - "libdd-trace-protobuf 4.0.0", + "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec)", ] [[package]] @@ -1246,7 +1262,7 @@ dependencies = [ "anyhow", "fluent-uri", "libdd-common 5.1.0", - "libdd-trace-protobuf 4.0.0", + "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec)", "libdd-trace-utils", "log", "percent-encoding", @@ -1256,8 +1272,8 @@ dependencies = [ [[package]] name = "libdd-trace-protobuf" -version = "3.0.2" -source = "git+https://github.com/DataDog/libdatadog.git?rev=7cdeb7896e92d1ba38bde495934e112dac2eda25#7cdeb7896e92d1ba38bde495934e112dac2eda25" +version = "4.0.0" +source = "git+https://github.com/DataDog/libdatadog.git?rev=0c6e2a5df2a163d34c4f385353ffc5d7257c72f4#0c6e2a5df2a163d34c4f385353ffc5d7257c72f4" dependencies = [ "prost", "serde", @@ -1291,8 +1307,9 @@ dependencies = [ "libdd-ddsketch 1.1.0", "libdd-dogstatsd-client", "libdd-shared-runtime 2.0.0", + "libdd-telemetry 6.0.0", "libdd-trace-obfuscation", - "libdd-trace-protobuf 4.0.0", + "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec)", "libdd-trace-utils", "rmp-serde", "serde", @@ -1323,7 +1340,7 @@ dependencies = [ "libdd-common 5.1.0", "libdd-tinybytes", "libdd-trace-normalization", - "libdd-trace-protobuf 4.0.0", + "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=15899dfe754d12186ce7db72f0ff41c1920d52ec)", "prost", "rand", "rmp", @@ -1360,7 +1377,7 @@ version = "0.2.0" dependencies = [ "anyhow", "getrandom 0.2.17", - "libdd-library-config 1.0.0", + "libdd-library-config", "serde", "serde-wasm-bindgen", "wasm-bindgen", @@ -1869,8 +1886,8 @@ name = "process-discovery" version = "0.1.0" dependencies = [ "anyhow", - "libdd-library-config 2.0.0", - "libdd-trace-protobuf 3.0.2", + "libdd-library-config", + "libdd-trace-protobuf 4.0.0 (git+https://github.com/DataDog/libdatadog.git?rev=0c6e2a5df2a163d34c4f385353ffc5d7257c72f4)", "napi", "napi-derive", ] diff --git a/crates/capabilities/Cargo.toml b/crates/capabilities/Cargo.toml index edb40ad..371b3f4 100644 --- a/crates/capabilities/Cargo.toml +++ b/crates/capabilities/Cargo.toml @@ -15,7 +15,7 @@ http = "1" bytes = "1.4" futures-core = "0.3" anyhow = "1" -libdd-capabilities = { git = "https://github.com/DataDog/libdatadog.git", rev = "3081603d3c74f209be4e3be951f78a1a7469397f" } +libdd-capabilities = { git = "https://github.com/DataDog/libdatadog.git", rev = "15899dfe754d12186ce7db72f0ff41c1920d52ec" } [dev-dependencies] wasm-bindgen-test = "0.3" diff --git a/crates/pipeline/src/lib.rs b/crates/pipeline/src/lib.rs index e5fa612..9129bc8 100644 --- a/crates/pipeline/src/lib.rs +++ b/crates/pipeline/src/lib.rs @@ -556,11 +556,9 @@ impl WasmSpanState { Ok(true) } - /// Force-flush the current stats bucket and stop the background workers. - /// The stats worker only flushes on its bucket interval, so spans - /// recorded between the last tick and process exit are lost unless this - /// is awaited during tracer shutdown. Consumes the exporter — subsequent - /// sends will error. + /// Gracefully shut down the exporter and stop the background workers. + /// Should be awaited during tracer shutdown to avoid losing in-flight + /// data. Consumes the exporter — subsequent sends will error. /// /// `timeoutMs` bounds the wait; on timeout returns an error and workers /// may still be finishing. `None` waits indefinitely.