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.
Some checks failed
ci / test (push) Has been cancelled
Some checks failed
ci / test (push) Has been cancelled
This commit is contained in:
@@ -1,27 +1,17 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime, timezone
|
from urllib.parse import urlsplit, urlunsplit
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
|
|
||||||
class JsonFormatter(logging.Formatter):
|
def redacted_url(url: str) -> str:
|
||||||
def format(self, record: logging.LogRecord) -> str:
|
parsed = urlsplit(url)
|
||||||
data: dict[str, Any] = {
|
host = parsed.hostname or ""
|
||||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
if ":" in host and not host.startswith("["):
|
||||||
"level": record.levelname,
|
host = f"[{host}]"
|
||||||
"logger": record.name,
|
if parsed.port:
|
||||||
"message": record.getMessage(),
|
host = f"{host}:{parsed.port}"
|
||||||
}
|
return urlunsplit((parsed.scheme, host, parsed.path, "REDACTED" if parsed.query else "", ""))
|
||||||
for key in ("event", "monitor_id", "url", "outcome", "error_code", "latency_ms"):
|
|
||||||
value = getattr(record, key, None)
|
|
||||||
if value is not None:
|
|
||||||
data[key] = value
|
|
||||||
return json.dumps(data, separators=(",", ":"), default=str)
|
|
||||||
|
|
||||||
|
|
||||||
def configure_logging(level: str) -> None:
|
def log_event(logger: logging.Logger, event: str, **fields: object) -> None:
|
||||||
handler = logging.StreamHandler()
|
logger.info(json.dumps({"event": event, **fields}, default=str, sort_keys=True))
|
||||||
handler.setFormatter(JsonFormatter())
|
|
||||||
root = logging.getLogger()
|
|
||||||
root.handlers[:] = [handler]
|
|
||||||
root.setLevel(level.upper())
|
|
||||||
|
|||||||
Reference in New Issue
Block a user