What the agent does
The agent reads the settings before it changes them. In a project shaped like HackSoftware/Django-Styleguide-Example, requirements/base.txt pins Django 5.2.7, djangorestframework 3.16.1, django-structlog 9.1.1, gunicorn 23.0.0 and sentry-sdk, with pytest-django, ruff and mypy in requirements/local.txt. config/django/base.py builds INSTALLED_APPS from LOCAL_APPS and THIRD_PARTY_APPS, lists eight middlewares starting with SecurityMiddleware and CorsMiddleware, points DATABASES at postgres:///styleguide_example through env.db, and then hands INSTALLED_APPS and MIDDLEWARE to LoggersSetup.setup_settings, which inserts the structlog middleware. config/urls.py mounts the API under api/ and the admin under admin/. Procfile runs gunicorn config.wsgi:application, and gunicorn.conf.py already formats access logs as logfmt through structlog, so the team cares about operational output.
The agent adds django-prometheus to requirements/base.txt and django_prometheus to THIRD_PARTY_APPS. In MIDDLEWARE it puts django_prometheus.middleware.PrometheusBeforeMiddleware first and PrometheusAfterMiddleware last, so request counts and latency histograms cover every other middleware, and does this before LoggersSetup.setup_settings runs so the structlog middleware is still inserted where that helper expects. DATABASES["default"]["ENGINE"] becomes django_prometheus.db.backends.postgresql, including the GITHUB_WORKFLOW branch that hard-codes the CI database, so query counts and errors are exported from both. config/urls.py gains path("", include("django_prometheus.urls")), which serves /metrics.
Because gunicorn forks workers, per-process counters would be wrong without multiprocess mode. The agent sets PROMETHEUS_MULTIPROC_DIR in gunicorn.conf.py through a child_exit hook that calls prometheus_client.multiprocess.mark_process_dead, documents the directory in .env.example, and leaves the Celery worker alone since its tasks are already traced by Sentry. A metrics test module under styleguide_example/api/tests/ uses DRF's APIClient to call GET /api/users/ and then GET /metrics, asserting the django_http_requests_total_by_view_transport_method counter names the users view and that the django_db_execute_total family is present.
The agent runs ruff check ., mypy --config mypy.ini styleguide_example/ and pytest, the three commands .github/workflows/django.yml runs on Python 3.12.4 against a PostgreSQL service, and also docker compose build since the docker_build job rebuilds docker/local.Dockerfile with the new requirement. It pastes a trimmed /metrics sample and the pytest summary in the pull request.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "codex",
"input": "Add Prometheus metrics to this Django REST Framework project and open a pull request. Read requirements/base.txt, config/django/base.py, config/settings/loggers/setup.py, config/urls.py, gunicorn.conf.py, Procfile, pytest.ini, .github/workflows/django.yml and styleguide_example/api/tests/pagination/test_get_paginated_response.py first. Add django-prometheus to requirements/base.txt and django_prometheus to THIRD_PARTY_APPS. Put PrometheusBeforeMiddleware first and PrometheusAfterMiddleware last in MIDDLEWARE before LoggersSetup.setup_settings is called. Switch both DATABASES definitions to the django_prometheus postgresql backend. Mount django_prometheus.urls at the root of config/urls.py so /metrics is served. Configure multiprocess mode for gunicorn with PROMETHEUS_MULTIPROC_DIR and a child_exit hook in gunicorn.conf.py, and document the variable in .env.example. Add a test under styleguide_example/api/tests/ that calls an API endpoint with APIClient and then asserts /metrics names that view and includes the database execute counter. Run ruff check ., mypy --config mypy.ini styleguide_example/, pytest and docker compose build, and paste the output plus a short /metrics sample in the pull request. Do not change the Celery setup, Sentry settings or any API view.",
"resources": [
{
"url": "https://github.com/your-org/your-app"
}
]
}- Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
- Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
- Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.
What comes back
What comes back is a pull request touching requirements/base.txt, config/django/base.py, config/urls.py, gunicorn.conf.py, .env.example and a new test module under styleguide_example/api/tests/. The description shows the pytest summary, the ruff and mypy results, and a dozen lines of /metrics output including the request counter labelled with the users view and the database counters.
On a repository with the styleguide's workflow, the pull request runs both jobs: the docker_build job rebuilds the image and runs ruff, mypy and pytest inside it, and the build job runs them against the postgres:latest service with GITHUB_WORKFLOW set, which exercises the CI branch of DATABASES the agent also switched.
A reviewer checks that /metrics is not reachable from the public internet without an allow-list or a scrape-only ingress, that PROMETHEUS_MULTIPROC_DIR is on a writable path in their container, and that the structlog middleware is still in the position LoggersSetup expects. Then they add the scrape target to their Prometheus config.
Run it on every service onboarded to monitoring
A platform team bringing services onto Prometheus sends one POST /o/{org}/sessions per repository with this prompt and that repository in resources. Each run reads its own settings module and WSGI server config, so a service on uvicorn gets the multiprocess setup for that server, and each run opens its own pull request.
Give the API key a webhook_url and Rush POSTs run.completed when each turn finishes, with the session_url to read the pull request and the /metrics sample from.
The use case this job follows, with its automation and trigger: Hand off a long task.
Why run it on Rush?
The library is a one-line install, but the three places it has to be wired, the middleware order, the database engine string and gunicorn's multiprocess directory, are exactly where a hand-made change goes wrong quietly: metrics that exist but miss half the requests, or counters that reset per worker. In a Rush sandbox the agent runs the suite against a real PostgreSQL, scrapes /metrics from a running gunicorn, and keeps every command and its output in the run's record.
The same request works unchanged on every Django service a team owns, and because the run opens a pull request through the Rush GitHub App, the monitoring change arrives with the workflow already green rather than as a checklist for whoever is on call.
Frequently asked questions
Why does the middleware have to be first and last?
PrometheusBeforeMiddleware records the start time and PrometheusAfterMiddleware the status and duration, so anything outside that pair, such as SecurityMiddleware redirects, would be uncounted. The pull request keeps LoggersSetup.setup_settings after the change so structlog is inserted into the already-wrapped list.
Does the database backend change affect migrations or queries?
No. django_prometheus.db.backends.postgresql subclasses Django's PostgreSQL backend and only wraps cursor execution to count queries and errors. Existing migrations, ATOMIC_REQUESTS and the test database behave the same.
Will /metrics be public?
It is served by the Django app like any URL, so whatever sits in front of gunicorn decides. The pull request notes that the path should be restricted to the scraper's network or protected at the proxy, and does not add authentication to it.
What about Celery tasks?
They are not instrumented by this change. django-prometheus covers HTTP requests, database queries, cache and model operations; task metrics would be a separate exporter, which the pull request leaves out on purpose.