Skip to content

Commit a6973de

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

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

ddtrace/profiling/scheduler.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- encoding: utf-8 -*-
22
import logging
33
import time
4-
from typing import Any # noqa F401
4+
from typing import Any
55
from typing import Callable
66
from typing import Optional
77

@@ -20,27 +20,25 @@ class Scheduler(periodic.PeriodicService):
2020

2121
def __init__(
2222
self,
23-
before_flush: Optional[Callable] = None,
23+
before_flush: Optional[Callable[[], None]] = None,
2424
tracer: Optional[Tracer] = ddtrace.tracer,
2525
interval: float = config.upload_interval,
26-
):
26+
) -> None:
2727
super(Scheduler, self).__init__(interval=interval)
28-
self.before_flush: Optional[Callable] = before_flush
28+
self.before_flush: Optional[Callable[[], None]] = before_flush
2929
self._configured_interval: float = self.interval
3030
self._last_export: int = 0 # Overridden in _start_service
31-
self._tracer = tracer
31+
self._tracer: Optional[Tracer] = tracer
3232
self._enable_code_provenance: bool = config.code_provenance
3333

34-
def _start_service(self):
35-
# type: (...) -> None
34+
def _start_service(self) -> None:
3635
"""Start the scheduler."""
3736
LOG.debug("Starting scheduler")
3837
super(Scheduler, self)._start_service()
3938
self._last_export = time.time_ns()
4039
LOG.debug("Scheduler started")
4140

42-
def flush(self):
43-
# type: (...) -> None
41+
def flush(self) -> None:
4442
"""Flush events from recorder to exporters."""
4543
LOG.debug("Flushing events")
4644
if self.before_flush is not None:
@@ -53,8 +51,7 @@ def flush(self):
5351

5452
self._last_export = time.time_ns()
5553

56-
def periodic(self):
57-
# type: (...) -> None
54+
def periodic(self) -> None:
5855
start_time = time.monotonic()
5956
try:
6057
self.flush()
@@ -67,22 +64,20 @@ class ServerlessScheduler(Scheduler):
6764
6865
The idea with this scheduler is to not sleep 60s, but to sleep 1s and flush out profiles after 60 sleeping period.
6966
As the service can be frozen a few seconds after flushing out a profile, we want to make sure the next flush is not
70-
> 60s later, but after at least 60 periods of 1s.
67+
> 60s later, but after at least 60 periods of 1s.
7168
7269
"""
7370

7471
# We force this interval everywhere
7572
FORCED_INTERVAL = 1.0
7673
FLUSH_AFTER_INTERVALS = 60.0
7774

78-
def __init__(self, *args, **kwargs):
79-
# type: (*Any, **Any) -> None
75+
def __init__(self, *args: Any, **kwargs: Any) -> None:
8076
kwargs.setdefault("interval", self.FORCED_INTERVAL)
8177
super(ServerlessScheduler, self).__init__(*args, **kwargs)
8278
self._profiled_intervals: int = 0
8379

84-
def periodic(self):
85-
# type: (...) -> None
80+
def periodic(self) -> None:
8681
# Check both the number of intervals and time frame to be sure we don't flush, e.g., empty profiles
8782
if self._profiled_intervals >= self.FLUSH_AFTER_INTERVALS and (time.time_ns() - self._last_export) >= (
8883
self.FORCED_INTERVAL * self.FLUSH_AFTER_INTERVALS

0 commit comments

Comments
 (0)