-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastapi_example.py
More file actions
49 lines (35 loc) · 992 Bytes
/
fastapi_example.py
File metadata and controls
49 lines (35 loc) · 992 Bytes
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
49
"""FastAPI middleware example for LogTide Python SDK."""
from fastapi import FastAPI
from logtide_sdk import ClientOptions, LogTideClient
from logtide_sdk.middleware import LogTideFastAPIMiddleware
app = FastAPI()
# Initialize LogTide client
client = LogTideClient(
ClientOptions(
api_url="http://localhost:8080",
api_key="lp_your_api_key_here",
)
)
# Add middleware
app.add_middleware(
LogTideFastAPIMiddleware,
client=client,
service_name="fastapi-api",
log_requests=True,
log_responses=True,
log_errors=True,
include_headers=False,
skip_health_check=True,
)
@app.get("/")
async def root():
return {"message": "Hello from FastAPI!"}
@app.get("/users/{user_id}")
async def get_user(user_id: int):
return {"id": user_id, "name": "John Doe"}
@app.get("/error")
async def error_route():
raise ValueError("Test error")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=3000)