Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] feat(libdatadog): Add library-config WASM module #44

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
349 changes: 349 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
default-members = [
"crates/crashtracker"
"crates/crashtracker",
"crates/library-config"
]
members = [
"crates/*"
Expand Down
24 changes: 24 additions & 0 deletions crates/library-config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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-ffi = { git = "https://github.com/DataDog/libdatadog.git", tag = "v14.3.1" }
datadog-library-config-ffi = { path = "../../../libdatadog/library-config-ffi" }

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"
100 changes: 100 additions & 0 deletions crates/library-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use wasm_bindgen::prelude::*;
use datadog_library_config_ffi::slice;

#[wasm_bindgen]
pub struct JsConfigurator {
configurator: Box<datadog_library_config_ffi::static_config::Configurator>,
envp: Vec<String>,
args: Vec<String>,
}

#[wasm_bindgen]
impl JsConfigurator {
#[wasm_bindgen(constructor)]
pub fn new(debug_logs: bool) -> Self {
JsConfigurator {
configurator: datadog_library_config_ffi::ddog_library_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<JsValue, JsValue> {
let envp_slices: Vec<slice::CharSlice> = self
.envp
.iter()
.map(|s| slice::CharSlice::from(s.as_str()))
.collect();
let envp_slice = slice::Slice::from(&envp_slices);

let args_slices: Vec<slice::CharSlice> = self
.args
.iter()
.map(|s| slice::CharSlice::from(s.as_str()))
.collect();
let args_slice = slice::Slice::from(&args_slices);

let res_config = self.configurator.get_configuration_from_bytes(
datadog_library_config_ffi::static_config::ProcessInfo {
envp: envp_slice,
args: args_slice,
language: slice::CharSlice::from("nodejs"),
},
slice::CharSlice::from(config_string.as_str()),
);

match res_config {
Ok(config) => {
let mut hashmap = std::collections::HashMap::new();
config.iter().for_each(|c| {
let key = String::from_utf8_lossy(c.name.to_env_name().to_bytes()).to_string();
let value: String;
match &c.value {
datadog_library_config_ffi::static_config::LibraryConfigValue::StrVal(
v,
) => {
value = String::from_utf8_lossy(v.as_cstr().into_std().to_bytes())
.to_string();
}
datadog_library_config_ffi::static_config::LibraryConfigValue::NumVal(
v,
) => {
value = v.to_string();
}
datadog_library_config_ffi::static_config::LibraryConfigValue::BoolVal(
v,
) => {
value = v.to_string();
}
}
hashmap.insert(key, value);
});
Ok(serde_wasm_bindgen::to_value(&hashmap)?)
}
Err(e) => Err(JsValue::from_str(&format!(
"Failed to get configuration: {:?}",
e
))),
}
}
}
1 change: 1 addition & 0 deletions test/library-config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pkg
10 changes: 10 additions & 0 deletions test/library-config/README.md
Original file line number Diff line number Diff line change
@@ -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
```
8 changes: 8 additions & 0 deletions test/library-config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
rules:
- selectors:
- origin: language
matches:
- nodejs
operator: equals
configuration:
DD_SERVICE: my-service
16 changes: 16 additions & 0 deletions test/library-config/index.js
Original file line number Diff line number Diff line change
@@ -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}`)
});
10 changes: 10 additions & 0 deletions test/library-config/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
Loading