Skip to content

Lesson 8 of 9 · 25m

Reliable Automation in Production: Logs, Metrics, Queues, and Timeouts

Automation becomes infrastructure; operate it with structured logs, useful metrics, bounded queues, deadlines, and a way to see what it is doing right now.

The first time automation runs unattended, it stops being a script and becomes production software. Too often it keeps the operational maturity of a shell one-liner: the queue backs up, nobody notices for three days, and the postmortem finds a cron email going to a deactivated mailbox.

Structured logs are for questions you have not asked yet

Write logs as structured events with stable fields. Plain text is fine for a terminal; it is terrible for reconstructing failure across twenty workers.

import json
import logging

logger = logging.getLogger("secops")

def event(level: str, message: str, **fields):
    logger.log(getattr(logging, level.upper()),
               json.dumps({"event": message, **fields}, sort_keys=True))

event("info", "finding_imported", source="tenable", count=142, duration_ms=812)

Log decisions, transitions, external calls, and failures. Do not log secrets, raw payloads containing credentials, or enormous blobs that make useful events impossible to find.

Metrics answer the operator's questions

Count work received, completed, retried, and failed. Track age of oldest queued item, dependency latency, and number of items in each workflow state.

automation_items_processed_total{workflow="phishing",result="success"} 128
automation_items_processed_total{workflow="phishing",result="failed"} 3
automation_queue_oldest_age_seconds{workflow="phishing"} 47
automation_dependency_latency_seconds{dependency="virustotal",quantile="0.95"} 2.8

A count of successes without failures is decoration. Queue depth without age hides the difference between many fresh items and one item stuck for six hours.

Queues need bounds, dead letters, and deadlines

Retries belong in queue policy, not an infinite recursive function. Give each message bounded attempts, exponential backoff, and a dead-letter destination with enough context for repair.

message = {
    "workflow": "phishing",
    "attempt": 2,
    "max_attempts": 5,
    "payload_hash": "sha256:9dd1...",
    "not_before": "2026-11-30T22:05:00Z",
}

Every external call needs a timeout, every workflow step a deadline, and every long-running job a heartbeat or lease so another worker can recover it. If a step regularly approaches its deadline, fix batching, caching, pagination, or capacity — do not raise timeouts forever.

Field notes

  • Emit one startup log line with version, config hash, environment, and worker ID.
  • Correlate events with a run ID or case ID; without it, concurrency turns logs into confetti.
  • Alert on symptoms such as queue age, repeated transition failure, and dependency error rate.
  • Cron is not a reliability system; use systemd timers, a scheduler, or queue workers with health checks.
  • Rehearse replaying dead-letter messages, or the dead-letter queue is just a graveyard.

Was this lesson helpful?

Sign in to rate