Skip to content

Commit 9b5eed4

Browse files
committed
fix: complete exception logging in publish_amqp
Replace logger.error() calls with logger.exception() to capture full stack traces in production Kubernetes logs. Adds structured context via extra={} for improved observability: - load_payload: Include file path on FileNotFoundError/JSONDecodeError - format_routing_key: Show template and available fields on KeyError - main: Include exchange/routing_key/host on publish failure Closes phase3-analysis #1 (error logging completion)
1 parent 95dd098 commit 9b5eed4

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

scripts/publish_amqp.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ def load_payload(payload_file: Path) -> dict[str, Any]:
2727
data: dict[str, Any] = json.loads(payload_file.read_text())
2828
return data
2929
except FileNotFoundError:
30-
logger.error("Payload file not found: %s", payload_file)
30+
logger.exception("Payload file not found", extra={"file": str(payload_file)})
3131
sys.exit(1)
32-
except json.JSONDecodeError as e:
33-
logger.error("Invalid JSON in payload file: %s", e)
32+
except json.JSONDecodeError:
33+
logger.exception("Invalid JSON in payload file", extra={"file": str(payload_file)})
3434
sys.exit(1)
3535

3636

@@ -41,8 +41,11 @@ def format_routing_key(template: str, payload: dict[str, Any]) -> str:
4141
"""
4242
try:
4343
return template.format(**payload)
44-
except KeyError as e:
45-
logger.error("Missing field %s in payload for routing key template", e)
44+
except KeyError:
45+
logger.exception(
46+
"Missing required field in payload for routing key template",
47+
extra={"template": template, "available_fields": list(payload.keys())},
48+
)
4649
sys.exit(1)
4750

4851

@@ -124,8 +127,15 @@ def main() -> None:
124127
payload=payload,
125128
virtual_host=args.virtual_host,
126129
)
127-
except Exception as e:
128-
logger.error("Failed to publish message: %s", e)
130+
except Exception:
131+
logger.exception(
132+
"Failed to publish AMQP message",
133+
extra={
134+
"exchange": args.exchange,
135+
"routing_key": routing_key,
136+
"host": args.host,
137+
},
138+
)
129139
sys.exit(1)
130140

131141

0 commit comments

Comments
 (0)