Skip to content

Conversation

@alexander-alderman-webb
Copy link
Contributor

@alexander-alderman-webb alexander-alderman-webb commented Nov 13, 2025

Description

Create spans for SQL commits issued when Django calls commit() on a PEP-249 database connection.

Patch the BaseDatabaseWrapper._commit method, the same method in which debug statements were added in django/django@798e38c.

Commit spans are generated for transaction.atomic blocks and for manual transction.commit() calls when auto-commit is disabled. Tests cover both cases, for SQLite and PostgreSQL, respectively.


Screenshot 2025-11-21 at 10 51 33

Trying for yourself

The easiest way to try the new spans out for yourself is to bootstrap off the Django sample in our tests. To get the sample running independently, modify the initialization in tests/integrations/django/myapp/settings.py

sentry_sdk.init(
+   dsn="...",
    integrations=[DjangoIntegration()],
+   traces_sample_rate=1.0,
+   debug=True,
)

and make the PostgreSQL database name deterministic:

DATABASES["postgres"] = {
        "ENGINE": db_engine,
        "HOST": os.environ.get("SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost"),
        "PORT": int(os.environ.get("SENTRY_PYTHON_TEST_POSTGRES_PORT", "5432")),
        "USER": os.environ.get("SENTRY_PYTHON_TEST_POSTGRES_USER", "postgres"),
        "PASSWORD": os.environ.get("SENTRY_PYTHON_TEST_POSTGRES_PASSWORD", "sentry"),
        "NAME": os.environ.get(
-            "SENTRY_PYTHON_TEST_POSTGRES_NAME", f"myapp_db_{os.getpid()}"
+            "SENTRY_PYTHON_TEST_POSTGRES_NAME", f"myapp_db"
        ),
    }

To start a PostgreSQL instance, you can create the server in a container as follows:

docker run --name postgres \
  -e POSTGRES_DB=myapp_db \
  -e POSTGRES_USER=postgres \
  -e POSTGRES_PASSWORD=sentry \
  -p 5432:5432 \
  -d postgres

Next, populate the PostgreSQL database and run the Django server with the series of commands:

python3 -m tests.integrations.django.myapp.manage makemigrations
python3 -m tests.integrations.django.myapp.manage migrate --database=postgres
python3 -m tests.integrations.django.myapp.manage runserver

You can trigger an SQL transaction by hitting one of the postgres_insert* endpoints. For instance, access http://localhost:8000/postgres-insert-no-autocommit.

Issues

Reminders

@codecov
Copy link

codecov bot commented Nov 13, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.98%. Comparing base (b8a0db2) to head (2c90889).
⚠️ Report is 22 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5100      +/-   ##
==========================================
- Coverage   84.01%   83.98%   -0.03%     
==========================================
  Files         180      180              
  Lines       18004    18179     +175     
  Branches     3200     3229      +29     
==========================================
+ Hits        15126    15268     +142     
- Misses       1906     1923      +17     
- Partials      972      988      +16     
Files with missing lines Coverage Δ
sentry_sdk/consts.py 99.41% <100.00%> (+<0.01%) ⬆️
sentry_sdk/integrations/django/__init__.py 84.73% <100.00%> (+0.37%) ⬆️

... and 20 files with indirect coverage changes

user = User.objects.db_manager("postgres").create_user(
username="user1",
)
return HttpResponse("ok {}".format(user))
Copy link

Choose a reason for hiding this comment

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

Bug: Duplicate username causes IntegrityError on repeated calls

Both postgres_insert_orm_no_autocommit and postgres_insert_orm_atomic views create users with the hardcoded username "user1". When these endpoints are called more than once (either the same endpoint twice or both endpoints once), Django raises an IntegrityError because usernames must be unique. This prevents manual testing as described in the PR description and makes the endpoints non-idempotent. The views need to either generate unique usernames, delete existing users first, or handle the integrity constraint violation.

Fix in Cursor Fix in Web

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tests should be as deterministic as possible. The sample is intended to be very minimal.

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.

2 participants