-
Notifications
You must be signed in to change notification settings - Fork 51
feat(tasks): add task-stream lifecycle metrics #387
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
Open
cyntwang99
wants to merge
7
commits into
main
Choose a base branch
from
cynthiawang/agx1-618-2a-task-stream-lifecycle-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+369
−27
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6fe03ed
feat(tasks): add task-stream lifecycle metrics
cyntwang99 5a3541a
fix(tasks): emit task_stream.active as an absolute StatsD gauge
cyntwang99 6d51442
fix(tasks): drop StatsD active gauge, keep concurrency OTel-only
cyntwang99 0ea6a65
Merge branch 'main' into cynthiawang/agx1-618-2a-task-stream-lifecycl…
cyntwang99 830cbcb
fix(tasks): guarantee stream open/close pairing; tag duration by outcome
cyntwang99 017dc06
Merge remote-tracking branch 'origin/main' into cynthiawang/agx1-618-…
cyntwang99 133f0bb
fix(tasks): drop direct StatsD emission from stream metrics, OTel-only
cyntwang99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| """ | ||
| Metrics instrumentation for the task-event SSE stream lifecycle. | ||
|
|
||
| Emission is OpenTelemetry-only: | ||
|
|
||
| - When an OTLP endpoint is configured (``OTEL_EXPORTER_OTLP_ENDPOINT``), the | ||
| instruments are recorded through the OpenTelemetry SDK. | ||
| - When it is not configured, every function here is a cheap no-op. | ||
|
|
||
| Unlike the older ``cache_metrics.py`` dual-emit path, there is no direct | ||
| StatsD/DogStatsD emission here. These are new metrics, and the target state | ||
| routes OTel to Datadog through the collector rather than emitting to the Datadog | ||
| Agent directly — so a second, DogStatsD-native copy of every point would just be | ||
| redundant. | ||
|
|
||
| **Why this exists (see AGX1-616/AGX1-618):** HTTP request-level RED for | ||
| ``GET /tasks/{task_id}/stream`` is already covered by auto-instrumentation — the | ||
| route records into ``http_server_request_duration_seconds`` at connection close. | ||
| But a single SSE request produces exactly one duration sample, which says nothing | ||
| about what happened *during* the stream's life: how many were concurrently open, | ||
| whether a stream ended normally vs. by client disconnect vs. by error, or whether | ||
| a stream went quiet (no events pushed) for a stretch. Those are the stream- | ||
| lifecycle signals request-level instrumentation cannot express, and they are what | ||
| this module adds. | ||
|
|
||
| Instrument names are deliberately dotted (``agentex.task_stream.*``); the OTLP → | ||
| Prometheus translation flattens dots to underscores, appends ``_total`` to | ||
| monotonic counters, and appends the unit (``s`` → ``_seconds``) to the histogram, | ||
| so the series land in Mimir as ``agentex_task_stream_opened_total``, | ||
| ``agentex_task_stream_closed_total``, ``agentex_task_stream_duration_seconds``, | ||
| ``agentex_task_stream_active`` and ``agentex_task_stream_stall_total``. These | ||
| hand-instrumented series carry only bounded attributes (``outcome``) and no | ||
| ``http_route`` label — group by ``outcome``, not ``http_route``. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Literal | ||
|
|
||
| from src.utils.logging import make_logger | ||
| from src.utils.otel_metrics import get_meter | ||
|
|
||
| if TYPE_CHECKING: | ||
| from opentelemetry.metrics import Counter, Histogram, UpDownCounter | ||
|
|
||
| logger = make_logger(__name__) | ||
|
|
||
| # How a stream ended. "completed" = the generator returned normally; | ||
| # "client_disconnect" = the client went away (asyncio.CancelledError); | ||
| # "error" = the stream loop raised a fatal, non-recoverable exception. HTTP | ||
| # status 200 at connection close cannot tell these three apart, which is why the | ||
| # distinction lives here as a bounded label rather than on the request metric. | ||
| StreamOutcome = Literal["completed", "client_disconnect", "error"] | ||
|
|
||
| # Lazily-created OTel instruments (created once, on first use). | ||
| _opened_counter: Counter | None = None | ||
| _closed_counter: Counter | None = None | ||
| _duration_histogram: Histogram | None = None | ||
| _active_updown: UpDownCounter | None = None | ||
| _stall_counter: Counter | None = None | ||
| _instruments_initialized = False | ||
|
|
||
|
|
||
| def _ensure_instruments() -> None: | ||
| """Create the OTel instruments on first use. No-op if OTel is not configured.""" | ||
| global _opened_counter, _closed_counter, _duration_histogram | ||
| global _active_updown, _stall_counter, _instruments_initialized | ||
|
|
||
| if _instruments_initialized: | ||
| return | ||
| _instruments_initialized = True | ||
|
|
||
| meter = get_meter("agentex.task_stream") | ||
| if meter is None: | ||
| # OTel not configured; every record_* call stays a no-op. | ||
| return | ||
|
|
||
| _opened_counter = meter.create_counter( | ||
| name="agentex.task_stream.opened", | ||
| description="Task-event SSE streams opened", | ||
| unit="{stream}", | ||
| ) | ||
| _closed_counter = meter.create_counter( | ||
| name="agentex.task_stream.closed", | ||
| description="Task-event SSE streams closed, tagged by outcome", | ||
| unit="{stream}", | ||
| ) | ||
| _duration_histogram = meter.create_histogram( | ||
| name="agentex.task_stream.duration", | ||
| description="Task-event SSE stream lifetime", | ||
| unit="s", | ||
| ) | ||
| _active_updown = meter.create_up_down_counter( | ||
| name="agentex.task_stream.active", | ||
| description="Currently open task-event SSE streams", | ||
| unit="{stream}", | ||
| ) | ||
| _stall_counter = meter.create_counter( | ||
| name="agentex.task_stream.stall", | ||
| description="Task-event SSE streams that went idle (no event pushed) past the stall threshold", | ||
| unit="{stream}", | ||
| ) | ||
|
|
||
|
|
||
| def record_stream_opened() -> None: | ||
| """ | ||
| Record a task-event stream opening: bumps the opened counter and the active gauge. | ||
|
|
||
| Pair every call with exactly one ``record_stream_closed`` so the active gauge | ||
| stays balanced. Never raises: see ``record_stream_closed``. | ||
| """ | ||
| try: | ||
| _ensure_instruments() | ||
|
|
||
| if _opened_counter is not None: | ||
| _opened_counter.add(1) | ||
| if _active_updown is not None: | ||
| _active_updown.add(1) | ||
| except Exception: | ||
| logger.debug("Failed to emit agentex.task_stream.opened metric", exc_info=True) | ||
|
|
||
|
|
||
| def record_stream_closed(outcome: StreamOutcome, duration_seconds: float) -> None: | ||
| """ | ||
| Record a task-event stream closing: bumps the closed counter (tagged by | ||
| outcome), records the stream lifetime (also tagged by outcome), and lowers | ||
| the active gauge. | ||
|
|
||
| Args: | ||
| outcome: One of "completed", "client_disconnect", "error". | ||
| duration_seconds: How long the stream was open. | ||
|
|
||
| Never raises: emission failures (e.g. an OTel SDK fault) are swallowed so | ||
| instrumentation can never disrupt the SSE path. | ||
| """ | ||
| try: | ||
| _ensure_instruments() | ||
|
|
||
| if _closed_counter is not None: | ||
| _closed_counter.add(1, {"outcome": outcome}) | ||
| if _duration_histogram is not None: | ||
| _duration_histogram.record(duration_seconds, {"outcome": outcome}) | ||
| if _active_updown is not None: | ||
| _active_updown.add(-1) | ||
| except Exception: | ||
| logger.debug("Failed to emit agentex.task_stream.closed metric", exc_info=True) | ||
|
|
||
|
|
||
| def record_stream_stall() -> None: | ||
| """ | ||
| Record that an open stream went idle — no event pushed to the client for | ||
| longer than the configured stall threshold. Emit once per stall episode | ||
| (the caller de-dupes) so the counter measures stall onsets, not idle cycles. | ||
|
|
||
| Never raises: see ``record_stream_closed``. | ||
| """ | ||
| try: | ||
| _ensure_instruments() | ||
|
|
||
| if _stall_counter is not None: | ||
| _stall_counter.add(1) | ||
| except Exception: | ||
| logger.debug("Failed to emit agentex.task_stream.stall metric", exc_info=True) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.