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:
@@ -2,16 +2,39 @@ import json
|
||||
import logging
|
||||
from urllib.parse import urlsplit, urlunsplit
|
||||
|
||||
|
||||
def redacted_url(url: str) -> str:
|
||||
parsed = urlsplit(url)
|
||||
host = parsed.hostname or ""
|
||||
if ":" in host and not host.startswith("["):
|
||||
host = f"[{host}]"
|
||||
if parsed.port:
|
||||
host = f"{host}:{parsed.port}"
|
||||
return urlunsplit((parsed.scheme, host, parsed.path, "REDACTED" if parsed.query else "", ""))
|
||||
_STANDARD = set(logging.makeLogRecord({}).__dict__) | {"message", "asctime"}
|
||||
|
||||
|
||||
def log_event(logger: logging.Logger, event: str, **fields: object) -> None:
|
||||
logger.info(json.dumps({"event": event, **fields}, default=str, sort_keys=True))
|
||||
def redact_url(value: str) -> str:
|
||||
try:
|
||||
parsed = urlsplit(value)
|
||||
if parsed.scheme not in {"http", "https"} or not parsed.hostname:
|
||||
return value
|
||||
host = f"[{parsed.hostname}]" if ":" in parsed.hostname else parsed.hostname
|
||||
port = f":{parsed.port}" if parsed.port else ""
|
||||
query = "<redacted>" if parsed.query else ""
|
||||
fragment = "<redacted>" if parsed.fragment else ""
|
||||
return urlunsplit((parsed.scheme, host + port, parsed.path, query, fragment))
|
||||
except ValueError:
|
||||
return "<invalid-url>"
|
||||
|
||||
|
||||
class StructuredJsonFormatter(logging.Formatter):
|
||||
def format(self, record: logging.LogRecord) -> str:
|
||||
payload: dict[str, object] = {
|
||||
"level": record.levelname.lower(),
|
||||
"logger": record.name,
|
||||
"message": record.getMessage(),
|
||||
}
|
||||
for key, value in record.__dict__.items():
|
||||
if key not in _STANDARD and not key.startswith("_"):
|
||||
payload[key] = value
|
||||
return json.dumps(payload, default=str, separators=(",", ":"))
|
||||
|
||||
|
||||
def configure_logging(level: str) -> None:
|
||||
handler = logging.StreamHandler()
|
||||
handler.setFormatter(StructuredJsonFormatter())
|
||||
root = logging.getLogger()
|
||||
root.handlers[:] = [handler]
|
||||
root.setLevel(level.upper())
|
||||
|
||||
Reference in New Issue
Block a user