8000 fix: Better memory usage for tracing by untitaker · Pull Request #431 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content

fix: Better memory usage for tracing #431

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

Merged
merged 5 commits into from
Jul 15, 2019
Merged
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
7 changes: 6 additions & 1 deletion sentry_sdk/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,9 @@ def start_span(
sample_rate = client and client.options["traces_sample_rate"] or 0
span.sampled = random.random() < sample_rate

if span.sampled:
span.init_finished_spans()

return span

def finish_span(
Expand Down Expand Up @@ -517,7 +520,9 @@ def finish_span(
"contexts": {"trace": span.get_trace_context()},
"timestamp": span.timestamp,
"start_timestamp": span.start_timestamp,
"spans": [s.to_json() for s in span._finished_spans if s is not span],
"spans": [
s.to_json() for s in (span._finished_spans or ()) if s is not span
],
}
)

Expand Down
29 changes: 18 additions & 11 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

from sentry_sdk.utils import capture_internal_exceptions, concat_strings
from sentry_sdk._compat import PY2
from sentry_sdk._types import MYPY

if PY2:
from collections import Mapping
else:
from collections.abc import Mapping

if False:
if MYPY:
import typing

from typing import Optional
Expand Down Expand Up @@ -76,15 +77,16 @@ class Span(object):

def __init__(
self,
trace_id=None,
span_id=None,
parent_span_id=None,
same_process_as_parent=True,
sampled=None,
transaction=None,
op=None,
description=None,
trace_id=None, # type: Optional[str]
span_id=None, # type: Optional[str]
parent_span_id=None, # type: Optional[str]
same_process_as_parent=True, # type: bool
sampled=None, # type: Optional[bool]
transaction=None, # type: Optional[str]
op=None, # type: Optional[str]
description=None, # type: Optional[str]
):
# type: (...) -> None
self.trace_id = trace_id or uuid.uuid4().hex
self.span_id = span_id or uuid.uuid4().hex[16:]
self.parent_span_id = parent_span_id
Expand All @@ -95,12 +97,16 @@ def __init__(
self.description = description
self._tags = {} # type: Dict[str, str]
self._data = {} # type: Dict[str, Any]
self._finished_spans = [] # type: List[Span]
self._finished_spans = None # type: Optional[List[Span]]
self.start_timestamp = datetime.now()

#: End timestamp of span
self.timestamp = None

def init_finished_spans(self):
if self._finished_spans is None:
self._finished_spans = []

def __repr__(self):
return (
"<%s(transaction=%r, trace_id=%r, span_id=%r, parent_span_id=%r, sampled=%r)>"
Expand Down Expand Up @@ -184,7 +190,8 @@ def set_data(self, key, value):

def finish(self):
self.timestamp = datetime.now()
self._finished_spans.append(self)
if self._finished_spans is not None:
self._finished_spans.append(self)

def to_json(self):
return {
Expand Down
34 changes: 34 additions & 0 deletions tests/test_tracing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import weakref
import gc

import pytest

from sentry_sdk import Hub, capture_message
Expand Down Expand Up @@ -93,3 +96,34 @@ def test_sampling_decided_only_for_transactions(sentry_init, capture_events):

with Hub.current.span() as span:
assert span.sampled is None


@pytest.mark.parametrize(
"args,expected_refcount",
[({"traces_sample_rate": 1.0}, 100), ({"traces_sample_rate": 0.0}, 0)],
)
def test_memory_usage(sentry_init, capture_events, args, expected_refcount):
sentry_init(**args)

references = weakref.WeakSet()

with Hub.current.span(transaction="hi"):
for i in range(100):
with Hub.current.span(
op="helloworld", description="hi {}".format(i)
) as span:

def foo():
pass

references.add(foo)
span.set_tag("foo", foo)
pass

del foo
del span

# required only for pypy (cpython frees immediately)
gc.collect()

assert len(references) == expected_refcount
0