Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
BaptisteFoy committed Jan 23, 2025
1 parent 1575326 commit b42da4d
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 17 deletions.
7 changes: 2 additions & 5 deletions ddtrace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion ddtrace/internal/native/__init__.py
Original file line number Diff line number Diff line change
@@ -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()
5 changes: 3 additions & 2 deletions ddtrace/internal/native/_native.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import List, Dict

class DDSketch:
def __init__(self): ...
Expand All @@ -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]: ...
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
),
],
)
2 changes: 1 addition & 1 deletion src/native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
16 changes: 11 additions & 5 deletions src/native/library_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use pyo3::types::PyDict;
#[pyclass(name = "PyConfigurator", module = "ddtrace.internal._native")]
pub struct PyConfigurator {
configurator: Box<Configurator>,
file: String,
envp: Vec<String>,
args: Vec<String>,
}
Expand All @@ -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(),
}
Expand All @@ -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<PyObject> {
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();
Expand All @@ -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);
Expand Down
41 changes: 41 additions & 0 deletions tests/internal/test_native.py
Original file line number Diff line number Diff line change
@@ -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 == {}
1 change: 1 addition & 0 deletions tests/suitespec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b42da4d

Please sign in to comment.