|
| 1 | +"""Regression coverage for work item 196 monitoring release fixes.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +from collections import Counter |
| 7 | +from pathlib import Path |
| 8 | +from typing import Any |
| 9 | + |
| 10 | + |
| 11 | +PROJECT_ROOT = Path(__file__).resolve().parents[2] |
| 12 | +DASHBOARD_DIRS = ( |
| 13 | + PROJECT_ROOT / "config" / "grafana" / "dashboards", |
| 14 | + PROJECT_ROOT / "postgres_ai_helm" / "config" / "grafana" / "dashboards", |
| 15 | +) |
| 16 | +DASHBOARD_1 = DASHBOARD_DIRS[0] / "Dashboard_1_Node_performance_overview.json" |
| 17 | +DASHBOARD_7 = DASHBOARD_DIRS[0] / "Dashboard_7_Autovacuum_and_xmin_horizon.json" |
| 18 | + |
| 19 | + |
| 20 | +def load_dashboard(path: Path) -> dict[str, Any]: |
| 21 | + return json.loads(path.read_text()) |
| 22 | + |
| 23 | + |
| 24 | +def iter_panels(value: Any): |
| 25 | + if isinstance(value, dict): |
| 26 | + if "id" in value and "type" in value: |
| 27 | + yield value |
| 28 | + for child in value.values(): |
| 29 | + yield from iter_panels(child) |
| 30 | + elif isinstance(value, list): |
| 31 | + for child in value: |
| 32 | + yield from iter_panels(child) |
| 33 | + |
| 34 | + |
| 35 | +def panel_by_title(dashboard: dict[str, Any], title: str) -> dict[str, Any]: |
| 36 | + for panel in iter_panels(dashboard): |
| 37 | + if panel.get("title") == title: |
| 38 | + return panel |
| 39 | + raise AssertionError(f"Panel {title!r} not found") |
| 40 | + |
| 41 | + |
| 42 | +def variable_by_name(dashboard: dict[str, Any], name: str) -> dict[str, Any]: |
| 43 | + for variable in dashboard.get("templating", {}).get("list", []): |
| 44 | + if variable.get("name") == name: |
| 45 | + return variable |
| 46 | + raise AssertionError(f"Variable {name!r} not found") |
| 47 | + |
| 48 | + |
| 49 | +def target_expr(panel: dict[str, Any]) -> str: |
| 50 | + targets = panel.get("targets") or [] |
| 51 | + assert targets, f"Panel {panel.get('title')!r} has no targets" |
| 52 | + return targets[0].get("expr", "") |
| 53 | + |
| 54 | + |
| 55 | +def test_dashboard_panel_ids_are_unique() -> None: |
| 56 | + for dashboard_dir in DASHBOARD_DIRS: |
| 57 | + for path in dashboard_dir.glob("*.json"): |
| 58 | + ids = [panel["id"] for panel in iter_panels(load_dashboard(path))] |
| 59 | + duplicates = sorted( |
| 60 | + panel_id for panel_id, count in Counter(ids).items() if count > 1 |
| 61 | + ) |
| 62 | + assert not duplicates, f"{path.relative_to(PROJECT_ROOT)} duplicate ids: {duplicates}" |
| 63 | + |
| 64 | + |
| 65 | +def test_helm_dashboard_parity_is_preserved() -> None: |
| 66 | + source_dir, helm_dir = DASHBOARD_DIRS |
| 67 | + for source_path in source_dir.glob("*.json"): |
| 68 | + helm_path = helm_dir / source_path.name |
| 69 | + assert helm_path.exists(), f"Missing helm dashboard {helm_path.name}" |
| 70 | + assert source_path.read_text() == helm_path.read_text(), source_path.name |
| 71 | + |
| 72 | + |
| 73 | +def test_vacuum_timeline_queries_are_scoped() -> None: |
| 74 | + expectations = { |
| 75 | + DASHBOARD_1: (), |
| 76 | + DASHBOARD_7: ('schema_name=~"$schema_name"', 'table_name=~"$table_name"'), |
| 77 | + } |
| 78 | + for path, extra_filters in expectations.items(): |
| 79 | + expr = target_expr(panel_by_title(load_dashboard(path), "Vacuum timeline")) |
| 80 | + for phase in range(1, 8): |
| 81 | + fragment = f'pgwatch_pg_vacuum_progress_index_vacuum_count{{cluster="$cluster_name", node_name="$node_name", datname="$db_name", phase="{phase}"' |
| 82 | + assert fragment in expr |
| 83 | + for extra_filter in extra_filters: |
| 84 | + assert extra_filter in expr |
| 85 | + assert 'pgwatch_pg_vacuum_progress_index_vacuum_count{phase="' not in expr |
| 86 | + |
| 87 | + |
| 88 | +def test_dashboard_7_schema_and_table_variables_are_scoped() -> None: |
| 89 | + dashboard = load_dashboard(DASHBOARD_7) |
| 90 | + schema_query = variable_by_name(dashboard, "schema_name")["query"]["query"] |
| 91 | + table_query = variable_by_name(dashboard, "table_name")["query"]["query"] |
| 92 | + |
| 93 | + for query in (schema_query, table_query): |
| 94 | + assert 'cluster="$cluster_name"' in query |
| 95 | + assert 'node_name="$node_name"' in query |
| 96 | + assert 'datname="$db_name"' in query |
| 97 | + |
| 98 | + assert 'schema!~"pg_.*|information_schema|_timescaledb.*"' in schema_query |
| 99 | + assert "allValue" not in variable_by_name(dashboard, "schema_name") |
| 100 | + assert 'schema=~"$schema_name"' in table_query |
| 101 | + |
| 102 | + |
| 103 | +def test_dashboard_7_wraparound_topn_excludes_system_schemas() -> None: |
| 104 | + dashboard = load_dashboard(DASHBOARD_7) |
| 105 | + system_schema_filter = 'schema!~"pg_.*|information_schema|_timescaledb.*"' |
| 106 | + |
| 107 | + xid_expr = target_expr(panel_by_title(dashboard, "Top-N tables by XID age (relfrozenxid)")) |
| 108 | + mxid_expr = target_expr(panel_by_title(dashboard, "Top-N tables by MultiXID age (relminmxid)")) |
| 109 | + |
| 110 | + for expr in (xid_expr, mxid_expr): |
| 111 | + assert 'cluster="$cluster_name"' in expr |
| 112 | + assert 'node_name="$node_name"' in expr |
| 113 | + assert 'datname=~"$db_name"' in expr |
| 114 | + assert system_schema_filter in expr |
| 115 | + |
| 116 | + |
| 117 | +def test_dashboard_1_surfaces_pg_wal_size_status_code() -> None: |
| 118 | + dashboard = load_dashboard(DASHBOARD_1) |
| 119 | + size_panel = panel_by_title(dashboard, "pg_wal directory size") |
| 120 | + status_panel = panel_by_title(dashboard, "pg_wal size collection status") |
| 121 | + |
| 122 | + assert size_panel["id"] != status_panel["id"] |
| 123 | + assert size_panel["gridPos"]["y"] == 113 |
| 124 | + assert status_panel["gridPos"]["y"] == 123 |
| 125 | + assert "[10m]" in target_expr(size_panel) |
| 126 | + status_expr = target_expr(status_panel) |
| 127 | + assert "pgwatch_pg_wal_size_status_code" in status_expr |
| 128 | + assert "[10m]" in status_expr |
| 129 | + mapping_text = json.dumps(status_panel.get("fieldConfig", {})) |
| 130 | + assert "pg_ls_waldir() unavailable" in mapping_text |
| 131 | + assert "EXECUTE missing" in mapping_text |
0 commit comments