-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
265 lines (227 loc) · 8.76 KB
/
main.py
File metadata and controls
265 lines (227 loc) · 8.76 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""
Entry point for the Resolver Analysis Engine API server.
Copyright (c) 2026 Stefan Kumarasinghe
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
"""
from __future__ import annotations
import asyncio
import contextlib
import logging
import sys
import time
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
import httpx
import uvicorn
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from fastapi.routing import APIRoute
from pydantic import BaseModel, Field
from api.routes import router
from api.routes.common import close_providers
from config import LOGS_BACKEND_LOKI, METRICS_BACKEND_MIMIR, TRACES_BACKEND_TEMPO, Settings, settings
from database import dispose_database, init_database, init_db
from datasources.exceptions import BackendStartupTimeout
from middleware.openapi import install_custom_openapi
from services.rca_job_service import rca_job_service
from services.security_service import InternalAuthMiddleware
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s %(message)s",
stream=sys.stdout,
)
log = logging.getLogger(__name__)
_BACKEND_READY = False
_BACKEND_STATUS: dict[str, str] = {}
OPENAPI_TAGS = [
{"name": "Health", "description": "Service and backend readiness endpoints."},
{"name": "RCA", "description": "Root cause analysis workflows and templates."},
{"name": "Metrics", "description": "Metric anomaly and changepoint analysis."},
{"name": "Logs", "description": "Log pattern and burst analysis."},
{"name": "Traces", "description": "Trace latency and error propagation analysis."},
{"name": "Correlation", "description": "Cross-signal temporal correlation endpoints."},
{"name": "SLO", "description": "Service-level objective error budget analysis."},
{"name": "Topology", "description": "Service dependency and blast radius analysis."},
{"name": "Events", "description": "Deployment event ingestion and lifecycle endpoints."},
{"name": "Forecast", "description": "Degradation trajectory and time-to-failure forecasting."},
{"name": "Causal", "description": "Causality analysis across metric and signal features."},
{"name": "ML", "description": "Adaptive signal-weight model controls and feedback."},
{"name": "RCA Jobs", "description": "Asynchronous RCA job creation, polling, and reports."},
]
def _openapi_servers() -> list[dict[str, str]]:
scheme = "https" if settings.ssl_enabled else "http"
explicit = f"{scheme}://{settings.host}:{settings.port}"
servers = [{"url": "/"}]
if settings.host and settings.port:
servers.append({"url": explicit})
return servers
def _generate_operation_id(route: APIRoute) -> str:
return route.name
class ResolverReadyResponse(BaseModel):
ready: bool = Field(description="Whether resolver dependencies are currently ready.")
backends: dict[str, str] = Field(
default_factory=dict,
description="Per-backend readiness details keyed by backend name.",
)
async def wait_for(
name: str,
url: str,
timeout: float,
headers: dict[str, str] | None = None,
accept_status: tuple[int, ...] = (200, 204, 404),
) -> None:
deadline = time.monotonic() + timeout
attempt = 0
async with httpx.AsyncClient() as client:
while time.monotonic() < deadline:
attempt += 1
try:
resp = await client.get(url, headers=headers or {}, timeout=3.0)
if resp.status_code in accept_status:
log.info("%s ready (attempt %d, status %d)", name, attempt, resp.status_code)
return
log.debug("%s probe returned %d (attempt %d)", name, resp.status_code, attempt)
except (TimeoutError, httpx.RequestError) as exc:
log.debug("%s not reachable (attempt %d): %s", name, attempt, exc)
await asyncio.sleep(2)
raise BackendStartupTimeout(f"{name} did not become ready within {timeout}s")
async def _wait_for_all_bg(data_settings: Settings, tenant_id: str) -> None:
scope = {"X-Scope-OrgID": tenant_id}
checks: list[tuple[str, str, dict[str, str], tuple[int, ...]]] = []
# Logs
if data_settings.logs_backend == LOGS_BACKEND_LOKI:
checks.append(
(
LOGS_BACKEND_LOKI,
f"{data_settings.loki_url}/loki/api/v1/labels",
scope,
(200, 404),
)
)
# Metrics
if data_settings.metrics_backend == METRICS_BACKEND_MIMIR:
checks.append(
(
METRICS_BACKEND_MIMIR,
f"{data_settings.mimir_url}/prometheus/api/v1/query?query=vector%281%29",
scope,
(200,),
)
)
# Traces
if data_settings.traces_backend == TRACES_BACKEND_TEMPO:
checks.append(
(
TRACES_BACKEND_TEMPO,
f"{data_settings.tempo_url}/api/echo",
scope,
(200,),
)
)
log.info("Backend readiness check starting (timeout=%ds) ...", data_settings.startup_timeout)
for name, url, hdrs, ok in checks:
_BACKEND_STATUS[name] = "waiting"
results = await asyncio.gather(
*[
wait_for(name, url, data_settings.startup_timeout, headers=hdrs, accept_status=ok)
for name, url, hdrs, ok in checks
],
return_exceptions=True,
)
all_ok = True
for (name, *_), result in zip(checks, results):
if isinstance(result, Exception):
log.error("%s failed readiness: %s", name, result)
_BACKEND_STATUS[name] = f"failed: {result}"
all_ok = False
else:
_BACKEND_STATUS[name] = "ready"
if all_ok:
globals()["_BACKEND_READY"] = True
log.info("All backends ready — engine fully operational")
else:
log.warning("Some backends failed readiness — partial functionality available")
globals()["_BACKEND_READY"] = False
@asynccontextmanager
async def lifespan(_app: FastAPI) -> AsyncIterator[None]:
if settings.database_url:
init_database(settings.database_url)
init_db()
await rca_job_service.startup_recovery()
tenant_id = settings.default_tenant_id
readiness_task = asyncio.create_task(_wait_for_all_bg(settings, tenant_id))
cleanup_task = asyncio.create_task(_cleanup_loop())
try:
yield
finally:
readiness_task.cancel()
cleanup_task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await readiness_task
with contextlib.suppress(asyncio.CancelledError):
await cleanup_task
await close_providers()
dispose_database()
async def _cleanup_loop() -> None:
while True:
await asyncio.sleep(300)
if settings.database_url:
await rca_job_service.cleanup_retention()
app = FastAPI(
title="Resolver Analysis Engine",
description="AI-powered root cause analysis and anomaly detection over logs, metrics, and traces.",
version="1.0.0",
generate_unique_id_function=_generate_operation_id,
servers=_openapi_servers(),
contact={
"name": "Resolver Maintainers",
"url": "https://github.com/stefankhacks/watchdog",
},
license_info={
"name": "Apache 2.0",
"identifier": "Apache-2.0",
},
openapi_tags=OPENAPI_TAGS,
lifespan=lifespan,
)
app.add_middleware(InternalAuthMiddleware)
app.include_router(router, prefix="/api/v1")
install_custom_openapi(app)
@app.get(
"/api/v1/ready",
tags=["Health"],
summary="Backend readiness probe",
description="Returns readiness state for configured backend dependencies.",
response_description="Resolver readiness state and per-backend status details.",
responses={
200: {"model": ResolverReadyResponse},
503: {"model": ResolverReadyResponse, "description": "Service Unavailable"},
},
)
async def ready() -> JSONResponse:
code = 200 if _BACKEND_READY else 503
return JSONResponse(
status_code=code,
content={"ready": _BACKEND_READY, "backends": _BACKEND_STATUS},
)
if __name__ == "__main__":
if settings.ssl_enabled:
uvicorn.run(
"main:app",
host=settings.host,
port=settings.port,
log_level="info",
access_log=True,
ssl_certfile=settings.ssl_certfile,
ssl_keyfile=settings.ssl_keyfile,
)
else:
uvicorn.run(
"main:app",
host=settings.host,
port=settings.port,
log_level="info",
access_log=True,
)