Reliability & governance helpers

Last updated:

Beyond produce/consume, the Python core ships optional, opt-in helpers that mechanise the reliability and governance contracts the wire spec defines. The core stays stdlib-only; the crypto and OTel references live behind extras. None touch the frozen envelope (schema_version: 1); each is a tooling layer over the codec. They are the Python face of the cross-SDK spec — follow the linked spec page for the full contract.

Idempotency

wrap(store, handler) makes a handler run at most once per meta.id, even on at-least-once redelivery. It keeps the handler’s signature (via functools.wraps):

from babelqueue import BabelQueue, InMemoryStore
from babelqueue.idempotency import wrap

app = BabelQueue("redis://localhost:6379/0", queue="orders")
store = InMemoryStore()  # tests / single process
app.register("urn:babel:orders:created", wrap(store, on_order_created))

The in-memory InMemoryStore is the reference; for a fleet, implement the IdempotencyStore protocol (seen / remember / forget) over a shared backend (no persistent store ships in core yet — bring your own). See Idempotency and the store deep-dive.

Transactional outbox

Outbox / OutboxRelay remove the producer dual write: persist the encoded envelope in the same DB transaction as your business row, then a relay publishes the durable rows verbatim.

from babelqueue import Outbox, OutboxRelay, InMemoryOutboxStore

store = InMemoryOutboxStore()   # production: a DB-backed OutboxStore
outbox = Outbox(store)

# inside YOUR DB transaction, beside the business write — no commit of its own:
row_id = outbox.write(envelope) # encodes via the codec, calls store.save()

# later, a relay drains the durable rows to the broker:
result = OutboxRelay(app.transport, store).drain()  # result.published / result.failed

OutboxStore is the four-method protocol (save / fetch_unpublished / mark_published / mark_failed); the relay forwards through the runtime’s publish-only Transport (publish(queue, body)). The transaction boundary is yours, and the relay backoff is in seconds. See Transactional Outbox.

DLQ redrive & replay-bypass

redrive(transport, dlq, ...) moves dead-lettered messages back onto a queue — dead_letter removed, attempts reset to 0, everything else preserved — with dry_run, select, to_queue (sandbox) and bypass. The replay guard skips effects that already fired:

from babelqueue import is_replay, bypass_external_effects
from babelqueue.redrive import redrive

# inspect / sandbox-replay, stamping bq-replay-bypass
res = redrive(app.transport, "orders.dlq", to_queue="sandbox", bypass=True)

@app.handler("urn:babel:orders:created")
def on_order_created(data, meta):
    save_order(data)                                  # idempotent core — always runs
    bypass_external_effects(lambda: send_email(data)) # external effect — skipped on replay

On Kafka, redrive keeps the DLQ record’s key (ReceivedMessage.key) on the write back to the work topic as well as on every restore write back to the DLQ: a null key stays null and an empty key stays empty.

is_replay() reads the per-message replay flag the runtime sets (a contextvars flag), so it takes no argument. See DLQ redrive & replay-bypass.

GDPR field encryption

protect / unprotect encrypt only the data leaves a schema marks x-gdpr-sensitive, in place. Cipher is a caller-bound protocol; the core ships no concrete cipher (Python’s stdlib has no AES-GCM) — the AES-256-GCM reference rides the optional babelqueue[gdpr] extra (cryptography). Validate cleartext — protect after validation on produce, unprotect before validation on consume.

from babelqueue import protect, unprotect, Cipher, DecryptError
from babelqueue.schema import sensitive_paths

protect(data, schema, cipher)      # producer: encrypt marked leaves (cipher implements Cipher)
try:
    unprotect(data, schema, cipher)  # consumer: inverse
except DecryptError:
    raise                            # wrong key / tampered → retry / dead-letter

See GDPR field encryption.

OpenTelemetry (spans, metrics and traceparent)

The babelqueue.otel module (the babelqueue[otel] extra, so importing the core stays dependency-free) emits a send <queue> span on publish and a process <queue> span per handled message, plus the standard metrics, and — on a header-carrying transport — injects/extracts the W3C traceparent so a consumer span is a true child of the producer span (degrading to trace_id correlation otherwise). Every Python transport carries the header on its broker’s native metadata channel.

Dashboard-breaking change. Spans are now send <queue> / process <queue> (they were publish <urn> / process <urn>), and the URN is the babelqueue.urn attribute. Pass system so messaging.system is the broker. Not passing it is deprecated. The migration table is in Observability.

The options are keyword arguments:

Keyword Applies to Meaning
system="kafka" wrap_handler, publish, dead_letter_observer the messaging.system value (the broker)
consumer_group="billing" wrap_handler messaging.consumer.group.name, consumer span only
meter=meter wrap_handler, publish; first argument of dead_letter_observer an OpenTelemetry Meter; without it nothing is recorded as a metric

otel.SEMCONV_VERSION (1.44.0) and otel.SEMCONV_SCHEMA_URL are public constants; pass the URL when you create the tracer: trace.get_tracer("orders", schema_url=otel.SEMCONV_SCHEMA_URL).

from opentelemetry import trace, metrics
from babelqueue import BabelQueue, otel

tracer = trace.get_tracer("orders", schema_url=otel.SEMCONV_SCHEMA_URL)
meter = metrics.get_meter("orders")

# dead-letter observer: feeds babelqueue.dlq.messages from the consume runtime
app = BabelQueue("redis://localhost:6379/0", queue="orders", dead_letter=True,
                 dead_letter_observer=otel.dead_letter_observer(meter, system="redis"))

# consumer: "process <queue>" span + consumed / retries / duration metrics
app.register("urn:babel:orders:created",
             otel.wrap_handler(tracer, on_order_created, system="redis", meter=meter))

# producer: "send <queue>" span + sent metric
otel.publish(tracer, app, "urn:babel:orders:created", {"order_id": 1042},
             system="redis", meter=meter)

Where the observer is wired. The Python runtime (BabelQueue) writes every dead-letter through one chokepoint, so dead_letter_observer= covers every transport. An observer that raises is logged and never changes how the message is settled. The Azure Service Bus transport reports whether its native settle dead-lettered, so its native dead-letters count; the SQS native settle is a no-op and never counts.

What babelqueue.dlq.messages counts. It counts a dead-letter the SDK itself carried out and confirmed: a record written to the BabelQueue DLQ, and a native dead-letter the SDK initiated (on Azure Service Bus, the SDK’s own DeadLetterMessage / deadLetter call made when the BabelQueue DLQ is off). There is no attribute that tells the two apart. It does not count a dead-letter the broker makes on its own (MaxDeliveryCount, SQS RedrivePolicy), the SQS native no-op, a failed DLQ write, or a message dropped because DLQ writing is disabled. On Azure Service Bus, do not add this counter to the broker’s own dead-letter metric: the messages the SDK dead-letters natively are in both, so the sum counts them twice.

To put trace_id, meta.id and job on your log lines, see Log correlation. The span shape, attribute tables, system values, the migration from the earlier publish <urn> shape and the full metric list are in Observability.

Per-URN schema validation

validate(provider, urn, data) (producer guard) / wrap(provider, urn, handler) (consumer safety net) validate a message’s data against the JSON Schema registered for its URN, bridged from a babelqueue-registry manifest via DirProvider. See Per-URN schema validation.