OrmaosDocs

Guides

Web backend

Record one request to a FastAPI service over SQLite, and read which queries it made, from which lines, and what its handlers caught.

View as Markdown

Record one request, not a server that waits for requests: a test, a script that calls the app through its test client, or one client call. The example is web-backend: a FastAPI service over SQLite with one route, asked for the orders of customer 3 through fastapi.testclient.

Record a request#

bash
ormaos run -- python main.py
web-backend
$ ormaos run -- python main.py
GET /customers/3/orders -> 200, 12 orders
/tmp/ormaos-docs-generate/venv/lib/python3.14/site-packages/fastapi/testclient.py:1: StarletteDeprecationWarning: Using `httpx` with `starlette.testclient` is deprecated; install `httpx2` instead.
  from starlette.testclient import TestClient as TestClient  # noqa
ormaos: r1 exit 0 in …: 1 Python process, 72 calls, 1 exception (1 swallowed), 111 io
ormaos: swallowed 1x KeyError at app.py:30, raised at app.py:29 [r1:e1]
ormaos: not collected: io: directory and file-system operations, at store.py:27 (once): a directory was listed, or a file or directory was made, renamed, changed or removed there; these are not recorded (+2 more)
ormaos: saved in .ormaos/r1, next: ormaos exceptions r1:e1 | ormaos tree r1:c1

The warning comes from the program (Starlette, through FastAPI's test client), before the summary. Then the summary: one Python process, one swallowed KeyError, and the first of three not collected notes.

Which queries did this request make, from which lines?#

The route's call is r1:c10, list_orders, in thread 2 (the test client runs the app in a thread of its own):

web-backend
$ ormaos io r1:c10
r1:c10 list_orders and the calls below it: 38 io events (sqlite 38)
  1x sqlite "SELECT id, name, tier FROM customers WHERE id = ?"  at app.py:21 in find_customer  [r1:i50]
  1x sqlite "SELECT id, placed FROM orders WHERE customer_id = ? ORDER BY placed"  at app.py:57 in list_orders  [r1:i51]
  12x sqlite "SELECT product_id, quantity FROM order_items WHERE order_id = ?"  at app.py:35 in items_of  [r1:i52]
  24x sqlite "SELECT cents FROM products WHERE id = ?"  at app.py:41 in price_of  [r1:i53]
calls directly below: 1 find_customer (1 io), 1 discount_for (no io), 12 order_total (36 io)
next: ormaos io r1:i50 | ormaos io r1:i51 | ormaos tree r1:c10

Each statement is grouped with the line of your code that ran it. The last line splits the I/O among the calls directly below list_orders: twelve order_total calls made 36 of the 38 queries.

The request itself is an HTTP event, made through httpx:

web-backend
$ ormaos io r1 --kind http
r1: 111 io events (sqlite 110, http 1), showing http
  1x http GET http://testserver/customers/3/orders -> 200  at main.py:14 in main  [r1:i48]
next: ormaos io r1:i48 | ormaos io r1:c5 | ormaos exceptions r1

What did the view call, in order?#

web-backend
$ ormaos tree r1:c10 --depth 1
r1:c10 list_orders(customer_id: int, names: bool, db: sqlite3.Connection) -> dict[2]  app.py:53  …  io 38  thread 2  calls below: 14
  r1:c11 find_customer(db: sqlite3.Connection, customer_id: int) -> sqlite3.Row  app.py:20  …  io 1  thread 2
  r1:c12 discount_for(tier: str[5]) -> int  app.py:27  …  thread 2  [swallowed 1x KeyError: r1:e1]
  r1:c13 order_total(db: sqlite3.Connection, order_id: int) -> int  app.py:49  …  io 4  thread 2  calls below: 2
  r1:c19 order_total(db: sqlite3.Connection, order_id: int) -> int  app.py:49  …  io 2  thread 2  calls below: 2
  r1:c23 order_total(db: sqlite3.Connection, order_id: int) -> int  app.py:49  …  io 3  thread 2  calls below: 2
  r1:c28 order_total(db: sqlite3.Connection, order_id: int) -> int  app.py:49  …  io 4  thread 2  calls below: 2
  r1:c34 order_total(db: sqlite3.Connection, order_id: int) -> int  app.py:49  …  io 2  thread 2  calls below: 2
  r1:c38 order_total(db: sqlite3.Connection, order_id: int) -> int  app.py:49  …  io 3  thread 2  calls below: 2
  r1:c43 order_total(db: sqlite3.Connection, order_id: int) -> int  app.py:49  …  io 4  thread 2  calls below: 2
  r1:c49 order_total(db: sqlite3.Connection, order_id: int) -> int  app.py:49  …  io 2  thread 2  calls below: 2
  r1:c53 order_total(db: sqlite3.Connection, order_id: int) -> int  app.py:49  …  io 3  thread 2  calls below: 2
  r1:c58 order_total(db: sqlite3.Connection, order_id: int) -> int  app.py:49  …  io 4  thread 2  calls below: 2
  r1:c64 order_total(db: sqlite3.Connection, order_id: int) -> int  app.py:49  …  io 2  thread 2  calls below: 2
  r1:c68 order_total(db: sqlite3.Connection, order_id: int) -> int  app.py:49  …  io 3  thread 2  calls below: 2
next: ormaos exceptions r1:e1 | ormaos tree r1:c13 | ormaos exceptions r1:c10

Did a handler catch an exception and answer anyway?#

web-backend
$ ormaos exceptions r1:e1
r1:e1 KeyError: swallowed
  raised at   app.py:29 in discount_for  [r1:c12]
  handled at  app.py:30 in discount_for  [r1:c12]  and not raised again
  message     not recorded; args: tuple[1 str]
next: ormaos tree r1:c12 | ormaos path r1:e1

discount_for raised a KeyError at line 29, caught it at line 30 and returned an int. The message (the key) is a value and is not recorded; args: tuple[1 str] says the key was one string. To see the lines around it, record again with --path app.py:discount_for and read ormaos path r2:e1.

What was logged?#

web-backend
$ ormaos logs r1:l1
r1:l1 INFO httpx "HTTP Request: %s %s \"%s %d %s\""  dropped by the level
  asked at    main.py:14 in main  [r1:c5]
  inside      r1:c1 <module>
  outcome     dropped: the level of the logger did not let it through
  arguments   5, not recorded
next: ormaos logs r1:c5 | ormaos tree r1:c5 | ormaos logs r1

httpx logged the request at INFO, and the level of its logger dropped the record. The template is recorded; its five arguments are not.

Notes for web backends#

  • A server that starts worker processes (uvicorn, gunicorn) is not in the compatibility matrix yet. Record a request through a test or the test client.
  • Sockets are not recorded; HTTP made through http.client, urllib, requests, urllib3 and httpx is.
  • To compare a request before and after a change: record both, then ormaos diff r1 r2. Queries are matched by statement and line.