|
| 1 | +use datadog_library_config::{Configurator, ProcessInfo}; |
| 2 | +use pyo3::exceptions::PyException; |
| 3 | +use pyo3::prelude::*; |
| 4 | +use pyo3::types::PyDict; |
| 5 | + |
| 6 | +#[pyclass(name = "PyConfigurator", module = "ddtrace.internal._native")] |
| 7 | +pub struct PyConfigurator { |
| 8 | + configurator: Box<Configurator>, |
| 9 | + local_file: String, |
| 10 | + fleet_file: String, |
| 11 | +} |
| 12 | + |
| 13 | +#[pymethods] |
| 14 | +impl PyConfigurator { |
| 15 | + #[new] |
| 16 | + pub fn new(debug_logs: bool) -> Self { |
| 17 | + PyConfigurator { |
| 18 | + configurator: Box::new(Configurator::new(debug_logs)), |
| 19 | + fleet_file: Configurator::FLEET_STABLE_CONFIGURATION_PATH.to_string(), |
| 20 | + local_file: Configurator::LOCAL_STABLE_CONFIGURATION_PATH.to_string(), |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + pub fn set_local_file_override(&mut self, file: String) -> PyResult<()> { |
| 25 | + self.local_file = file; |
| 26 | + Ok(()) |
| 27 | + } |
| 28 | + |
| 29 | + pub fn get_configuration(&self, py: Python<'_>) -> PyResult<PyObject> { |
| 30 | + let res_config = self |
| 31 | + .configurator |
| 32 | + .get_config_from_file( |
| 33 | + self.local_file.as_ref(), |
| 34 | + self.fleet_file.as_ref(), |
| 35 | + ProcessInfo::detect_global("python".to_string()), |
| 36 | + ); |
| 37 | + match res_config { |
| 38 | + Ok(config) => { |
| 39 | + let dict = PyDict::new_bound(py); |
| 40 | + for c in config.iter() { |
| 41 | + let key = c.name.to_str().to_owned(); |
| 42 | + let _ = dict.set_item(key, c.value.clone()); |
| 43 | + } |
| 44 | + Ok(dict.into()) |
| 45 | + } |
| 46 | + Err(e) => { |
| 47 | + let err_msg = format!("Failed to get configuration: {:?}", e); |
| 48 | + Err(PyException::new_err(err_msg)) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments