Skip to content

Commit 7413526

Browse files
chore(profiling): improve typing in profiler.py
1 parent ba60fe5 commit 7413526

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

ddtrace/profiling/profiler.py

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import logging
33
import os
44
from typing import Any
5+
from typing import Callable
56
from typing import Dict
6-
from typing import List # noqa:F401
7-
from typing import Optional # noqa:F401
8-
from typing import Type # noqa:F401
9-
from typing import Union # noqa:F401
7+
from typing import List
8+
from typing import Optional
9+
from typing import Type
10+
from typing import Union
1011

1112
import ddtrace
1213
from ddtrace import config
@@ -28,9 +29,6 @@
2829
from ddtrace.profiling.collector import stack
2930
from ddtrace.profiling.collector import threading
3031

31-
32-
# TODO(vlad): add type annotations
33-
3432
LOG = logging.getLogger(__name__)
3533

3634

@@ -42,10 +40,10 @@ class Profiler(object):
4240
4341
"""
4442

45-
def __init__(self, *args, **kwargs):
46-
self._profiler = _ProfilerInstance(*args, **kwargs)
43+
def __init__(self, *args: Any, **kwargs: Any) -> None:
44+
self._profiler: "_ProfilerInstance" = _ProfilerInstance(*args, **kwargs)
4745

48-
def start(self, stop_on_exit=True, profile_children=True):
46+
def start(self, stop_on_exit: bool = True, profile_children: bool = True) -> None:
4947
"""Start the profiler.
5048
5149
:param stop_on_exit: Whether to stop the profiler and flush the profile on exit.
@@ -75,7 +73,7 @@ def start(self, stop_on_exit=True, profile_children=True):
7573

7674
telemetry_writer.product_activated(TELEMETRY_APM_PRODUCT.PROFILER, True)
7775

78-
def stop(self, flush=True):
76+
def stop(self, flush: bool = True) -> None:
7977
"""Stop the profiler.
8078
8179
:param flush: Flush last profile.
@@ -88,7 +86,7 @@ def stop(self, flush=True):
8886
# Not a best practice, but for backward API compatibility that allowed to call `stop` multiple times.
8987
pass
9088

91-
def _restart_on_fork(self):
89+
def _restart_on_fork(self) -> None:
9290
# Be sure to stop the parent first, since it might have to e.g. unpatch functions
9391
# Do not flush data as we don't want to have multiple copies of the parent profile exported.
9492
try:
@@ -99,11 +97,7 @@ def _restart_on_fork(self):
9997
self._profiler = self._profiler.copy()
10098
self._profiler.start()
10199

102-
def __getattr__(
103-
self,
104-
key, # type: str
105-
):
106-
# type: (...) -> Any
100+
def __getattr__(self, key: str) -> Any:
107101
return getattr(self._profiler, key)
108102

109103

@@ -145,22 +139,22 @@ def __init__(
145139
self.endpoint_collection_enabled: bool = endpoint_collection_enabled
146140

147141
# Non-user-supplied values
148-
self._collectors: List[Union[stack.StackCollector, memalloc.MemoryCollector]] = []
149-
self._collectors_on_import: Any = None
142+
self._collectors: List[collector.Collector] = []
143+
self._collectors_on_import: Optional[List[tuple[str, Callable[[Any], None]]]] = None
150144
self._scheduler: Optional[Union[scheduler.Scheduler, scheduler.ServerlessScheduler]] = None
151145
self._lambda_function_name: Optional[str] = os.environ.get("AWS_LAMBDA_FUNCTION_NAME")
152146

153147
self.__post_init__()
154148

155-
def __eq__(self, other):
149+
def __eq__(self, other: Any) -> bool:
156150
for k, v in vars(self).items():
157151
if k.startswith("_") or k in self._COPY_IGNORE_ATTRIBUTES:
158152
continue
159153
if v != getattr(other, k, None):
160154
return False
161155
return True
162156

163-
def _build_default_exporters(self):
157+
def _build_default_exporters(self) -> None:
164158
if self._lambda_function_name is not None:
165159
self.tags.update({"functionname": self._lambda_function_name})
166160

@@ -185,8 +179,7 @@ def _build_default_exporters(self):
185179
)
186180
ddup.start()
187181

188-
def __post_init__(self):
189-
# type: (...) -> None
182+
def __post_init__(self) -> None:
190183

191184
if self._stack_collector_enabled:
192185
LOG.debug("Profiling collector (stack) enabled")
@@ -228,7 +221,7 @@ def start_collector(collector_class: Type) -> None:
228221

229222
if self._pytorch_collector_enabled:
230223

231-
def start_collector(collector_class: Type) -> None:
224+
def start_collector(collector_class: Type[collector.Collector]) -> None:
232225
with self._service_lock:
233226
col = collector_class()
234227

@@ -258,14 +251,16 @@ def start_collector(collector_class: Type) -> None:
258251

259252
self._build_default_exporters()
260253

261-
scheduler_class = scheduler.ServerlessScheduler if self._lambda_function_name else scheduler.Scheduler # type: (Type[Union[scheduler.Scheduler, scheduler.ServerlessScheduler]])
254+
scheduler_class: Type[Union[scheduler.Scheduler, scheduler.ServerlessScheduler]] = (
255+
scheduler.ServerlessScheduler if self._lambda_function_name else scheduler.Scheduler
256+
)
262257

263258
self._scheduler = scheduler_class(
264259
before_flush=self._collectors_snapshot,
265260
tracer=self.tracer,
266261
)
267262

268-
def _collectors_snapshot(self):
263+
def _collectors_snapshot(self) -> None:
269264
for c in self._collectors:
270265
try:
271266
c.snapshot()
@@ -274,7 +269,7 @@ def _collectors_snapshot(self):
274269

275270
_COPY_IGNORE_ATTRIBUTES = {"status"}
276271

277-
def copy(self):
272+
def copy(self) -> "_ProfilerInstance":
278273
return self.__class__(
279274
**{
280275
key: value
@@ -283,8 +278,7 @@ def copy(self):
283278
}
284279
)
285280

286-
def _start_service(self):
287-
# type: (...) -> None
281+
def _start_service(self) -> None:
288282
"""Start the profiler."""
289283
collectors = []
290284
for col in self._collectors:
@@ -301,8 +295,7 @@ def _start_service(self):
301295
if self._scheduler is not None:
302296
self._scheduler.start()
303297

304-
def _stop_service(self, flush=True, join=True):
305-
# type: (bool, bool) -> None
298+
def _stop_service(self, flush: bool = True, join: bool = True) -> None:
306299
"""Stop the profiler.
307300
308301
:param flush: Flush a last profile.

0 commit comments

Comments
 (0)