Добавить наблюдаемость OpenTelemetry и Grafana на оценку 3
This commit is contained in:
+18
-17
@@ -30,9 +30,10 @@ from app.errors import (
|
||||
Unavailable,
|
||||
api_error_handler,
|
||||
common_headers,
|
||||
incoming_request_id,
|
||||
)
|
||||
from app.lifecycle import draining
|
||||
from app.config import settings
|
||||
from app.telemetry import HttpTelemetryMiddleware, configure, shutdown
|
||||
from app.models import (
|
||||
Attachment,
|
||||
Chat,
|
||||
@@ -61,30 +62,26 @@ from app.services import (
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_app: FastAPI) -> AsyncIterator[None]:
|
||||
configure("messenger-api", settings.instance_id)
|
||||
await storage.start()
|
||||
janitor = asyncio.create_task(janitor_loop())
|
||||
yield
|
||||
janitor.cancel()
|
||||
await asyncio.gather(janitor, return_exceptions=True)
|
||||
await storage.close()
|
||||
await session_cache.close()
|
||||
await engine.dispose()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
janitor.cancel()
|
||||
await asyncio.gather(janitor, return_exceptions=True)
|
||||
await storage.close()
|
||||
await session_cache.close()
|
||||
await engine.dispose()
|
||||
await asyncio.to_thread(shutdown)
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="MTUSI Messenger — лабораторная 5",
|
||||
version="1.5.0",
|
||||
title="MTUSI Messenger — лабораторная 6",
|
||||
version="1.6.0",
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
@app.middleware("http")
|
||||
async def add_request_id(request: Request, call_next):
|
||||
request.state.request_id = incoming_request_id(request)
|
||||
|
||||
response = await call_next(request)
|
||||
response.headers.update(common_headers(request))
|
||||
return response
|
||||
|
||||
@app.exception_handler(ApiError)
|
||||
async def handle_api_error(
|
||||
request: Request,
|
||||
@@ -458,3 +455,7 @@ async def download_attachment_route(
|
||||
media_type=headers.pop("Content-Type"),
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
|
||||
# Wrap outside FastAPI ServerErrorMiddleware to observe its final 500 response.
|
||||
app = HttpTelemetryMiddleware(app)
|
||||
|
||||
Reference in New Issue
Block a user