-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
46 lines (31 loc) · 989 Bytes
/
main.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
import time
import uvicorn
from fastapi import FastAPI, HTTPException, Response
from autometrics import autometrics
from autometrics.objectives import Objective, ObjectiveLatency, ObjectivePercentile
from prometheus_client import start_http_server, generate_latest
app = FastAPI()
API_SLO = Objective(
"api",
success_rate=ObjectivePercentile.P99_9,
latency=(ObjectiveLatency.Ms250, ObjectivePercentile.P90),
)
@app.get("/")
@autometrics(objective=API_SLO)
async def read_root():
return {"Hello": "World"}
@app.get("/error")
@autometrics(objective=API_SLO)
async def error_function():
raise HTTPException(status_code=404, detail="Internal Server Error")
@app.get("/slow")
@autometrics(objective=API_SLO)
async def slow_function():
time.sleep(2.4)
return {"I am": "Slow"}
@app.get("/metrics")
def metrics():
return Response(generate_latest())
start_http_server(8080)
if __name__ == "__main__":
uvicorn.run(app, host="localhost", port=8080)