|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import os |
| 6 | +import threading |
6 | 7 | from pathlib import Path |
7 | 8 |
|
8 | 9 | from dotenv import load_dotenv |
|
12 | 13 | from slowapi.errors import RateLimitExceeded |
13 | 14 | from starlette.middleware.trustedhost import TrustedHostMiddleware |
14 | 15 |
|
15 | | -from server.routers import dynamic, index, ingest, metrics |
| 16 | +from server.metrics_server import start_metrics_server |
| 17 | +from server.routers import dynamic, index, ingest |
16 | 18 | from server.server_config import templates |
17 | 19 | from server.server_utils import lifespan, limiter, rate_limit_exception_handler |
18 | 20 |
|
|
26 | 28 | # Register the custom exception handler for rate limits |
27 | 29 | app.add_exception_handler(RateLimitExceeded, rate_limit_exception_handler) |
28 | 30 |
|
| 31 | +# Start metrics server in a separate thread if enabled |
| 32 | +if os.getenv("GITINGEST_METRICS_ENABLED", "false").lower() == "true": |
| 33 | + metrics_host = os.getenv("GITINGEST_METRICS_HOST", "127.0.0.1") |
| 34 | + metrics_port = int(os.getenv("GITINGEST_METRICS_PORT", "9090")) |
| 35 | + metrics_thread = threading.Thread( |
| 36 | + target=start_metrics_server, |
| 37 | + args=(metrics_host, metrics_port), |
| 38 | + daemon=True, |
| 39 | + ) |
| 40 | + metrics_thread.start() |
| 41 | + |
29 | 42 |
|
30 | 43 | # Mount static files dynamically to serve CSS, JS, and other static assets |
31 | 44 | static_dir = Path(__file__).parent.parent / "static" |
@@ -159,8 +172,6 @@ def openapi_json() -> JSONResponse: |
159 | 172 |
|
160 | 173 |
|
161 | 174 | # Include routers for modular endpoints |
162 | | -if os.getenv("GITINGEST_PROMETHEUS_TOKEN") is not None: |
163 | | - app.include_router(metrics) |
164 | 175 | app.include_router(index) |
165 | 176 | app.include_router(ingest) |
166 | 177 | app.include_router(dynamic) |
0 commit comments