Skip to content
Merged
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
18 changes: 16 additions & 2 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import re
import uuid
import contextlib
import time

from datetime import datetime
from datetime import datetime, timedelta

import sentry_sdk

Expand Down Expand Up @@ -101,6 +102,7 @@ class Span(object):
"op",
"description",
"start_timestamp",
"_start_timestamp_monotonic",
"timestamp",
"_tags",
"_data",
Expand Down Expand Up @@ -134,6 +136,14 @@ def __init__(
self._tags = {} # type: Dict[str, str]
self._data = {} # type: Dict[str, Any]
self.start_timestamp = 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()
except AttributeError:
pass

#: End timestamp of span
self.timestamp = None # type: Optional[datetime]
Expand Down Expand Up @@ -309,7 +319,11 @@ def finish(self, hub=None):
# This transaction is already finished, so we should not flush it again.
return None

self.timestamp = datetime.utcnow()
try:
duration_seconds = time.perf_counter() - self._start_timestamp_monotonic
self.timestamp = self.start_timestamp + timedelta(seconds=duration_seconds)
except AttributeError:
self.timestamp = datetime.utcnow()

_maybe_create_breadcrumbs_from_span(hub, self)

Expand Down