feat(grafana-metric-gateway): slice 1 — backend gateway transport

Route all prometheus widget queries through Grafana /api/ds/query instead of
direct Prom HTTP. PrometheusConfig: drop base_url, add grafana_url +
datasource_uid; secret grafana_api_key (required). PrometheusWidgetSource →
MetricSource with _gateway_query POST method. normalize_grafana_frames
recovered from 65bae95 + shared _dedup_label helper. Gateway-path status
check. Startup old-config validation. CHANGELOG migration note. All adapter
tests rewritten for POST /api/ds/query + Grafana frames mock. Backend: 331
pytest pass, ruff clean. Frontend: build green (unchanged in S1).
This commit is contained in:
Developer
2026-07-09 21:24:44 +00:00
parent 798196ffc7
commit df80c68f89
10 changed files with 573 additions and 250 deletions
+17 -17
View File
@@ -117,7 +117,7 @@ def test_widget_kind_lookup():
def test_service_config_schema_is_json_schema():
schema = get_service_definition("prometheus").config_schema
assert schema["type"] == "object"
assert "base_url" in schema["properties"]
assert "grafana_url" in schema["properties"]
# ---------------------------------------------------------------------------
@@ -182,7 +182,7 @@ def test_list_service_types(client):
def test_service_type_includes_secret_and_widget_metadata(client):
response = client.get("/api/services/types")
prom = next(item for item in response.json() if item["service_type"] == "prometheus")
assert [sf["key"] for sf in prom["secret_fields"]] == ["api_key"]
assert [sf["key"] for sf in prom["secret_fields"]] == ["grafana_api_key"]
assert set(wk["kind"] for wk in prom["widget_kinds"]) == {"metric", "chart", "gauge", "mean"}
@@ -195,8 +195,8 @@ def _prometheus_payload(**overrides):
payload = {
"service_type": "prometheus",
"name": "Production Prometheus",
"config": {"base_url": "https://prometheus.example.com"},
"secrets": {"api_key": "secret-token"},
"config": {"grafana_url": "https://grafana.example.com", "datasource_uid": "prometheus"},
"secrets": {"grafana_api_key": "secret-token"},
"enabled": True,
}
payload.update(overrides)
@@ -208,10 +208,10 @@ def test_create_and_list_service(client):
assert response.status_code == 201
created = response.json()
assert created["service_type"] == "prometheus"
assert created["config"]["base_url"] == "https://prometheus.example.com"
assert created["config"]["grafana_url"] == "https://grafana.example.com"
# Plaintext secrets are never returned.
assert "secrets" not in created
assert created["secrets_set"] == {"api_key": True}
assert created["secrets_set"] == {"grafana_api_key": True}
response = client.get("/api/services/instances")
assert response.status_code == 200
@@ -242,11 +242,11 @@ def test_update_service_preserves_unsent_secrets(client):
json={
"service_type": "prometheus",
"name": "Renamed Prometheus",
"config": {"base_url": "https://prometheus.example.com", "timeout_seconds": 10},
"config": {"grafana_url": "https://grafana.example.com", "timeout_seconds": 10},
},
).json()
assert updated["name"] == "Renamed Prometheus"
assert updated["secrets_set"] == {"api_key": True}
assert updated["secrets_set"] == {"grafana_api_key": True}
def test_update_service_can_clear_secret(client):
@@ -256,11 +256,11 @@ def test_update_service_can_clear_secret(client):
json={
"service_type": "prometheus",
"name": "Production Prometheus",
"config": {"base_url": "https://prometheus.example.com"},
"secrets": {"api_key": ""},
"config": {"grafana_url": "https://grafana.example.com"},
"secrets": {"grafana_api_key": ""},
},
).json()
assert updated["secrets_set"] == {"api_key": False}
assert updated["secrets_set"] == {"grafana_api_key": False}
def test_unknown_service_type_rejected(client):
@@ -274,7 +274,7 @@ def test_unknown_service_type_rejected(client):
def test_invalid_config_rejected(client):
response = client.post(
"/api/services/instances",
json={"service_type": "prometheus", "name": "x", "config": {"base_url": ""}},
json={"service_type": "prometheus", "name": "x", "config": {"grafana_url": ""}},
)
assert response.status_code == 422
# Force a real validation error via bad type.
@@ -292,10 +292,10 @@ def test_service_base_url_requires_http_schema(bad_url):
"""Every service base_url must include an http:// or https:// schema."""
model = get_service_definition("prometheus").config_model
with pytest.raises(ValidationError):
model.model_validate({"base_url": bad_url, "timeout_seconds": 5})
model.model_validate({"grafana_url": bad_url, "timeout_seconds": 5})
@pytest.mark.parametrize("service_type", ["prometheus", "alertmanager", "jellyfin", "authentik", "nextcloud"])
@pytest.mark.parametrize("service_type", ["alertmanager", "jellyfin", "authentik", "nextcloud"])
def test_service_base_url_accepts_absolute_urls(service_type):
model = get_service_definition(service_type).config_model
instance = model.model_validate({"base_url": "https://example.com"})
@@ -308,7 +308,7 @@ def test_unknown_secret_field_rejected(client):
json={
"service_type": "prometheus",
"name": "x",
"config": {"base_url": "https://prometheus.example.com"},
"config": {"grafana_url": "https://grafana.example.com"},
"secrets": {"password": "leak"},
},
)
@@ -321,7 +321,7 @@ def test_credential_key_in_config_rejected(client):
json={
"service_type": "prometheus",
"name": "x",
"config": {"base_url": "https://prometheus.example.com", "api_key": "leak"},
"config": {"grafana_url": "https://grafana.example.com", "api_key": "leak"},
},
)
assert response.status_code == 422
@@ -369,7 +369,7 @@ def test_delete_service_cascades_to_widgets(client, tmp_path):
"""
store = app.dependency_overrides[get_settings_store]()
service = store.upsert_service(
{"service_type": "prometheus", "name": "Prometheus", "config": {"base_url": "u"}, "enabled": True}
{"service_type": "prometheus", "name": "Prometheus", "config": {"grafana_url": "u"}, "enabled": True}
)
# Ensure the service_id column exists and seed a referencing widget.