-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfastapi-example.py
48 lines (34 loc) · 951 Bytes
/
fastapi-example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import asyncio
import uvicorn
from autometrics import autometrics
from fastapi import FastAPI, Response
from prometheus_client import generate_latest
app = FastAPI()
# Set up a metrics endpoint for Prometheus to scrape
# `generate_latest` returns the latest metrics data in the Prometheus text format
@app.get("/metrics")
def metrics():
return Response(generate_latest())
# Set up the root endpoint of the API
@app.get("/")
@autometrics
def read_root():
do_something()
return {"Hello": "World"}
# Set up an async handler
@app.get("/async")
@autometrics
async def async_route():
message = await do_something_async()
return {"Hello": message}
@autometrics
def do_something():
print("done")
@autometrics
async def do_something_async():
print("async start")
await asyncio.sleep(2.0)
print("async done")
return "async world"
if __name__ == "__main__":
uvicorn.run(app, host="localhost", port=8080)