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:
@@ -1,21 +1,8 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from urllib.parse import urlsplit, urlunsplit
|
|
||||||
|
|
||||||
|
_FIELDS = ("event", "monitor_id", "url", "outcome", "latency_ms", "http_status", "error")
|
||||||
SAFE_EXTRA = ("monitor_id", "url", "status", "http_status", "latency_ms", "error")
|
|
||||||
|
|
||||||
|
|
||||||
def redact_url(value: str) -> str:
|
|
||||||
try:
|
|
||||||
parts = urlsplit(value)
|
|
||||||
host = parts.hostname or ""
|
|
||||||
if parts.port:
|
|
||||||
host = f"{host}:{parts.port}"
|
|
||||||
return urlunsplit((parts.scheme, host, parts.path, "", ""))
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
return "[invalid-url]"
|
|
||||||
|
|
||||||
|
|
||||||
class JsonFormatter(logging.Formatter):
|
class JsonFormatter(logging.Formatter):
|
||||||
@@ -24,12 +11,14 @@ class JsonFormatter(logging.Formatter):
|
|||||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||||
"level": record.levelname,
|
"level": record.levelname,
|
||||||
"logger": record.name,
|
"logger": record.name,
|
||||||
"event": record.getMessage(),
|
"message": record.getMessage(),
|
||||||
}
|
}
|
||||||
for key in SAFE_EXTRA:
|
for field in _FIELDS:
|
||||||
if hasattr(record, key):
|
value = getattr(record, field, None)
|
||||||
value = getattr(record, key)
|
if value is not None:
|
||||||
payload[key] = redact_url(str(value)) if key == "url" else value
|
payload[field] = value
|
||||||
|
if record.exc_info:
|
||||||
|
payload["exception"] = self.formatException(record.exc_info)
|
||||||
return json.dumps(payload, separators=(",", ":"), default=str)
|
return json.dumps(payload, separators=(",", ":"), default=str)
|
||||||
|
|
||||||
|
|
||||||
@@ -37,5 +26,6 @@ def configure_logging(level: str) -> None:
|
|||||||
handler = logging.StreamHandler()
|
handler = logging.StreamHandler()
|
||||||
handler.setFormatter(JsonFormatter())
|
handler.setFormatter(JsonFormatter())
|
||||||
root = logging.getLogger()
|
root = logging.getLogger()
|
||||||
root.handlers = [handler]
|
root.handlers.clear()
|
||||||
root.setLevel(level.upper())
|
root.addHandler(handler)
|
||||||
|
root.setLevel(level)
|
||||||
|
|||||||
Reference in New Issue
Block a user