Python
Flask, FastAPI, Starlette, Django or plain Python 3.9 and newer. No dependencies.
1. Install#
The SDK is one file, kept with your code (it isn't on PyPI yet). In your server's folder, with your Controxity's address:
curl -fsSL https://controxity.example.com/sdk/python.py -o controxity.pyimport controxity then finds it beside your code.
Get a key under Where it runs → Add a place and keep it in CONTROXITY_KEY.
2. Start it, as early as you can#
import os, controxity
controxity.init(os.environ["CONTROXITY_KEY"], os.environ["CONTROXITY_URL"], version="1.4.2")3. Connect your framework#
controxity.flask(app) # older Flask also needs: pip install blinkercontroxity.fastapi(app) # FastAPI or StarletteMIDDLEWARE = [
# …
"controxity.Django",
]# in your error handler (new_trail() at the start of each request keeps its steps apart):
controxity.capture_error(error, input={"params": params, "query": query, "body": body})4. Check it works#
print(controxity.check()) # (True, "Connected to controxity.example.com")Then make one route raise: it's on Problems within a minute.
The hooks send the request's route, params and query with each error, as steps and a shape (Flask and Django: its body too). Each request keeps its own steps.
5. Ship safely (optional)#
# What must always be true. A broken one is a problem, but nothing is raised:
controxity.expect("total-not-negative", total >= 0, input={"cart": cart})
# New or risky code, off until a person turns it on, and off again by itself if it hurts:
if controxity.flag("new-checkout", id=user.id):
return new_checkout(cart)
# A step worth seeing before an error (routes, calls out and events are noted by themselves):
controxity.step("cart.loaded")Expectations · Switches · Steps before an error. In CI: Releases from CI.
Also useful#
controxity.capture_error(error, where="orders") # an error you caught
controxity.health("database", False, "high") # down; health("database", True) clears it
controxity.event("payment_failed") # counted
controxity.report(title="Booking failed", what=text) # what a person told you
controxity.problems() # where your errors standEvery function, with what it sends: Python SDK reference.
Is it up?#
It sends a heartbeat once a minute. If they stop for 3 minutes, the server shows Down. shutdown(), called by itself when Python exits normally, says it's stopping on purpose. In a script that isn't a server, pass heartbeat=False.
If nothing shows up#
- Look for a line starting
controxity:in your log. - "The key was refused": nothing more is sent for an hour (an app with an old key doesn't knock on Controxity every minute). Fix the key and restart, or run
check(), and it sends at once. - Run
check(). - A route's error needs step 3.
- It never raises into your app. If Controxity can't be reached, signals wait for the next send.
Turning things off#
Argument of init() | Off means |
|---|---|
capture_input=False | No input shapes |
breadcrumbs=False | No steps |
capture_uncaught=False | Uncaught exceptions aren't reported |
heartbeat=False | No heartbeat (not a server) |