add prometheus metric for in_flight
This commit is contained in:
parent
c9bf53ac06
commit
dcde4b2907
|
|
@ -95,6 +95,8 @@ class EmbedTaskGate:
|
|||
prometheus_metrics.record_embed_now_task_shed()
|
||||
return False
|
||||
self.in_flight += 1
|
||||
if settings.METRICS.ENABLED:
|
||||
prometheus_metrics.set_embed_now_tasks_in_flight(self.in_flight)
|
||||
background_tasks.add_task(self._run, message_ids)
|
||||
return True
|
||||
|
||||
|
|
@ -103,6 +105,8 @@ class EmbedTaskGate:
|
|||
await embed_messages_now(message_ids)
|
||||
finally:
|
||||
self.in_flight -= 1
|
||||
if settings.METRICS.ENABLED:
|
||||
prometheus_metrics.set_embed_now_tasks_in_flight(self.in_flight)
|
||||
|
||||
|
||||
embed_task_gate = EmbedTaskGate()
|
||||
|
|
|
|||
|
|
@ -93,6 +93,12 @@ embed_now_tasks_shed_counter = NamespacedCounter(
|
|||
["namespace"],
|
||||
)
|
||||
|
||||
embed_now_tasks_in_flight_gauge = NamespacedGauge(
|
||||
"embed_now_tasks_in_flight",
|
||||
"Immediate-embed background tasks currently in flight for this process",
|
||||
["namespace"],
|
||||
)
|
||||
|
||||
dialectic_calls_counter = NamespacedCounter(
|
||||
"dialectic_calls",
|
||||
"Total dialectic calls",
|
||||
|
|
@ -216,6 +222,12 @@ class PrometheusMetrics:
|
|||
except Exception as e:
|
||||
self._handle_metric_error("record_embed_now_task_shed", e)
|
||||
|
||||
def set_embed_now_tasks_in_flight(self, count: int) -> None:
|
||||
try:
|
||||
embed_now_tasks_in_flight_gauge.labels().set(count)
|
||||
except Exception as e:
|
||||
self._handle_metric_error("set_embed_now_tasks_in_flight", e)
|
||||
|
||||
def record_dialectic_call(
|
||||
self,
|
||||
*,
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ from unittest.mock import AsyncMock, patch
|
|||
import pytest
|
||||
from fastapi import BackgroundTasks
|
||||
from nanoid import generate as generate_nanoid
|
||||
from prometheus_client import REGISTRY
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker
|
||||
|
||||
|
|
@ -125,6 +126,27 @@ class TestEmbedTaskGate:
|
|||
await tasks()
|
||||
assert embed_task_gate.in_flight == 0
|
||||
|
||||
async def test_gauge_mirrors_in_flight_count(self) -> None:
|
||||
"""With metrics enabled, the Prometheus gauge tracks the gate's
|
||||
in-flight count through schedule and release."""
|
||||
|
||||
def gauge_value() -> float | None:
|
||||
return REGISTRY.get_sample_value(
|
||||
"embed_now_tasks_in_flight", {"namespace": "test"}
|
||||
)
|
||||
|
||||
tasks = BackgroundTasks()
|
||||
with (
|
||||
patch.object(settings.EMBEDDING, "MAX_PENDING_EMBED_TASKS", 2),
|
||||
patch.object(settings.METRICS, "ENABLED", True),
|
||||
patch.object(settings.METRICS, "NAMESPACE", "test"),
|
||||
patch("src.reconciler.embed_now.embed_messages_now", new=AsyncMock()),
|
||||
):
|
||||
assert embed_task_gate.try_schedule(tasks, ["msg_1"]) is True
|
||||
assert gauge_value() == 1
|
||||
await tasks()
|
||||
assert gauge_value() == 0
|
||||
|
||||
async def test_zero_cap_disables_fast_path(self) -> None:
|
||||
"""MAX_PENDING_EMBED_TASKS=0 rejects every schedule attempt."""
|
||||
tasks = BackgroundTasks()
|
||||
|
|
|
|||
Loading…
Reference in New Issue