feat: add extra observability metrics for PrometheusMiddleware and Receiver#604
Open
Mohammed0tarek wants to merge 3 commits intotaskiq-python:masterfrom
Open
Conversation
…tamp to PrometheusMiddleware
Add production observability for the Receiver via an observer protocol that tracks prefetch queue depth, semaphore availability, active task count, unknown task lookups, and deserialization errors. - Add ReceiverObserver protocol (taskiq/receiver/observer.py) - Instrument Receiver with guarded observer callbacks at 5 sites - Add PrometheusReceiverObserver implementation with Gauges/Counters - Wire observer from middleware to receiver via broker attribute - Remove redundant in_flight_tasks gauge (replaced by active_tasks_count)
- Fix typos in observer docstring and metric descriptions - Add missing docstrings to observer protocol and implementation methods - Remove unused Gauge import from PrometheusMiddleware.__init__ - Remove unused ReceiverObserver import from run.py - Fix import ordering (ruff I001) - Add noqa for expected complexity in runner method - Run black formatting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
queue_wait_secondshistogram (end-to-end queue latency via_taskiq_enqueue_timestamplabel injected inpre_send),task_errors_by_typecounter(error count broken down by exception class name), and idempotent timestamp injection so multiple middlewares can coexist
typing.Protocol(taskiq/receiver/observer.py) that lets external implementations observe receiver internals — prefetch queue depth, semaphoreavailability, active task count, unknown task lookups, and deserialization errors — with zero overhead when no observer is set (
if self.observer is not Noneguards)PrometheusMiddleware.set_broker()→broker._receiver_observer→Receiver(observer=...)New metrics
queue_wait_secondstask_errors_by_typetask_name,error_type)prefetch_queue_sizesemaphore_availableworker_active_tasks_counttask_not_found_totaltask_name)deserialize_error_countSome decisions decisions
ReceiverObserveris aProtocol(not ABC) so there's no coupling — any class with matching methods satisfies itqueue_wait_secondsgracefully degrades: if the sender doesn't usePrometheusMiddleware, the timestamp label is absent and the metric is simply not observedTesting
Honestly still no idea how to test this via py test. But I'll be working on it.