Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ The following default metrics are exposed:

##### Task Processor metrics

- `flagsmith_task_processor_finished_tasks_total`: Counter labeled with `task_identifier` and `result` (`"success"`, `"failure"`).
- `flagsmith_task_processor_task_duration_seconds`: Histogram labeled with `task_identifier` and `result` (`"success"`, `"failure"`).
- `flagsmith_task_processor_finished_tasks_total`: Counter labeled with `task_identifier`, `task_type` (`"recurring"`, `"standard"`) and `result` (`"success"`, `"failure"`).
- `flagsmith_task_processor_task_duration_seconds`: Histogram labeled with `task_identifier`, `task_type` (`"recurring"`, `"standard"`) and `result` (`"success"`, `"failure"`).

##### Guidelines

Expand Down
4 changes: 2 additions & 2 deletions src/task_processor/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
flagsmith_task_processor_finished_tasks_total = prometheus_client.Counter(
"flagsmith_task_processor_finished_tasks_total",
"Total number of finished tasks",
["task_identifier", "result"],
["task_identifier", "task_type", "result"],
)
flagsmith_task_processor_task_duration_seconds = Histogram(
"flagsmith_task_processor_task_duration_seconds",
"Task processor task duration in seconds",
["task_identifier", "result"],
["task_identifier", "task_type", "result"],
)
17 changes: 8 additions & 9 deletions src/task_processor/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
TaskResult,
TaskRun,
)
from task_processor.task_registry import get_task

T = typing.TypeVar("T", bound=AbstractBaseTask)
AnyTaskRun = TaskRun | RecurringTaskRun
Expand Down Expand Up @@ -150,17 +151,15 @@ def _run_task(
exc_info=True,
)

result_label_value = result.lower()
labels = {
"task_identifier": task_identifier,
"task_type": get_task(task_identifier).task_type.value.lower(),
"result": result.lower(),
}

timer.labels(
task_identifier=task_identifier,
result=result_label_value,
) # type: ignore[no-untyped-call]
timer.labels(**labels) # type: ignore[no-untyped-call]
ctx.close()

metrics.flagsmith_task_processor_finished_tasks_total.labels(
task_identifier=task_identifier,
result=result_label_value,
).inc()
metrics.flagsmith_task_processor_finished_tasks_total.labels(**labels).inc()

return task, task_run
33 changes: 33 additions & 0 deletions tests/unit/task_processor/test_unit_task_processor_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,18 @@ def test_run_tasks__expected_metrics(
mocker: MockerFixture,
) -> None:
# Given
@register_recurring_task(run_every=timedelta(milliseconds=200))
def _fake_recurring_task() -> None:
pass

initialise()

dummy_task_identifier = dummy_task.task_identifier
raise_exception_task_identifier = raise_exception_task.task_identifier
recurring_task_identifier = RecurringTask.objects.latest(
"created_at",
).task_identifier

Task.create(
dummy_task_identifier,
scheduled_for=timezone.now(),
Expand All @@ -600,13 +610,15 @@ def test_run_tasks__expected_metrics(

# When
run_tasks(2)
run_recurring_tasks()

# Then
assert_metric(
name="flagsmith_task_processor_finished_tasks_total",
value=1.0,
labels={
"task_identifier": dummy_task_identifier,
"task_type": "standard",
"result": "success",
},
)
Expand All @@ -615,14 +627,25 @@ def test_run_tasks__expected_metrics(
value=1.0,
labels={
"task_identifier": raise_exception_task_identifier,
"task_type": "standard",
"result": "failure",
},
)
assert_metric(
name="flagsmith_task_processor_finished_tasks_total",
value=1.0,
labels={
"task_identifier": recurring_task_identifier,
"task_type": "recurring",
"result": "success",
},
)
assert_metric(
name="flagsmith_task_processor_task_duration_seconds",
value=mocker.ANY,
labels={
"task_identifier": dummy_task_identifier,
"task_type": "standard",
"result": "success",
},
)
Expand All @@ -631,9 +654,19 @@ def test_run_tasks__expected_metrics(
value=mocker.ANY,
labels={
"task_identifier": raise_exception_task_identifier,
"task_type": "standard",
"result": "failure",
},
)
assert_metric(
name="flagsmith_task_processor_task_duration_seconds",
value=mocker.ANY,
labels={
"task_identifier": recurring_task_identifier,
"task_type": "recurring",
"result": "success",
},
)


@pytest.mark.django_db(transaction=True)
Expand Down