From c0225043bdd78bffb8683114a1178e69900e805a Mon Sep 17 00:00:00 2001 From: Sutu Sebastian Date: Sun, 15 Nov 2026 14:09:46 +0200 Subject: [PATCH 1/2] bench: add five curated getsentry/sentry-python corpus cases Four real bug cases and one clean control from the Python SDK, every PR merged inside the 24mo window: - 4121 replaces `"params": record.args` with a generator over record.args, so a mapping logged against a %(name)s template ships its keys instead of its values. Reverted by e71ccbf19, which states the regression and adds two tests asserting the params are the dict. - 4133 swaps `if not logger.handlers:` for `logger.hasHandlers()`, so once the application configures the root logger the SDK never installs its own. Reverted by 5689bc09f. - 4281 reads channels.__version__ inside a try that only catches ImportError, so a shadowing `channels` module raises AttributeError out of init(); fixed by a35050694 (#5299), which splits the block and widens the handler. - 6168 writes SpanBatcher its own _flush_loop with two unguarded _flush calls, so one failed send ends the drain thread for the process lifetime; fixed by e17291eea. The pattern predates the PR in Batcher, disclosed in provenance.note. - 8119 is the control: a one-line guard on the third-party baggage items, exact because an empty field means there was nothing to append. Ground truth per case is a line the reviewed PR added and the later commit deletes or rewrites. None of the four kinds is in the published table, so each vocabulary is its kind plus keywords, every keyword unspelled by the case's own diff. Behaviour was reproduced by extracting the statement from both commits and running it in a harness (the SDK cannot be imported on this host); the probes are pinned in TestCheckedInCorpusKeywordsGradeBothWays. Corpus: 44 cases (8 seeded, 27 real, 9 control). --- .../case.json | 35 + .../full.diff | 17 + .../hunks.tsv | 2 + .../case.json | 33 + .../full.diff | 13 + .../hunks.tsv | 2 + .../case.json | 34 + .../full.diff | 204 ++++++ .../hunks.tsv | 12 + .../case.json | 35 + .../full.diff | 684 ++++++++++++++++++ .../hunks.tsv | 17 + .../case.json | 16 + .../full.diff | 63 ++ .../hunks.tsv | 6 + internal/core/corpus/keywords_test.go | 80 ++ 16 files changed, 1253 insertions(+) create mode 100644 bench/corpus/sentry-python-4121-logging-mapping-args/case.json create mode 100644 bench/corpus/sentry-python-4121-logging-mapping-args/full.diff create mode 100644 bench/corpus/sentry-python-4121-logging-mapping-args/hunks.tsv create mode 100644 bench/corpus/sentry-python-4133-debug-parent-logger/case.json create mode 100644 bench/corpus/sentry-python-4133-debug-parent-logger/full.diff create mode 100644 bench/corpus/sentry-python-4133-debug-parent-logger/hunks.tsv create mode 100644 bench/corpus/sentry-python-4281-threading-channels-version/case.json create mode 100644 bench/corpus/sentry-python-4281-threading-channels-version/full.diff create mode 100644 bench/corpus/sentry-python-4281-threading-channels-version/hunks.tsv create mode 100644 bench/corpus/sentry-python-6168-span-batcher-flush-thread/case.json create mode 100644 bench/corpus/sentry-python-6168-span-batcher-flush-thread/full.diff create mode 100644 bench/corpus/sentry-python-6168-span-batcher-flush-thread/hunks.tsv create mode 100644 bench/corpus/sentry-python-8119-clean-baggage-trailing-comma/case.json create mode 100644 bench/corpus/sentry-python-8119-clean-baggage-trailing-comma/full.diff create mode 100644 bench/corpus/sentry-python-8119-clean-baggage-trailing-comma/hunks.tsv diff --git a/bench/corpus/sentry-python-4121-logging-mapping-args/case.json b/bench/corpus/sentry-python-4121-logging-mapping-args/case.json new file mode 100644 index 000000000..d7d2d8edf --- /dev/null +++ b/bench/corpus/sentry-python-4121-logging-mapping-args/case.json @@ -0,0 +1,35 @@ +{ + "schema_version": "1", + "id": "sentry-python-4121-logging-mapping-args", + "kind": "real", + "language": "python", + "provenance": { + "kind": "real", + "repo": "getsentry/sentry-python", + "pr": 4121, + "base_sha": "42ad8df79815cc6113d4106ce19c32a195a18cfb", + "head_sha": "4c9731bbe68b6523cccec73fb764e04e61e441cb", + "fix_commit": "https://github.com/getsentry/sentry-python/commit/e71ccbf19f644fe7928db37f6e4a09e1febbc4e2", + "source": "PR #4121 \"Coerce None values into strings in logentry params\", squash-merged 2025-03-12; the fix commit e71ccbf19 reverts it and says why (fixes #4267)", + "note": "introduced by this PR: the base passes record.args through untouched, and the PR replaces it with a tuple built by iterating record.args, which flattens a mapping argument to its keys. The fix commit reverts the hunk on 2025-04-14 and states the consequence: 'Because we iterate over `record.args`, that change caused us to only send the dictionary's keys, not the values.' By then the statement had moved to logging.py:268-272 (identical text), so the revert deletes the five lines this diff adds at a shifted line number, restores `\"params\": record.args,`, and adds two tests asserting the params are the dict." + }, + "bugs": [ + { + "path": "sentry_sdk/integrations/logging.py", + "line": 252, + "kind": "mapping-args-sent-as-keys", + "severity": "important", + "keywords": [ + "only the mapping's keys reach the report", + "the values of a dictionary passed as arguments are lost", + "a mapping logged with percent placeholders keeps just its key names", + "dict arguments are flattened into their keys", + "the message renders with key names instead of the substituted values", + "mapping-style interpolation receives the key names", + "the interpolated parameters collapse to the dictionary keys" + ], + "note": "stdlib logging accepts a single mapping as record.args for %(name)s templates; the generator iterates it, so keys, not values, are sent. The generator also rewrites a None argument to the string \"None\", which was the PR's intent.", + "also_lines": [251] + } + ] +} diff --git a/bench/corpus/sentry-python-4121-logging-mapping-args/full.diff b/bench/corpus/sentry-python-4121-logging-mapping-args/full.diff new file mode 100644 index 000000000..823e97b1d --- /dev/null +++ b/bench/corpus/sentry-python-4121-logging-mapping-args/full.diff @@ -0,0 +1,17 @@ +diff --git a/sentry_sdk/integrations/logging.py b/sentry_sdk/integrations/logging.py +index b792510d6..28809de4a 100644 +--- a/sentry_sdk/integrations/logging.py ++++ b/sentry_sdk/integrations/logging.py +@@ -248,7 +248,11 @@ class EventHandler(_BaseHandler): + else: + event["logentry"] = { + "message": to_string(record.msg), +- "params": record.args, ++ "params": ( ++ tuple(str(arg) if arg is None else arg for arg in record.args) ++ if record.args ++ else () ++ ), + } + + event["extra"] = self._extra_from_record(record) diff --git a/bench/corpus/sentry-python-4121-logging-mapping-args/hunks.tsv b/bench/corpus/sentry-python-4121-logging-mapping-args/hunks.tsv new file mode 100644 index 000000000..a87e26b00 --- /dev/null +++ b/bench/corpus/sentry-python-4121-logging-mapping-args/hunks.tsv @@ -0,0 +1,2 @@ +sentry_sdk/integrations/logging.py OLD 248 254 +sentry_sdk/integrations/logging.py NEW 248 258 diff --git a/bench/corpus/sentry-python-4133-debug-parent-logger/case.json b/bench/corpus/sentry-python-4133-debug-parent-logger/case.json new file mode 100644 index 000000000..04c4dc258 --- /dev/null +++ b/bench/corpus/sentry-python-4133-debug-parent-logger/case.json @@ -0,0 +1,33 @@ +{ + "schema_version": "1", + "id": "sentry-python-4133-debug-parent-logger", + "kind": "real", + "language": "python", + "provenance": { + "kind": "real", + "repo": "getsentry/sentry-python", + "pr": 4133, + "base_sha": "4f51ff37a26b1e774b8050119da75074d1a1d5ed", + "head_sha": "37930840dcefba96e7708b19e461013a919e83a5", + "fix_commit": "https://github.com/getsentry/sentry-python/commit/5689bc09fd223f80f65290e2ccb685b8acb9a5f2", + "source": "PR #4133 \"fix(debug): Take into account parent handlers for debug logger\", squash-merged 2025-03-13; the fix commit 5689bc09 reverts it (fixes #4266)", + "note": "introduced by the one-line change: the SDK's own logger is configured only when no handler is attached anywhere in the hierarchy, so once an application configures the root logger the SDK never installs its own handler. The fix commit reverts to the attribute check on 2025-04-14: 'we should not consider parent loggers, since we only want the SDK to log debug messages when configured to do so via debug=True ... With 37930840d... a custom root logger configuration would also cause SDK logs to be emitted.' The revert names the emission direction; the converse is what the same line does to a host root level above DEBUG, where the SDK's own diagnostics disappear entirely." + }, + "bugs": [ + { + "path": "sentry_sdk/debug.py", + "line": 22, + "kind": "debug-output-hijacked", + "severity": "important", + "keywords": [ + "an application root configuration silently suppresses the diagnostics", + "the library's diagnostics end up in the host application's log stream instead of its own", + "the library prints nothing when the host has its own log configuration", + "a parent log setup decides whether anything is emitted", + "any existing root setup stops the library from installing its own output", + "a configured root makes the library skip its own setup" + ], + "note": "init_debug_support runs when the debug option is on; the inherited-handler walk makes it a no-op whenever the application has a root handler, so the SDK either emits nothing (host root level above DEBUG) or emits its diagnostics through the application's own handler and formatter. Both are observable; the keywords cover the suppression direction and the emission direction, and the revert cites the emission one." + } + ] +} diff --git a/bench/corpus/sentry-python-4133-debug-parent-logger/full.diff b/bench/corpus/sentry-python-4133-debug-parent-logger/full.diff new file mode 100644 index 000000000..f22713a0c --- /dev/null +++ b/bench/corpus/sentry-python-4133-debug-parent-logger/full.diff @@ -0,0 +1,13 @@ +diff --git a/sentry_sdk/debug.py b/sentry_sdk/debug.py +index e4c686a3e..f740d92de 100644 +--- a/sentry_sdk/debug.py ++++ b/sentry_sdk/debug.py +@@ -19,7 +19,7 @@ class _DebugFilter(logging.Filter): + + def init_debug_support(): + # type: () -> None +- if not logger.handlers: ++ if not logger.hasHandlers(): + configure_logger() + + diff --git a/bench/corpus/sentry-python-4133-debug-parent-logger/hunks.tsv b/bench/corpus/sentry-python-4133-debug-parent-logger/hunks.tsv new file mode 100644 index 000000000..c6353b6fe --- /dev/null +++ b/bench/corpus/sentry-python-4133-debug-parent-logger/hunks.tsv @@ -0,0 +1,2 @@ +sentry_sdk/debug.py OLD 19 25 +sentry_sdk/debug.py NEW 19 25 diff --git a/bench/corpus/sentry-python-4281-threading-channels-version/case.json b/bench/corpus/sentry-python-4281-threading-channels-version/case.json new file mode 100644 index 000000000..95d90c07c --- /dev/null +++ b/bench/corpus/sentry-python-4281-threading-channels-version/case.json @@ -0,0 +1,34 @@ +{ + "schema_version": "1", + "id": "sentry-python-4281-threading-channels-version", + "kind": "real", + "language": "python", + "provenance": { + "kind": "real", + "repo": "getsentry/sentry-python", + "pr": 4281, + "base_sha": "706d2d29e68848a3cb085f043287d908255344b5", + "head_sha": "2d392af3ea6da91ddbdde55d18e15c24dce6b59b", + "fix_commit": "https://github.com/getsentry/sentry-python/commit/a350506948ad12ffc6d564cef3985f0dd5435b20", + "source": "PR #4281 \"fix: Data leak in ThreadingIntegration between threads\", squash-merged 2025-04-15; fix commit a35050694 \"fix(threading): Handle channels shadowing (#5299)\", closes #5298", + "note": "introduced by this PR, which reads channels.__version__ inside a block whose only failure handler is ImportError, so a module named channels that has no __version__ raises AttributeError out of setup_once(). By the time of the fix the file had gained an unrelated constant and the block sits two lines lower; the two lines the fix deletes are the ones this diff adds." + }, + "bugs": [ + { + "path": "sentry_sdk/integrations/threading.py", + "line": 57, + "kind": "shadowed-module-crashes-setup", + "severity": "critical", + "keywords": [ + "a lookalike package without a version attribute aborts the initialization", + "the version read is assumed to exist on any module of that name", + "a shadowing module makes the attribute access raise instead of skipping", + "initialization crashes instead of disabling the integration", + "the guard only covers a missing package, so a missing attribute crashes setup", + "bootstrapping the client fails when a local module of that name has no version" + ], + "note": "ThreadingIntegration is a default integration and setup_integrations only catches DidNotEnable around its setup_once, so the AttributeError reaches sentry_sdk.init() and the application fails to start. The fix moves `import channels` and the version read into their own try whose handler is `(ImportError, AttributeError)`.", + "also_lines": [55, 58] + } + ] +} diff --git a/bench/corpus/sentry-python-4281-threading-channels-version/full.diff b/bench/corpus/sentry-python-4281-threading-channels-version/full.diff new file mode 100644 index 000000000..eb9368aa7 --- /dev/null +++ b/bench/corpus/sentry-python-4281-threading-channels-version/full.diff @@ -0,0 +1,204 @@ +diff --git a/sentry_sdk/integrations/threading.py b/sentry_sdk/integrations/threading.py +index 5de736e23..9c99a8e89 100644 +--- a/sentry_sdk/integrations/threading.py ++++ b/sentry_sdk/integrations/threading.py +@@ -1,4 +1,5 @@ + import sys ++import warnings + from functools import wraps + from threading import Thread, current_thread + +@@ -49,6 +50,15 @@ class ThreadingIntegration(Integration): + # type: () -> None + old_start = Thread.start + ++ try: ++ from django import VERSION as django_version # noqa: N811 ++ import channels # type: ignore[import-not-found] ++ ++ channels_version = channels.__version__ ++ except ImportError: ++ django_version = None ++ channels_version = None ++ + @wraps(old_start) + def sentry_start(self, *a, **kw): + # type: (Thread, *Any, **Any) -> Any +@@ -57,8 +67,27 @@ class ThreadingIntegration(Integration): + return old_start(self, *a, **kw) + + if integration.propagate_scope: +- isolation_scope = sentry_sdk.get_isolation_scope() +- current_scope = sentry_sdk.get_current_scope() ++ if ( ++ sys.version_info < (3, 9) ++ and channels_version is not None ++ and channels_version < "4.0.0" ++ and django_version is not None ++ and django_version >= (3, 0) ++ and django_version < (4, 0) ++ ): ++ warnings.warn( ++ "There is a known issue with Django channels 2.x and 3.x when using Python 3.8 or older. " ++ "(Async support is emulated using threads and some Sentry data may be leaked between those threads.) " ++ "Please either upgrade to Django channels 4.0+, use Django's async features " ++ "available in Django 3.1+ instead of Django channels, or upgrade to Python 3.9+.", ++ stacklevel=2, ++ ) ++ isolation_scope = sentry_sdk.get_isolation_scope() ++ current_scope = sentry_sdk.get_current_scope() ++ ++ else: ++ isolation_scope = sentry_sdk.get_isolation_scope().fork() ++ current_scope = sentry_sdk.get_current_scope().fork() + else: + isolation_scope = None + current_scope = None +diff --git a/tests/integrations/django/asgi/test_asgi.py b/tests/integrations/django/asgi/test_asgi.py +index 063aed63a..82eae30b1 100644 +--- a/tests/integrations/django/asgi/test_asgi.py ++++ b/tests/integrations/django/asgi/test_asgi.py +@@ -38,9 +38,25 @@ async def test_basic(sentry_init, capture_events, application): + + events = capture_events() + +- comm = HttpCommunicator(application, "GET", "/view-exc?test=query") +- response = await comm.get_response() +- await comm.wait() ++ import channels # type: ignore[import-not-found] ++ ++ if ( ++ sys.version_info < (3, 9) ++ and channels.__version__ < "4.0.0" ++ and django.VERSION >= (3, 0) ++ and django.VERSION < (4, 0) ++ ): ++ # We emit a UserWarning for channels 2.x and 3.x on Python 3.8 and older ++ # because the async support was not really good back then and there is a known issue. ++ # See the TreadingIntegration for details. ++ with pytest.warns(UserWarning): ++ comm = HttpCommunicator(application, "GET", "/view-exc?test=query") ++ response = await comm.get_response() ++ await comm.wait() ++ else: ++ comm = HttpCommunicator(application, "GET", "/view-exc?test=query") ++ response = await comm.get_response() ++ await comm.wait() + + assert response["status"] == 500 + +diff --git a/tests/integrations/threading/test_threading.py b/tests/integrations/threading/test_threading.py +index 0d14fae35..4395891d6 100644 +--- a/tests/integrations/threading/test_threading.py ++++ b/tests/integrations/threading/test_threading.py +@@ -1,5 +1,6 @@ + import gc + from concurrent import futures ++from textwrap import dedent + from threading import Thread + + import pytest +@@ -172,3 +173,103 @@ def test_wrapper_attributes(sentry_init): + assert Thread.run.__qualname__ == original_run.__qualname__ + assert t.run.__name__ == "run" + assert t.run.__qualname__ == original_run.__qualname__ ++ ++ ++@pytest.mark.parametrize( ++ "propagate_scope", ++ (True, False), ++ ids=["propagate_scope=True", "propagate_scope=False"], ++) ++def test_scope_data_not_leaked_in_threads(sentry_init, propagate_scope): ++ sentry_init( ++ integrations=[ThreadingIntegration(propagate_scope=propagate_scope)], ++ ) ++ ++ sentry_sdk.set_tag("initial_tag", "initial_value") ++ initial_iso_scope = sentry_sdk.get_isolation_scope() ++ ++ def do_some_work(): ++ # check if we have the initial scope data propagated into the thread ++ if propagate_scope: ++ assert sentry_sdk.get_isolation_scope()._tags == { ++ "initial_tag": "initial_value" ++ } ++ else: ++ assert sentry_sdk.get_isolation_scope()._tags == {} ++ ++ # change data in isolation scope in thread ++ sentry_sdk.set_tag("thread_tag", "thread_value") ++ ++ t = Thread(target=do_some_work) ++ t.start() ++ t.join() ++ ++ # check if the initial scope data is not modified by the started thread ++ assert initial_iso_scope._tags == { ++ "initial_tag": "initial_value" ++ }, "The isolation scope in the main thread should not be modified by the started thread." ++ ++ ++@pytest.mark.parametrize( ++ "propagate_scope", ++ (True, False), ++ ids=["propagate_scope=True", "propagate_scope=False"], ++) ++def test_spans_from_multiple_threads( ++ sentry_init, capture_events, render_span_tree, propagate_scope ++): ++ sentry_init( ++ traces_sample_rate=1.0, ++ integrations=[ThreadingIntegration(propagate_scope=propagate_scope)], ++ ) ++ events = capture_events() ++ ++ def do_some_work(number): ++ with sentry_sdk.start_span( ++ op=f"inner-run-{number}", name=f"Thread: child-{number}" ++ ): ++ pass ++ ++ threads = [] ++ ++ with sentry_sdk.start_transaction(op="outer-trx"): ++ for number in range(5): ++ with sentry_sdk.start_span( ++ op=f"outer-submit-{number}", name="Thread: main" ++ ): ++ t = Thread(target=do_some_work, args=(number,)) ++ t.start() ++ threads.append(t) ++ ++ for t in threads: ++ t.join() ++ ++ (event,) = events ++ if propagate_scope: ++ assert render_span_tree(event) == dedent( ++ """\ ++ - op="outer-trx": description=null ++ - op="outer-submit-0": description="Thread: main" ++ - op="inner-run-0": description="Thread: child-0" ++ - op="outer-submit-1": description="Thread: main" ++ - op="inner-run-1": description="Thread: child-1" ++ - op="outer-submit-2": description="Thread: main" ++ - op="inner-run-2": description="Thread: child-2" ++ - op="outer-submit-3": description="Thread: main" ++ - op="inner-run-3": description="Thread: child-3" ++ - op="outer-submit-4": description="Thread: main" ++ - op="inner-run-4": description="Thread: child-4"\ ++""" ++ ) ++ ++ elif not propagate_scope: ++ assert render_span_tree(event) == dedent( ++ """\ ++ - op="outer-trx": description=null ++ - op="outer-submit-0": description="Thread: main" ++ - op="outer-submit-1": description="Thread: main" ++ - op="outer-submit-2": description="Thread: main" ++ - op="outer-submit-3": description="Thread: main" ++ - op="outer-submit-4": description="Thread: main"\ ++""" ++ ) diff --git a/bench/corpus/sentry-python-4281-threading-channels-version/hunks.tsv b/bench/corpus/sentry-python-4281-threading-channels-version/hunks.tsv new file mode 100644 index 000000000..98faf991b --- /dev/null +++ b/bench/corpus/sentry-python-4281-threading-channels-version/hunks.tsv @@ -0,0 +1,12 @@ +sentry_sdk/integrations/threading.py OLD 1 4 +sentry_sdk/integrations/threading.py NEW 1 5 +sentry_sdk/integrations/threading.py OLD 49 54 +sentry_sdk/integrations/threading.py NEW 50 64 +sentry_sdk/integrations/threading.py OLD 57 64 +sentry_sdk/integrations/threading.py NEW 67 93 +tests/integrations/django/asgi/test_asgi.py OLD 38 46 +tests/integrations/django/asgi/test_asgi.py NEW 38 62 +tests/integrations/threading/test_threading.py OLD 1 5 +tests/integrations/threading/test_threading.py NEW 1 6 +tests/integrations/threading/test_threading.py OLD 172 174 +tests/integrations/threading/test_threading.py NEW 173 275 diff --git a/bench/corpus/sentry-python-6168-span-batcher-flush-thread/case.json b/bench/corpus/sentry-python-6168-span-batcher-flush-thread/case.json new file mode 100644 index 000000000..3416d64ee --- /dev/null +++ b/bench/corpus/sentry-python-6168-span-batcher-flush-thread/case.json @@ -0,0 +1,35 @@ +{ + "schema_version": "1", + "id": "sentry-python-6168-span-batcher-flush-thread", + "kind": "real", + "language": "python", + "provenance": { + "kind": "real", + "repo": "getsentry/sentry-python", + "pr": 6168, + "base_sha": "6436518b21943881122e0cc6e0e49fe1355fd23f", + "head_sha": "d90a9238ccf84dd49b2c994e8cbc9cc7b8fdbf80", + "fix_commit": "https://github.com/getsentry/sentry-python/commit/e17291eea3d274c6a64252760af956b43f94947c", + "source": "PR #6168 \"ref(batcher): Only flush the bucket that triggered the flush event\", squash-merged 2026-05-04; fix commit e17291eea \"Do not let a failed flush kill the flusher thread\", closes #7138", + "note": "pre-existing pattern, authored again here: the PR writes SpanBatcher its own _flush_loop (the base class inherited an equally unguarded one), so the reviewed lines are new code that repeats the flaw. The fix wraps both flush calls in capture_internal_exceptions; by then the same lines had moved down one line, and the loop body is byte-identical apart from the guard." + }, + "bugs": [ + { + "path": "sentry_sdk/_span_batcher.py", + "line": 93, + "kind": "flusher-thread-death", + "severity": "important", + "keywords": [ + "one failed send permanently ends the background drain", + "the loop dies on the first failure and nothing drains the queue again", + "every later item is discarded because nothing drains the accumulator", + "an unguarded failure stops the worker for good", + "the send is not guarded, so the first failure ends the flusher", + "the buffer keeps accepting items that nothing will send", + "no exception handling means a single failure ends the periodic drain" + ], + "note": "the daemon loop calls _flush unguarded; the fix's body states the consequence: the thread ends, _ensure_thread still sees the same pid, items keep accumulating to MAX_BEFORE_DROP and are then dropped for the rest of the process lifetime.", + "also_lines": [99, 100] + } + ] +} diff --git a/bench/corpus/sentry-python-6168-span-batcher-flush-thread/full.diff b/bench/corpus/sentry-python-6168-span-batcher-flush-thread/full.diff new file mode 100644 index 000000000..8eef560b2 --- /dev/null +++ b/bench/corpus/sentry-python-6168-span-batcher-flush-thread/full.diff @@ -0,0 +1,684 @@ +diff --git a/sentry_sdk/_span_batcher.py b/sentry_sdk/_span_batcher.py +index 9e1b96f0c..762b377af 100644 +--- a/sentry_sdk/_span_batcher.py ++++ b/sentry_sdk/_span_batcher.py +@@ -1,7 +1,9 @@ ++import random ++import threading ++import time + from collections import defaultdict + from datetime import datetime, timezone + import os +-import threading + from typing import TYPE_CHECKING + import weakref + +@@ -16,14 +18,15 @@ if TYPE_CHECKING: + + class SpanBatcher(Batcher["StreamedSpan"]): + # MAX_BEFORE_FLUSH should be lower than MAX_BEFORE_DROP, so that there is +- # a bit of a buffer for spans that appear between setting the flush event ++ # a bit of a buffer for spans that appear between the trigger to flush + # and actually flushing the buffer. + # +- # The max limits are all per trace. ++ # The max limits are all per trace (per bucket). + MAX_ENVELOPE_SIZE = 1000 # spans + MAX_BEFORE_FLUSH = 1000 + MAX_BEFORE_DROP = 2000 + MAX_BYTES_BEFORE_FLUSH = 5 * 1024 * 1024 # 5 MB ++ + FLUSH_WAIT_TIME = 5.0 + + TYPE = "span" +@@ -47,7 +50,9 @@ class SpanBatcher(Batcher["StreamedSpan"]): + self._lock = threading.Lock() + self._active: "threading.local" = threading.local() + +- self._flush_event: "threading.Event" = threading.Event() ++ self._last_full_flush: float = time.monotonic() # drives time-based flushes ++ self._flush_event = threading.Event() ++ self._pending_flush: set[str] = set() # buckets to be flushed + + self._flusher: "Optional[threading.Thread]" = None + self._flusher_pid: "Optional[int]" = None +@@ -71,11 +76,29 @@ class SpanBatcher(Batcher["StreamedSpan"]): + self._lock = threading.Lock() + self._active = threading.local() + ++ self._last_full_flush = time.monotonic() + self._flush_event = threading.Event() ++ self._pending_flush = set() + + self._flusher = None + self._flusher_pid = None + ++ def _flush_loop(self) -> None: ++ self._active.flag = True ++ while self._running: ++ jitter = random.random() * self.FLUSH_WAIT_TIME * 0.1 ++ self._flush_event.wait(timeout=self.FLUSH_WAIT_TIME + jitter) ++ self._flush_event.clear() ++ ++ self._flush(only_pending=True) ++ ++ if ( ++ time.monotonic() - self._last_full_flush ++ >= self.FLUSH_WAIT_TIME + jitter ++ ): ++ self._flush() ++ self._last_full_flush = time.monotonic() ++ + def add(self, span: "StreamedSpan") -> None: + # Bail out if the current thread is already executing batcher code. + # This prevents deadlocks when code running inside the batcher (e.g. +@@ -104,13 +127,17 @@ class SpanBatcher(Batcher["StreamedSpan"]): + self._span_buffer[span.trace_id].append(span) + self._running_size[span.trace_id] += self._estimate_size(span) + +- if size + 1 >= self.MAX_BEFORE_FLUSH: +- self._flush_event.set() +- return +- +- if self._running_size[span.trace_id] >= self.MAX_BYTES_BEFORE_FLUSH: +- self._flush_event.set() +- return ++ if ( ++ size + 1 >= self.MAX_BEFORE_FLUSH ++ or self._running_size[span.trace_id] >= self.MAX_BYTES_BEFORE_FLUSH ++ ): ++ self._pending_flush.add(span.trace_id) ++ notify = True ++ else: ++ notify = False ++ ++ if notify: ++ self._flush_event.set() + finally: + self._active.flag = False + +@@ -154,50 +181,62 @@ class SpanBatcher(Batcher["StreamedSpan"]): + + return res + +- def _flush(self) -> None: ++ def _flush(self, only_pending: bool = False) -> None: + with self._lock: +- if len(self._span_buffer) == 0: ++ if only_pending: ++ buckets = list(self._pending_flush) ++ else: ++ # flush whole buffer, e.g. if the SDK is shutting down ++ buckets = list(self._span_buffer.keys()) ++ ++ self._pending_flush.clear() ++ ++ if not buckets: + return + + envelopes = [] +- for spans in self._span_buffer.values(): +- if spans: +- dsc = spans[0]._dynamic_sampling_context() + +- # Max per envelope is 1000, so if we happen to have more than +- # 1000 spans in one bucket, we'll need to separate them. +- for start in range(0, len(spans), self.MAX_ENVELOPE_SIZE): +- end = min(start + self.MAX_ENVELOPE_SIZE, len(spans)) ++ for bucket_id in buckets: ++ spans = self._span_buffer.get(bucket_id) ++ if not spans: ++ continue + +- envelope = Envelope( +- headers={ +- "sent_at": format_timestamp(datetime.now(timezone.utc)), +- "trace": dsc, +- } +- ) ++ dsc = spans[0]._dynamic_sampling_context() ++ ++ # Max per envelope is 1000, so if we happen to have more than ++ # 1000 spans in one bucket, we'll need to separate them. ++ for start in range(0, len(spans), self.MAX_ENVELOPE_SIZE): ++ end = min(start + self.MAX_ENVELOPE_SIZE, len(spans)) + +- envelope.add_item( +- Item( +- type=self.TYPE, +- content_type=self.CONTENT_TYPE, +- headers={ +- "item_count": end - start, +- }, +- payload=PayloadRef( +- json={ +- "items": [ +- self._to_transport_format(spans[j]) +- for j in range(start, end) +- ] +- } +- ), +- ) ++ envelope = Envelope( ++ headers={ ++ "sent_at": format_timestamp(datetime.now(timezone.utc)), ++ "trace": dsc, ++ } ++ ) ++ ++ envelope.add_item( ++ Item( ++ type=self.TYPE, ++ content_type=self.CONTENT_TYPE, ++ headers={ ++ "item_count": end - start, ++ }, ++ payload=PayloadRef( ++ json={ ++ "items": [ ++ self._to_transport_format(spans[j]) ++ for j in range(start, end) ++ ] ++ } ++ ), + ) ++ ) + +- envelopes.append(envelope) ++ envelopes.append(envelope) + +- self._span_buffer.clear() +- self._running_size.clear() ++ del self._span_buffer[bucket_id] ++ del self._running_size[bucket_id] + + for envelope in envelopes: + self._capture_func(envelope) +diff --git a/tests/tracing/test_span_buffer.py b/tests/tracing/test_span_buffer.py +new file mode 100644 +index 000000000..15823a421 +--- /dev/null ++++ b/tests/tracing/test_span_buffer.py +@@ -0,0 +1,427 @@ ++import time ++from unittest import mock ++ ++import sentry_sdk ++from sentry_sdk._span_batcher import SpanBatcher ++ ++ ++def test_envelope_by_trace_id(sentry_init, capture_envelopes, monkeypatch): ++ """Envelopes only contain spans of one trace ID.""" ++ sentry_init( ++ traces_sample_rate=1.0, ++ _experiments={"trace_lifecycle": "stream"}, ++ ) ++ ++ envelopes = capture_envelopes() ++ ++ sentry_sdk.traces.new_trace() ++ ++ with sentry_sdk.traces.start_span(name="span 1a") as span1: ++ trace_id1 = span1.trace_id ++ with sentry_sdk.traces.start_span(name="span 1b"): ++ pass ++ ++ sentry_sdk.traces.new_trace() ++ ++ with sentry_sdk.traces.start_span(name="span 2a") as span2: ++ trace_id2 = span2.trace_id ++ with sentry_sdk.traces.start_span(name="span 2b"): ++ pass ++ ++ sentry_sdk.flush() ++ ++ assert len(envelopes) == 2 ++ ++ assert envelopes[0].headers["trace"]["trace_id"] == trace_id1 ++ assert len(envelopes[0].items[0].payload.json["items"]) == 2 ++ assert envelopes[0].items[0].payload.json["items"][0]["name"] == "span 1a" ++ assert envelopes[0].items[0].payload.json["items"][0]["trace_id"] == trace_id1 ++ assert envelopes[0].items[0].payload.json["items"][1]["name"] == "span 1b" ++ assert envelopes[0].items[0].payload.json["items"][1]["trace_id"] == trace_id1 ++ ++ assert envelopes[1].headers["trace"]["trace_id"] == trace_id2 ++ assert len(envelopes[1].items[0].payload.json["items"]) == 2 ++ assert envelopes[1].items[0].payload.json["items"][0]["name"] == "span 2a" ++ assert envelopes[1].items[0].payload.json["items"][0]["trace_id"] == trace_id2 ++ assert envelopes[1].items[0].payload.json["items"][1]["name"] == "span 2b" ++ assert envelopes[1].items[0].payload.json["items"][1]["trace_id"] == trace_id2 ++ ++ ++def test_max_envelope_size(sentry_init, capture_envelopes, monkeypatch): ++ """Envelope max size is respected.""" ++ monkeypatch.setattr(SpanBatcher, "MAX_ENVELOPE_SIZE", 2) ++ # set the time-based flush limit to something huge so that we're not observing ++ # time-based flushes ++ monkeypatch.setattr(SpanBatcher, "FLUSH_WAIT_TIME", 100000) ++ ++ sentry_init( ++ traces_sample_rate=1.0, ++ _experiments={"trace_lifecycle": "stream"}, ++ ) ++ ++ envelopes = capture_envelopes() ++ ++ with sentry_sdk.traces.start_span(name="span 1"): ++ pass ++ with sentry_sdk.traces.start_span(name="span 2"): ++ pass ++ with sentry_sdk.traces.start_span(name="span 3"): ++ pass ++ with sentry_sdk.traces.start_span(name="span 4"): ++ pass ++ with sentry_sdk.traces.start_span(name="span 5"): ++ pass ++ ++ sentry_sdk.flush() ++ ++ assert len(envelopes) == 3 ++ ++ assert len(envelopes[0].items[0].payload.json["items"]) == 2 ++ assert envelopes[0].items[0].payload.json["items"][0]["name"] == "span 1" ++ assert envelopes[0].items[0].payload.json["items"][1]["name"] == "span 2" ++ assert len(envelopes[1].items[0].payload.json["items"]) == 2 ++ assert envelopes[1].items[0].payload.json["items"][0]["name"] == "span 3" ++ assert envelopes[1].items[0].payload.json["items"][1]["name"] == "span 4" ++ assert len(envelopes[2].items[0].payload.json["items"]) == 1 ++ assert envelopes[2].items[0].payload.json["items"][0]["name"] == "span 5" ++ ++ ++def test_drop_after_max_reached( ++ sentry_init, capture_envelopes, capture_record_lost_event_calls, monkeypatch ++): ++ """New spans are dropped if a bucket reaches MAX_BEFORE_DROP spans.""" ++ monkeypatch.setattr(SpanBatcher, "MAX_BEFORE_DROP", 2) ++ # set the time-based flush limit to something huge so that we're not flushing ++ # prematurely ++ monkeypatch.setattr(SpanBatcher, "FLUSH_WAIT_TIME", 100000) ++ ++ sentry_init( ++ traces_sample_rate=1.0, ++ _experiments={"trace_lifecycle": "stream"}, ++ ) ++ ++ envelopes = capture_envelopes() ++ record_lost_event_calls = capture_record_lost_event_calls() ++ ++ with sentry_sdk.traces.start_span(name="span 1"): ++ pass ++ with sentry_sdk.traces.start_span(name="span 2"): ++ pass ++ with sentry_sdk.traces.start_span(name="span 3"): ++ pass ++ ++ sentry_sdk.flush() ++ ++ assert len(envelopes) == 1 ++ ++ assert len(envelopes[0].items[0].payload.json["items"]) == 2 ++ assert envelopes[0].items[0].payload.json["items"][0]["name"] == "span 1" ++ assert envelopes[0].items[0].payload.json["items"][1]["name"] == "span 2" ++ ++ assert ("queue_overflow", "span", None, 1) in record_lost_event_calls ++ ++ ++def test_drop_isolated_per_bucket( ++ sentry_init, capture_envelopes, capture_record_lost_event_calls, monkeypatch ++): ++ """A bucket reaching MAX_BEFORE_DROP only drops spans from that trace.""" ++ monkeypatch.setattr(SpanBatcher, "MAX_BEFORE_DROP", 2) ++ monkeypatch.setattr(SpanBatcher, "FLUSH_WAIT_TIME", 100000) ++ ++ sentry_init( ++ traces_sample_rate=1.0, ++ _experiments={"trace_lifecycle": "stream"}, ++ ) ++ ++ envelopes = capture_envelopes() ++ record_lost_event_calls = capture_record_lost_event_calls() ++ ++ sentry_sdk.traces.new_trace() ++ with sentry_sdk.traces.start_span(name="a1") as span_a: ++ trace_id_a = span_a.trace_id ++ with sentry_sdk.traces.start_span(name="a2"): ++ pass ++ with sentry_sdk.traces.start_span(name="a3"): ++ pass ++ ++ sentry_sdk.traces.new_trace() ++ with sentry_sdk.traces.start_span(name="b1") as span_b: ++ trace_id_b = span_b.trace_id ++ with sentry_sdk.traces.start_span(name="b2"): ++ pass ++ ++ sentry_sdk.flush() ++ ++ assert len(envelopes) == 2 ++ ++ assert envelopes[0].headers["trace"]["trace_id"] == trace_id_a ++ assert len(envelopes[0].items[0].payload.json["items"]) == 2 ++ assert envelopes[0].items[0].payload.json["items"][0]["name"] == "a1" ++ assert envelopes[0].items[0].payload.json["items"][1]["name"] == "a2" ++ ++ assert envelopes[1].headers["trace"]["trace_id"] == trace_id_b ++ assert len(envelopes[1].items[0].payload.json["items"]) == 2 ++ assert envelopes[1].items[0].payload.json["items"][0]["name"] == "b1" ++ assert envelopes[1].items[0].payload.json["items"][1]["name"] == "b2" ++ ++ assert record_lost_event_calls.count(("queue_overflow", "span", None, 1)) == 1 ++ ++ ++def test_length_based_flushing(sentry_init, capture_items, monkeypatch): ++ """A flush event is triggered when a bucket contains MAX_BEFORE_FLUSH spans.""" ++ monkeypatch.setattr(SpanBatcher, "MAX_BEFORE_FLUSH", 1) ++ # set the time-based flush limit to something huge so that we're not hitting ++ # it since we want to test MAX_BEFORE_FLUSH instead ++ monkeypatch.setattr(SpanBatcher, "FLUSH_WAIT_TIME", 100000) ++ ++ sentry_init( ++ traces_sample_rate=1.0, ++ _experiments={"trace_lifecycle": "stream"}, ++ ) ++ ++ items = capture_items("span") ++ ++ with sentry_sdk.traces.start_span(name="span"): ++ pass ++ ++ time.sleep(0.1) ++ ++ assert len(items) == 1 ++ assert items[0].payload["name"] == "span" ++ ++ ++def test_weight_based_flushing(sentry_init, capture_envelopes, monkeypatch): ++ """When a bucket reaches MAX_BYTES_BEFORE_FLUSH, it'll be flushed.""" ++ monkeypatch.setattr(SpanBatcher, "MAX_BYTES_BEFORE_FLUSH", 1) ++ # set the time-based flush limit to something huge so that it doesn't ++ # interfere ++ monkeypatch.setattr(SpanBatcher, "FLUSH_WAIT_TIME", 100000) ++ ++ sentry_init( ++ traces_sample_rate=1.0, ++ _experiments={"trace_lifecycle": "stream"}, ++ ) ++ ++ envelopes = capture_envelopes() ++ ++ with sentry_sdk.traces.start_span(name="span"): ++ pass ++ ++ time.sleep(0.1) ++ ++ assert len(envelopes) == 1 ++ ++ assert len(envelopes[0].items[0].payload.json["items"]) == 1 ++ assert envelopes[0].items[0].payload.json["items"][0]["name"] == "span" ++ ++ ++def test_weight_based_flushing_by_attribute_size( ++ sentry_init, capture_envelopes, monkeypatch ++): ++ """Attribute size is taken into account when using MAX_BYTES_BEFORE_FLUSH.""" ++ monkeypatch.setattr(SpanBatcher, "FLUSH_WAIT_TIME", 100000) ++ monkeypatch.setattr(SpanBatcher, "MAX_BEFORE_FLUSH", 100000) ++ ++ sentry_init( ++ traces_sample_rate=1.0, ++ _experiments={"trace_lifecycle": "stream"}, ++ ) ++ ++ envelopes = capture_envelopes() ++ ++ with sentry_sdk.traces.start_span(name="small span") as bare_span: ++ pass ++ ++ bare_span_size = SpanBatcher._estimate_size(bare_span) ++ big_attr = "x" * bare_span_size ++ ++ monkeypatch.setattr(SpanBatcher, "MAX_BYTES_BEFORE_FLUSH", bare_span_size * 3) ++ ++ time.sleep(0.1) ++ ++ # The first span alone is well under the byte limit, so no flush yet. ++ assert len(envelopes) == 0 ++ ++ with sentry_sdk.traces.start_span(name="big span", attributes={"big": big_attr}): ++ pass ++ ++ time.sleep(0.1) ++ ++ assert len(envelopes) == 1 ++ assert envelopes[0].items[0].payload.json["items"][0]["name"] == "small span" ++ assert envelopes[0].items[0].payload.json["items"][1]["name"] == "big span" ++ ++ ++def test_bucket_recreated_after_flush(sentry_init, capture_envelopes, monkeypatch): ++ """Spans for a trace that arrive after that trace's bucket was flushed land in a fresh bucket.""" ++ monkeypatch.setattr(SpanBatcher, "MAX_BEFORE_FLUSH", 2) ++ monkeypatch.setattr(SpanBatcher, "FLUSH_WAIT_TIME", 100000) ++ ++ sentry_init( ++ traces_sample_rate=1.0, ++ _experiments={"trace_lifecycle": "stream"}, ++ ) ++ ++ envelopes = capture_envelopes() ++ ++ sentry_sdk.traces.new_trace() ++ ++ with sentry_sdk.traces.start_span(name="span 1") as span1: ++ trace_id = span1.trace_id ++ with sentry_sdk.traces.start_span(name="span 2"): ++ pass ++ ++ time.sleep(0.1) ++ ++ assert len(envelopes) == 1 ++ ++ with sentry_sdk.traces.start_span(name="span 3"): ++ pass ++ with sentry_sdk.traces.start_span(name="span 4"): ++ pass ++ ++ time.sleep(0.1) ++ ++ assert len(envelopes) == 2 ++ ++ assert envelopes[0].headers["trace"]["trace_id"] == trace_id ++ assert len(envelopes[0].items[0].payload.json["items"]) == 2 ++ assert envelopes[0].items[0].payload.json["items"][0]["name"] == "span 1" ++ assert envelopes[0].items[0].payload.json["items"][0]["trace_id"] == trace_id ++ assert envelopes[0].items[0].payload.json["items"][1]["name"] == "span 2" ++ assert envelopes[0].items[0].payload.json["items"][1]["trace_id"] == trace_id ++ ++ assert envelopes[1].headers["trace"]["trace_id"] == trace_id ++ assert len(envelopes[1].items[0].payload.json["items"]) == 2 ++ assert envelopes[1].items[0].payload.json["items"][0]["name"] == "span 3" ++ assert envelopes[1].items[0].payload.json["items"][0]["trace_id"] == trace_id ++ assert envelopes[1].items[0].payload.json["items"][1]["name"] == "span 4" ++ assert envelopes[1].items[0].payload.json["items"][1]["trace_id"] == trace_id ++ ++ ++def test_quiet_buckets_flush_eventually(sentry_init, capture_envelopes, monkeypatch): ++ """Even if a bucket doesn't trigger any size limits, it'll get flushed eventually.""" ++ monkeypatch.setattr(SpanBatcher, "FLUSH_WAIT_TIME", 0.1) ++ # these are purposefully high so as to never trigger in this test scenario ++ monkeypatch.setattr(SpanBatcher, "MAX_BEFORE_FLUSH", 10000000) ++ monkeypatch.setattr(SpanBatcher, "MAX_BYTES_BEFORE_FLUSH", 10000000) ++ ++ sentry_init( ++ traces_sample_rate=1.0, ++ _experiments={"trace_lifecycle": "stream"}, ++ ) ++ ++ envelopes = capture_envelopes() ++ ++ with sentry_sdk.traces.start_span(name="span 1"): ++ pass ++ ++ time.sleep(0.3) ++ ++ assert len(envelopes) == 1 ++ ++ assert len(envelopes[0].items[0].payload.json["items"]) == 1 ++ assert envelopes[0].items[0].payload.json["items"][0]["name"] == "span 1" ++ ++ ++def test_quiet_buckets_flushed_with_busy_neighbors( ++ sentry_init, capture_envelopes, monkeypatch ++): ++ """When there are both busy and quiet buckets, all buckets get flushed eventually.""" ++ monkeypatch.setattr(SpanBatcher, "FLUSH_WAIT_TIME", 0.1) ++ monkeypatch.setattr(SpanBatcher, "MAX_BEFORE_FLUSH", 5) ++ ++ sentry_init( ++ traces_sample_rate=1.0, ++ _experiments={"trace_lifecycle": "stream"}, ++ ) ++ ++ envelopes = capture_envelopes() ++ ++ sentry_sdk.traces.new_trace() ++ ++ with sentry_sdk.traces.start_span(name="span 1") as span1: ++ trace_id1 = span1.trace_id ++ ++ sentry_sdk.traces.new_trace() ++ ++ with sentry_sdk.traces.start_span(name="span 2") as span2: ++ trace_id2 = span2.trace_id ++ ++ for i in range(3, 10): ++ with sentry_sdk.traces.start_span(name=f"span {i}"): ++ pass ++ ++ time.sleep(0.3) ++ ++ assert len(envelopes) >= 2 ++ ++ # we don't care how exactly the spans are distributed over envelopes ++ # (since both the time and length based limits are effective, the ++ # distribution might not be stable); just check whether the spans are all ++ # there and that each envelope only contains spans from one trace ++ seen = set() ++ for envelope in envelopes: ++ assert envelope.headers["trace"]["trace_id"] in (trace_id1, trace_id2) ++ ++ if envelope.headers["trace"]["trace_id"] == trace_id1: ++ assert len(envelope.items[0].payload.json["items"]) == 1 ++ assert envelope.items[0].payload.json["items"][0]["name"] == "span 1" ++ seen.add(envelope.items[0].payload.json["items"][0]["name"]) ++ ++ elif envelope.headers["trace"]["trace_id"] == trace_id2: ++ for span in envelope.items[0].payload.json["items"]: ++ seen.add(span["name"]) ++ ++ assert seen == {f"span {i}" for i in range(1, 10)} ++ ++ ++def test_transport_format(sentry_init, capture_envelopes): ++ sentry_init( ++ server_name="test-server", ++ release="1.0.0", ++ traces_sample_rate=1.0, ++ _experiments={"trace_lifecycle": "stream"}, ++ ) ++ ++ envelopes = capture_envelopes() ++ ++ with sentry_sdk.traces.start_span(name="test"): ++ ... ++ ++ sentry_sdk.get_client().flush() ++ ++ assert len(envelopes) == 1 ++ assert len(envelopes[0].items) == 1 ++ item = envelopes[0].items[0] ++ ++ assert item.type == "span" ++ assert item.headers == { ++ "type": "span", ++ "item_count": 1, ++ "content_type": "application/vnd.sentry.items.span.v2+json", ++ } ++ assert item.payload.json == { ++ "items": [ ++ { ++ "trace_id": mock.ANY, ++ "span_id": mock.ANY, ++ "name": "test", ++ "status": "ok", ++ "is_segment": True, ++ "start_timestamp": mock.ANY, ++ "end_timestamp": mock.ANY, ++ "attributes": mock.ANY, ++ } ++ ] ++ } ++ for attribute, value in item.payload.json["items"][0]["attributes"].items(): ++ assert isinstance(attribute, str) ++ ++ assert isinstance(value, dict) ++ assert ( ++ len(value) == 2 ++ ) # technically, "unit" is also supported, but we don't currently set it anywhere ++ assert "value" in value ++ assert "type" in value ++ assert value["type"] in ("string", "boolean", "integer", "double", "array") +diff --git a/tests/tracing/test_span_streaming.py b/tests/tracing/test_span_streaming.py +index 94bc2346b..43df3c7e2 100644 +--- a/tests/tracing/test_span_streaming.py ++++ b/tests/tracing/test_span_streaming.py +@@ -1505,7 +1505,7 @@ def test_profile_stops_when_segment_ends( + assert get_profiler_id() is None, "profiler should have stopped" + + +-def test_transport_format(sentry_init, capture_envelopes): ++def test_default_attributes(sentry_init, capture_envelopes): + sentry_init( + server_name="test-server", + release="1.0.0", +@@ -1530,30 +1530,17 @@ def test_transport_format(sentry_init, capture_envelopes): + "item_count": 1, + "content_type": "application/vnd.sentry.items.span.v2+json", + } +- assert item.payload.json == { +- "items": [ +- { +- "trace_id": mock.ANY, +- "span_id": mock.ANY, +- "name": "test", +- "status": "ok", +- "is_segment": True, +- "start_timestamp": mock.ANY, +- "end_timestamp": mock.ANY, +- "attributes": { +- "thread.id": {"value": mock.ANY, "type": "string"}, +- "thread.name": {"value": "MainThread", "type": "string"}, +- "sentry.segment.id": {"value": mock.ANY, "type": "string"}, +- "sentry.segment.name": {"value": "test", "type": "string"}, +- "sentry.sdk.name": {"value": "sentry.python", "type": "string"}, +- "sentry.sdk.version": {"value": mock.ANY, "type": "string"}, +- "server.address": {"value": "test-server", "type": "string"}, +- "sentry.environment": {"value": "production", "type": "string"}, +- "sentry.release": {"value": "1.0.0", "type": "string"}, +- "sentry.origin": {"value": "manual", "type": "string"}, +- }, +- } +- ] ++ assert item.payload.json["items"][0]["attributes"] == { ++ "thread.id": {"value": mock.ANY, "type": "string"}, ++ "thread.name": {"value": "MainThread", "type": "string"}, ++ "sentry.segment.id": {"value": mock.ANY, "type": "string"}, ++ "sentry.segment.name": {"value": "test", "type": "string"}, ++ "sentry.sdk.name": {"value": "sentry.python", "type": "string"}, ++ "sentry.sdk.version": {"value": mock.ANY, "type": "string"}, ++ "server.address": {"value": "test-server", "type": "string"}, ++ "sentry.environment": {"value": "production", "type": "string"}, ++ "sentry.release": {"value": "1.0.0", "type": "string"}, ++ "sentry.origin": {"value": "manual", "type": "string"}, + } + + diff --git a/bench/corpus/sentry-python-6168-span-batcher-flush-thread/hunks.tsv b/bench/corpus/sentry-python-6168-span-batcher-flush-thread/hunks.tsv new file mode 100644 index 000000000..beb0b880d --- /dev/null +++ b/bench/corpus/sentry-python-6168-span-batcher-flush-thread/hunks.tsv @@ -0,0 +1,17 @@ +sentry_sdk/_span_batcher.py OLD 1 7 +sentry_sdk/_span_batcher.py NEW 1 9 +sentry_sdk/_span_batcher.py OLD 16 29 +sentry_sdk/_span_batcher.py NEW 18 32 +sentry_sdk/_span_batcher.py OLD 47 53 +sentry_sdk/_span_batcher.py NEW 50 58 +sentry_sdk/_span_batcher.py OLD 71 81 +sentry_sdk/_span_batcher.py NEW 76 104 +sentry_sdk/_span_batcher.py OLD 104 116 +sentry_sdk/_span_batcher.py NEW 127 143 +sentry_sdk/_span_batcher.py OLD 154 203 +sentry_sdk/_span_batcher.py NEW 181 242 +tests/tracing/test_span_buffer.py NEW 1 427 +tests/tracing/test_span_streaming.py OLD 1505 1511 +tests/tracing/test_span_streaming.py NEW 1505 1511 +tests/tracing/test_span_streaming.py OLD 1530 1559 +tests/tracing/test_span_streaming.py NEW 1530 1546 diff --git a/bench/corpus/sentry-python-8119-clean-baggage-trailing-comma/case.json b/bench/corpus/sentry-python-8119-clean-baggage-trailing-comma/case.json new file mode 100644 index 000000000..4afbbbd60 --- /dev/null +++ b/bench/corpus/sentry-python-8119-clean-baggage-trailing-comma/case.json @@ -0,0 +1,16 @@ +{ + "schema_version": "1", + "id": "sentry-python-8119-clean-baggage-trailing-comma", + "kind": "control", + "language": "python", + "provenance": { + "kind": "real", + "repo": "getsentry/sentry-python", + "pr": 8119, + "base_sha": "ed74ba7d42ebdbc046fbcc1687eda9f31921ef47", + "head_sha": "63707d7379521871c3c88a743b7d321c6754c8d5", + "source": "PR #8119 \"fix(tracing): Drop trailing comma when serializing baggage without third-party items\", squash-merged 2026-10-28", + "note": "clean control: Baggage.serialize appended third_party_items even when it was the empty string, so a sentry-only baggage header ended with a comma. The guard is exact because from_incoming_header only ever stores stripped, comma-joined items in that field, so an empty field means there was nothing to append. No later commit touches the statement (git log -L over Baggage.serialize lists only older commits; one later commit touches tracing_utils.py at all) and no revert or fix names the PR." + }, + "bugs": [] +} diff --git a/bench/corpus/sentry-python-8119-clean-baggage-trailing-comma/full.diff b/bench/corpus/sentry-python-8119-clean-baggage-trailing-comma/full.diff new file mode 100644 index 000000000..2ff3798bb --- /dev/null +++ b/bench/corpus/sentry-python-8119-clean-baggage-trailing-comma/full.diff @@ -0,0 +1,63 @@ +diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py +index 8a6961ba1..2ac3a67e4 100644 +--- a/sentry_sdk/tracing_utils.py ++++ b/sentry_sdk/tracing_utils.py +@@ -973,7 +973,7 @@ class Baggage: + item = Baggage.SENTRY_PREFIX + quote(key) + "=" + quote(str(val)) + items.append(item) + +- if include_third_party: ++ if include_third_party and self.third_party_items: + items.append(self.third_party_items) + + return ",".join(items) +diff --git a/tests/test_tracing_utils.py b/tests/test_tracing_utils.py +index 3098e7366..293d83b43 100644 +--- a/tests/test_tracing_utils.py ++++ b/tests/test_tracing_utils.py +@@ -133,11 +133,11 @@ def test_strip_sentry_baggage(header, expected): + (Baggage(sentry_items={}, mutable=False), ''), + ( + Baggage(sentry_items={"foo": "bar"}), +- '', ++ '', + ), + ( + Baggage(sentry_items={"foo": "bar"}, mutable=False), +- '', ++ '', + ), + ( + Baggage(sentry_items={"foo": "bar"}, third_party_items="asdf=1234,"), +diff --git a/tests/tracing/test_baggage.py b/tests/tracing/test_baggage.py +index 1e0075fea..3d70a7cfe 100644 +--- a/tests/tracing/test_baggage.py ++++ b/tests/tracing/test_baggage.py +@@ -67,6 +67,27 @@ def test_mixed_baggage(): + ) + + ++def test_sentry_only_baggage(): ++ header = ( ++ "sentry-trace_id=771a43a4192642f0b136d5159a501700," ++ "sentry-public_key=49d0f7386ad645858ae85020e393bef3," ++ "sentry-sample_rate=0.01337" ++ ) ++ baggage = Baggage.from_incoming_header(header) ++ ++ assert baggage.third_party_items == "" ++ ++ serialized = baggage.serialize(include_third_party=True) ++ ++ # with no third-party items, include_third_party should change nothing ++ assert serialized == baggage.serialize() ++ assert serialized == ( ++ "sentry-trace_id=771a43a4192642f0b136d5159a501700," ++ "sentry-public_key=49d0f7386ad645858ae85020e393bef3," ++ "sentry-sample_rate=0.01337" ++ ) ++ ++ + def test_malformed_baggage(): + header = "," + diff --git a/bench/corpus/sentry-python-8119-clean-baggage-trailing-comma/hunks.tsv b/bench/corpus/sentry-python-8119-clean-baggage-trailing-comma/hunks.tsv new file mode 100644 index 000000000..7df8bd95f --- /dev/null +++ b/bench/corpus/sentry-python-8119-clean-baggage-trailing-comma/hunks.tsv @@ -0,0 +1,6 @@ +sentry_sdk/tracing_utils.py OLD 973 979 +sentry_sdk/tracing_utils.py NEW 973 979 +tests/test_tracing_utils.py OLD 133 143 +tests/test_tracing_utils.py NEW 133 143 +tests/tracing/test_baggage.py OLD 67 72 +tests/tracing/test_baggage.py NEW 67 93 diff --git a/internal/core/corpus/keywords_test.go b/internal/core/corpus/keywords_test.go index 88265eb44..0239c8bde 100644 --- a/internal/core/corpus/keywords_test.go +++ b/internal/core/corpus/keywords_test.go @@ -560,6 +560,86 @@ var keywordCases = []keywordCase{ "Note that result.dispatcher.destroy() tears down the dispatcher when count drops to zero.", }, }, + // sentry-python. Four kinds outside the published category table, so each + // row's vocabulary is its kind and keywords alone. Every keyword was + // checked to be unspelled by the case's own full.diff, and every row below + // grades the same way against the filtered runtime vocabulary + // (usablePhrases), not only against the unfiltered pin here. + { + id: "sentry-python-4121-logging-mapping-args", + passes: []string{ + "Only the mapping's keys reach the report, because the generator iterates the dict that was passed as the single argument.", + "The values of a dictionary passed as arguments are lost; only its keys are sent with the event.", + "A mapping logged with percent placeholders keeps just its key names, so the message renders as a tuple of keys.", + "Dict arguments are flattened into their keys before they reach Sentry.", + "The interpolated parameters collapse to the dictionary keys, so the substitution never sees its value.", + "Mapping-style interpolation receives the key names, not the values, whenever the caller passes a single dict.", + }, + fails: []string{ + "Coercing the absent value into a string keeps the payload serializable, which is what the linked issue asked for.", + "Building a tuple here copies the arguments once per event, which is cheap.", + "The message field keeps the raw template, so placeholders are never substituted.", + "The string conversion truncates messages longer than the event size limit.", + "The handler drops records below the configured level before the event is built.", + }, + }, + { + id: "sentry-python-4133-debug-parent-logger", + passes: []string{ + "An application root configuration silently suppresses the diagnostics, so turning the option on produces nothing.", + "The library's diagnostics end up in the host application's log stream instead of its own sink.", + "The library prints nothing when the host has its own log configuration at INFO level.", + "A parent log setup decides whether anything is emitted, so the SDK's own switch no longer controls it.", + "Any existing root setup stops the library from installing its own output.", + "A configured root makes the library skip its own setup, so the diagnostics arrive in the application's format.", + }, + fails: []string{ + "Using the public accessor makes the check consider inherited configuration, which is the documented behavior.", + "This swaps an internal attribute read for a public call, and both read the same value.", + "The level is set to INFO instead of DEBUG, so quieter records are lost.", + "A second call adds another output stream, duplicating every line.", + "The check runs before the filter is attached, so the first record escapes.", + "The warning about missing dependencies goes through the standard warnings module.", + }, + }, + { + id: "sentry-python-6168-span-batcher-flush-thread", + passes: []string{ + "One failed send permanently ends the background drain, so nothing is uploaded again.", + "The loop dies on the first failure and nothing drains the queue again.", + "Every later item is discarded because nothing drains the accumulator.", + "An unguarded failure stops the worker for good.", + "The send is not guarded, so the first failure ends the flusher and the buffer keeps growing.", + "No exception handling means a single failure ends the periodic drain.", + "The buffer keeps accepting items that nothing will send.", + }, + fails: []string{ + "Flushing only the buckets that filled up is what the specification asks for.", + "The jitter between waits spreads the flushes out across instances.", + "Taking a snapshot of the bucket list while holding the lock avoids a modification error.", + "The pending set stores trace ids, so a bucket is queued only once.", + "Recording the last flush timestamp after the send keeps the interval accurate.", + "The envelope size cap splits a large bucket into several items.", + }, + }, + { + id: "sentry-python-4281-threading-channels-version", + passes: []string{ + "A lookalike package without a version attribute aborts the initialization.", + "The version read is assumed to exist on any module of that name, and a local channels module does not have it.", + "A shadowing module makes the attribute access raise instead of skipping the integration.", + "Initialization crashes instead of disabling the integration when the import resolves to an unrelated module.", + "The guard only covers a missing package, so a missing attribute crashes setup.", + "Bootstrapping the client fails when a local module of that name has no version.", + }, + fails: []string{ + "Forking the scopes into the started thread stops data leaking between threads, which is the point of the change.", + "The warning lists three upgrade paths, which is more than most users need.", + "Threads started before the integration is set up never get the warning.", + "The integration is skipped when the client is not active.", + "The deprecation notice should be emitted once per process, not once per start.", + }, + }, } // TestCheckedInCorpusKeywordsGradeBothWays proves the shipped vocabulary both From 2ecc61ff10e271591ee812f5597dd76d22d8599b Mon Sep 17 00:00:00 2001 From: Sutu Sebastian Date: Sun, 15 Nov 2026 14:30:18 +0200 Subject: [PATCH 2/2] bench: repair sentry-python keywords after cross-family audit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The audit read the control clean and accepted the revert grounding, 6168's pre-existing disclosure and 4281's trigger, but found two defects in the vocabulary that the case-level probe could not see. Three keywords described intent, not a failure, so a neutral or approving comment graded as a hit: 4133's "any existing root setup stops the library from installing its own output" and "a configured root makes the library skip its own setup", and 4281's "the version read is assumed to exist on any module of that name". They are gone, and the audit's sentences carrying them are now must-grade-false rows, so re-adding any of the three reproduces the failure. Every keyword was a 7-14 word clause, so a reviewer who words the same defect differently graded as a miss. Each bug case gains 2-3 short directional phrases instead — "keys instead of values", "debug=true fails", "crashes init", "kills the background thread" — vetted one at a time against a plausible non-negated neutral sentence; the audit's direction-free suggestions (uncaught exception, host application's log, tuple of keys, never initialized) stay out for that reason. Three of the audit's eight paraphrased findings now grade true; the five that need an identifier or the mechanism are recorded as known misses, not bought by loosening a keyword into a bare consequence. Probe run over the runtime vocabulary with each case's full.diff, four mutations observed failing in the intended direction, every file restored from a byte copy. --- .../case.json | 5 +++- .../case.json | 5 ++-- .../case.json | 5 ++-- .../case.json | 5 +++- internal/core/corpus/keywords_test.go | 24 ++++++++++++++++--- 5 files changed, 35 insertions(+), 9 deletions(-) diff --git a/bench/corpus/sentry-python-4121-logging-mapping-args/case.json b/bench/corpus/sentry-python-4121-logging-mapping-args/case.json index d7d2d8edf..bb2630667 100644 --- a/bench/corpus/sentry-python-4121-logging-mapping-args/case.json +++ b/bench/corpus/sentry-python-4121-logging-mapping-args/case.json @@ -26,7 +26,10 @@ "dict arguments are flattened into their keys", "the message renders with key names instead of the substituted values", "mapping-style interpolation receives the key names", - "the interpolated parameters collapse to the dictionary keys" + "the interpolated parameters collapse to the dictionary keys", + "keys instead of values", + "flattens a mapping", + "only keys survive" ], "note": "stdlib logging accepts a single mapping as record.args for %(name)s templates; the generator iterates it, so keys, not values, are sent. The generator also rewrites a None argument to the string \"None\", which was the PR's intent.", "also_lines": [251] diff --git a/bench/corpus/sentry-python-4133-debug-parent-logger/case.json b/bench/corpus/sentry-python-4133-debug-parent-logger/case.json index 04c4dc258..57c6b4365 100644 --- a/bench/corpus/sentry-python-4133-debug-parent-logger/case.json +++ b/bench/corpus/sentry-python-4133-debug-parent-logger/case.json @@ -24,8 +24,9 @@ "the library's diagnostics end up in the host application's log stream instead of its own", "the library prints nothing when the host has its own log configuration", "a parent log setup decides whether anything is emitted", - "any existing root setup stops the library from installing its own output", - "a configured root makes the library skip its own setup" + "debug=true produces nothing", + "debug=true fails", + "leaked to the root" ], "note": "init_debug_support runs when the debug option is on; the inherited-handler walk makes it a no-op whenever the application has a root handler, so the SDK either emits nothing (host root level above DEBUG) or emits its diagnostics through the application's own handler and formatter. Both are observable; the keywords cover the suppression direction and the emission direction, and the revert cites the emission one." } diff --git a/bench/corpus/sentry-python-4281-threading-channels-version/case.json b/bench/corpus/sentry-python-4281-threading-channels-version/case.json index 95d90c07c..15b101a34 100644 --- a/bench/corpus/sentry-python-4281-threading-channels-version/case.json +++ b/bench/corpus/sentry-python-4281-threading-channels-version/case.json @@ -21,11 +21,12 @@ "severity": "critical", "keywords": [ "a lookalike package without a version attribute aborts the initialization", - "the version read is assumed to exist on any module of that name", "a shadowing module makes the attribute access raise instead of skipping", "initialization crashes instead of disabling the integration", "the guard only covers a missing package, so a missing attribute crashes setup", - "bootstrapping the client fails when a local module of that name has no version" + "bootstrapping the client fails when a local module of that name has no version", + "crashes init", + "crashes the client" ], "note": "ThreadingIntegration is a default integration and setup_integrations only catches DidNotEnable around its setup_once, so the AttributeError reaches sentry_sdk.init() and the application fails to start. The fix moves `import channels` and the version read into their own try whose handler is `(ImportError, AttributeError)`.", "also_lines": [55, 58] diff --git a/bench/corpus/sentry-python-6168-span-batcher-flush-thread/case.json b/bench/corpus/sentry-python-6168-span-batcher-flush-thread/case.json index 3416d64ee..c6811cfa4 100644 --- a/bench/corpus/sentry-python-6168-span-batcher-flush-thread/case.json +++ b/bench/corpus/sentry-python-6168-span-batcher-flush-thread/case.json @@ -26,7 +26,10 @@ "an unguarded failure stops the worker for good", "the send is not guarded, so the first failure ends the flusher", "the buffer keeps accepting items that nothing will send", - "no exception handling means a single failure ends the periodic drain" + "no exception handling means a single failure ends the periodic drain", + "kills the background thread", + "kills the flusher", + "the drain never resumes" ], "note": "the daemon loop calls _flush unguarded; the fix's body states the consequence: the thread ends, _ensure_thread still sees the same pid, items keep accumulating to MAX_BEFORE_DROP and are then dropped for the rest of the process lifetime.", "also_lines": [99, 100] diff --git a/internal/core/corpus/keywords_test.go b/internal/core/corpus/keywords_test.go index 0239c8bde..c920dc4db 100644 --- a/internal/core/corpus/keywords_test.go +++ b/internal/core/corpus/keywords_test.go @@ -565,6 +565,14 @@ var keywordCases = []keywordCase{ // checked to be unspelled by the case's own full.diff, and every row below // grades the same way against the filtered runtime vocabulary // (usablePhrases), not only against the unfiltered pin here. + // + // audit-11 (cross-family): 4133 and 4281 lost the phrases that described + // the PR's intended mechanism (a neutral or approving comment could carry + // them), and each case gained 2-4 short directional phrases so a reviewer + // who words the defect differently still grades as a hit. The three + // sentences those phrases were written against are pinned as must-grade- + // false rows, and the audit's own paraphrases are pinned where a phrase + // covers them (4133 both, 6168 one; the rest are known misses). { id: "sentry-python-4121-logging-mapping-args", passes: []string{ @@ -574,6 +582,9 @@ var keywordCases = []keywordCase{ "Dict arguments are flattened into their keys before they reach Sentry.", "The interpolated parameters collapse to the dictionary keys, so the substitution never sees its value.", "Mapping-style interpolation receives the key names, not the values, whenever the caller passes a single dict.", + "A dict argument is flattened into keys instead of values, so the placeholder cannot be interpolated.", + "The generator flattens a mapping before the event is built, so what ships is the key set.", + "Only keys survive the tuple conversion, so the substituted values never reach the report.", }, fails: []string{ "Coercing the absent value into a string keeps the payload serializable, which is what the linked issue asked for.", @@ -590,8 +601,9 @@ var keywordCases = []keywordCase{ "The library's diagnostics end up in the host application's log stream instead of its own sink.", "The library prints nothing when the host has its own log configuration at INFO level.", "A parent log setup decides whether anything is emitted, so the SDK's own switch no longer controls it.", - "Any existing root setup stops the library from installing its own output.", - "A configured root makes the library skip its own setup, so the diagnostics arrive in the application's format.", + "Checking the parent handlers skips the SDK's own setup, so debug=true fails to attach the stream the option promises.", + "If the application configures a root logger, the SDK's lines are leaked to the root and debug=true produces nothing.", + "With the host's root level above DEBUG, debug=true produces nothing at all.", }, fails: []string{ "Using the public accessor makes the check consider inherited configuration, which is the documented behavior.", @@ -600,6 +612,8 @@ var keywordCases = []keywordCase{ "A second call adds another output stream, duplicating every line.", "The check runs before the filter is attached, so the first record escapes.", "The warning about missing dependencies goes through the standard warnings module.", + "A configured root makes the library skip its own setup, avoiding duplicate log output.", + "Any existing root setup stops the library from installing its own output, which is the intent of checking parent handlers.", }, }, { @@ -612,6 +626,8 @@ var keywordCases = []keywordCase{ "The send is not guarded, so the first failure ends the flusher and the buffer keeps growing.", "No exception handling means a single failure ends the periodic drain.", "The buffer keeps accepting items that nothing will send.", + "With no try/except around the flush, one transient transport or serialization error kills the background thread.", + "A single failure kills the flusher, and the drain never resumes while items keep queueing.", }, fails: []string{ "Flushing only the buckets that filled up is what the specification asks for.", @@ -626,9 +642,10 @@ var keywordCases = []keywordCase{ id: "sentry-python-4281-threading-channels-version", passes: []string{ "A lookalike package without a version attribute aborts the initialization.", - "The version read is assumed to exist on any module of that name, and a local channels module does not have it.", + "An unrelated channels module with no version attribute crashes the client during setup.", "A shadowing module makes the attribute access raise instead of skipping the integration.", "Initialization crashes instead of disabling the integration when the import resolves to an unrelated module.", + "A local channels module without a version attribute crashes init() before the integration is enabled.", "The guard only covers a missing package, so a missing attribute crashes setup.", "Bootstrapping the client fails when a local module of that name has no version.", }, @@ -638,6 +655,7 @@ var keywordCases = []keywordCase{ "Threads started before the integration is set up never get the warning.", "The integration is skipped when the client is not active.", "The deprecation notice should be emitted once per process, not once per start.", + "The version read is assumed to exist on any module of that name, which matches how the other integrations detect versions.", }, }, }