Observability#
Binomic produces observability data: traces and metrics are emitted through the OpenTelemetry (OTel) API, while logs go through structlog and can be bridged into OTel by the application. Collection is optional: the OTel dependency ships in a separate extra and is disabled by default. Binomic only produces telemetry — the SDK, its providers and any exporter are yours to assemble. The library registers no exporter and never writes to a collector.
Install and enable#
# uv
uv add "binomic[otel]" opentelemetry-sdk
# pip
pip install "binomic[otel]" opentelemetry-sdk
binomic[otel] only pulls opentelemetry-api; the examples below also import
opentelemetry.sdk, so the SDK and a backend-specific exporter are installed
separately.
from binomic.client import BinomicConfig
config = BinomicConfig(
queues=["default"],
otel_enabled=True,
)
otel_enabled defaults to False: Binomic makes no OTel calls, emits no
spans or metrics and writes no trace context to Redis, so behaviour matches a
build without observability. structlog logging is not governed by this
switch and always flows as configured.
Without the extra installed, the module-level binomic.otel.otel is None:
a Binomic / Master / Worker with otel_enabled set skips every telemetry
call and emits nothing (logging still flows). Constructing Telemetry()
directly raises an instructive RuntimeError, which makes the missing
dependency explicit.
Litestar#
BinomicPlugin’s third argument is a complete BinomicConfig, so observability
settings travel with it:
from binomic.client import BinomicConfig
from binomic.plugin.litestar import BinomicPlugin
from myapp.telemetry import init_worker
plugin = BinomicPlugin(
app_name="myapp",
broker_dsn="redis://localhost:6379/0",
config=BinomicConfig(
queues=["default"],
otel_enabled=True,
worker_init=init_worker,
),
)
The plugin does not initialise the SDK for you: set the provider in the
Litestar process (client side) before startup, and let worker_init cover the
worker subprocesses — both as described above.
Traces#
Application process (client)#
Install the global provider before constructing Binomic. The SDK choice
is yours:
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
provider = TracerProvider()
provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(provider)
A process may set its global provider only once; later calls warn and do nothing.
Every submit (and every cron/interval trigger) opens a create {queue}
PRODUCER span and stores the current W3C trace context (traceparent,
tracestate, baggage) in the broker’s dedicated context store (the Redis
implementation uses {namespace}:{queue}:context, keyed by message id). The
message envelope (Message / MessageEntry) carries no telemetry fields.
Every delivery (a direct submission or each cron / interval round)
writes it during its own enqueue.
Worker subprocesses#
The master starts workers with spawn, so a subprocess does not inherit the
parent’s provider. The only place to install the SDK inside a worker is the
worker_init hook:
# myapp/telemetry.py
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
def init_worker() -> None:
provider = TracerProvider()
trace.set_tracer_provider(provider)
from binomic.client import BinomicConfig
from myapp.telemetry import init_worker
config = BinomicConfig(
queues=["default"],
otel_enabled=True,
worker_init=init_worker,
)
worker_initruns once per worker subprocess, synchronously, before the event loop is created; it must be a synchronous callable – the coroutine returned by an async function is never awaited;it must be a module-level function (spawn pickles callbacks by reference), never a lambda or closure;
pickle transports the
module:qualnamereference, not the code, so that module must be importable inside the worker subprocess (same environment and package layout as the parent);if it raises, the subprocess exits and the master’s liveness supervision restarts it — persistent failure surfaces as a restart loop rather than silent loss.
Execution opens a process {task} CONSUMER span whose parent is read from the
stored context by message id. Missing context (for example an old message)
degrades to a root span. The consumer links with parent-child semantics — the
single-message, no-ambient-span case, matching Celery and aio-pika defaults.
Semantic conventions#
Span names and attributes follow the shape of the OpenTelemetry messaging
conventions: messaging.system=binomic,
messaging.operation.name=create|process,
messaging.destination.name=<queue>, messaging.message.id=<uuid>. Custom
attributes use the binomic. prefix. traceparent, tracestate and
baggage all travel with messages.
Metrics#
All instruments live under the binomic. namespace and use UCUM units:
Metric |
Type |
Unit |
Meaning |
|---|---|---|---|
|
Histogram |
|
Wall-clock task function duration |
|
Histogram |
|
Total delivery delay: execution start − |
|
Counter |
|
Submission attempts (silent enqueue degradation is not a failure) |
|
Counter |
|
Capacity rejections (attributes |
|
Counter |
|
Executions, retries included |
|
Counter |
|
Acks |
|
Counter |
|
Backoff redeliveries |
|
Counter |
|
DLQ transfers (with |
|
Counter |
|
Reclaimed messages |
|
UpDownCounter |
|
In-flight tasks (per process) |
|
Counter |
|
Visor-initiated restarts ( |
|
ObservableGauge |
|
Heartbeat age per worker (reads as enormous before the first heartbeat) |
|
ObservableGauge |
|
Stream length (pending included) |
|
ObservableGauge |
|
Age of the oldest entry |
About delivery.lag: it is anchored on enqueued_at, the message’s total
delivery deadline stamp. Retries preserve that stamp, so a retried task
accumulates its earlier waiting time — that is the definition (total delivery
delay), not the delay of one individual delivery.
Explicit histogram buckets are configured by your own View; Binomic does not impose them. The OTel semconv second-scale bucket set works well as a default.
Supervision (Master)#
Supervision instruments are emitted by the Master — the supervision loop
inside the client process — through the client process’s provider; worker_init
plays no part:
binomic.worker.restartsincrements when the visor decides a worker is dead or stalled;close()’s orderly shutdown is not counted, so alert rules need no shutdown-window exclusion.close()does not clear heartbeat cells — in the client shape the process exits right away; a standalone process that keeps living pastclose()sees unbounded ages and no further restart counts;binomic.worker.heartbeat.agereports “now − last beat” per worker. A worker that has not sent a heartbeat yet reads as an enormous age — that is the semantics: a missing heartbeat reads as maximally stale, and threshold alerts naturally cover slow starts;the restart counter and the age gauge are the two alerting primitives: a restart-rate spike on the counter, a worker gone silent on the age threshold; “who, when and why” is answered by correlating the attributes with the structlog
Worker restartedlog.
The heartbeat-age gauge is a process-global registration and the first registration of its name wins — one Master per process (the shape the client builds) is exactly right.
Logs#
Binomic logs through structlog.stdlib.get_logger and deliberately ships no
logging configuration (a library must not override its host’s setup). Bridging
logs into OTel happens in your application — Python’s Logs API is still in
Development, so the mechanics may evolve:
import logging
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
logger_provider = LoggerProvider()
logging.getLogger().addHandler(
LoggingHandler(level=logging.INFO, logger_provider=logger_provider),
)
With structlog’s stdlib ProcessorFormatter, the handler above receives both
Binomic’s and your application’s records. Level convention: per-message events
(submission, execution start/finish, retry scheduling) are debug; lifecycle
events (client, worker and broker start/stop) are info; failure paths
(execution failure, timeout, stale message, DLQ, worker restart) are warning.
Custom brokers#
Handing the trace context across processes and observing queue depth are
optional capabilities. A custom broker implements the two methods of
BrokerTelemetryCapability (trace_context and observe, no Lua required): store
the context in the middleware’s own metadata (for example AMQP or Kafka
message headers) and read it back by message id at execution time.
Without the mixin binomic degrades gracefully and never raises: messages are delivered and executed as usual, the process span becomes a root span and queue-depth gauges are not registered. The client only passes the context to brokers that implement the capability.
Multiple processes#
A global provider may be installed only once per process; repeated attempts warn and do nothing;
every worker subprocess reports independently, so metrics are per-process series and aggregation is the backend’s call;
context capture/read failures, span error marking and gauge callback failures are swallowed and downgraded to debug logs, never interrupting delivery, execution or acks; creating instruments and opening spans relies on the OTel API’s no-throw contract;
without the extra
otelisNoneand every call site skips telemetry; constructingTelemetry()is the only deliberate failure point in the telemetry module.