Using highlight.io with Python FastAPI
Make sure that you followed the fullstack mapping guide.
H.init("<YOUR_PROJECT_ID>", {
tracingOrigins: ['localhost', 'example.myapp.com/backend'],
networkRecording: {
enabled: true,
recordHeadersAndBody: true,
},
});
Download the package from pypi and save it to your requirements. If you use a zip or s3 file upload to publish your function, you will want to make sure highlight-io
is part of the build.
poetry add highlight-io[FastAPI]
# or with pip
pip install highlight-io[FastAPI]
Setup the SDK to with the FastAPI integration.
from fastapi import FastAPI, Request
import highlight_io
from highlight_io.integrations.fastapi import FastAPIMiddleware
H = highlight_io.H("YOUR_PROJECT_ID", record_logs=True)
app = FastAPI()
app.add_middleware(FastAPIMiddleware)
Check that your installation is valid by throwing an error. Add the following code to your FastAPI app and start the FastAPI server. Visit http://127.0.0.1:5000/hello in your browser. You should see a DivideByZero
error in the Highlight errors page within a few moments.
from fastapi import FastAPI, Request
import highlight_io
from highlight_io.integrations.fastapi import FastAPIMiddleware
H = highlight_io.H("YOUR_PROJECT_ID", record_logs=True)
app = FastAPI()
app.add_middleware(FastAPIMiddleware)
@app.get("/")
async def root(request: Request):
return {"message": f"This might not be a great idea {5 / 0}"}
With the Python SDK, your logs are reported automatically from the builtin logging methods (as long as record_logs=True
is provided to the highlight_io.H
constructor). Visit the highlight logs portal and check that backend logs are coming in.