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

Misc Promethus changes #130

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Changes from 2 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: 22 additions & 12 deletions servicelayer/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
from banal import ensure_list
from abc import ABC, abstractmethod

from prometheus_client import start_http_server, Counter, Histogram
from prometheus_client import (
start_http_server,
Counter,
Histogram,
REGISTRY,
GC_COLLECTOR,
PROCESS_COLLECTOR,
)

from servicelayer import settings
from servicelayer.jobs import Stage
Expand All @@ -22,26 +29,29 @@
INTERVAL = 2
TASK_FETCH_RETRY = 60 / INTERVAL

TASK_STARTED = Counter(
"task_started_total",
REGISTRY.unregister(GC_COLLECTOR)
REGISTRY.unregister(PROCESS_COLLECTOR)
stchris marked this conversation as resolved.
Show resolved Hide resolved

TASKS_STARTED = Counter(
"servicelayer_tasks_started_total",
"Number of tasks that a worker started processing",
["stage"],
)

TASK_SUCCEEDED = Counter(
"task_succeeded_total",
TASKS_SUCCEEDED = Counter(
"servicelayer_tasks_succeeded_total",
"Number of successfully processed tasks",
["stage", "retries"],
)

TASK_FAILED = Counter(
"task_failed_total",
TASKS_FAILED = Counter(
"servicelayer_tasks_failed_total",
"Number of failed tasks",
["stage", "retries", "failed_permanently"],
)

TASK_DURATION = Histogram(
"task_duration_seconds",
"servicelayer_task_duration_seconds",
"Task duration in seconds",
["stage"],
# The bucket sizes are a rough guess right now, we might want to adjust
Expand Down Expand Up @@ -98,12 +108,12 @@ def handle_safe(self, task):
retries = unpack_int(task.context.get("retries"))

try:
TASK_STARTED.labels(task.stage.stage).inc()
TASKS_STARTED.labels(task.stage.stage).inc()
start_time = default_timer()
self.handle(task)
duration = max(0, default_timer() - start_time)
TASK_DURATION.labels(task.stage.stage).observe(duration)
TASK_SUCCEEDED.labels(task.stage.stage, retries).inc()
TASKS_SUCCEEDED.labels(task.stage.stage, retries).inc()
except SystemExit as exc:
self.exit_code = exc.code
self.retry(task)
Expand Down Expand Up @@ -143,14 +153,14 @@ def retry(self, task):
log.warning(
f"Queueing failed task for retry #{retry_count}/{settings.WORKER_RETRY}..." # noqa
)
TASK_FAILED.labels(task.stage.stage, retries, False).inc()
TASKS_FAILED.labels(task.stage.stage, retries, False).inc()
task.context["retries"] = retry_count
task.stage.queue(task.payload, task.context)
else:
log.warning(
f"Failed task, exhausted retry count of {settings.WORKER_RETRY}"
)
TASK_FAILED.labels(task.stage.stage, retries, True).inc()
TASKS_FAILED.labels(task.stage.stage, retries, True).inc()
stchris marked this conversation as resolved.
Show resolved Hide resolved

def process(self, blocking=True, interval=INTERVAL):
retries = 0
Expand Down
Loading