Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(internal): remove time functions from compat module #10861

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
from time import monotonic_ns
from types import FunctionType
from types import ModuleType
from types import TracebackType
Expand Down Expand Up @@ -55,7 +56,6 @@
from ddtrace.debugging._uploader import LogsIntakeUploaderV1
from ddtrace.debugging._uploader import UploaderProduct
from ddtrace.internal import atexit
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 @@ -229,11 +229,11 @@ def _open_contexts(self) -> None:
contexts.append(self._collector.attach(signal))

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

def _close_contexts(self, retval=None, exc_info=(None, None, None)) -> None:
end_time = compat.monotonic_ns()
end_time = monotonic_ns()
contexts = self.get("contexts")
while contexts:
# Open probe signal contexts are ordered, with those that have
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
10 changes: 5 additions & 5 deletions ddtrace/internal/rate_limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +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
from ..internal.constants import DEFAULT_SAMPLING_RATE_LIMIT


Expand Down Expand Up @@ -50,7 +50,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 @@ -82,7 +82,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 @@ -218,7 +218,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 @@ -234,7 +234,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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to do the same here to spare some compute cycles

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


Expand All @@ -11,7 +12,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 @@ -186,7 +186,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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

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
12 changes: 6 additions & 6 deletions ddtrace/profiling/collector/stack.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ from itertools import chain
import logging
import sys
import typing
import time

from ddtrace.internal._unpatched import _threading as ddtrace_threading
from ddtrace._trace import context
from ddtrace._trace import span as ddspan
from ddtrace._trace.tracer import Tracer
from ddtrace.internal import compat
from ddtrace.internal._threads import periodic_threads
from ddtrace.internal.datadog.profiling import ddup
from ddtrace.internal.datadog.profiling import stack_v2
Expand Down Expand Up @@ -131,10 +131,10 @@ ELSE:
cdef stdint.int64_t _last_process_time

def __init__(self):
self._last_process_time = compat.process_time_ns()
self._last_process_time = time.process_time_ns()

def __call__(self, pthread_ids):
current_process_time = compat.process_time_ns()
current_process_time = time.process_time_ns()
cpu_time = current_process_time - self._last_process_time
self._last_process_time = current_process_time
# Spread the consumed CPU time on all threads.
Expand Down Expand Up @@ -520,7 +520,7 @@ class StackCollector(collector.PeriodicCollector):
def _init(self):
# type: (...) -> None
self._thread_time = _ThreadTime()
self._last_wall_time = compat.monotonic_ns()
self._last_wall_time = time.monotonic_ns()
if self.tracer is not None:
self._thread_span_links = _ThreadSpanLinks()
self.tracer.context_provider._on_activate(self._thread_span_links.link_span)
Expand Down Expand Up @@ -563,7 +563,7 @@ class StackCollector(collector.PeriodicCollector):

def collect(self):
# Compute wall time
now = compat.monotonic_ns()
now = time.monotonic_ns()
wall_time = now - self._last_wall_time
self._last_wall_time = now
all_events = []
Expand All @@ -581,7 +581,7 @@ class StackCollector(collector.PeriodicCollector):
now_ns=now,
)

used_wall_time_ns = compat.monotonic_ns() - now
used_wall_time_ns = time.monotonic_ns() - now
self.interval = self._compute_new_interval(used_wall_time_ns)

if self._stack_collector_v2_enabled:
Expand Down
4 changes: 2 additions & 2 deletions ddtrace/profiling/event.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from collections import namedtuple
import time
import typing

from ddtrace._trace import span as ddspan # noqa:F401
from ddtrace.internal import compat


_T = typing.TypeVar("_T")
Expand All @@ -16,7 +16,7 @@ class Event(object):

__slots__ = ("timestamp",)

def __init__(self, timestamp=compat.time_ns()):
def __init__(self, timestamp=time.time_ns()):
taegyunkim marked this conversation as resolved.
Show resolved Hide resolved
# type: (typing.Optional[int]) -> None
self.timestamp = timestamp

Expand Down
Loading
Loading