-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (48 loc) · 1.78 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
# Import necessary modules
from asyncio import sleep
from typing import Any
from fastapi import FastAPI, Request
from asgi_correlation_id import CorrelationIdMiddleware, correlation_id
import structlog
import sys
import logging
from other_file import some_helper_function
def add_correlation(
logger: logging.Logger, method_name: str, event_dict: dict[str, Any]
) -> dict[str, Any]:
"""Add request id to log message."""
if request_id := correlation_id.get():
event_dict["request_id"] = request_id
return event_dict
# Configure structlog to print to console
structlog.configure(
processors=[
add_correlation,
structlog.processors.TimeStamper(fmt="iso"),
structlog.stdlib.add_log_level,
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.dev.ConsoleRenderer() # Use ConsoleRenderer to print to console
],
cache_logger_on_first_use=True,
)
logger = structlog.get_logger()
# Initialize the FastAPI app
app = FastAPI()
# Add CorrelationIdMiddleware to the FastAPI app
app.add_middleware(CorrelationIdMiddleware)
# Sample endpoint to demonstrate correlation ID
@app.get("/")
async def read_root(request: Request):
# Get the correlation ID from the request context
current_correlation_id = correlation_id.get()
# Log a message that includes the correlation ID
logger.info("Handling request")
await sleep(2)
await some_helper_function()
await sleep(2)
logger.info("Finished request")
return {"message": "Hello, world!", "correlation_id": current_correlation_id}
# Run with: uvicorn filename:app --reload
# Ensure you have installed the dependencies asgi_correlation_id, fastapi, and structlog
# pip install fastapi asgi-correlation-id uvicorn structlog