Tracepanel

What it records

The parts of Django that Tracepanel records with no setup, the Django libraries it traces, and the issues it finds in them.

With tracepanel.django installed, Tracepanel records these parts of Django. You don't configure any of them.

The ORM

Tracepanel records every SQL query on every database connection, with its parameters, its time, the rows it returned and the line of your code that ran it. The async ORM counts too, and Tracepanel keeps the place in your code across sync_to_async().

From the queries, the agent finds these issues:

  • N+1 queries. The same query repeated for each row, with the select_related() or prefetch_related() that removes it.
  • Duplicate queries. The same query with the same parameters, more than once in a request.
  • Slow SQL. A query over slow_sql_ms. The console runs EXPLAIN on it. A LIKE pattern that starts with %, as contains and icontains make, gets a hint about why no index helps.
  • Large results. A query without a LIMIT that returned 1,000 rows or more. Tracepanel counts the rows as Django fetches them. The hint names Paginator, .iterator() and .values_list().

A query that a template runs, such as a relation read inside {% for %}, shows under that template's render with the template's line, so you see which template caused an N+1. The N+1's hint names the template and the line. A query outside a child template's blocks names the base template. A query that no code of yours made, such as one from the admin, a paginator or a template tag, shows the library's line that asked for it, marked as a library's.

Transactions

Each atomic() block is a transaction, and a nested one is a savepoint. Each on_commit() callback shows where it ran. The agent reports two issues here:

  • Long transactions. An outer atomic() block longer than long_transaction_ms, with the work inside it.
  • Work inside a transaction. An email sent, a task queued or an HTTP call made before COMMIT. If the transaction rolls back, the email is already sent. The fix is transaction.on_commit().

Views, templates and the request

Each request names its route and its view, after the view's function or class. Each template render is a step in the call tree, with Django's engine or its Jinja2 backend. The panel shows the logged-in user, and a logout records the user who left.

The panel's Request tab shows what the request carried: its headers, query, cookies, the body as far as the view read it, the session the view used, and the response's headers and body. A form shows as fields, and an uploaded file by its name and size. A value under a secret's name, such as password, csrfmiddlewaretoken or sessionid, is masked. See Request.

A form's POST that answers with a redirect, such as a sign-in, shows in the panel before the page it led to.

Each template render keeps the context it got, with the context processors' entries, as Django's template debug panels show it. A value shows as repr() shows it, masked under a secret's name, and never runs a query: a QuerySet that hasn't run shows its model only. A template included in a loop keeps the context of its first 3 renders.

The panel's Profile button profiles your requests: where their time went, by package and by function, in a flame graph. See Profile.

The panel can show your site as another user. See Switching the user.

Cache, signals and emails

  • Cache. Each call on every backend you create through caches, with its key and whether it hit. On Redis, the Redis command shows inside the cache call. A cache call repeated per item is reported like an N+1, once the repeats cost 5 ms together.
  • Signals. Each signal with receivers, named by the signal, such as post_save, or by its dotted path, such as shop.signals.order_placed. The step has the sender, such as shop.Order, and the line that sent it, and a step for each receiver with its own file and line, so a slow post_save handler shows where it ran. An exception that send_robust() swallows shows on its receiver's step. A send that no receiver gets makes no step, and the trace counts it in signal.unreceived. The ignore_signals setting leaves out pre_init, post_init and class_prepared by default: they fire for every object a query loads and every model class built.
  • Emails. Each email sent with send_mail(), EmailMessage or EmailMultiAlternatives, with its text and HTML. The console's Emails page lists them.

File storage

Each call a file storage makes to its backend is a step, such as storage save avatars/1.png: save, open, delete, exists, listdir, size and the file's times. The step has the storage's alias in STORAGES, its class, its system, such as filesystem or s3, the path and the bytes written or read. This covers Django's storages, django-storages' and your own. A call made inside another, such as exists() inside save() to find a free name, is part of it. url() is a step where it does more than join strings, such as signing a URL on S3.

The agent finds the same issues as for other systems: an exists() per file is an N+1 with a listdir() hint, and the same file read twice is a duplicate. On the local disk, only many calls together count. A storage call over the network slower than slow_http_ms is slow. The console's Storage page lists the calls.

collectstatic shows where its time went: a step for each phase, clear, copy and post-process, with the 10 slowest files of each, and how many files it copied, linked, left unmodified and post-processed. Its job's first tab in the console sums it up.

Migrations

manage.py migrate is a trace of its own. Each migration it applies is a step, such as migrate shop 0012_add_sku, or unapply going back, with whether it ran in a transaction and whether it was faked. Each operation is a step inside, such as AddField Product.sku or RunPython backfill_sku, with the SQL the schema editor ran or the queries its code made, and the rows they changed. sqlmigrate runs nothing and records nothing.

An operation that locks or rewrites a table on your database is marked, with the reason:

  • PostgreSQL: a column type change that isn't binary coercible, a column made NOT NULL, and AddIndex without CONCURRENTLY, or a RemoveIndex unapplied.
  • MySQL: a column type change.
  • Any database: RunPython or RunSQL without its reverse.

Applied for real, not faked, such an operation is a migration-risk issue, with what to do instead. In the console, the job's first tab says what it did, such as 1 applied · 1 faked · 2 risky operations, and lists each migration with its operations, their rows and time, and the migrations of the plan it didn't get to.

The app's settings and checks

When a process connects to the agent, Tracepanel sends Django's settings and the versions of Python, Django and the packages that matter. The console shows them on the app's page, which a trace's App fact and the Services page open, and the panel on its App tab. A setting whose name holds a secret, such as SECRET_KEY or a PASSWORD in DATABASES, is masked, and so are the credentials in a URL.

A server process also works out what Django finds wrong with the app, once, in a thread of its own. Both list it above the settings, with its count on the App tab:

  • the system checks, as manage.py check reports them, the deployment ones too when DEBUG is off
  • the migrations not applied to the default database yet, as runserver warns of them
  • model changes with no migration, as makemigrations --check finds them

After a migrate trace, the warning drops the migrations it applied and links to it. capture_settings = false leaves the settings out.

Calls to other services

Tracepanel records what your app sends to other systems, each as a step in the call tree with the line of your code that made it:

  • HTTP calls from requests, urllib3, httpx and aiohttp. A call over slow_http_ms is reported as slow.
  • AWS calls from boto3 and aiobotocore, named as AWS names them, such as S3 PutObject avatars/1.png. A django-storages S3 storage's calls sit inside its storage steps. See AWS.
  • LLM calls through the openai and anthropic SDKs, with their model, tokens and estimated cost. See LLM calls.
  • Redis commands from redis-py and valkey-py, when you use Redis directly. A command repeated per item is reported like an N+1.
  • MongoDB queries from pymongo and Motor. See MongoDB.

The console has a page for each system your app called, with the calls, their time and the failures.

When your app calls another app that Tracepanel or OpenTelemetry traces, the two traces link. An outgoing HTTP call carries a traceparent header, and a Django request that comes with one continues that trace. The call tree shows the other app's trace under the call, and the console's Services page draws an arrow from app to app.

The console's Services page: the apps of a project, the servers they call, and an arrow for each kind of call

MongoDB

Each pymongo command is a step, from MongoClient, AsyncMongoClient or Motor. Tracepanel records the clients the app creates after it imports pymongo. A step shows the command's shape, such as {"find": "users", "filter": {"email": "?"}}, and keeps its values apart, as it does SQL parameters.

The agent finds these issues in MongoDB queries:

  • N+1 queries. A find for each item, with $in or $lookup as the fix.
  • Duplicate queries. The same find with the same values.
  • Slow calls. A query over slow_sql_ms. The hint names the index its filter and sort need, as the shell creates it: db.users.createIndex({"email": 1}).

The console's MongoDB page: the calls by collection and operation, with a slow find

Async views

In an async view, a synchronous call blocks the event loop, and every other request waits for it. That happens with the sync cache API, such as cache.get(), an HTTP call through requests, a redis-py command or a database driver used directly. The ORM itself refuses to run synchronously in an async view and raises SynchronousOnlyOperation.

Tracepanel marks such calls, and the agent reports them as blocking calls: one issue for each place in your code, once its calls cost 5 ms together. The hint names the async API to use instead, such as aget() and aset() on the cache, httpx.AsyncClient or redis.asyncio. A call that runs in a thread, through sync_to_async() or asyncio.to_thread(), blocks nothing and isn't marked.

Management commands

Each management command is a trace of its own, with the same SQL and issues as a request. The terminal prints a line with a link to it when the command ends. The ignore_commands setting leaves out the dev server, shells, the test runner and task workers.

Django libraries

Tracepanel traces these libraries when they are installed:

LibrarySettingWhat Tracepanel records
Django REST frameworkdrf_enabledThe view named after the ViewSet action, such as PostViewSet.list. Authentication, permissions with the class that refused, throttles, validation with the 400's errors, and serialization as steps.
Django Ninjaninja_enabledThe view named after the operation's function. Authentication, input validation with the 422's errors, and response serialization as steps.
Channelschannels_enabledEach WebSocket event as a trace: the connect, each frame, each channel layer message and the disconnect. Group messages link to the span that sent them. A message to a worker channel that runworker handles is a task.
Django Tasksdjango_tasks_enabledEach task run as a trace, linked to the request that queued it. django.tasks on Django 6.0 and later, and the django-tasks backport on Django 5.2.
Celerycelery_enabledEach task run as a trace, linked to the request that queued it, with its time in the queue, retries, failures, arguments and result.
Hueyhuey_enabledThe same for each task, through huey.contrib.djhuey or plain Huey.
django-q2django_q_enabledThe same for each task a cluster runs, async_task and schedule as the enqueue, and each hook as a trace of its own.
Strawberrystrawberry_enabledGraphQL requests named after the operation, and your resolvers as steps.
Graphenegraphene_enabledThe same for graphene-django's GraphQLView, with no MIDDLEWARE entry.

RQ, Dramatiq, Taskiq, SQLAlchemy, the database drivers, the HTTP clients, AWS, OpenAI, Anthropic, Redis, MongoDB, htmx and OpenTelemetry work the same as in any Python app. With mssql-django, SQL Server's queries get the same issues as other databases', and EXPLAIN shows SQL Server's estimated plan. See Integrations.

Tasks and the request that queued them

When a request queues a task, the call tree shows the enqueue as a step. Each run of the task is a trace of its own that links back to it. A task queued inside atomic() is reported as work inside a transaction, since the worker may run it before the data is committed.

The console's Tasks page: Now with Celery tasks and RQ jobs running and queued, and the workers that run them

A task that waited in its queue for a second or more is reported as a queue wait: the workers were all busy. Every task queue records the wait.

While tasks are queued, running or waiting for a retry, the Tasks page's Now lists them, with the workers and what each runs. Celery, RQ, Dramatiq, Huey, django-q2, Channels workers and Django Tasks send it, from a worker you start as usual. Tasks that run inside their caller, such as those of Django's ImmediateBackend and DummyBackend and eager Celery tasks, send nothing. See Tasks.

A task that a profiled request queues is profiled too, on the worker. Django Tasks carry nothing to the worker, so their runs aren't.

Turn a part off

Each library above has an *_enabled setting. Set it to False in the TRACEPANEL dict, and Tracepanel leaves the library alone:

# settings.py
TRACEPANEL = {"drf_enabled": False}

Tracepanel installs the integrations when Django starts. Restart the server after you change one. See Settings.

On this page