WIP autoscaler agent
This commit is contained in:
parent
c610a3e284
commit
28059dcedf
34 changed files with 2409 additions and 35 deletions
46
agent/nix_builder_autoscaler/logging.py
Normal file
46
agent/nix_builder_autoscaler/logging.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
"""Structured JSON logging setup."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import sys
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
|
||||
class JSONFormatter(logging.Formatter):
|
||||
"""Format log records as single-line JSON."""
|
||||
|
||||
EXTRA_FIELDS = ("slot_id", "reservation_id", "instance_id", "request_id")
|
||||
|
||||
def format(self, record: logging.LogRecord) -> str:
|
||||
"""Format a log record as JSON."""
|
||||
entry: dict[str, Any] = {
|
||||
"ts": datetime.now(UTC).isoformat(),
|
||||
"level": record.levelname,
|
||||
"logger": record.name,
|
||||
"message": record.getMessage(),
|
||||
}
|
||||
for field in self.EXTRA_FIELDS:
|
||||
val = getattr(record, field, None)
|
||||
if val is not None:
|
||||
entry[field] = val
|
||||
if record.exc_info and record.exc_info[1] is not None:
|
||||
entry["exception"] = self.formatException(record.exc_info)
|
||||
return json.dumps(entry, default=str)
|
||||
|
||||
|
||||
def setup_logging(level: str = "INFO") -> None:
|
||||
"""Configure the root logger with JSON output to stderr.
|
||||
|
||||
Args:
|
||||
level: Log level name (DEBUG, INFO, WARNING, ERROR).
|
||||
"""
|
||||
handler = logging.StreamHandler(sys.stderr)
|
||||
handler.setFormatter(JSONFormatter())
|
||||
|
||||
root = logging.getLogger()
|
||||
root.handlers.clear()
|
||||
root.addHandler(handler)
|
||||
root.setLevel(getattr(logging, level.upper(), logging.INFO))
|
||||
Loading…
Add table
Add a link
Reference in a new issue