diff --git a/Cargo.lock b/Cargo.lock index c042060..cfd8ad4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -267,6 +267,16 @@ dependencies = [ "tokio", ] +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + [[package]] name = "convert_case" version = "0.6.0" @@ -383,6 +393,15 @@ dependencies = [ "prost", ] +[[package]] +name = "datadog-library-config" +version = "0.0.1" +dependencies = [ + "anyhow", + "serde", + "serde_yaml", +] + [[package]] name = "datadog-trace-normalization" version = "6.0.0" @@ -979,6 +998,18 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "library-config" +version = "0.1.0" +dependencies = [ + "anyhow", + "datadog-library-config", + "serde", + "serde-wasm-bindgen", + "wasm-bindgen", + "wasm-bindgen-test", +] + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -1015,6 +1046,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "minicov" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f27fe9f1cc3c22e1687f9446c2083c4c5fc7f0bcf1c7a86bdbded14985895b4b" +dependencies = [ + "cc", + "walkdir", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1556,6 +1597,15 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "schannel" version = "0.1.26" @@ -1589,6 +1639,12 @@ dependencies = [ "syn 2.0.87", ] +[[package]] +name = "scoped-tls" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" + [[package]] name = "sct" version = "0.7.1" @@ -1643,6 +1699,17 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-wasm-bindgen" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3b4c031cd0d9014307d82b8abf653c0290fbdaeb4c02d00c63cf52f728628bf" +dependencies = [ + "js-sys", + "serde", + "wasm-bindgen", +] + [[package]] name = "serde_bytes" version = "0.11.15" @@ -1686,6 +1753,19 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml" +version = "0.9.34+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "shlex" version = "1.3.0" @@ -1901,6 +1981,12 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + [[package]] name = "untrusted" version = "0.7.1" @@ -1929,6 +2015,16 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "want" version = "0.3.1" @@ -1970,6 +2066,18 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc7ec4f8827a71586374db3e87abdb5a2bb3a15afed140221307c3ec06b1f63b" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-macro" version = "0.2.95" @@ -1999,6 +2107,32 @@ version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" +[[package]] +name = "wasm-bindgen-test" +version = "0.3.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d381749acb0943d357dcbd8f0b100640679883fcdeeef04def49daf8d33a5426" +dependencies = [ + "console_error_panic_hook", + "js-sys", + "minicov", + "scoped-tls", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-bindgen-test-macro", +] + +[[package]] +name = "wasm-bindgen-test-macro" +version = "0.3.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c97b2ef2c8d627381e51c071c2ab328eac606d3f69dd82bcbca20a9e389d95f0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + [[package]] name = "web-sys" version = "0.3.72" @@ -2047,6 +2181,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys 0.59.0", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 7731b6c..408ea9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] default-members = [ - "crates/crashtracker" + "crates/crashtracker", + "crates/library-config" ] members = [ "crates/*" diff --git a/crates/library-config/Cargo.toml b/crates/library-config/Cargo.toml new file mode 100644 index 0000000..c668d88 --- /dev/null +++ b/crates/library-config/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "library-config" +version = "0.1.0" +edition = "2018" + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +anyhow = "1" +# TODO: set the right dependencies +# datadog-library-config = { git = "https://github.com/DataDog/libdatadog.git", tag = "v14.3.1" } +datadog-library-config = { path = "../../../libdatadog/library-config" } + +wasm-bindgen = "0.2.84" +serde = { version = "1.0", features = ["derive"] } +serde-wasm-bindgen = "0.4" + +[dev-dependencies] +wasm-bindgen-test = "0.3.34" + +[profile.release] +# Tell `rustc` to optimize for small code size. +opt-level = "s" diff --git a/crates/library-config/src/lib.rs b/crates/library-config/src/lib.rs new file mode 100644 index 0000000..0f6921f --- /dev/null +++ b/crates/library-config/src/lib.rs @@ -0,0 +1,79 @@ +use std::collections::HashMap; + +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub struct JsConfigurator { + configurator: Box, + envp: Vec, + args: Vec, +} + +#[wasm_bindgen] +impl JsConfigurator { + #[wasm_bindgen(constructor)] + pub fn new(debug_logs: bool) -> Self { + JsConfigurator { + configurator: Box::new(datadog_library_config::Configurator::new(debug_logs)), + envp: Vec::new(), + args: Vec::new(), + } + } + + #[wasm_bindgen] + pub fn set_envp(&mut self, envp: Box<[JsValue]>) -> Result<(), JsValue> { + self.envp = envp.iter() + .filter_map(|val| val.as_string()) + .collect(); + Ok(()) + } + + #[wasm_bindgen] + pub fn set_args(&mut self, args: Box<[JsValue]>) -> Result<(), JsValue> { + self.args = args.iter() + .filter_map(|val| val.as_string()) + .collect(); + Ok(()) + } + + #[wasm_bindgen] + pub fn get_configuration( + &self, + config_string: String, + ) -> Result { + let envp: Vec<&[u8]> = self + .envp + .iter() + .map(|s| s.as_bytes()) + .collect(); + + let args: Vec<_> = self + .args + .iter() + .map(|s| s.as_bytes()) + .collect(); + + let res_config = self.configurator.get_config_from_bytes( + config_string.as_bytes(), + datadog_library_config::ProcessInfo { + envp: &envp, + args: &args, + language: b"nodejs", + }, + ); + + match res_config { + Ok(config) => { + let hashmap: HashMap = config.into_iter().map(|c| { + let key = c.name.to_str().to_owned(); + (key, c.value) + }).collect(); + Ok(serde_wasm_bindgen::to_value(&hashmap)?) + }, + Err(e) => Err(JsValue::from_str(&format!( + "Failed to get configuration: {:?}", + e + ))), + } + } +} diff --git a/test/library-config/.gitignore b/test/library-config/.gitignore new file mode 100644 index 0000000..5fff1d9 --- /dev/null +++ b/test/library-config/.gitignore @@ -0,0 +1 @@ +pkg diff --git a/test/library-config/README.md b/test/library-config/README.md new file mode 100644 index 0000000..c80ffee --- /dev/null +++ b/test/library-config/README.md @@ -0,0 +1,10 @@ +# Libconfig example + +## How to run +From repository root +```bash +wasm-pack build ./crates/library-config +mv ./crates/library-config/pkg ./test/library-config + +node --experimental-modules --experimental-wasm-modules test/library-config/index.js +``` diff --git a/test/library-config/config.yaml b/test/library-config/config.yaml new file mode 100644 index 0000000..597c05b --- /dev/null +++ b/test/library-config/config.yaml @@ -0,0 +1,8 @@ +rules: + - selectors: + - origin: language + matches: + - nodejs + operator: equals + configuration: + DD_SERVICE: my-service-{{ environment[HOME] }} diff --git a/test/library-config/index.js b/test/library-config/index.js new file mode 100644 index 0000000..580d505 --- /dev/null +++ b/test/library-config/index.js @@ -0,0 +1,16 @@ +import * as libconfig from '/Users/baptiste.foy/go/src/github.com/DataDog/libdatadog-nodejs/test/library-config/pkg/library_config.js'; +import path from 'path'; +import { fileURLToPath } from 'url'; +import fs from 'fs'; + +const rawConfig = fs.readFileSync(path.join(path.dirname(fileURLToPath(import.meta.url)), 'config.yaml')); +let configurator = new libconfig.JsConfigurator(false); + +configurator.set_envp(Object.entries(process.env).map(([key, value]) => `${key}=${value}`)) +configurator.set_args(process.argv) + +// Apply each configuration as an environment variable +console.log("Configuration:") +configurator.get_configuration(rawConfig.toString()).forEach((value, key, map) => { + console.log(` - ${key}: ${value}`) +}); diff --git a/test/library-config/package.json b/test/library-config/package.json new file mode 100644 index 0000000..4e4bada --- /dev/null +++ b/test/library-config/package.json @@ -0,0 +1,10 @@ +{ + "private": true, + "main": "index.js", + "dependencies": { + "@datadog/segfaultify": "^0.1.1", + "body-parser": "^1.20.3", + "express": "^4.19.2" + }, + "type": "module" +}