Skip to content

Commit 42f7365

Browse files
authored
feat(task-processor): Add task_type label to task processor metrics (#51)
* feat(task-processor): Add `task_type` label to task processor metrics
1 parent 1d35c43 commit 42f7365

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ The following default metrics are exposed:
6969

7070
##### Task Processor metrics
7171

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

7575
##### Guidelines
7676

src/task_processor/metrics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
flagsmith_task_processor_finished_tasks_total = prometheus_client.Counter(
1414
"flagsmith_task_processor_finished_tasks_total",
1515
"Total number of finished tasks",
16-
["task_identifier", "result"],
16+
["task_identifier", "task_type", "result"],
1717
)
1818
flagsmith_task_processor_task_duration_seconds = Histogram(
1919
"flagsmith_task_processor_task_duration_seconds",
2020
"Task processor task duration in seconds",
21-
["task_identifier", "result"],
21+
["task_identifier", "task_type", "result"],
2222
)

src/task_processor/processor.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
TaskResult,
1818
TaskRun,
1919
)
20+
from task_processor.task_registry import get_task
2021

2122
T = typing.TypeVar("T", bound=AbstractBaseTask)
2223
AnyTaskRun = TaskRun | RecurringTaskRun
@@ -150,17 +151,15 @@ def _run_task(
150151
exc_info=True,
151152
)
152153

153-
result_label_value = result.lower()
154+
labels = {
155+
"task_identifier": task_identifier,
156+
"task_type": get_task(task_identifier).task_type.value.lower(),
157+
"result": result.lower(),
158+
}
154159

155-
timer.labels(
156-
task_identifier=task_identifier,
157-
result=result_label_value,
158-
) # type: ignore[no-untyped-call]
160+
timer.labels(**labels) # type: ignore[no-untyped-call]
159161
ctx.close()
160162

161-
metrics.flagsmith_task_processor_finished_tasks_total.labels(
162-
task_identifier=task_identifier,
163-
result=result_label_value,
164-
).inc()
163+
metrics.flagsmith_task_processor_finished_tasks_total.labels(**labels).inc()
165164

166165
return task, task_run

tests/unit/task_processor/test_unit_task_processor_processor.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,18 @@ def test_run_tasks__expected_metrics(
585585
mocker: MockerFixture,
586586
) -> None:
587587
# Given
588+
@register_recurring_task(run_every=timedelta(milliseconds=200))
589+
def _fake_recurring_task() -> None:
590+
pass
591+
592+
initialise()
593+
588594
dummy_task_identifier = dummy_task.task_identifier
589595
raise_exception_task_identifier = raise_exception_task.task_identifier
596+
recurring_task_identifier = RecurringTask.objects.latest(
597+
"created_at",
598+
).task_identifier
599+
590600
Task.create(
591601
dummy_task_identifier,
592602
scheduled_for=timezone.now(),
@@ -600,13 +610,15 @@ def test_run_tasks__expected_metrics(
600610

601611
# When
602612
run_tasks(2)
613+
run_recurring_tasks()
603614

604615
# Then
605616
assert_metric(
606617
name="flagsmith_task_processor_finished_tasks_total",
607618
value=1.0,
608619
labels={
609620
"task_identifier": dummy_task_identifier,
621+
"task_type": "standard",
610622
"result": "success",
611623
},
612624
)
@@ -615,14 +627,25 @@ def test_run_tasks__expected_metrics(
615627
value=1.0,
616628
labels={
617629
"task_identifier": raise_exception_task_identifier,
630+
"task_type": "standard",
618631
"result": "failure",
619632
},
620633
)
634+
assert_metric(
635+
name="flagsmith_task_processor_finished_tasks_total",
636+
value=1.0,
637+
labels={
638+
"task_identifier": recurring_task_identifier,
639+
"task_type": "recurring",
640+
"result": "success",
641+
},
642+
)
621643
assert_metric(
622644
name="flagsmith_task_processor_task_duration_seconds",
623645
value=mocker.ANY,
624646
labels={
625647
"task_identifier": dummy_task_identifier,
648+
"task_type": "standard",
626649
"result": "success",
627650
},
628651
)
@@ -631,9 +654,19 @@ def test_run_tasks__expected_metrics(
631654
value=mocker.ANY,
632655
labels={
633656
"task_identifier": raise_exception_task_identifier,
657+
"task_type": "standard",
634658
"result": "failure",
635659
},
636660
)
661+
assert_metric(
662+
name="flagsmith_task_processor_task_duration_seconds",
663+
value=mocker.ANY,
664+
labels={
665+
"task_identifier": recurring_task_identifier,
666+
"task_type": "recurring",
667+
"result": "success",
668+
},
669+
)
637670

638671

639672
@pytest.mark.django_db(transaction=True)

0 commit comments

Comments
 (0)