Skip to content
Merged
11 changes: 8 additions & 3 deletions sentry_sdk/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,11 @@ def __init__(
self._default_active_thread_id = get_current_thread_id() or 0 # type: int
self.active_thread_id = None # type: Optional[int]

self.start_ns = 0 # type: int
try:
self.start_ns = transaction._start_timestamp_monotonic_ns # type: int
except AttributeError:
self.start_ns = 0

self.stop_ns = 0 # type: int
self.active = False # type: bool

Expand Down Expand Up @@ -524,7 +528,8 @@ def start(self):
assert self.scheduler, "No scheduler specified"
logger.debug("[Profiling] Starting profile")
self.active = True
self.start_ns = nanosecond_time()
if not self.start_ns:
self.start_ns = nanosecond_time()
self.scheduler.start_profiling(self)

def stop(self):
Expand Down Expand Up @@ -643,7 +648,7 @@ def to_json(self, event_opt, options):
"platform": "python",
"profile": profile,
"release": event_opt.get("release", ""),
"timestamp": event_opt["timestamp"],
"timestamp": event_opt["start_timestamp"],
"version": "1",
"device": {
"architecture": platform.machine(),
Expand Down
17 changes: 7 additions & 10 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import uuid
import random
import time

from datetime import datetime, timedelta

import sentry_sdk
from sentry_sdk.consts import INSTRUMENTER
from sentry_sdk.utils import logger
from sentry_sdk.utils import logger, nanosecond_time
from sentry_sdk._types import MYPY


Expand Down Expand Up @@ -87,7 +86,7 @@ class Span(object):
"op",
"description",
"start_timestamp",
"_start_timestamp_monotonic",
"_start_timestamp_monotonic_ns",
"status",
"timestamp",
"_tags",
Expand Down Expand Up @@ -142,11 +141,9 @@ def __init__(
self._containing_transaction = containing_transaction
self.start_timestamp = start_timestamp or datetime.utcnow()
try:
# TODO: For Python 3.7+, we could use a clock with ns resolution:
# self._start_timestamp_monotonic = time.perf_counter_ns()

# Python 3.3+
self._start_timestamp_monotonic = time.perf_counter()
# profiling depends on this value and requires that
# it is measured in nanoseconds
self._start_timestamp_monotonic_ns = nanosecond_time()
except AttributeError:
pass

Expand Down Expand Up @@ -483,9 +480,9 @@ def finish(self, hub=None, end_timestamp=None):
if end_timestamp:
self.timestamp = end_timestamp
else:
duration_seconds = time.perf_counter() - self._start_timestamp_monotonic
elapsed = nanosecond_time() - self._start_timestamp_monotonic_ns
self.timestamp = self.start_timestamp + timedelta(
seconds=duration_seconds
microseconds=elapsed / 1000
)
except AttributeError:
self.timestamp = datetime.utcnow()
Expand Down
2 changes: 0 additions & 2 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1173,12 +1173,10 @@ def nanosecond_time():

def nanosecond_time():
# type: () -> int

return int(time.perf_counter() * 1e9)

else:

def nanosecond_time():
# type: () -> int

raise AttributeError