decomposer: generate deliverable files for Define the service contract and project architecture for the FastAPI endpoint monitoring service.; Implement the typed monitor CRUD API and concurrency-safe in-memory state according to the service design.; Implement secure on-demand endpoint checks with status updates, latency measurement, robust error handling, and redacted structured logs.; Add operational API endpoints and environment-driven runtime configuration to the monitoring service.; Create automated tests for the monitoring service.; Package the service with Docker and developer documentation.; Validate the complete project.
This commit is contained in:
120
app/checker.py
120
app/checker.py
@@ -1,71 +1,97 @@
|
|||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
from datetime import datetime
|
|
||||||
from urllib.parse import urljoin
|
from urllib.parse import urljoin
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
from .logging_config import redact_url
|
from app.config import Settings
|
||||||
from .models import CheckResult, MonitorStatus, utcnow
|
from app.models import CurrentStatus, MonitorState, utc_now
|
||||||
from .security import Resolver, UnsafeTarget, system_resolver, validate_target
|
from app.security import Resolver, UnsafeTarget, redact_url, system_resolver, validate_target
|
||||||
|
|
||||||
logger = logging.getLogger("monitor.checker")
|
logger = logging.getLogger("endpoint_monitor.checker")
|
||||||
|
_REDIRECTS = {301, 302, 303, 307, 308}
|
||||||
|
|
||||||
|
|
||||||
class EndpointChecker:
|
class EndpointChecker:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
client: httpx.AsyncClient,
|
settings: Settings,
|
||||||
timeout: float,
|
*,
|
||||||
max_redirects: int,
|
transport: httpx.AsyncBaseTransport | None = None,
|
||||||
max_response_bytes: int,
|
|
||||||
resolver: Resolver = system_resolver,
|
resolver: Resolver = system_resolver,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.client = client
|
self.settings = settings
|
||||||
self.timeout = timeout
|
self.transport = transport
|
||||||
self.max_redirects = max_redirects
|
|
||||||
self.max_response_bytes = max_response_bytes
|
|
||||||
self.resolver = resolver
|
self.resolver = resolver
|
||||||
|
|
||||||
async def check(self, monitor_id: str, initial_url: str) -> CheckResult:
|
async def check(self, monitor_id: str, target_url: str) -> CurrentStatus:
|
||||||
started = time.perf_counter()
|
started = time.monotonic()
|
||||||
checked_at: datetime = utcnow()
|
current_url = target_url
|
||||||
current_url = initial_url
|
timeout = httpx.Timeout(
|
||||||
|
connect=self.settings.connect_timeout_seconds,
|
||||||
|
read=self.settings.read_timeout_seconds,
|
||||||
|
write=self.settings.write_timeout_seconds,
|
||||||
|
pool=self.settings.pool_timeout_seconds,
|
||||||
|
)
|
||||||
|
limits = httpx.Limits(max_connections=self.settings.max_connections)
|
||||||
try:
|
try:
|
||||||
for redirect_count in range(self.max_redirects + 1):
|
async with httpx.AsyncClient(
|
||||||
|
timeout=timeout,
|
||||||
|
limits=limits,
|
||||||
|
transport=self.transport,
|
||||||
|
follow_redirects=False,
|
||||||
|
headers={"User-Agent": self.settings.user_agent},
|
||||||
|
) as client:
|
||||||
|
for redirect_count in range(self.settings.max_redirects + 1):
|
||||||
await validate_target(current_url, self.resolver)
|
await validate_target(current_url, self.resolver)
|
||||||
async with self.client.stream(
|
async with client.stream("GET", current_url) as response:
|
||||||
"GET", current_url, follow_redirects=False, timeout=self.timeout,
|
if response.status_code in _REDIRECTS and response.headers.get("location"):
|
||||||
headers={"User-Agent": "endpoint-monitor/1"},
|
if redirect_count >= self.settings.max_redirects:
|
||||||
) as response:
|
raise httpx.TooManyRedirects("redirect limit exceeded")
|
||||||
consumed = 0
|
current_url = urljoin(current_url, response.headers["location"])
|
||||||
async for chunk in response.aiter_bytes():
|
|
||||||
consumed += len(chunk)
|
|
||||||
if consumed >= self.max_response_bytes:
|
|
||||||
break
|
|
||||||
if response.is_redirect and response.headers.get("location"):
|
|
||||||
if redirect_count == self.max_redirects:
|
|
||||||
return self._result(started, checked_at, MonitorStatus.error, error="too_many_redirects")
|
|
||||||
current_url = urljoin(str(response.url), response.headers["location"])
|
|
||||||
continue
|
continue
|
||||||
status = MonitorStatus.up if 200 <= response.status_code < 400 else MonitorStatus.down
|
state = MonitorState.UP if response.status_code < 400 else MonitorState.DOWN
|
||||||
return self._result(started, checked_at, status, response.status_code)
|
result = self._status(started, state, response.status_code)
|
||||||
except UnsafeTarget as exc:
|
self._log(monitor_id, current_url, result)
|
||||||
result = self._result(started, checked_at, MonitorStatus.error, error=str(exc))
|
|
||||||
except httpx.TimeoutException:
|
|
||||||
result = self._result(started, checked_at, MonitorStatus.error, error="timeout")
|
|
||||||
except httpx.HTTPError:
|
|
||||||
result = self._result(started, checked_at, MonitorStatus.error, error="network_error")
|
|
||||||
except Exception:
|
|
||||||
logger.exception("check_unexpected_error", extra={"monitor_id": monitor_id, "url": redact_url(initial_url)})
|
|
||||||
result = self._result(started, checked_at, MonitorStatus.error, error="internal_error")
|
|
||||||
self._log(monitor_id, initial_url, result)
|
|
||||||
return result
|
return result
|
||||||
|
except UnsafeTarget:
|
||||||
|
logger.warning(
|
||||||
|
"check blocked",
|
||||||
|
extra={"event": "check_blocked", "monitor_id": monitor_id, "url": redact_url(current_url)},
|
||||||
|
)
|
||||||
|
raise
|
||||||
|
except (httpx.HTTPError, OSError) as exc:
|
||||||
|
result = self._status(started, MonitorState.ERROR, error=str(exc)[:500])
|
||||||
|
self._log(monitor_id, current_url, result)
|
||||||
|
return result
|
||||||
|
raise RuntimeError("unreachable redirect loop")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _result(started: float, checked_at: datetime, status: MonitorStatus, http_status: int | None = None, error: str | None = None) -> CheckResult:
|
def _status(
|
||||||
return CheckResult(status=status, checked_at=checked_at, latency_ms=max(0.0, (time.perf_counter() - started) * 1000), http_status=http_status, error=error)
|
started: float,
|
||||||
|
state: MonitorState,
|
||||||
|
http_status: int | None = None,
|
||||||
|
error: str | None = None,
|
||||||
|
) -> CurrentStatus:
|
||||||
|
return CurrentStatus(
|
||||||
|
state=state,
|
||||||
|
checked_at=utc_now(),
|
||||||
|
latency_ms=round((time.monotonic() - started) * 1000, 3),
|
||||||
|
http_status=http_status,
|
||||||
|
error=error,
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _log(monitor_id: str, url: str, result: CheckResult) -> None:
|
def _log(monitor_id: str, url: str, result: CurrentStatus) -> None:
|
||||||
logger.info("check_completed", extra={"monitor_id": monitor_id, "url": url, **result.model_dump()})
|
logger.info(
|
||||||
|
"check completed",
|
||||||
|
extra={
|
||||||
|
"event": "check_completed",
|
||||||
|
"monitor_id": monitor_id,
|
||||||
|
"url": redact_url(url),
|
||||||
|
"outcome": result.state.value,
|
||||||
|
"latency_ms": result.latency_ms,
|
||||||
|
"http_status": result.http_status,
|
||||||
|
"error": result.error,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user