diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index c56a7e729c..00fe816e8f 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -183,16 +183,22 @@ def __init__( only_if_parent=False, # type: bool parent_span=None, # type: Optional[Span] otel_span=None, # type: Optional[OtelSpan] + span=None, # type: Optional[Span] ): # type: (...) -> None """ If otel_span is passed explicitly, just acts as a proxy. + If span is passed explicitly, use it. The only purpose of this param + if backwards compatibility with start_transaction(transaction=...). + If only_if_parent is True, just return an INVALID_SPAN and avoid instrumentation if there's no active parent span. """ if otel_span is not None: self._otel_span = otel_span + elif span is not None: + self._otel_span = span._otel_span else: skip_span = False if only_if_parent and parent_span is None: diff --git a/tests/integrations/logging/test_logging.py b/tests/integrations/logging/test_logging.py index e1b24a9853..e54fd829f1 100644 --- a/tests/integrations/logging/test_logging.py +++ b/tests/integrations/logging/test_logging.py @@ -296,7 +296,10 @@ def test_ignore_logger_wildcard(sentry_init, capture_events): def test_logging_dictionary_interpolation(sentry_init, capture_events): """Here we test an entire dictionary being interpolated into the log message.""" - sentry_init(integrations=[LoggingIntegration()], default_integrations=False) + sentry_init( + integrations=[LoggingIntegration(event_level=logging.ERROR)], + default_integrations=False, + ) events = capture_events() logger.error("this is a log with a dictionary %s", {"foo": "bar"}) @@ -312,7 +315,10 @@ def test_logging_dictionary_interpolation(sentry_init, capture_events): def test_logging_dictionary_args(sentry_init, capture_events): """Here we test items from a dictionary being interpolated into the log message.""" - sentry_init(integrations=[LoggingIntegration()], default_integrations=False) + sentry_init( + integrations=[LoggingIntegration(event_level=logging.ERROR)], + default_integrations=False, + ) events = capture_events() logger.error( diff --git a/tests/integrations/threading/test_threading.py b/tests/integrations/threading/test_threading.py index b1770874e5..11b4ee5301 100644 --- a/tests/integrations/threading/test_threading.py +++ b/tests/integrations/threading/test_threading.py @@ -104,7 +104,6 @@ def double(number): assert len(event["spans"]) == 0 -@pytest.mark.skip(reason="Temporarily disable to release SDK 2.0a1.") def test_circular_references(sentry_init, request): sentry_init(default_integrations=False, integrations=[ThreadingIntegration()]) @@ -232,7 +231,7 @@ def do_some_work(number): threads = [] - with sentry_sdk.start_transaction(op="outer-trx"): + with sentry_sdk.start_span(op="outer-trx"): for number in range(5): with sentry_sdk.start_span( op=f"outer-submit-{number}", name="Thread: main" diff --git a/tests/opentelemetry/test_compat.py b/tests/opentelemetry/test_compat.py index 1ae73494cd..381d9ad22e 100644 --- a/tests/opentelemetry/test_compat.py +++ b/tests/opentelemetry/test_compat.py @@ -1,4 +1,5 @@ import sentry_sdk +from sentry_sdk.tracing import Transaction def test_transaction_name_span_description_compat( @@ -52,3 +53,47 @@ def test_transaction_name_span_description_compat( assert span["op"] == "span-op" assert span["data"]["sentry.op"] == "span-op" assert span["data"]["sentry.description"] == "span-desc" + + +def test_start_transaction_compat( + sentry_init, + capture_events, +): + sentry_init(traces_sample_rate=1.0) + + events = capture_events() + + with sentry_sdk.start_transaction( + name="trx-name", + op="trx-op", + ): + ... + + transaction = events[0] + assert transaction["transaction"] == "trx-name" + assert transaction["contexts"]["trace"]["op"] == "trx-op" + assert transaction["contexts"]["trace"]["data"]["sentry.op"] == "trx-op" + assert transaction["contexts"]["trace"]["data"]["sentry.name"] == "trx-name" + assert "sentry.description" not in transaction["contexts"]["trace"]["data"] + + +def test_start_transaction_with_explicit_transaction_compat( + sentry_init, + capture_events, +): + """It should still be possible to provide a ready-made Transaction to start_transaction.""" + sentry_init(traces_sample_rate=1.0) + + events = capture_events() + + transaction = Transaction(name="trx-name", op="trx-op") + + with sentry_sdk.start_transaction(transaction=transaction): + pass + + transaction = events[0] + assert transaction["transaction"] == "trx-name" + assert transaction["contexts"]["trace"]["op"] == "trx-op" + assert transaction["contexts"]["trace"]["data"]["sentry.op"] == "trx-op" + assert transaction["contexts"]["trace"]["data"]["sentry.name"] == "trx-name" + assert "sentry.description" not in transaction["contexts"]["trace"]["data"]