Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow metrics registry to be passed through PrometheusMiddleware #176

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions taskiq/middlewares/prometheus_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@

logger = getLogger("taskiq.prometheus")

try:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ok to pull all this out from __init__?

from prometheus_client import ( # noqa: WPS433
REGISTRY,
CollectorRegistry,
Counter,
Histogram,
)
except ImportError as imp_exc:
raise ImportError(
"Cannot initialize metrics. Please install 'taskiq[metrics]'.",
) from imp_exc


class PrometheusMiddleware(TaskiqMiddleware):
"""
Expand All @@ -24,6 +36,7 @@ class PrometheusMiddleware(TaskiqMiddleware):
def __init__(
self,
metrics_path: Optional[Path] = None,
metrics_registry: CollectorRegistry = REGISTRY,
server_port: int = 9000,
server_addr: str = "0.0.0.0", # noqa: S104
) -> None:
Expand All @@ -41,38 +54,37 @@ def __init__(

logger.debug("Initializing metrics")

try:
from prometheus_client import Counter, Histogram # noqa: WPS433
except ImportError as exc:
raise ImportError(
"Cannot initialize metrics. Please install 'taskiq[metrics]'.",
) from exc

self.found_errors = Counter(
"found_errors",
"Number of found errors",
["task_name"],
registry=metrics_registry,
)
self.received_tasks = Counter(
"received_tasks",
"Number of received tasks",
["task_name"],
registry=metrics_registry,
)
self.success_tasks = Counter(
"success_tasks",
"Number of successfully executed tasks",
["task_name"],
registry=metrics_registry,
)
self.saved_results = Counter(
"saved_results",
"Number of saved results in result backend",
["task_name"],
registry=metrics_registry,
)
self.execution_time = Histogram(
"execution_time",
"Tome of function execution",
"Time of function execution",
["task_name"],
registry=metrics_registry,
)
self.metrics_registry = metrics_registry
self.server_port = server_port
self.server_addr = server_addr

Expand All @@ -87,7 +99,11 @@ def startup(self) -> None:

if self.broker.is_worker_process:
try:
start_http_server(port=self.server_port, addr=self.server_addr)
start_http_server(
port=self.server_port,
addr=self.server_addr,
registry=self.metrics_registry,
)
except OSError as exc:
logger.debug("Cannot start prometheus server: %s", exc)

Expand Down