Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ddtrace/debugging/_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ddtrace.debugging._signal.log import LogSignal
from ddtrace.debugging._signal.snapshot import Snapshot
from ddtrace.internal import forksafe
from ddtrace.internal import process_tags
from ddtrace.internal._encoding import BufferFull
from ddtrace.internal.logger import get_logger
from ddtrace.internal.utils.formats import format_trace_id
Expand Down Expand Up @@ -113,6 +114,9 @@ def _build_log_track_payload(
"timestamp": int(signal.timestamp * 1e3), # milliseconds,
}

if p_tags := process_tags.process_tags:
payload["process_tags"] = p_tags

# Add the correlation IDs if available
if context is not None and context.trace_id is not None:
payload["dd"] = {
Expand Down
67 changes: 67 additions & 0 deletions tests/debugging/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,73 @@ def test_batch_json_encoder():
assert queue.count == 0


def test_process_tags_are_not_included_by_default():
s = Snapshot(
probe=create_snapshot_line_probe(probe_id="batch-test", source_file="foo.py", line=42),
frame=inspect.currentframe(),
thread=threading.current_thread(),
)
buffer_size = 30 * (1 << 20)
queue = SignalQueue(encoder=LogSignalJsonEncoder(None), buffer_size=buffer_size)

s.line({})

queue = SignalQueue(encoder=LogSignalJsonEncoder("test-service"))
queue.put(s)
data = queue.flush()
assert data is not None
payload, _ = data
decoded = json.loads(payload.decode())
assert "process_tags" not in decoded[0]


def test_process_tags_are_included():
from unittest.mock import patch

from ddtrace.internal.process_tags import _process_tag_reload
from ddtrace.internal.process_tags.constants import ENTRYPOINT_BASEDIR_TAG
from ddtrace.internal.process_tags.constants import ENTRYPOINT_NAME_TAG
from ddtrace.internal.process_tags.constants import ENTRYPOINT_TYPE_SCRIPT
from ddtrace.internal.process_tags.constants import ENTRYPOINT_TYPE_TAG
from ddtrace.internal.process_tags.constants import ENTRYPOINT_WORKDIR_TAG
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here it's enough to test that the field is set. Testing for the actual values should be done in a unit test for the process tags feature itself to avoid redundancy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 0972b8d

from ddtrace.settings._config import config

try:
with patch("sys.argv", ["/path/to/test_script.py"]), patch("os.getcwd", return_value="/path/to/workdir"):
config._process_tags_enabled = True
_process_tag_reload()
s = Snapshot(
probe=create_snapshot_line_probe(probe_id="batch-test", source_file="foo.py", line=42),
frame=inspect.currentframe(),
thread=threading.current_thread(),
)
buffer_size = 30 * (1 << 20)
queue = SignalQueue(encoder=LogSignalJsonEncoder(None), buffer_size=buffer_size)

s.line({})

queue = SignalQueue(encoder=LogSignalJsonEncoder("test-service"))
queue.put(s)
data = queue.flush()
assert data is not None
payload, _ = data
decoded = json.loads(payload.decode())

assert "process_tags" in decoded[0]

expected_raw = (
f"{ENTRYPOINT_BASEDIR_TAG}:to,"
f"{ENTRYPOINT_NAME_TAG}:test_script,"
f"{ENTRYPOINT_TYPE_TAG}:{ENTRYPOINT_TYPE_SCRIPT},"
f"{ENTRYPOINT_WORKDIR_TAG}:workdir"
)

assert decoded[0]["process_tags"] == expected_raw
finally:
config._process_tags_enabled = False
_process_tag_reload()


def test_batch_flush_reencode():
s = Snapshot(
probe=create_snapshot_line_probe(probe_id="batch-test", source_file="foo.py", line=42),
Expand Down
Loading