From f572f62443bfb63b5b298d9f81f57f879ac2bf72 Mon Sep 17 00:00:00 2001 From: Baptiste Foy Date: Thu, 6 Feb 2025 14:51:11 +0100 Subject: [PATCH 1/4] upgrade(library-config): Bump to libdatadog 16.0.2 --- Cargo.lock | 2 +- crates/library_config/Cargo.toml | 2 +- crates/library_config/src/lib.rs | 90 +++++++++++++++---- test/wasm/library_config/README.md | 2 +- .../{config.yaml => config_local.yaml} | 0 test/wasm/library_config/config_managed.yaml | 9 ++ test/wasm/library_config/index.js | 24 +++-- 7 files changed, 104 insertions(+), 25 deletions(-) rename test/wasm/library_config/{config.yaml => config_local.yaml} (100%) create mode 100644 test/wasm/library_config/config_managed.yaml diff --git a/Cargo.lock b/Cargo.lock index 25049a6..826ae3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -396,7 +396,7 @@ dependencies = [ [[package]] name = "datadog-library-config" version = "0.0.1" -source = "git+https://github.com/DataDog/libdatadog.git?tag=v15.0.0#0ef49864317b0728648b2b7f26fe2f1deeeeebc4" +source = "git+https://github.com/DataDog/libdatadog.git?rev=v16.0.2#52bd068269da43fb216e278a8576184bf6307177" dependencies = [ "anyhow", "serde", diff --git a/crates/library_config/Cargo.toml b/crates/library_config/Cargo.toml index c5459ee..db03c1f 100644 --- a/crates/library_config/Cargo.toml +++ b/crates/library_config/Cargo.toml @@ -8,7 +8,7 @@ crate-type = ["cdylib", "rlib"] [dependencies] anyhow = "1" -datadog-library-config = { git = "https://github.com/DataDog/libdatadog.git", tag = "v15.0.0" } +datadog-library-config = { git = "https://github.com/DataDog/libdatadog.git", rev = "v16.0.2" } wasm-bindgen = "0.2.84" serde = { version = "1.0", features = ["derive"] } diff --git a/crates/library_config/src/lib.rs b/crates/library_config/src/lib.rs index 897885b..d2f3ca4 100644 --- a/crates/library_config/src/lib.rs +++ b/crates/library_config/src/lib.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; - use wasm_bindgen::prelude::*; #[wasm_bindgen] @@ -9,6 +7,38 @@ pub struct JsConfigurator { args: Vec, } +#[wasm_bindgen] +pub struct ConfigEntry { + name: String, + value: String, + source: String, + config_id: String, +} + +#[wasm_bindgen] +impl ConfigEntry { + #[wasm_bindgen(constructor)] + pub fn new(name: String, value: String, source: String, config_id: String) -> ConfigEntry { + ConfigEntry { name, value, source, config_id } + } + #[wasm_bindgen(getter)] + pub fn name(&self) -> String { + self.name.clone() + } + #[wasm_bindgen(getter)] + pub fn value(&self) -> String { + self.value.clone() + } + #[wasm_bindgen(getter)] + pub fn source(&self) -> String { + self.source.clone() + } + #[wasm_bindgen(getter)] + pub fn config_id(&self) -> String { + self.config_id.clone() + } +} + #[wasm_bindgen] impl JsConfigurator { #[wasm_bindgen(constructor)] @@ -36,39 +66,67 @@ impl JsConfigurator { Ok(()) } + #[wasm_bindgen] + pub fn get_config_local_path(&self, target: String) -> Result { + let target_enum = match target.as_str() { + "linux" => datadog_library_config::Target::Linux, + "win32" => datadog_library_config::Target::Windows, + "darwin" => datadog_library_config::Target::Macos, + _ => return Err(JsValue::from_str("Unsupported target")), + }; + Ok(datadog_library_config::Configurator::local_stable_configuration_path(target_enum).to_string()) + } + + #[wasm_bindgen] + pub fn get_config_managed_path(&self, target: String) -> Result { + let target_enum = match target.as_str() { + "linux" => datadog_library_config::Target::Linux, + "win32" => datadog_library_config::Target::Windows, + "darwin" => datadog_library_config::Target::Macos, + _ => return Err(JsValue::from_str("Unsupported target")), + }; + Ok(datadog_library_config::Configurator::fleet_stable_configuration_path(target_enum).to_string()) + } + #[wasm_bindgen] pub fn get_configuration( &self, - config_string: String, - ) -> Result { - let envp: Vec<&[u8]> = self + config_string_local: String, + config_string_managed: String, + ) -> Result, JsValue> { + let envp: Vec> = self .envp .iter() - .map(|s| s.as_bytes()) + .map(|s| s.as_bytes().to_vec()) .collect(); - let args: Vec<_> = self + let args: Vec> = self .args .iter() - .map(|s| s.as_bytes()) + .map(|s| s.as_bytes().to_vec()) .collect(); let res_config = self.configurator.get_config_from_bytes( - config_string.as_bytes(), + config_string_local.as_bytes(), + config_string_managed.as_bytes(), datadog_library_config::ProcessInfo { - envp: &envp, - args: &args, - language: b"nodejs", + envp: envp, + args: args, + language: b"nodejs".to_vec(), }, ); match res_config { Ok(config) => { - let hashmap: HashMap = config.into_iter().map(|c| { - let key = c.name.to_str().to_owned(); - (key, c.value) + let config_entries: Vec = config.into_iter().map(|c| { + ConfigEntry { + name: c.name.to_str().into(), + value: c.value, + source: c.source.to_str().into(), + config_id: c.config_id.unwrap_or_default(), + } }).collect(); - Ok(serde_wasm_bindgen::to_value(&hashmap)?) + Ok(config_entries) }, Err(e) => Err(JsValue::from_str(&format!( "Failed to get configuration: {:?}", diff --git a/test/wasm/library_config/README.md b/test/wasm/library_config/README.md index 4e58f07..ca98e96 100644 --- a/test/wasm/library_config/README.md +++ b/test/wasm/library_config/README.md @@ -1,6 +1,6 @@ # Libconfig example -## How to run +## How to run From repository root ```bash yarn build-wasm diff --git a/test/wasm/library_config/config.yaml b/test/wasm/library_config/config_local.yaml similarity index 100% rename from test/wasm/library_config/config.yaml rename to test/wasm/library_config/config_local.yaml diff --git a/test/wasm/library_config/config_managed.yaml b/test/wasm/library_config/config_managed.yaml new file mode 100644 index 0000000..28c3302 --- /dev/null +++ b/test/wasm/library_config/config_managed.yaml @@ -0,0 +1,9 @@ +rules: + - selectors: + - origin: language + matches: + - nodejs + operator: equals + configuration: + DD_SERVICE: my-service_butremote +config_id: abc diff --git a/test/wasm/library_config/index.js b/test/wasm/library_config/index.js index a5283e8..76f1e7f 100644 --- a/test/wasm/library_config/index.js +++ b/test/wasm/library_config/index.js @@ -6,17 +6,29 @@ const assert = require('assert'); const libconfig = loader.load('library_config'); assert(libconfig !== undefined); -const rawConfig = fs.readFileSync(path.join(__dirname, 'config.yaml')); +const rawConfigLocal = fs.readFileSync(path.join(__dirname, 'config_local.yaml')); +const rawConfigManaged = fs.readFileSync(path.join(__dirname, 'config_managed.yaml')); let configurator = new libconfig.JsConfigurator(); configurator.set_envp(Object.entries(process.env).map(([key, value]) => `${key}=${value}`)) configurator.set_args(process.argv) // Apply each configuration as an environment variable -let values = {} -configurator.get_configuration(rawConfig.toString()).forEach((value, key, map) => { - values[key] = value - console.log(`Got ${key}=${value}`) +let values = configurator.get_configuration(rawConfigLocal.toString(), rawConfigManaged.toString()) +values.forEach((value, key, map) => { + console.log(`name: ${value.name}, value: ${value.value}, source: ${value.source}, config_id: ${value.config_id}`) }); -assert.strictEqual(values['DD_SERVICE'], 'my-service') +assert.strictEqual(values.length, 1) +assert.strictEqual(values[0].name, 'DD_SERVICE') +assert.strictEqual(values[0].value, 'my-service_butremote') +assert.strictEqual(values[0].source, 'fleet_stable_config') +assert.strictEqual(values[0].config_id, 'abc') + +if (process.platform == 'linux') { + assert.strictEqual(configurator.get_config_local_path(process.platform), "/etc/datadog-agent/application_monitoring.yaml"); +} else if (process.platform == 'darwin') { + assert.strictEqual(configurator.get_config_local_path(process.platform), "/opt/datadog-agent/etc/application_monitoring.yaml"); +} else if (process.platform == 'win32') { + assert.strictEqual(configurator.get_config_local_path(process.platform), "C:\\ProgramData\\Datadog\\application_monitoring.yaml"); +} From 5def27f38adda5a23aed80c98725fd25be201641 Mon Sep 17 00:00:00 2001 From: Baptiste Foy Date: Mon, 10 Feb 2025 09:51:26 +0100 Subject: [PATCH 2/4] add test for phase 2 --- test/wasm/library_config/config_local.yaml | 8 -- .../library_config/config_local_phase1.yaml | 2 + .../library_config/config_local_phase2.yaml | 2 + ...anaged.yaml => config_managed_phase2.yaml} | 0 test/wasm/library_config/index.js | 73 ++++++++++++------- 5 files changed, 52 insertions(+), 33 deletions(-) delete mode 100644 test/wasm/library_config/config_local.yaml create mode 100644 test/wasm/library_config/config_local_phase1.yaml create mode 100644 test/wasm/library_config/config_local_phase2.yaml rename test/wasm/library_config/{config_managed.yaml => config_managed_phase2.yaml} (100%) diff --git a/test/wasm/library_config/config_local.yaml b/test/wasm/library_config/config_local.yaml deleted file mode 100644 index 0bdcd11..0000000 --- a/test/wasm/library_config/config_local.yaml +++ /dev/null @@ -1,8 +0,0 @@ -rules: - - selectors: - - origin: language - matches: - - nodejs - operator: equals - configuration: - DD_SERVICE: my-service diff --git a/test/wasm/library_config/config_local_phase1.yaml b/test/wasm/library_config/config_local_phase1.yaml new file mode 100644 index 0000000..f9c6b62 --- /dev/null +++ b/test/wasm/library_config/config_local_phase1.yaml @@ -0,0 +1,2 @@ +apm_configuration_default: + DD_RUNTIME_METRICS_ENABLED: true diff --git a/test/wasm/library_config/config_local_phase2.yaml b/test/wasm/library_config/config_local_phase2.yaml new file mode 100644 index 0000000..f9c6b62 --- /dev/null +++ b/test/wasm/library_config/config_local_phase2.yaml @@ -0,0 +1,2 @@ +apm_configuration_default: + DD_RUNTIME_METRICS_ENABLED: true diff --git a/test/wasm/library_config/config_managed.yaml b/test/wasm/library_config/config_managed_phase2.yaml similarity index 100% rename from test/wasm/library_config/config_managed.yaml rename to test/wasm/library_config/config_managed_phase2.yaml diff --git a/test/wasm/library_config/index.js b/test/wasm/library_config/index.js index 76f1e7f..64a60f8 100644 --- a/test/wasm/library_config/index.js +++ b/test/wasm/library_config/index.js @@ -6,29 +6,52 @@ const assert = require('assert'); const libconfig = loader.load('library_config'); assert(libconfig !== undefined); -const rawConfigLocal = fs.readFileSync(path.join(__dirname, 'config_local.yaml')); -const rawConfigManaged = fs.readFileSync(path.join(__dirname, 'config_managed.yaml')); -let configurator = new libconfig.JsConfigurator(); - -configurator.set_envp(Object.entries(process.env).map(([key, value]) => `${key}=${value}`)) -configurator.set_args(process.argv) - -// Apply each configuration as an environment variable -let values = configurator.get_configuration(rawConfigLocal.toString(), rawConfigManaged.toString()) -values.forEach((value, key, map) => { - console.log(`name: ${value.name}, value: ${value.value}, source: ${value.source}, config_id: ${value.config_id}`) -}); - -assert.strictEqual(values.length, 1) -assert.strictEqual(values[0].name, 'DD_SERVICE') -assert.strictEqual(values[0].value, 'my-service_butremote') -assert.strictEqual(values[0].source, 'fleet_stable_config') -assert.strictEqual(values[0].config_id, 'abc') - -if (process.platform == 'linux') { - assert.strictEqual(configurator.get_config_local_path(process.platform), "/etc/datadog-agent/application_monitoring.yaml"); -} else if (process.platform == 'darwin') { - assert.strictEqual(configurator.get_config_local_path(process.platform), "/opt/datadog-agent/etc/application_monitoring.yaml"); -} else if (process.platform == 'win32') { - assert.strictEqual(configurator.get_config_local_path(process.platform), "C:\\ProgramData\\Datadog\\application_monitoring.yaml"); +// Test 1: phase 1 (host selection) +function test_host_wide() { + const rawConfigLocal = fs.readFileSync(path.join(__dirname, 'config_local_phase1.yaml')); + let configurator = new libconfig.JsConfigurator(); + + configurator.set_envp(Object.entries(process.env).map(([key, value]) => `${key}=${value}`)) + configurator.set_args(process.argv) + + let values = configurator.get_configuration(rawConfigLocal.toString(), "") + values.forEach((value, key, map) => { + console.log(`(phase 1) name: ${value.name}, value: ${value.value}, source: ${value.source}, config_id: ${value.config_id}`) + }); + + assert.strictEqual(values.length, 1) + assert.strictEqual(values[0].name, 'DD_RUNTIME_METRICS_ENABLED') + assert.strictEqual(values[0].value, 'true') + assert.strictEqual(values[0].source, 'local_stable_config') +} + +// Test 2: managed > local, phase 2 (service selection) +function test_service_selector() { + const rawConfigLocal = fs.readFileSync(path.join(__dirname, 'config_local_phase2.yaml')); + const rawConfigManaged = fs.readFileSync(path.join(__dirname, 'config_managed_phase2.yaml')); + let configurator = new libconfig.JsConfigurator(); + + configurator.set_envp(Object.entries(process.env).map(([key, value]) => `${key}=${value}`)) + configurator.set_args(process.argv) + + values = configurator.get_configuration(rawConfigLocal.toString(), rawConfigManaged.toString()) + values.forEach((value, key, map) => { + console.log(`(phase 2) name: ${value.name}, value: ${value.value}, source: ${value.source}, config_id: ${value.config_id}`) + }); + + assert.strictEqual(values.length, 2) + assert.strictEqual(values[0].name, 'DD_RUNTIME_METRICS_ENABLED') + assert.strictEqual(values[0].value, 'true') + assert.strictEqual(values[0].source, 'local_stable_config') + + if (process.platform == 'linux') { + assert.strictEqual(configurator.get_config_local_path(process.platform), "/etc/datadog-agent/application_monitoring.yaml"); + } else if (process.platform == 'darwin') { + assert.strictEqual(configurator.get_config_local_path(process.platform), "/opt/datadog-agent/etc/application_monitoring.yaml"); + } else if (process.platform == 'win32') { + assert.strictEqual(configurator.get_config_local_path(process.platform), "C:\\ProgramData\\Datadog\\application_monitoring.yaml"); + } } + +test_host_wide(); +test_service_selector(); From a28dc930a1f493c9878b15509e5fe35eb0022b1d Mon Sep 17 00:00:00 2001 From: Baptiste Foy Date: Mon, 10 Feb 2025 13:59:10 +0100 Subject: [PATCH 3/4] fmt --- crates/library_config/src/lib.rs | 48 ++++++++++++++++---------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/crates/library_config/src/lib.rs b/crates/library_config/src/lib.rs index d2f3ca4..61c0712 100644 --- a/crates/library_config/src/lib.rs +++ b/crates/library_config/src/lib.rs @@ -19,7 +19,12 @@ pub struct ConfigEntry { impl ConfigEntry { #[wasm_bindgen(constructor)] pub fn new(name: String, value: String, source: String, config_id: String) -> ConfigEntry { - ConfigEntry { name, value, source, config_id } + ConfigEntry { + name, + value, + source, + config_id, + } } #[wasm_bindgen(getter)] pub fn name(&self) -> String { @@ -52,17 +57,13 @@ impl JsConfigurator { #[wasm_bindgen] pub fn set_envp(&mut self, envp: Box<[JsValue]>) -> Result<(), JsValue> { - self.envp = envp.iter() - .filter_map(|val| val.as_string()) - .collect(); + 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(); + self.args = args.iter().filter_map(|val| val.as_string()).collect(); Ok(()) } @@ -74,7 +75,10 @@ impl JsConfigurator { "darwin" => datadog_library_config::Target::Macos, _ => return Err(JsValue::from_str("Unsupported target")), }; - Ok(datadog_library_config::Configurator::local_stable_configuration_path(target_enum).to_string()) + Ok( + datadog_library_config::Configurator::local_stable_configuration_path(target_enum) + .to_string(), + ) } #[wasm_bindgen] @@ -85,7 +89,10 @@ impl JsConfigurator { "darwin" => datadog_library_config::Target::Macos, _ => return Err(JsValue::from_str("Unsupported target")), }; - Ok(datadog_library_config::Configurator::fleet_stable_configuration_path(target_enum).to_string()) + Ok( + datadog_library_config::Configurator::fleet_stable_configuration_path(target_enum) + .to_string(), + ) } #[wasm_bindgen] @@ -94,17 +101,9 @@ impl JsConfigurator { config_string_local: String, config_string_managed: String, ) -> Result, JsValue> { - let envp: Vec> = self - .envp - .iter() - .map(|s| s.as_bytes().to_vec()) - .collect(); + let envp: Vec> = self.envp.iter().map(|s| s.as_bytes().to_vec()).collect(); - let args: Vec> = self - .args - .iter() - .map(|s| s.as_bytes().to_vec()) - .collect(); + let args: Vec> = self.args.iter().map(|s| s.as_bytes().to_vec()).collect(); let res_config = self.configurator.get_config_from_bytes( config_string_local.as_bytes(), @@ -118,16 +117,17 @@ impl JsConfigurator { match res_config { Ok(config) => { - let config_entries: Vec = config.into_iter().map(|c| { - ConfigEntry { + let config_entries: Vec = config + .into_iter() + .map(|c| ConfigEntry { name: c.name.to_str().into(), value: c.value, source: c.source.to_str().into(), config_id: c.config_id.unwrap_or_default(), - } - }).collect(); + }) + .collect(); Ok(config_entries) - }, + } Err(e) => Err(JsValue::from_str(&format!( "Failed to get configuration: {:?}", e From bb62ba88223b5e8c663c8ee28600be4dfb7d731a Mon Sep 17 00:00:00 2001 From: Baptiste Foy Date: Mon, 10 Feb 2025 16:59:20 +0100 Subject: [PATCH 4/4] rev -> tag --- Cargo.lock | 2 +- crates/library_config/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 826ae3d..211bf24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -396,7 +396,7 @@ dependencies = [ [[package]] name = "datadog-library-config" version = "0.0.1" -source = "git+https://github.com/DataDog/libdatadog.git?rev=v16.0.2#52bd068269da43fb216e278a8576184bf6307177" +source = "git+https://github.com/DataDog/libdatadog.git?tag=v16.0.2#52bd068269da43fb216e278a8576184bf6307177" dependencies = [ "anyhow", "serde", diff --git a/crates/library_config/Cargo.toml b/crates/library_config/Cargo.toml index db03c1f..459ace0 100644 --- a/crates/library_config/Cargo.toml +++ b/crates/library_config/Cargo.toml @@ -8,7 +8,7 @@ crate-type = ["cdylib", "rlib"] [dependencies] anyhow = "1" -datadog-library-config = { git = "https://github.com/DataDog/libdatadog.git", rev = "v16.0.2" } +datadog-library-config = { git = "https://github.com/DataDog/libdatadog.git", tag = "v16.0.2" } wasm-bindgen = "0.2.84" serde = { version = "1.0", features = ["derive"] }