Skip to content

Commit dd79a0f

Browse files
Julien Danjoumergify[bot]Kyle-VerhoogP403n1x87
authored
feat(profiling): add DD_PROFILING_HEAP_ENABLED env var to enable heap profiler (#2580)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Kyle Verhoog <[email protected]> Co-authored-by: Gabriele N. Tornetta <[email protected]>
1 parent 7f11feb commit dd79a0f

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

ddtrace/profiling/collector/memalloc.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ class MemoryHeapSampleEvent(event.StackBasedEvent):
4848
"""The sampling size."""
4949

5050

51-
_ENABLED = False
52-
53-
5451
def _get_default_heap_sample_size(
5552
default_heap_sample_size=512 * 1024, # type: int
5653
):
@@ -59,7 +56,7 @@ def _get_default_heap_sample_size(
5956
if heap_sample_size is not None:
6057
return int(heap_sample_size)
6158

62-
if not _ENABLED:
59+
if not formats.asbool(os.environ.get("DD_PROFILING_HEAP_ENABLED", "0")):
6360
return 0
6461

6562
try:

docs/configuration.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ below:
121121
- Integer
122122
- 64
123123
- The maximum number of frames to capture in stack execution tracing.
124+
* - ``DD_PROFILING_HEAP_ENABLED``
125+
- Boolean
126+
- False
127+
- Whether to enable the heap memory profiler.
124128
* - ``DD_PROFILING_CAPTURE_PCT``
125129
- Float
126130
- 2
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
The Python heap profiler can now be enabled by setting the
5+
``DD_PROFILING_HEAP_ENABLED`` environment variable to ``1``.

tests/profiling/collector/test_memalloc.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,9 @@ def test_memalloc_speed(benchmark, heap_sample_size):
333333
(False, (lambda v: v == 0, lambda v: v == 0, lambda v: v == 0, lambda v: v == 0)),
334334
),
335335
)
336-
def test_memalloc_sample_size(enabled, predicates):
337-
restore = memalloc._ENABLED
338-
memalloc._ENABLED = enabled
339-
try:
340-
assert predicates[0](memalloc._get_default_heap_sample_size())
341-
assert predicates[1](memalloc._get_default_heap_sample_size(1))
342-
assert predicates[2](memalloc._get_default_heap_sample_size(512))
343-
assert predicates[3](memalloc._get_default_heap_sample_size(512 * 1024 * 1024))
344-
finally:
345-
memalloc._ENABLED = restore
336+
def test_memalloc_sample_size(enabled, predicates, monkeypatch):
337+
monkeypatch.setenv("DD_PROFILING_HEAP_ENABLED", str(enabled))
338+
assert predicates[0](memalloc._get_default_heap_sample_size())
339+
assert predicates[1](memalloc._get_default_heap_sample_size(1))
340+
assert predicates[2](memalloc._get_default_heap_sample_size(512))
341+
assert predicates[3](memalloc._get_default_heap_sample_size(512 * 1024 * 1024))

0 commit comments

Comments
 (0)