Skip to content

Commit e44906e

Browse files
chore(profiling): improve typing in threading.py
1 parent a5d1649 commit e44906e

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

ddtrace/internal/datadog/profiling/stack_v2/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def link_span(span: Optional[Union[context.Context, ddspan.Span]]) -> None: ...
2020

2121
# Thread management
2222
def register_thread(python_thread_id: int, native_id: int, name: str) -> None: ...
23-
def unregister_thread(name: str) -> None: ...
23+
def unregister_thread(python_thread_id: int) -> None: ...
2424

2525
# Asyncio support
2626
def track_asyncio_loop(thread_id: int, loop: Optional[asyncio.AbstractEventLoop]) -> None: ...
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from sys import version_info
2-
from typing import Any # noqa:F401
2+
from typing import Any
33

44
from ddtrace.internal.logger import get_logger
55

@@ -8,8 +8,7 @@
88

99

1010
# 3.11 and above
11-
def _sanitize_string_check(value):
12-
# type: (Any) -> str
11+
def _sanitize_string_check(value: Any) -> str:
1312
if isinstance(value, str):
1413
return value
1514
elif value is None:
@@ -22,10 +21,9 @@ def _sanitize_string_check(value):
2221

2322

2423
# 3.10 and below (the noop version)
25-
def _sanitize_string_identity(value):
26-
# type: (Any) -> str
24+
def _sanitize_string_identity(value: Any) -> str:
2725
return value or ""
2826

2927

3028
# Assign based on version
31-
sanitize_string = _sanitize_string_check if version_info[:2] > (3, 10) else _sanitize_string_identity
29+
sanitize_string: object = _sanitize_string_check if version_info[:2] > (3, 10) else _sanitize_string_identity

ddtrace/profiling/collector/threading.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,24 @@ def _set_patch_target(
5151
# Also patch threading.Thread so echion can track thread lifetimes
5252
def init_stack_v2() -> None:
5353
if config.stack.enabled and stack_v2.is_available:
54-
_thread_set_native_id = ddtrace_threading.Thread._set_native_id # type: ignore[attr-defined]
55-
_thread_bootstrap_inner = ddtrace_threading.Thread._bootstrap_inner # type: ignore[attr-defined]
56-
57-
def thread_set_native_id(self, *args, **kwargs):
58-
_thread_set_native_id(self, *args, **kwargs)
59-
stack_v2.register_thread(self.ident, self.native_id, self.name)
60-
61-
def thread_bootstrap_inner(self, *args, **kwargs):
54+
_thread_set_native_id = typing.cast(
55+
typing.Callable[[threading.Thread], None],
56+
ddtrace_threading.Thread._set_native_id, # type: ignore[attr-defined]
57+
)
58+
_thread_bootstrap_inner = typing.cast(
59+
typing.Callable[[threading.Thread], None],
60+
ddtrace_threading.Thread._bootstrap_inner, # type: ignore[attr-defined]
61+
)
62+
63+
def thread_set_native_id(self: threading.Thread) -> None:
64+
_thread_set_native_id(self)
65+
if self.ident is not None and self.native_id is not None:
66+
stack_v2.register_thread(self.ident, self.native_id, self.name)
67+
68+
def thread_bootstrap_inner(self: threading.Thread, *args: typing.Any, **kwargs: typing.Any) -> None:
6269
_thread_bootstrap_inner(self, *args, **kwargs)
63-
stack_v2.unregister_thread(self.ident)
70+
if self.ident is not None:
71+
stack_v2.unregister_thread(self.ident)
6472

6573
ddtrace_threading.Thread._set_native_id = thread_set_native_id # type: ignore[attr-defined]
6674
ddtrace_threading.Thread._bootstrap_inner = thread_bootstrap_inner # type: ignore[attr-defined]

0 commit comments

Comments
 (0)