langgraph/libs/cli/tests/unit_tests/test_deployment_metrics.py
hari-dhanushkodi cf35b4a3a1
feat(cli): add deploy metrics command (#9479)
Adds `langgraph deploy metrics` with compact, agent-friendly JSON
grouped by section, chart, and series. Existing time-range and metric
filters are preserved; host-backend and its UI response schema are
unchanged.

- Default: real `project_metric`, inferred units, three-decimal values,
latest/min/max/avg, and `[ISO timestamp, value]` points; empty sections
are explicit.
- `--summary`: same shape without points. `--raw`: original host-backend
schema without reshaping or rounding. Mutually exclusive.
- Bounds always report the requested window: end defaults to now (UTC),
and start defaults to 15 minutes before the resolved end. With only
`--end-time`, start is 15 minutes before that value; with only
`--start-time`, end is now. The CLI sends both bounds explicitly and
reports identical values in the output. `step_seconds` is the most
common within-series timestamp gap (smallest on ties), uses an integer
for whole seconds, and is null only when no series has two timestamps.
Statistics ignore missing/non-finite values; latest means latest
non-missing sample.

Size comparison (realistic synthetic unit-test fixture, identical
compact JSON serialization; not a live deployment benchmark):

| Before: raw UI schema | After: default | After: summary |
| --- | --- | --- |
| 4,302 bytes | 1,226 bytes (72% smaller) | 806 bytes (81% smaller) |

Known gotcha (pre-existing, unchanged here): pass `--host-url` after the
subcommand. A group-level `langgraph deploy --host-url ... metrics <id>`
silently ignores that option and can hit prod instead, producing a
confusing 403.

```bash
langgraph deploy metrics --host-url https://dev.api.host.langchain.com <id>
```

Follow-up: https://github.com/langchain-ai/langgraph/pull/9562 adds
compute and Postgres tier scaling, stacked on this branch. Retarget it
to `main` after this PR merges.

## References
- [Slack
thread](https://langchain.slack.com/archives/C0BHM1C5R5L/p1793216413707489)

Made by [Open SWE](https://github.com/langchain-ai/open-swe) · [view
thread](https://openswe.langchain.dev/agents/5c8cafeb-c089-587f-aa85-af220f2f13f6)
· openai:gpt-6.1-sol (medium)

---------

Co-authored-by: Hari Dhanushkodi <hari-dhanushkodi@users.noreply.github.com>
Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 18:20:14 -05:00

139 lines
5 KiB
Python

from copy import deepcopy
import pytest
from langgraph_cli.deployment_metrics import compact_deployment_metrics
def reshape(response, deployment_id, **kwargs):
return compact_deployment_metrics(
response,
deployment_id,
start_time=kwargs.pop("start_time", "2026-10-28T00:00:00Z"),
end_time=kwargs.pop("end_time", "2026-10-28T01:00:00Z"),
**kwargs,
)
def test_compact_metrics(deployment_metrics_response):
original = deepcopy(deployment_metrics_response)
result = reshape(deployment_metrics_response, "deployment-id")
assert deployment_metrics_response == original
assert result["deployment_id"] == "deployment-id"
assert result["start_time"] == "2026-10-28T00:00:00Z"
assert result["end_time"] == "2026-10-28T01:00:00Z"
assert result["step_seconds"] == 60
assert result["sections"]["Postgres"] == {"_empty": True}
queue = result["sections"]["Queue"]
memory = queue["Memory Usage %"]
assert memory["Avg Memory Usage %"] == {
"metric": "memory_usage",
"unit": "percent",
"latest": 20.235,
"min": 10.123,
"max": 20.235,
"avg": 15.179,
"points": [
["2026-10-28T00:00:00Z", 10.123],
["2026-10-28T00:01:00Z", 20.235],
["2026-10-28T00:02:00Z", None],
],
}
assert memory["Max Memory Usage %"]["max"] == 50.333
assert queue["Restarts"]["Restarts"]["unit"] == "count"
assert queue["Queue wait time"]["Queue wait time"] == {
"metric": "run_queue_wait_time",
"unit": None,
"latest": None,
"min": None,
"max": None,
"avg": None,
"points": [],
}
assert result["sections"]["API"]["Latency"]["P95 Latency"]["unit"] is None
def test_summary_and_requested_bounds(deployment_metrics_response):
result = reshape(
deployment_metrics_response,
"id",
start_time="2026-10-27T23:00:00Z",
end_time="2026-10-28T01:00:00Z",
summary=True,
)
assert result["start_time"] == "2026-10-27T23:00:00Z"
assert result["end_time"] == "2026-10-28T01:00:00Z"
for section in result["sections"].values():
if section == {"_empty": True}:
continue
for chart in section.values():
for series in chart.values():
assert set(series) == {"metric", "unit", "latest", "min", "max", "avg"}
@pytest.mark.parametrize("values", [[], [None], [float("nan"), float("inf")]])
def test_empty_and_nonfinite_values(deployment_metrics_response, values):
chart = deployment_metrics_response["sub_sections"][0]["charts"][0]
chart["series"] = chart["series"][:1]
chart["data"] = [
{
"series_id": chart["series"][0]["id"],
"timestamp": f"2026-10-28T00:0{i}:00Z",
"value": value,
}
for i, value in enumerate(values)
]
result = reshape(deployment_metrics_response, "id")
series = result["sections"]["Queue"]["Memory Usage %"]["Avg Memory Usage %"]
assert all(series[key] is None for key in ["latest", "min", "max", "avg"])
assert all(point[1] is None for point in series["points"])
def test_tied_spacing_and_top_level_charts(deployment_metrics_response):
response = deployment_metrics_response
chart = response["sub_sections"][1]["charts"][0]
chart["data"][0]["timestamp"] = "2026-10-28T00:02:30Z"
response["charts"] = [chart]
response["sub_sections"] = []
result = reshape(response, "id")
assert result["step_seconds"] == 60
assert "Latency" in result["sections"][response["title"]]
def test_missing_point_preserves_common_spacing(deployment_metrics_response):
chart = deployment_metrics_response["sub_sections"][0]["charts"][0]
series_id = chart["series"][0]["id"]
chart["data"] = [
point
for point in chart["data"]
if not (point["series_id"] == series_id and "00:01:00" in point["timestamp"])
]
result = reshape(deployment_metrics_response, "id")
assert result["step_seconds"] == 60
assert type(result["step_seconds"]) is int
def test_only_single_point_and_empty_series(deployment_metrics_response):
for section in deployment_metrics_response["sub_sections"]:
for chart in section["charts"]:
chart["data"] = chart["data"][:1]
assert reshape(deployment_metrics_response, "id")["step_seconds"] is None
def test_fractional_spacing(deployment_metrics_response):
chart = deployment_metrics_response["sub_sections"][1]["charts"][0]
chart["data"][0]["timestamp"] = "2026-10-28T00:00:01Z"
chart["data"][1]["timestamp"] = "2026-10-28T00:00:00.500Z"
deployment_metrics_response["sub_sections"] = []
deployment_metrics_response["charts"] = [chart]
assert reshape(deployment_metrics_response, "id")["step_seconds"] == 0.5
def test_no_data():
assert reshape({"charts": [], "sub_sections": []}, "id") == {
"deployment_id": "id",
"start_time": "2026-10-28T00:00:00Z",
"end_time": "2026-10-28T01:00:00Z",
"step_seconds": None,
"sections": {"_empty": True},
}