-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
43 lines (34 loc) · 1.25 KB
/
Copy pathmain.py
File metadata and controls
43 lines (34 loc) · 1.25 KB
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
from fastapi import FastAPI
from contextlib import asynccontextmanager
from app import api, health
from app import database, telemetry
from prometheus_fastapi_instrumentator import Instrumentator
from loguru import logger
import sys
# Remove default loguru handler
logger.remove()
# Initialize loguru handler for structured logging
logger.add(sys.stderr, serialize=True)
# Initialize OpenTelemetry tracer
tracer = telemetry.setup_opentelemetry()
# FastAPI lifespan to initialize the database and tables on startup
@asynccontextmanager
async def lifespan(_app: FastAPI):
# Startup logic. Fail fast if database is not available
try:
logger.info("Attempting to initialize database...")
database.create_db_and_tables()
logger.info("Database initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize database: {e}")
sys.exit(1)
yield
# Initialize FastAPI app
app = FastAPI(lifespan=lifespan)
# Instrument FastAPI with OpenTelemetry tracing
telemetry.instrument_fastapi(app)
# Instrument FastAPI with Prometheus metrics
Instrumentator().instrument(app).expose(app, include_in_schema=False, should_gzip=True)
# Include routers
app.include_router(api.router)
app.include_router(health.router)