Skip to content

Commit

Permalink
chore: remove time module functions from ddtrace.internal.compat (#11799
Browse files Browse the repository at this point in the history
)

Looks like these functions were added to support Python < 3.7 and we no
longer support those.

## Checklist
- [x] PR author has checked that all the criteria below are met
- The PR description includes an overview of the change
- The PR description articulates the motivation for the change
- The change includes tests OR the PR description describes a testing
strategy
- The PR description notes risks associated with the change, if any
- Newly-added code is easy to change
- The change follows the [library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
- The change includes or references documentation updates if necessary
- Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist
- [x] Reviewer has checked that all the criteria below are met 
- Title is accurate
- All changes are related to the pull request's stated goal
- Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- Testing strategy adequately addresses listed risks
- Newly-added code is easy to change
- Release note makes sense to a user of the library
- If necessary, author has acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment
- Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
  • Loading branch information
taegyunkim authored Dec 20, 2024
1 parent 55c8dd0 commit 6ea56c5
Show file tree
Hide file tree
Showing 23 changed files with 80 additions and 297 deletions.
3 changes: 2 additions & 1 deletion benchmarks/rate_limiter/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class RateLimiter(bm.Scenario):
num_windows: int

def run(self):
from ddtrace.internal.compat import time_ns
from time import time_ns

from ddtrace.internal.rate_limiter import RateLimiter

rate_limiter = RateLimiter(rate_limit=self.rate_limit, time_window=self.time_window)
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/_trace/span.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import math
import pprint
import sys
from time import time_ns
import traceback
from types import TracebackType
from typing import Any
Expand Down Expand Up @@ -46,7 +47,6 @@
from ddtrace.internal.compat import StringIO
from ddtrace.internal.compat import ensure_text
from ddtrace.internal.compat import is_integer
from ddtrace.internal.compat import time_ns
from ddtrace.internal.constants import MAX_UINT_64BITS as _MAX_UINT_64BITS
from ddtrace.internal.constants import SPAN_API_DATADOG
from ddtrace.internal.logger import get_logger
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/contrib/internal/botocore/services/kinesis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime
import json
from time import time_ns
from typing import Any
from typing import Dict
from typing import List
Expand All @@ -12,7 +13,6 @@
from ddtrace.contrib.trace_utils import ext_service
from ddtrace.ext import SpanTypes
from ddtrace.internal import core
from ddtrace.internal.compat import time_ns
from ddtrace.internal.logger import get_logger
from ddtrace.internal.schema import schematize_cloud_messaging_operation
from ddtrace.internal.schema import schematize_service_name
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/contrib/internal/kafka/patch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
from time import time_ns

import confluent_kafka

Expand All @@ -12,7 +13,6 @@
from ddtrace.ext import SpanTypes
from ddtrace.ext import kafka as kafkax
from ddtrace.internal import core
from ddtrace.internal.compat import time_ns
from ddtrace.internal.constants import COMPONENT
from ddtrace.internal.constants import MESSAGING_SYSTEM
from ddtrace.internal.logger import get_logger
Expand Down
6 changes: 3 additions & 3 deletions ddtrace/debugging/_debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pathlib import Path
import sys
import threading
import time
from types import CodeType
from types import FunctionType
from types import ModuleType
Expand Down Expand Up @@ -43,7 +44,6 @@
from ddtrace.debugging._signal.model import SignalState
from ddtrace.debugging._uploader import LogsIntakeUploaderV1
from ddtrace.debugging._uploader import UploaderProduct
from ddtrace.internal import compat
from ddtrace.internal.logger import get_logger
from ddtrace.internal.metrics import Metrics
from ddtrace.internal.module import ModuleHookType
Expand Down Expand Up @@ -202,11 +202,11 @@ def _open_signals(self) -> None:
signals.append(signal)

# Save state on the wrapping context
self.set("start_time", compat.monotonic_ns())
self.set("start_time", time.monotonic_ns())
self.set("signals", signals)

def _close_signals(self, retval=None, exc_info=(None, None, None)) -> None:
end_time = compat.monotonic_ns()
end_time = time.monotonic_ns()
signals = cast(Deque[Signal], self.get("signals"))
while signals:
# Open probe signals are ordered, with those that have created new
Expand Down
6 changes: 3 additions & 3 deletions ddtrace/debugging/_origin/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from itertools import count
from pathlib import Path
import sys
import time

# from threading import current_thread
from types import FrameType
Expand All @@ -24,7 +25,6 @@
# from ddtrace.debugging._signal.snapshot import Snapshot
from ddtrace.debugging._signal.model import Signal
from ddtrace.ext import EXIT_SPAN_TYPES
from ddtrace.internal import compat
from ddtrace.internal import core
from ddtrace.internal.packages import is_user_code
from ddtrace.internal.safety import _isinstance
Expand Down Expand Up @@ -170,7 +170,7 @@ def __enter__(self):
# span.set_tag_str("_dd.code_origin.frames.0.snapshot_id", snapshot.uuid)

# self.set("context", context)
# self.set("start_time", compat.monotonic_ns())
# self.set("start_time", time.monotonic_ns())

return self

Expand All @@ -181,7 +181,7 @@ def _close_signal(self, retval=None, exc_info=(None, None, None)):
# No snapshot was created
return

signal.do_exit(retval, exc_info, compat.monotonic_ns() - self.get("start_time"))
signal.do_exit(retval, exc_info, time.monotonic_ns() - self.get("start_time"))

def __return__(self, retval):
self._close_signal(retval=retval)
Expand Down
35 changes: 0 additions & 35 deletions ddtrace/internal/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,41 +122,6 @@ def is_integer(obj):
return isinstance(obj, int) and not isinstance(obj, bool)


try:
from time import time_ns
except ImportError:
from time import time as _time

def time_ns():
# type: () -> int
return int(_time() * 10e5) * 1000


try:
from time import monotonic
except ImportError:
from ddtrace.vendor.monotonic import monotonic


try:
from time import monotonic_ns
except ImportError:

def monotonic_ns():
# type: () -> int
return int(monotonic() * 1e9)


try:
from time import process_time_ns
except ImportError:
from time import clock as _process_time # type: ignore[attr-defined]

def process_time_ns():
# type: () -> int
return int(_process_time() * 1e9)


main_thread = threading.main_thread()


Expand Down
2 changes: 1 addition & 1 deletion ddtrace/internal/opentelemetry/span.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from time import time_ns
import traceback
from typing import TYPE_CHECKING

Expand All @@ -15,7 +16,6 @@
from ddtrace.constants import ERROR_STACK
from ddtrace.constants import ERROR_TYPE
from ddtrace.constants import SPAN_KIND
from ddtrace.internal.compat import time_ns
from ddtrace.internal.logger import get_logger
from ddtrace.internal.utils.formats import flatten_key_value
from ddtrace.internal.utils.formats import is_sequence
Expand Down
11 changes: 5 additions & 6 deletions ddtrace/internal/rate_limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
from dataclasses import field
import random
import threading
import time
from typing import Any # noqa:F401
from typing import Callable # noqa:F401
from typing import Optional # noqa:F401

from ddtrace.internal.utils.deprecations import DDTraceDeprecationWarning
from ddtrace.vendor.debtcollector import deprecate

from ..internal import compat


class RateLimiter(object):
"""
Expand Down Expand Up @@ -49,7 +48,7 @@ def __init__(self, rate_limit: int, time_window: float = 1e9):
self.tokens = rate_limit # type: float
self.max_tokens = rate_limit

self.last_update_ns = compat.monotonic_ns()
self.last_update_ns = time.monotonic_ns()

self.current_window_ns = 0 # type: float
self.tokens_allowed = 0
Expand Down Expand Up @@ -77,7 +76,7 @@ def is_allowed(self, timestamp_ns: Optional[int] = None) -> bool:

# rate limits are tested and mocked in pytest so we need to compute the timestamp here
# (or move the unit tests to rust)
timestamp_ns = timestamp_ns or compat.monotonic_ns()
timestamp_ns = timestamp_ns or time.monotonic_ns()
allowed = self._is_allowed(timestamp_ns)
# Update counts used to determine effective rate
self._update_rate_counts(allowed, timestamp_ns)
Expand Down Expand Up @@ -213,7 +212,7 @@ class BudgetRateLimiterWithJitter:
call_once: bool = False
budget: float = field(init=False)
max_budget: float = field(init=False)
last_time: float = field(init=False, default_factory=compat.monotonic)
last_time: float = field(init=False, default_factory=time.monotonic)
_lock: threading.Lock = field(init=False, default_factory=threading.Lock)

def __post_init__(self):
Expand All @@ -229,7 +228,7 @@ def limit(self, f: Optional[Callable[..., Any]] = None, *args: Any, **kwargs: An
"""Make rate-limited calls to a function with the given arguments."""
should_call = False
with self._lock:
now = compat.monotonic()
now = time.monotonic()
self.budget += self.limit_rate * (now - self.last_time) * (0.5 + random.random()) # jitter
should_call = self.budget >= 1.0
if self.budget > self.max_budget:
Expand Down
14 changes: 7 additions & 7 deletions ddtrace/internal/utils/time.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from datetime import datetime
import time
from types import TracebackType
from typing import Optional
from typing import Type # noqa:F401

from ddtrace.internal import compat
from ddtrace.internal.logger import get_logger


Expand Down Expand Up @@ -46,7 +46,7 @@ def __init__(self) -> None:
def start(self):
# type: () -> StopWatch
"""Starts the watch."""
self._started_at = compat.monotonic()
self._started_at = time.monotonic()
return self

def elapsed(self) -> float:
Expand All @@ -59,7 +59,7 @@ def elapsed(self) -> float:
if self._started_at is None:
raise RuntimeError("Can not get the elapsed time of a stopwatch" " if it has not been started/stopped")
if self._stopped_at is None:
now = compat.monotonic()
now = time.monotonic()
else:
now = self._stopped_at
return now - self._started_at
Expand All @@ -81,15 +81,15 @@ def stop(self):
"""Stops the watch."""
if self._started_at is None:
raise RuntimeError("Can not stop a stopwatch that has not been" " started")
self._stopped_at = compat.monotonic()
self._stopped_at = time.monotonic()
return self


class HourGlass(object):
"""An implementation of an hourglass."""

def __init__(self, duration: float) -> None:
t = compat.monotonic()
t = time.monotonic()

self._duration = duration
self._started_at = t - duration
Expand All @@ -99,7 +99,7 @@ def __init__(self, duration: float) -> None:

def turn(self) -> None:
"""Turn the hourglass."""
t = compat.monotonic()
t = time.monotonic()
top_0 = self._end_at - self._started_at
bottom = self._duration - top_0 + min(t - self._started_at, top_0)

Expand All @@ -119,7 +119,7 @@ def _trickled(self):

def _trickling(self):
# type: () -> bool
if compat.monotonic() < self._end_at:
if time.monotonic() < self._end_at:
return True

# No longer trickling, so we change state
Expand Down
8 changes: 4 additions & 4 deletions ddtrace/profiling/collector/_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import abc
import os.path
import sys
import time
import types
import typing

import wrapt

from ddtrace._trace.tracer import Tracer
from ddtrace.internal import compat
from ddtrace.internal.datadog.profiling import ddup
from ddtrace.internal.logger import get_logger
from ddtrace.profiling import _threading
Expand Down Expand Up @@ -117,12 +117,12 @@ def _acquire(self, inner_func, *args, **kwargs):
if not self._self_capture_sampler.capture():
return inner_func(*args, **kwargs)

start = compat.monotonic_ns()
start = time.monotonic_ns()
try:
return inner_func(*args, **kwargs)
finally:
try:
end = self._self_acquired_at = compat.monotonic_ns()
end = self._self_acquired_at = time.monotonic_ns()
thread_id, thread_name = _current_thread()
task_id, task_name, task_frame = _task.get_task(thread_id)
self._maybe_update_self_name()
Expand Down Expand Up @@ -185,7 +185,7 @@ def _release(self, inner_func, *args, **kwargs):
try:
if hasattr(self, "_self_acquired_at"):
try:
end = compat.monotonic_ns()
end = time.monotonic_ns()
thread_id, thread_name = _current_thread()
task_id, task_name, task_frame = _task.get_task(thread_id)
lock_name = (
Expand Down
4 changes: 2 additions & 2 deletions ddtrace/profiling/collector/memalloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from math import ceil
import os
import threading
import time
import typing # noqa:F401
from typing import Optional

Expand All @@ -12,7 +13,6 @@
except ImportError:
_memalloc = None # type: ignore[assignment]

from ddtrace.internal import compat
from ddtrace.internal.datadog.profiling import ddup
from ddtrace.profiling import _threading
from ddtrace.profiling import collector
Expand Down Expand Up @@ -189,7 +189,7 @@ def collect(self):
if thread_id in thread_id_ignore_set:
continue
handle = ddup.SampleHandle()
handle.push_monotonic_ns(compat.monotonic_ns())
handle.push_monotonic_ns(time.monotonic_ns())
handle.push_alloc(int((ceil(size) * alloc_count) / count), count) # Roundup to help float precision
handle.push_threadinfo(
thread_id, _threading.get_thread_native_id(thread_id), _threading.get_thread_name(thread_id)
Expand Down
Loading

0 comments on commit 6ea56c5

Please sign in to comment.