diff --git a/ddtrace/__init__.py b/ddtrace/__init__.py index 45369d12cea..738caf20e7f 100644 --- a/ddtrace/__init__.py +++ b/ddtrace/__init__.py @@ -6,12 +6,9 @@ LOADED_MODULES = frozenset(sys.modules.keys()) # Configuration for the whole tracer from file. Do it before anything else happens. -from ddtrace.internal.native import PyConfigurator +from ddtrace.internal.native import get_configuration_from_disk -configurator = PyConfigurator(debug_logs=False) -configurator.set_envp(["%s=%s" % (k, v) for k, v in os.environ.items()]) -configurator.set_args(sys.argv) -for key, value in configurator.get_configuration().items(): +for key, value in get_configuration_from_disk().items(): os.environ[key] = str(value).lower() from ddtrace.internal.module import ModuleWatchdog diff --git a/ddtrace/internal/native/__init__.py b/ddtrace/internal/native/__init__.py index b2d320fa563..35af5561034 100644 --- a/ddtrace/internal/native/__init__.py +++ b/ddtrace/internal/native/__init__.py @@ -1,2 +1,23 @@ +import os +import sys +from typing import Dict + from ._native import DDSketch # noqa: F401 -from ._native import PyConfigurator # noqa: F401 +from ._native import PyConfigurator + + +def get_configuration_from_disk(debug_logs: bool = False, file_override="") -> Dict[str, str]: + """ + Retrieves the tracer configuration from disk. Calls the PyConfigurator object + to read the configuration from the disk using the libdatadog shared library + and returns the corresponding configuration + """ + configurator = PyConfigurator(debug_logs) + configurator.set_envp(["%s=%s" % (k, v) for k, v in os.environ.items()]) + configurator.set_args(sys.argv) + + # Set the file override if provided. Only used for testing purposes. + if file_override: + configurator.set_file_override(file_override) + + return configurator.get_configuration() diff --git a/ddtrace/internal/native/_native.pyi b/ddtrace/internal/native/_native.pyi index 2fe0367db97..9c228431731 100644 --- a/ddtrace/internal/native/_native.pyi +++ b/ddtrace/internal/native/_native.pyi @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Dict class DDSketch: def __init__(self): ... @@ -11,4 +11,5 @@ class PyConfigurator: def __init__(self, debug_logs: bool): ... def set_envp(self, envp: List[str]) -> None: ... def set_args(self, args: List[str]) -> None: ... - def get_configuration(self) -> dict: ... + def set_file_override(self, file: str) -> None: ... + def get_configuration(self) -> Dict[str, str]: ... diff --git a/setup.py b/setup.py index 155a363149d..89adf166b74 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,6 @@ import cmake from setuptools_rust import Binding from setuptools_rust import RustExtension -from setuptools_rust import Strip from setuptools import Extension, find_packages, setup # isort: skip @@ -666,7 +665,6 @@ def get_exts_for(name): py_limited_api="auto", binding=Binding.PyO3, debug=os.getenv("_DD_RUSTC_DEBUG") == "1", - strip=Strip.No if os.getenv("_DD_RUSTC_DEBUG") == "1" else Strip.All, ), ], ) diff --git a/src/native/Cargo.lock b/src/native/Cargo.lock index 4e0386d092c..451deabea78 100644 --- a/src/native/Cargo.lock +++ b/src/native/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "anyhow" diff --git a/src/native/Cargo.toml b/src/native/Cargo.toml index 245b320e54b..1a4aaa48611 100644 --- a/src/native/Cargo.toml +++ b/src/native/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" [profile.release] lto = true strip = "debuginfo" -opt-level = 3 +opt-level = 3 # Optimize for performance [dependencies] pyo3 = { version = "0.22.3", features = ["extension-module"] } diff --git a/src/native/library_config.rs b/src/native/library_config.rs index bc6af307cd1..e3d3209893f 100644 --- a/src/native/library_config.rs +++ b/src/native/library_config.rs @@ -6,6 +6,7 @@ use pyo3::types::PyDict; #[pyclass(name = "PyConfigurator", module = "ddtrace.internal._native")] pub struct PyConfigurator { configurator: Box, + file: String, envp: Vec, args: Vec, } @@ -16,6 +17,8 @@ impl PyConfigurator { pub fn new(debug_logs: bool) -> Self { PyConfigurator { configurator: Box::new(Configurator::new(debug_logs)), + file: "/etc/datadog-agent/managed/datadog-apm-libraries/stable/libraries_config.yaml" + .to_string(), // TODO: Use const from libdatadog (PR pending) envp: Vec::new(), args: Vec::new(), } @@ -31,6 +34,11 @@ impl PyConfigurator { Ok(()) } + pub fn set_file_override(&mut self, file: String) -> PyResult<()> { + self.file = file; + Ok(()) + } + pub fn get_configuration(&self, py: Python<'_>) -> PyResult { let envp: Vec<&[u8]> = self.envp.iter().map(|s| s.as_bytes()).collect(); let args: Vec<&[u8]> = self.args.iter().map(|s| s.as_bytes()).collect(); @@ -41,11 +49,9 @@ impl PyConfigurator { language: b"python", }; - let res_config = self.configurator.get_config_from_file( - "/etc/datadog-agent/managed/datadog-apm-libraries/stable/libraries_config.yaml" - .as_ref(), - process_info, - ); + let res_config = self + .configurator + .get_config_from_file(self.file.as_ref(), process_info); match res_config { Ok(config) => { let dict = PyDict::new_bound(py); diff --git a/tests/internal/test_native.py b/tests/internal/test_native.py new file mode 100644 index 00000000000..a00260a7ba4 --- /dev/null +++ b/tests/internal/test_native.py @@ -0,0 +1,41 @@ +from ddtrace.internal.native import get_configuration_from_disk + + +def test_get_configuration_from_disk(tmp_path): + # First test -- config matches & should be returned + config_1 = tmp_path / "config_1.yaml" + config_1.write_text( + """ +rules: + - selectors: + - origin: language + matches: + - python + operator: equals + configuration: + DD_SERVICE: my-service +""", + encoding="utf-8", + ) + + config = get_configuration_from_disk(file_override=str(config_1)) + assert config == {"DD_SERVICE": "my-service"} + + # Second test -- config does not match & should not be returned + config_2 = tmp_path / "config_2.yaml" + config_2.write_text( + """ +rules: + - selectors: + - origin: language + matches: + - nodejs + operator: equals + configuration: + DD_SERVICE: my-service +""", + encoding="utf-8", + ) + + config = get_configuration_from_disk(file_override=str(config_2)) + assert config == {} diff --git a/tests/suitespec.yml b/tests/suitespec.yml index 96062f1202b..b135ba986c8 100644 --- a/tests/suitespec.yml +++ b/tests/suitespec.yml @@ -62,6 +62,7 @@ components: - ddtrace/internal/logger.py - ddtrace/internal/metrics.py - ddtrace/internal/module.py + - ddtrace/internal/native/* - ddtrace/internal/packages.py - ddtrace/internal/third-party.tar.gz - ddtrace/internal/periodic.py