Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions ddtrace/profiling/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ddtrace import config
from ddtrace.internal import atexit
from ddtrace.internal import forksafe
from ddtrace.internal import process_tags
from ddtrace.internal import service
from ddtrace.internal import uwsgi
from ddtrace.internal.datadog.profiling import ddup
Expand Down Expand Up @@ -168,6 +169,10 @@ def _build_default_exporters(self):
profiler_config = config_str(profiling_config)
self.tags.update({"profiler_config": profiler_config})

# add process_tags
if p_tags := process_tags.process_tags:
self.tags.update({"process_tags": p_tags})

endpoint_call_counter_span_processor = self.tracer._endpoint_call_counter_span_processor
if self.endpoint_collection_enabled:
endpoint_call_counter_span_processor.enable()
Expand Down
23 changes: 23 additions & 0 deletions tests/profiling/test_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import pytest

import ddtrace
from ddtrace.internal.settings._config import config
from ddtrace.profiling import collector
from ddtrace.profiling import profiler
from ddtrace.profiling import scheduler
from ddtrace.profiling.collector import asyncio
from ddtrace.profiling.collector import stack
from ddtrace.profiling.collector import threading
from tests.utils import process_tag_reload


def test_status():
Expand Down Expand Up @@ -145,3 +147,24 @@ def test_profiler_serverless(monkeypatch):
p = profiler.Profiler()
assert isinstance(p._scheduler, scheduler.ServerlessScheduler)
assert p.tags["functionname"] == "foobar"


def test_process_tags_deactivated():
p = profiler.Profiler(tags={})
assert "process_tags" not in p.tags


def test_process_tags_activated():
# type: (...) -> None
try:
config._process_tags_enabled = True
process_tag_reload()

# Pass explicit tags dict to avoid mutating shared profiling_config.tags
p = profiler.Profiler(tags={})

# Verify that process_tags are in the profiler tags
assert "process_tags" in p.tags
finally:
config._process_tags_enabled = False
process_tag_reload()
44 changes: 44 additions & 0 deletions tests/profiling_v2/exporter/test_ddup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,47 @@ def test_tags_propagated():
# Profiler could add tags, so check that tags is a superset of config.tags
for k, v in config.tags.items():
assert tags[k] == v


@pytest.mark.subprocess()
def test_process_tags_deactivated():
import sys
from unittest import mock

sys.modules["ddtrace.internal.datadog.profiling.ddup"] = mock.Mock()

from ddtrace.profiling.profiler import Profiler # noqa: I001
from ddtrace.internal.datadog.profiling import ddup

# When Profiler is instantiated and libdd is enabled, it should call ddup.config
Profiler()

ddup.config.assert_called()

tags = ddup.config.call_args.kwargs["tags"]

assert "process_tags" not in tags


@pytest.mark.subprocess(
env=dict(
DD_EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED="true",
)
)
def test_process_tags_activated():
import sys
from unittest import mock

sys.modules["ddtrace.internal.datadog.profiling.ddup"] = mock.Mock()

from ddtrace.profiling.profiler import Profiler # noqa: I001
from ddtrace.internal.datadog.profiling import ddup

# When Profiler is instantiated and libdd is enabled, it should call ddup.config
Profiler()

ddup.config.assert_called()

tags = ddup.config.call_args.kwargs["tags"]

assert "process_tags" in tags
Loading