Skip to content

Make compatible with sentry sdk 3.0.0 #39

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 24 commits into from
May 23, 2025

Conversation

antonpirker
Copy link
Member

@antonpirker antonpirker commented May 20, 2025

NOTE: When this PR is released, pytest-sentry is not compatible to SDK 2.x anymore!

This PR:

@antonpirker antonpirker marked this pull request as ready for review May 20, 2025 14:17
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates pytest-sentry for compatibility with Sentry SDK 3.0.0 by migrating legacy scope and transaction APIs to the new OpenTelemetry-based implementations.

  • Swapped out sentry_sdk.Scope for PotelScope and updated use_scope imports.
  • Refactored _start_transaction to use continue_trace and start_span context managers.
  • Bumped sentry-sdk requirement to >=3.0.0a1.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
tests/test_scope.py Replaced sentry_sdk.Scope usage with PotelScope as Scope and updated scope checks.
tests/test_envvars.py Updated use_scope import to sentry_sdk.opentelemetry.scope.use_scope.
setup.cfg Updated sentry-sdk dependency to >=3.0.0a1.
pytest_sentry.py Adjusted imports for new scope APIs, refactored transaction start logic, and modified scope resolution to set clients.
Comments suppressed due to low confidence (4)

pytest_sentry.py:259

  • Mutating the current scope and returning it can cause client configuration to leak between tests. Instead, construct and return a fresh Scope instance with the desired client (e.g., return Scope(client=Client(marker_value))).
scope.set_client(Client(marker_value))

pytest_sentry.py:264

  • Mutating the current scope and returning it can cause client configuration to leak between tests. Instead, construct and return a fresh Scope instance with the desired client (e.g., return Scope(client=Client(**marker_value))).
scope.set_client(Client(**marker_value))

pytest_sentry.py:269

  • Mutating the current scope and returning it can cause client configuration to leak between tests. Instead, construct and return a fresh Scope instance with the desired client (e.g., return Scope(client=marker_value)).
scope.set_client(marker_value)

pytest_sentry.py:166

  • Using start_span instead of start_transaction may produce a Span rather than a Transaction, potentially breaking the intended use of Sentry transactions. Consider using sentry_sdk.start_transaction to maintain transaction semantics.
with sentry_sdk.start_span(**kwargs) as root_span:

Copy link
Member

@sl0thentr0py sl0thentr0py left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use top level api everywhere pls

@antonpirker antonpirker requested a review from sl0thentr0py May 21, 2025 08:04
@antonpirker antonpirker dismissed sl0thentr0py’s stale review May 21, 2025 12:10

addressed all requested changes.

@antonpirker
Copy link
Member Author

If you wonder how the trace of a pytest testsuite looks like:

Screenshot 2025-05-22 at 17 23 20

I have set pytest --reruns=5 in pytest (needs the required pytest-rerunfailures package).

This is the test suite:
https://github.com/antonpirker/testing-sentry/blob/main/test-pytest-sentry/test_something.py

The screenshot shows:

  • one successful test that loads one fixture
  • one flaky test that failed 2 times (and then suceeded, but there is no span for the sucess...)
  • one test that always fails, so it is rerun 5 times

@antonpirker antonpirker merged commit 82d2685 into main May 23, 2025
11 checks passed
@antonpirker antonpirker deleted the antonpirker/make-compatible-with-sentry-sdk-3.0.0 branch May 23, 2025 10:25
sl0thentr0py added a commit to getsentry/sentry that referenced this pull request Jul 2, 2025
Update the codebase so it works with `Sentry Python SDK 3.0.0 alpha`.

Sentry SDK 3.0.0 has some backwards incompatibilities and uses
Opentelementry under the hood to create spans.

### What this PR does:
- replace `sentry_sdk.start_transaction()` with
`sentry_sdk.start_span()`
- replace `span.set_data()` with `span.set_attribute()`
- replace `scope.transaction` with `scope.root_span`
- replace `Span.containing_transaction` with `Span.root_span`
- replace `custom_sampling_context` with `attributes` parameter
- replace `sentry_sdk.scope.get_*_scope()` with
`sentry_sdk.get_*_scope()`
- replace one `scope.add_attachment()` with
`sentry_sdk.add_attachment()`
- change one `continue_trace()` to the new context manager.
- update `traces_sampler` to use new `sampling_context` format
- added a few tests

See also the migration guide:
https://docs.sentry.io/platforms/python/migration/2.x-to-3.x

---


### Related, but split out into separate PRs:
- make sure `traces_sampler` works the same as before (check for
`sample_rate`): #93011
- make **all** `get_isolation_scope` calls use the top-level API.
Currently, some calls where we `from sentry_sdk import Scope` then call
`Scope.get_isolation_scope` still need to be converted to the top-level
API: #93307
- check `event["measurements"]` places if those can be removed (because
SDK does not support measurements anymore):
#92718
- change set_span_data() to set_span_attribute() but check for value NOT
be a dict: #92946
- replace `with sentry_sdk.init(dsn=...)` with `with
sentry_sdk.new_scope() as scope:
scope.set_client(sentry_sdk.Client(dsn=...))` in
`src/sentry/runner/main.py`: #92944
- remove `propagate_hub=True` from `ThreadingIntegration`: #93016
- remove minimetrics from the sentry codebase, because the metrics
product was never released: #93595
- getsentry/getsentry#17553
- getsentry/devenv#194
- getsentry/devservices#277
- getsentry/pytest-sentry#39
- getsentry/sentry-docs#13939

---------

Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
Co-authored-by: Daniel Szoke <[email protected]>
Co-authored-by: Daniel Szoke <[email protected]>
Co-authored-by: Ivana Kellyer <[email protected]>
Co-authored-by: Neel Shah <[email protected]>
andrewshie-sentry pushed a commit to getsentry/sentry that referenced this pull request Jul 14, 2025
Update the codebase so it works with `Sentry Python SDK 3.0.0 alpha`.

Sentry SDK 3.0.0 has some backwards incompatibilities and uses
Opentelementry under the hood to create spans.

### What this PR does:
- replace `sentry_sdk.start_transaction()` with
`sentry_sdk.start_span()`
- replace `span.set_data()` with `span.set_attribute()`
- replace `scope.transaction` with `scope.root_span`
- replace `Span.containing_transaction` with `Span.root_span`
- replace `custom_sampling_context` with `attributes` parameter
- replace `sentry_sdk.scope.get_*_scope()` with
`sentry_sdk.get_*_scope()`
- replace one `scope.add_attachment()` with
`sentry_sdk.add_attachment()`
- change one `continue_trace()` to the new context manager.
- update `traces_sampler` to use new `sampling_context` format
- added a few tests

See also the migration guide:
https://docs.sentry.io/platforms/python/migration/2.x-to-3.x

---


### Related, but split out into separate PRs:
- make sure `traces_sampler` works the same as before (check for
`sample_rate`): #93011
- make **all** `get_isolation_scope` calls use the top-level API.
Currently, some calls where we `from sentry_sdk import Scope` then call
`Scope.get_isolation_scope` still need to be converted to the top-level
API: #93307
- check `event["measurements"]` places if those can be removed (because
SDK does not support measurements anymore):
#92718
- change set_span_data() to set_span_attribute() but check for value NOT
be a dict: #92946
- replace `with sentry_sdk.init(dsn=...)` with `with
sentry_sdk.new_scope() as scope:
scope.set_client(sentry_sdk.Client(dsn=...))` in
`src/sentry/runner/main.py`: #92944
- remove `propagate_hub=True` from `ThreadingIntegration`: #93016
- remove minimetrics from the sentry codebase, because the metrics
product was never released: #93595
- getsentry/getsentry#17553
- getsentry/devenv#194
- getsentry/devservices#277
- getsentry/pytest-sentry#39
- getsentry/sentry-docs#13939

---------

Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
Co-authored-by: Daniel Szoke <[email protected]>
Co-authored-by: Daniel Szoke <[email protected]>
Co-authored-by: Ivana Kellyer <[email protected]>
Co-authored-by: Neel Shah <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants