Skip to content

Commit 0879c2c

Browse files
chore(profiling): improve typing in memalloc.py (#15500)
## Description https://datadoghq.atlassian.net/browse/PROF-13197 This improves typing in `memalloc.py`.
1 parent 395edf5 commit 0879c2c

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

ddtrace/profiling/collector/memalloc.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from typing import Type
1010
from typing import cast
1111

12+
from typing_extensions import Self
13+
1214

1315
try:
1416
from ddtrace.profiling.collector import _memalloc
@@ -55,8 +57,9 @@ def start(self) -> None:
5557
_memalloc.stop()
5658
_memalloc.start(self.max_nframe, self.heap_sample_size)
5759

58-
def __enter__(self) -> None:
60+
def __enter__(self) -> Self:
5961
self.start()
62+
return self
6063

6164
def __exit__(
6265
self,
@@ -66,7 +69,7 @@ def __exit__(
6669
) -> None:
6770
self.stop()
6871

69-
def join(self) -> None:
72+
def join(self, timeout: Optional[float] = None) -> None:
7073
pass
7174

7275
def stop(self) -> None:
@@ -76,7 +79,8 @@ def stop(self) -> None:
7679
except RuntimeError:
7780
LOG.debug("Failed to stop memalloc profiling on shutdown", exc_info=True)
7881

79-
def _get_thread_id_ignore_set(self) -> Set[int]:
82+
@staticmethod
83+
def _get_thread_id_ignore_set() -> Set[int]:
8084
# This method is not perfect and prone to race condition in theory, but very little in practice.
8185
# Anyhow it's not a big deal — it's a best effort feature.
8286
return {
@@ -86,7 +90,8 @@ def _get_thread_id_ignore_set(self) -> Set[int]:
8690
}
8791

8892
def snapshot(self) -> None:
89-
thread_id_ignore_set = self._get_thread_id_ignore_set()
93+
"""Take a snapshot of collected data, to be exported."""
94+
thread_id_ignore_set = MemoryCollector._get_thread_id_ignore_set()
9095

9196
try:
9297
if _memalloc is None:

ddtrace/profiling/profiler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ def __init__(
142142
self.endpoint_collection_enabled: bool = endpoint_collection_enabled
143143

144144
# Non-user-supplied values
145-
self._collectors: List[collector.Collector] = []
145+
# Note: memalloc.MemoryCollector is not a subclass of collector.Collector, so we need to use a union type.
146+
# This is because its snapshot method cannot be static.
147+
self._collectors: List[collector.Collector | memalloc.MemoryCollector] = []
146148
self._collectors_on_import: Optional[List[tuple[str, Callable[[Any], None]]]] = None
147149
self._scheduler: Optional[Union[scheduler.Scheduler, scheduler.ServerlessScheduler]] = None
148150
self._lambda_function_name: Optional[str] = os.environ.get("AWS_LAMBDA_FUNCTION_NAME")
@@ -251,7 +253,7 @@ def start_collector(collector_class: Type[collector.Collector]) -> None:
251253
ModuleWatchdog.register_module_hook(module, hook)
252254

253255
if self._memory_collector_enabled:
254-
self._collectors.append(memalloc.MemoryCollector()) # type: ignore[arg-type]
256+
self._collectors.append(memalloc.MemoryCollector())
255257

256258
self._build_default_exporters()
257259

0 commit comments

Comments
 (0)