Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 14 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions crates/capabilities/src/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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;
}

#[derive(Debug, Clone)]
pub struct WasmEnvCapability;

impl EnvCapability for WasmEnvCapability {
fn new() -> Self {
Self
}

fn get(&self, name: &str) -> Result<Option<String>, EnvError> {
// Node coerces every process.env value to a string, so NotUnicode is unreachable here.
Ok(js_env_get(name).as_string())
}
}
7 changes: 7 additions & 0 deletions crates/capabilities/src/env_transport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

const { env } = process

module.exports.get = (name) => {
return env[name]
}
21 changes: 17 additions & 4 deletions crates/capabilities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@
//! 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;

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;
Expand All @@ -38,6 +40,7 @@ pub struct WasmCapabilities {
sleep: WasmSleepCapability,
/// Filesystem access delegated to the Node.js `fs` transport.
file: WasmFileCapability,
env: WasmEnvCapability,
}

impl Default for WasmCapabilities {
Expand All @@ -52,6 +55,7 @@ impl WasmCapabilities {
http: WasmHttpClient::new_client(),
sleep: WasmSleepCapability,
file: WasmFileCapability,
env: WasmEnvCapability,
}
}
}
Expand Down Expand Up @@ -122,3 +126,12 @@ impl FileCapability for WasmCapabilities {
}
}

impl EnvCapability for WasmCapabilities {
fn new() -> Self {
Self::new()
}

fn get(&self, name: &str) -> Result<Option<String>, EnvError> {
self.env.get(name)
}
}
2 changes: 1 addition & 1 deletion crates/library_config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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", rev = "0c6e2a5df2a163d34c4f385353ffc5d7257c72f4" }

wasm-bindgen = "0.2.100"
serde = { version = "1.0", features = ["derive"] }
Expand Down
7 changes: 2 additions & 5 deletions crates/process_discovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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", 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 }
31 changes: 31 additions & 0 deletions test/env-transport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'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('returns the value when the var is set', () => {
process.env[NAME] = 'value1'
assert.strictEqual(envTransport.get(NAME), 'value1')
})
})
Loading