Python
opentelemetry-instrument with WizeLens defaults. N+1 queries, slow queries, SQL parameters, request bodies, and logs for FastAPI, Flask, and Django.
On this page 7 sections
Start with opentelemetry-instrument
Python apps send to WizeLens through OpenTelemetry auto-instrumentation. It traces FastAPI, Flask, Django, SQLAlchemy, psycopg, sqlite3, requests, httpx, and more, with no code change. Requires Python 3.10 or newer.
Install from GitHub: the first release of
wizelensis not on PyPI yet. Until it is, install it from GitHub:pip install "wizelens @ git+https://github.com/wizeworksllc/wizelens#subdirectory=python". The commands below then work as written.
pip install wizelens
opentelemetry-bootstrap -a install # instrumentations for the libraries you use
OTEL_SERVICE_NAME=orders-api opentelemetry-instrument uvicorn app:app
For Django, name the settings in the environment and turn off the reloader. Without DJANGO_SETTINGS_MODULE, the
instrumentation loads Django first and it stops with "You must set settings.ALLOWED_HOSTS". Under Gunicorn or
uWSGI, run opentelemetry-instrument gunicorn shop.wsgi the same way.
DJANGO_SETTINGS_MODULE=shop.settings OTEL_SERVICE_NAME=shop opentelemetry-instrument python manage.py runserver --noreload
opentelemetry-instrument finds the wizelens package on its own: it is an OpenTelemetry distro and configurator.
Do not also install opentelemetry-distro. If you must, set OTEL_PYTHON_DISTRO=wizelens and
OTEL_PYTHON_CONFIGURATOR=wizelens.
The insights work on what the instrumentations send: N+1 queries, N+1 HTTP calls, slow queries, and database waits.
What the package adds
| What | Plain OpenTelemetry | With wizelens |
|---|---|---|
| Where it sends | You set OTEL_ |
WIZELENS_, else http:/ |
| Sampling | Head sampling: errors can be dropped | Keeps every trace with an error or a slow span |
| SQL parameters | None, so no duplicate-query insight | On every DB-API driver and SQLAlchemy, sensitive names redacted |
connection. (sqlite3, psycopg 3) |
Not traced | Traced |
| Logs | Off unless you turn them on | Every logging line, with its trace |
| Metrics | You set OTEL_ |
Sent with traces and logs (OTEL_) |
| Request and response headers | Off | On, secrets redacted, Set-Cookie names and flags kept |
| FastAPI | A span per message sent | One server span per request |
Anything you set yourself with OTEL_* wins.
Request and response bodies
Add one line for your framework, then set WIZELENS_CAPTURE_BODY=true:
| Framework | Code |
|---|---|
| FastAPI, Starlette | app. (from wizelens import WizeLensMiddleware) |
| Flask | capture_ (from wizelens import capture_) |
| Django | Add "wizelens. to MIDDLEWARE (the instrumentation puts its own first) |
Text and JSON bodies are kept, up to 16 KB each. JSON keys that look like secrets are redacted.
Without opentelemetry-instrument
import wizelens
wizelens.configure(service="orders-api") # before your app imports its libraries
Then instrument libraries yourself, for example FastAPIInstrumentor.instrument_app(app). For a driver WizeLens does
not patch, put the parameters on the span with wizelens.record_parameters(params).
configure also takes settings=wizelens.Settings(...) to set values in code instead of WIZELENS_* variables. If you set
up OpenTelemetry yourself without configure, wizelens.capture_dbapi_parameters(settings) and
wizelens.capture_sqlalchemy_parameters(settings) put SQL parameters on the DB-API and SQLAlchemy spans.
Answers you expect
When a 5xx is on purpose, such as a 503 "come back later", set wizelens.expected on the request's server span. WizeLens
then leaves it out of error rates, and the sampler does not keep the trace for that status alone:
from opentelemetry import trace
trace.get_current_span().set_attribute("wizelens.expected", True)
See Answers you expect.
Settings
| Variable | Default | What it does |
|---|---|---|
WIZELENS_ |
http:/ |
Hub or cloud address |
WIZELENS_ |
none | Hub token or cloud ingest key |
WIZELENS_ |
none | Project on a shared hub |
WIZELENS_ |
1 |
Share of traces kept. |
WIZELENS_ |
1000 |
Keep traces with a span this slow at any rate (off for none) |
WIZELENS_ |
true |
Keep traces with an error at any rate |
WIZELENS_ |
false |
Request and response bodies |
WIZELENS_ |
true |
SQL parameters |
WIZELENS_ |
true |
logging lines |
WIZELENS_ |
none | More key fragments to redact, comma-separated |
WIZELENS_ |
true |
Send the app's package names and versions once at start, for the known-vulnerability check |
WIZELENS_ |
true |
false turns OpenTelemetry off |
The package is on wherever it runs, in development and in production. With WIZELENS_PACKAGES on, the hub or the cloud
checks the packages against OSV and shows each vulnerable version on the Security page. Only names and versions leave the
app.
A runnable sample
samples/python-fastapi is a FastAPI
service with SQLite. GET /orders shows N+1 queries, a duplicate query, and N+1 HTTP calls to the Go sample;
GET /report shows a slow query and a database wait; GET /fail shows an error with its log line.
cd samples/python-fastapi
pip install -r requirements.txt && opentelemetry-bootstrap -a install
OTEL_SERVICE_NAME=orders-python WIZELENS_CAPTURE_BODY=true opentelemetry-instrument uvicorn app:app --port 5302