Skip to content

Commit

Permalink
Implement span limits on span processor (#3881)
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py authored Dec 18, 2024
1 parent 5a4bdde commit 858cacd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
2 changes: 0 additions & 2 deletions sentry_sdk/integrations/arq.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import sys

from opentelemetry.trace.status import StatusCode

import sentry_sdk
from sentry_sdk.consts import OP, SPANSTATUS
from sentry_sdk.integrations import DidNotEnable, Integration
Expand Down
7 changes: 6 additions & 1 deletion sentry_sdk/integrations/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@
# Note: Ignoring by logger name here is better than mucking with thread-locals.
# We do not necessarily know whether thread-locals work 100% correctly in the user's environment.
_IGNORED_LOGGERS = set(
["sentry_sdk.errors", "urllib3.connectionpool", "urllib3.connection", "opentelemetry.*"]
[
"sentry_sdk.errors",
"urllib3.connectionpool",
"urllib3.connection",
"opentelemetry.*",
]
)


Expand Down
19 changes: 18 additions & 1 deletion sentry_sdk/integrations/opentelemetry/span_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
from sentry_sdk._types import Event


DEFAULT_MAX_SPANS = 1000


class SentrySpanProcessor(SpanProcessor):
"""
Converts OTel spans into Sentry spans so they can be sent to the Sentry backend.
Expand Down Expand Up @@ -79,7 +82,7 @@ def on_end(self, span):
# if have a root span ending, we build a transaction and send it
self._flush_root_span(span)
else:
self._children_spans[span.parent.span_id].append(span)
self._append_child_span(span)

# TODO-neel-potel not sure we need a clear like JS
def shutdown(self):
Expand Down Expand Up @@ -150,6 +153,20 @@ def _flush_root_span(self, span):

sentry_sdk.capture_event(transaction_event)

def _append_child_span(self, span):
# type: (ReadableSpan) -> None
if not span.parent:
return

max_spans = (
sentry_sdk.get_client().options["_experiments"].get("max_spans")
or DEFAULT_MAX_SPANS
)

children_spans = self._children_spans[span.parent.span_id]
if len(children_spans) < max_spans:
children_spans.append(span)

def _collect_children(self, span):
# type: (ReadableSpan) -> List[ReadableSpan]
if not span.context:
Expand Down
19 changes: 19 additions & 0 deletions tests/tracing/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@
from sentry_sdk.utils import Dsn


def test_span_trimming(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0, _experiments={"max_spans": 3})
events = capture_events()

with start_span(name="hi"):
for i in range(10):
with start_span(op="foo{}".format(i)):
pass

(event,) = events

assert len(event["spans"]) == 3

span1, span2, span3 = event["spans"]
assert span1["op"] == "foo0"
assert span2["op"] == "foo1"
assert span3["op"] == "foo2"


def test_transaction_naming(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()
Expand Down

0 comments on commit 858cacd

Please sign in to comment.