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 b1766fc
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 14 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
24 changes: 23 additions & 1 deletion ddtrace/internal/native/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
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]:
# type: (bool, str) -> 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]: ...
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,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.

4 changes: 2 additions & 2 deletions src/native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ version = "0.1.0"
edition = "2021"

[profile.release]
lto = true
lto = "fat" # lto = true breaks with libdatadog 15.0.0, so we use the next best thing
strip = "debuginfo"
opt-level = 3
opt-level = 3 # Optimize for performance

[dependencies]
pyo3 = { version = "0.22.3", features = ["extension-module"] }
Expand Down
10 changes: 8 additions & 2 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,7 @@ 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 @@ -30,6 +32,11 @@ impl PyConfigurator {
self.args = args;
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();
Expand All @@ -42,8 +49,7 @@ impl PyConfigurator {
};

let res_config = self.configurator.get_config_from_file(
"/etc/datadog-agent/managed/datadog-apm-libraries/stable/libraries_config.yaml"
.as_ref(),
self.file.as_ref(),
process_info,
);
match res_config {
Expand Down
34 changes: 34 additions & 0 deletions tests/internal/test_native.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 b1766fc

Please sign in to comment.