Skip to content

Prometheus middleware fails with AttributeError due to missing after_worker_boot #703

@nathan-whelchel

Description

@nathan-whelchel

dramatiq version: 1.17.1

The dramatiq.middleware.prometheus.Prometheus class defines after_process_boot(self, broker), but the Dramatiq broker calls emit_after("worker_boot", ...), which looks for after_worker_boot(...) instead. As a result, after_process_boot() is never invoked and key attributes like .message_durations, .inprogress_messages, etc., are never initialized.

The Prometheus middleware should hook properly into the worker_boot lifecycle and initialize its internal metrics.

Add this method to Prometheus middleware (in dramatiq.middleware.prometheus):

def after_worker_boot(self, broker, worker):
self.after_process_boot(broker)

Work-around ================================================

Derive a class:

class PatchedPrometheus(Prometheus):
def after_worker_boot(self, broker, worker):
self.after_process_boot(broker)

In your worker =================================================

Remove any existing Prometheus middleware

dramatiq.get_broker().middleware = [
mw for mw in dramatiq.get_broker().middleware
if not isinstance(mw, Prometheus)
]
dramatiq.get_broker().add_middleware(PatchedPrometheus())

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions