This release is a pre-release and may not be stable for production use.
flask-redline
The Flask + SQLAlchemy adapter for Redline: a request profiler and
backend analyzer. Every request becomes a trace with a span timeline, the SQL it ran, N+1
and duplicate-query detection with concrete fix diffs, one-click EXPLAIN, optional
flamegraphs, and a fast web UI served from your own app.
from flask import Flask
from sqlalchemy import create_engine
from flask_redline import Redline
app = Flask(__name__)
engine = create_engine("sqlite:///app.db")
# access="local": localhost only (unset, Redline answers 404 outside debug mode)
Redline(app, engine=engine, settings={"access": "local"}) # UI at http://localhost:5000/_redline/
With Flask-SQLAlchemy you do not even pass the engine: its engines (including
SQLALCHEMY_BINDS) are found and instrumented automatically.
db = SQLAlchemy(app)
Redline(app)
Traces are stored in their own SQLite file (.redline/redline.db), never in your database.
Install
pip install \
"flask-redline @ git+https://github.com/Pythondeveloper6/redline#subdirectory=packages/flask-redline" \
"redline-core @ git+https://github.com/Pythondeveloper6/redline#subdirectory=packages/redline-core"
pip install "flask[async]" # only if you use `async def` views
Not on PyPI yet: install from GitHub, naming redline-core in the same command (the adapter
pins it exactly). Docs: https://pythondeveloper6.github.io/redline/.
Add the extra to the core requirement for flamegraphs:
"redline-core[profile] @ git+https://github.com/Pythondeveloper6/redline#subdirectory=packages/redline-core".
Python 3.10+, Flask 3.0+, SQLAlchemy 2.x.
Redline
Redline(
app=None,
*,
engine=None, # Engine | [engines] | {"alias": engine} (default: Flask-SQLAlchemy's)
settings=None, # dict | redline_core.Settings
url_prefix=None, # default: the mount_path setting, else "/_redline"
permission=None, # callable(request) -> bool (see "Access control")
store=None, # custom Store (default: SQLite file at storage_path)
)
redline.init_app(app) # application factories
What it does:
- wraps
app.wsgi_appin a WSGI middleware that times the request and records status, headers and (size-capped) bodies; - connects Flask hooks for the route template (
/orders/<int:order_id>), the view function, templates and unhandled errors; - instruments the engines;
- registers a blueprint with the UI and JSON API under
url_prefix, and theflask redlinecommand group.
settings takes any Redline setting with case-insensitive
keys. They can also live in app.config["REDLINE"] (a dict); keys passed to Redline
win. A Settings instance passed to Redline is used as-is.
Redline(app, engine=engine, settings={
"sample_rate": 0.1, # keep 10% of requests ...
"always_keep_slower_than_ms": 300, # ... plus every slow one and every error
"slow_query_ms": 50,
})
Application factory
redline = Redline(engine=engine)
def create_app():
app = Flask(__name__)
redline.init_app(app)
return app
redline.store, .settings, .engines, .add_engine(engine, alias=None) and .close()
act on the current app (inside an app context) or on the last app initialised. The
store is flushed and closed at interpreter exit.
Engines
The first engine of a list is the "default" db alias; others are named after their
database file, or pass a dict. The alias is stored on each query so EXPLAIN runs on the
right engine.
Redline(app, engine=[main_engine, analytics_engine])
Redline(app, engine={"main": main_engine, "analytics": analytics_engine})
redline.add_engine(late_engine) # an engine created later
When engine is omitted and Flask-SQLAlchemy 3.x is set up on the app, its engines are
discovered on the first request (they may not exist yet when Redline(app) runs), named
default and after each bind key.
What is recorded
- Request / response: method, path, route template, view dotted name, status, headers,
query string and body. Secrets are redacted by the core (
Authorization, cookies,password/token/cardkeys...). - Bodies are never consumed or buffered unboundedly. The request body is copied as the
view reads it; response bodies, including streams, are kept only up to
max_body_bytes. Astream_with_contextgenerator is not read ahead, and the trace ends when the last chunk is sent, so its work counts towards the latency.send_fileand anything the server serves throughwsgi.file_wrapper(direct_passthrough) is handed back untouched: only its size is recorded. - SQL through
before/after_cursor_execute: statement, bound params (redacted), duration, rowcount, vendor, executemany,db_alias, and the project line that issued the query. - Spans: a
viewspan around the view function (not thebefore_requesthooks), and atemplatespan perrender_template/render_template_string; queries a template triggers (lazy loading) nest under it. - Unhandled exceptions are recorded (Flask's
got_request_exception) and re-raised or turned into a 500 exactly as before.abort(404)and other 4xxHTTPExceptions are client errors, not failures: the trace has noerror, andalways_keep_errorsdoes not keep it. A 5xx status is an error. async defviews (Flask's asgiref bridge) keep the trace: queries and spans created inside the coroutine land in the request's trace. Work that starts a barethreading.Threadis not attributed to any request.- Your own spans:
with redline_core.span("pricing.compute"): ...(also as a decorator). X-Redline-Run: <id>;step=<n>becomes tags (used by the load tester).X-Redline-Check: <id>;req=<i>;n=<k>becomes tags (used byredline check, only for a check run that exists in the store).- Requests under
url_prefixare never recorded.
If you put werkzeug.middleware.proxy_fix.ProxyFix (or any other WSGI middleware) around
the app, wrap after creating Redline so the recorded client address is the real one.
Apps mounted under a SCRIPT_NAME (DispatcherMiddleware) record the full path and route.
Outbound HTTP (opt-in)
Nothing is patched unless you ask:
from flask_redline import instrument_httpx, instrument_requests
instrument_httpx() # every httpx.Client / AsyncClient call becomes an http_out span
instrument_requests() # same for requests.Session.send (needs `requests` installed)
URLs are redacted (?api_key=... is masked). uninstrument_httpx() and
uninstrument_requests() undo it.
Profiling
Flamegraphs are opt-in per request: add ?_redline=profile or the header
X-Redline-Profile: 1 (needs the [profile] extra).
Fix suggestions
For an N+1, SQLAlchemyFixSuggester maps the repeated query back to your models and tells
you how to eager-load it:
| Repeated query | Suggestion |
|---|---|
| child table filtered by its foreign key | selectinload(Parent.children) |
| parent looked up by primary key, once per row | joinedload(Child.parent) |
| association (secondary) table filtered by the parent | selectinload(Parent.tags) |
It then walks the call stack to find the select(Parent) or session.query(Parent) in
your source and produces a real unified diff. This works the same with Flask-SQLAlchemy's
db.session and db.Model. When the statement cannot be located you get the one-line
code hint with lower confidence instead.
Access control
The UI exposes your SQL, request bodies and EXPLAIN, so treat it like an admin tool.
Default. With no permission, the access setting decides (default local, served only in debug mode until you set access): only loopback clients (127.0.0.0/8, ::1; unix sockets only with trust_unix_socket)
that address the server by a loopback name (Host: localhost, 127.0.0.1, [::1], any
port; this blocks DNS rebinding) are served, and a warning is logged at startup. Refusals
are JSON {"error": {"code": "forbidden", "message"}}. Add other host names with the
allowed_hosts setting. A loopback peer sending X-Forwarded-For without being in
trusted_proxies is refused; behind a reverse proxy use access="token" and
trusted_proxies. Tokens, basic auth, allowed_ips and read_only are described in the
access control docs.
Your own check. permission is any callable(request) -> bool, called with Flask's
request; passing it implies access="framework" and replaces the loopback default, so the
Host check becomes yours (the core gate still applies allowed_ips, read_only, features
and CSRF).
from flask_login import current_user
Redline(app, engine=engine, permission=lambda request: current_user.is_admin)
Raise werkzeug.exceptions.abort(401) (or any HTTPException) inside it to answer with
that status; it is rendered as {"error": {"code": "unauthorized", ...}}, keeping a
WWW-Authenticate header.
CSRF. Every non-GET request is refused with 403 csrf_failed when it carries
Sec-Fetch-Site: cross-site or same-site, or an Origin (including null) that does not
match the request host. Browsers always send Origin on those methods, so another website
cannot drive your Redline through a logged-in browser. Non-browser clients (curl, scripts)
send no Origin and are only subject to the access policy.
Redline's own responses carry X-Frame-Options: DENY.
EXPLAIN
POST /_redline/api/explain runs EXPLAIN for a recorded SELECT on the engine it came
from (matched by db_alias) inside a transaction that is always rolled back (and
READ ONLY on PostgreSQL). EXPLAIN ANALYZE executes the query, so it is off unless
explain_analyze=True.
Command line
flask redline clear [--yes] # delete every recorded trace
flask redline prune [--max-age-days N] [--max-rows N] # apply retention now (0 disables a rule)
Development
uv sync --all-packages
cd packages/flask-redline && uv run pytest
A runnable demo with deliberately bad views lives in examples/flask.
License
MIT
Metadata
Release files for flask-redline 0.3.0a1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| flask_redline-0.3.0a1.tar.gz | 43.1 kB | With provenance | Details for flask_redline-0.3.0a1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| flask_redline-0.3.0a1-py3-none-any.whl | Python 3 | none | any | Details for flask_redline-0.3.0a1-py3-none-any.whl |
Total release size: 71.1 kB
Release files / flask_redline-0.3.0a1.tar.gz
| Download URL | flask_redline-0.3.0a1.tar.gz |
|---|---|
| Size | 43.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8f9450d7c685cd7328afbb7f728fce713416166dde3051f8282ef7847ca55940
|
|
BLAKE2b-256 checksum How to use checksums |
f478a7009fbe28855675d42bd97fb40ada4544393ddba4853f9d936a89eeab90
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.
Transparency logRelease files / flask_redline-0.3.0a1-py3-none-any.whl
| Download URL | flask_redline-0.3.0a1-py3-none-any.whl |
|---|---|
| Size | 28.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cd09178a0fa11330a6d8eb8afd5ffd097a9a3bb60c4573388d5b159bc9745b9f
|
|
BLAKE2b-256 checksum How to use checksums |
f70f505d6ae9c7041384bb425a14688a2a19a9011cfdd4b7639d611b593cdf83
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.
Transparency logSecurity for flask-redline 0.3.0a1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for flask_redline-0.3.0a1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.