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,37 +1,20 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from urllib.parse import urlsplit, urlunsplit
|
from datetime import UTC, datetime
|
||||||
|
|
||||||
|
_STANDARD = set(logging.makeLogRecord({}).__dict__) | {"message", "asctime"}
|
||||||
SENSITIVE_KEYS = {"url", "location"}
|
|
||||||
|
|
||||||
|
|
||||||
def redact_url(value: str) -> str:
|
|
||||||
try:
|
|
||||||
parts = urlsplit(value)
|
|
||||||
if not parts.scheme or not parts.hostname:
|
|
||||||
return "[redacted]"
|
|
||||||
host = parts.hostname
|
|
||||||
if ":" in host:
|
|
||||||
host = f"[{host}]"
|
|
||||||
if parts.port:
|
|
||||||
host = f"{host}:{parts.port}"
|
|
||||||
return urlunsplit((parts.scheme, host, parts.path, "", ""))
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
return "[redacted]"
|
|
||||||
|
|
||||||
|
|
||||||
class JsonFormatter(logging.Formatter):
|
class JsonFormatter(logging.Formatter):
|
||||||
def format(self, record: logging.LogRecord) -> str:
|
def format(self, record: logging.LogRecord) -> str:
|
||||||
payload: dict[str, object] = {
|
payload: dict[str, object] = {
|
||||||
|
"timestamp": datetime.now(UTC).isoformat(),
|
||||||
"level": record.levelname,
|
"level": record.levelname,
|
||||||
"logger": record.name,
|
"event": record.getMessage(),
|
||||||
"message": record.getMessage(),
|
|
||||||
}
|
}
|
||||||
fields = getattr(record, "fields", {})
|
for key, value in record.__dict__.items():
|
||||||
if isinstance(fields, dict):
|
if key not in _STANDARD and key not in {"args", "msg"}:
|
||||||
for key, value in fields.items():
|
payload[key] = value
|
||||||
payload[key] = redact_url(str(value)) if key in SENSITIVE_KEYS else value
|
|
||||||
return json.dumps(payload, default=str, separators=(",", ":"))
|
return json.dumps(payload, default=str, separators=(",", ":"))
|
||||||
|
|
||||||
|
|
||||||
@@ -39,5 +22,5 @@ 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[:] = [handler]
|
||||||
root.setLevel(level)
|
root.setLevel(level.upper())
|
||||||
|
|||||||
Reference in New Issue
Block a user