Skip to content

Commit

Permalink
feat(libconfig): Add ddog_library_configurator_get_from_bytes method
Browse files Browse the repository at this point in the history
  • Loading branch information
BaptisteFoy committed Dec 31, 2024
1 parent 8ffe755 commit ac91fd7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
35 changes: 14 additions & 21 deletions library-config-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,26 @@ pub extern "C" fn ddog_library_configurator_get<'a>(
configurator: &'a Configurator,
process_info: ProcessInfo<'a>,
) -> ffi::Result<ffi::Vec<LibraryConfig>> {
if configurator.debug_logs {
eprintln!("Called library_config_common_component:");
eprintln!("\tconfigurator: {:?}", configurator);
let args: Vec<String> = process_info
.args
.iter()
.map(|arg| arg.to_string())
.collect();
eprintln!("\tprocess args: {:?}", args);
// TODO: this is for testing purpose, we don't want to log env variables
let envs: Vec<String> = process_info
.envp
.iter()
.map(|env| env.to_string())
.collect();
eprintln!("\tprocess envs: {:?}", envs);
eprintln!(
"\tprocess language: {:?}",
process_info.language.to_string()
);
}
configurator.log_process_info(&process_info);
configurator
.get_configuration(process_info)
.map(ffi::Vec::from_std)
.into()
}

#[no_mangle]
pub extern "C" fn ddog_library_configurator_get_from_bytes<'a>(
configurator: &'a Configurator,
process_info: ProcessInfo<'a>,
config_bytes: ffi::CharSlice<'a>,
) -> ffi::Result<ffi::Vec<LibraryConfig>> {
configurator.log_process_info(&process_info);
configurator
.get_configuration_from_bytes(process_info, config_bytes)
.map(ffi::Vec::from_std)
.into()
}

#[no_mangle]
pub extern "C" fn ddog_library_config_name_to_env(name: LibraryConfigName) -> ffi::CStr<'static> {
ffi::CStr::from_std(name.to_env_name())
Expand Down
47 changes: 47 additions & 0 deletions library-config-ffi/src/static_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,53 @@ impl Configurator {
Ok(library_config)
}

pub fn get_configuration_from_bytes(
&self,
process_info: ProcessInfo<'_>,
config_bytes: ffi::CharSlice<'_>,
) -> anyhow::Result<Vec<LibraryConfig>> {
let static_config = serde_yaml::from_str(config_bytes.to_string().as_str())?;
if self.debug_logs {
eprintln!("Read the following static config: {static_config:?}");
}

let Some(configs) = find_static_config(&static_config, &process_info) else {
if self.debug_logs {
eprintln!("No selector matched");
}
return Ok(Vec::new());
};
let library_config = template_configs(configs, &process_info)?;
if self.debug_logs {
eprintln!("Will apply the following configuration: {library_config:?}");
}
Ok(library_config)
}

pub fn log_process_info(&self, process_info: &ProcessInfo) {
if self.debug_logs {
eprintln!("Called library_config_common_component:");
eprintln!("\tconfigurator: {:?}", self);
let args: Vec<String> = process_info
.args
.iter()
.map(|arg| arg.to_string())
.collect();
eprintln!("\tprocess args: {:?}", args);
// TODO: this is for testing purpose, we don't want to log env variables
let envs: Vec<String> = process_info
.envp
.iter()
.map(|env| env.to_string())
.collect();
eprintln!("\tprocess envs: {:?}", envs);
eprintln!(
"\tprocess language: {:?}",
process_info.language.to_string()
);
}
}

fn parse_static_config(&self) -> anyhow::Result<StaticConfig> {
let mut f = match std::fs::File::open(&self.static_config_file_path) {
Ok(f) => f,
Expand Down

0 comments on commit ac91fd7

Please sign in to comment.