ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
Wire Grafana to Prometheus and Loki by attaching
Juan Denis722072a14b61
diff --git a/backend/app/api/app_attachments.py b/backend/app/api/app_attachments.pyindex fd0229af..3738158c 100644--- a/backend/app/api/app_attachments.py+++ b/backend/app/api/app_attachments.py@@ -29,7 +29,8 @@ def list_attachments(app_id):app = AppAttachmentService.live_app(app_id)if app is None or not ResourceGrantService.can_access_app(get_current_user(), app):raise NotFoundError('Application not found')- return jsonify({'attachments': [a.to_dict() for a in AppAttachmentService.list_for_app(app)]})+ return jsonify({'attachments': [a.to_dict() for a in AppAttachmentService.list_for_app(app)],+ 'kinds': AppAttachmentService.kinds_for(app)})def _kind_or_400(kind):diff --git a/backend/app/data/grafana/serverkit-overview.json b/backend/app/data/grafana/serverkit-overview.jsonnew file mode 100644index 00000000..5255b05a--- /dev/null+++ b/backend/app/data/grafana/serverkit-overview.json@@ -0,0 +1,59 @@+{+ "uid": "serverkit-overview",+ "title": "ServerKit servers",+ "tags": ["serverkit"],+ "timezone": "browser",+ "schemaVersion": 39,+ "refresh": "30s",+ "time": {"from": "now-6h", "to": "now"},+ "templating": {+ "list": [+ {+ "name": "server",+ "label": "Server",+ "type": "query",+ "datasource": {"type": "prometheus", "uid": "${DS_UID}"},+ "query": "label_values(serverkit_server_up, server)",+ "includeAll": true,+ "multi": true,+ "current": {"text": "All", "value": "$__all"},+ "refresh": 2+ }+ ]+ },+ "panels": [+ {+ "id": 1, "type": "stat", "title": "Servers online",+ "gridPos": {"x": 0, "y": 0, "w": 6, "h": 4},+ "datasource": {"type": "prometheus", "uid": "${DS_UID}"},+ "targets": [{"refId": "A", "expr": "sum(serverkit_server_up{server=~\"$server\"})"}]+ },+ {+ "id": 2, "type": "stat", "title": "Running containers",+ "gridPos": {"x": 6, "y": 0, "w": 6, "h": 4},+ "datasource": {"type": "prometheus", "uid": "${DS_UID}"},+ "targets": [{"refId": "A", "expr": "sum(serverkit_containers_running{server=~\"$server\"})"}]+ },+ {+ "id": 3, "type": "timeseries", "title": "CPU %",+ "gridPos": {"x": 0, "y": 4, "w": 12, "h": 8},+ "datasource": {"type": "prometheus", "uid": "${DS_UID}"},+ "fieldConfig": {"defaults": {"unit": "percent", "min": 0, "max": 100}, "overrides": []},+ "targets": [{"refId": "A", "expr": "serverkit_cpu_percent{server=~\"$server\"}", "legendFormat": "{{server}}"}]+ },+ {+ "id": 4, "type": "timeseries", "title": "Memory %",+ "gridPos": {"x": 12, "y": 4, "w": 12, "h": 8},+ "datasource": {"type": "prometheus", "uid": "${DS_UID}"},+ "fieldConfig": {"defaults": {"unit": "percent", "min": 0, "max": 100}, "overrides": []},+ "targets": [{"refId": "A", "expr": "serverkit_memory_percent{server=~\"$server\"}", "legendFormat": "{{server}}"}]+ },+ {+ "id": 5, "type": "timeseries", "title": "Disk %",+ "gridPos": {"x": 0, "y": 12, "w": 24, "h": 8},+ "datasource": {"type": "prometheus", "uid": "${DS_UID}"},+ "fieldConfig": {"defaults": {"unit": "percent", "min": 0, "max": 100}, "overrides": []},+ "targets": [{"refId": "A", "expr": "serverkit_disk_percent{server=~\"$server\"}", "legendFormat": "{{server}}"}]+ }+ ]+}diff --git a/backend/app/models/app_attachment.py b/backend/app/models/app_attachment.pyindex 813b66e7..8d279b42 100644--- a/backend/app/models/app_attachment.py+++ b/backend/app/models/app_attachment.py@@ -21,7 +21,7 @@ class AppAttachment(JsonColumnMixin, db.Model):db.UniqueConstraint('app_id', 'kind', name='uq_app_attachments_app_kind'),)- KINDS = ('storage', 'cache', 'queue')+ KINDS = ('storage', 'cache', 'queue', 'metrics', 'logs')id = db.Column(db.Integer, primary_key=True)app_id = db.Column(db.Integer, db.ForeignKey('applications.id', ondelete='CASCADE'),diff --git a/backend/app/services/app_attachment_service.py b/backend/app/services/app_attachment_service.pyindex a1d2348d..0fafc50b 100644--- a/backend/app/services/app_attachment_service.py+++ b/backend/app/services/app_attachment_service.py@@ -68,6 +68,19 @@ def _is_amqp(template, engine):return template.get('id') == 'rabbitmq'+def _is_prometheus(template, engine):+ return template.get('id') == 'prometheus'+++def _is_loki(template, engine):+ return template.get('id') == 'loki'+++# Kinds that provision a data source on a Grafana consumer (plan 86 §A4)+# instead of writing env; which service types each accepts.+GRAFANA_KINDS = {'metrics': _is_prometheus, 'logs': _is_loki}++CONNECTION_KINDS = {'cache': [(_is_redis, ('REDIS_URL',))],'queue': [(_is_amqp, ('AMQP_URL', 'BROKER_URL')), (_is_redis, ('BROKER_URL',))],@@ -78,6 +91,8 @@ FAILURE_MODES = {'storage': 'Objects outlive the app: detaching keeps the bucket and its data.','cache': 'Cached data can be stale; invalidate on write, and never keep the only copy in a cache.','queue': 'A job can be delivered twice; make jobs safe to run again.',+ 'metrics': 'Grafana shows what Prometheus kept: past its retention, history is gone.',+ 'logs': 'Grafana shows what Loki kept: past its retention, logs are gone.',}@@ -125,11 +140,35 @@ class AppAttachmentService:return keysreturn None+ @staticmethod+ def _template_and_engine(app_row):+ from app.services.service_connection_service import _template_for+ from app.services.template_service import TemplateService+ template = _template_for(app_row) or {}+ return template, TemplateService.engine_metadata(template) or {}++ @classmethod+ def kinds_for(cls, app_row):+ """What ``app_row`` can attach: every app takes cache / storage / queue;+ a Grafana install also takes metrics and logs data sources."""+ kinds = ['cache', 'storage', 'queue']+ if cls.is_grafana(app_row):+ kinds += list(GRAFANA_KINDS)+ return kinds++ @classmethod+ def is_grafana(cls, app_row):+ return cls._template_and_engine(app_row)[0].get('id') == 'grafana'+@classmethoddef services_for(cls, kind):"""Installed services an app can attach as ``kind``."""if kind == 'storage':return cls.storage_services()+ if kind in GRAFANA_KINDS:+ from app.models.application import Application+ return [a for a in Application.query_active().order_by(Application.name).all()+ if GRAFANA_KINDS[kind](*cls._template_and_engine(a))]from app.models.application import Applicationreturn [a for a in Application.query_active().order_by(Application.name).all()if cls._connection_env_keys(a, kind)]@@ -138,6 +177,8 @@ class AppAttachmentService:def attach(cls, app, service_app, kind, user_id=None):if kind == 'storage':return cls.attach_storage(app, service_app, user_id=user_id)+ if kind in GRAFANA_KINDS:+ return cls.attach_grafana_source(app, service_app, kind)if kind not in CONNECTION_KINDS:raise AttachmentError(f'Unknown attachment kind: {kind}')return cls.attach_connection(app, service_app, kind, user_id=user_id)@@ -220,6 +261,38 @@ class AppAttachmentService:db.session.commit()return row, True+ @classmethod+ def attach_grafana_source(cls, app, service_app, kind, grafana=None):+ """Create ``service_app`` as a data source on the Grafana ``app``."""+ from app.models.app_attachment import AppAttachment+ from app.services.grafana_provisioner import GrafanaError, GrafanaProvisioner+ from app.services.service_connection_service import ServiceConnectionService++ if not cls.is_grafana(app):+ raise AttachmentError(f'Only a Grafana install can attach {kind}')+ if not GRAFANA_KINDS[kind](*cls._template_and_engine(service_app)):+ raise AttachmentError(f'{service_app.name} cannot be used as {kind}')+ existing = AppAttachment.query.filter_by(app_id=app.id, kind=kind).first()+ if existing is not None:+ if existing.service_app_id != service_app.id:+ raise AttachmentError(f'This Grafana already has {kind} attached; detach it first')+ return existing, False+ url, error = ServiceConnectionService.resolve(service_app, 'url')+ if error:+ raise AttachmentError(error)+ try:+ grafana = grafana or GrafanaProvisioner.for_app(app)+ uid = grafana.upsert_datasource(kind, service_app.name, url)+ if kind == 'metrics':+ grafana.import_dashboard(uid)+ except GrafanaError as exc:+ raise AttachmentError(f'Grafana setup failed: {exc}') from exc+ row = AppAttachment(app_id=app.id, service_app_id=service_app.id, kind=kind)+ row.details = {'datasource_uid': uid, 'env_keys': []}+ db.session.add(row)+ db.session.commit()+ return row, True+@classmethoddef detach(cls, attachment, user_id=None, admin=None):"""Remove the key, its secret and the env the attach wrote. Keeps the@@ -237,6 +310,13 @@ class AppAttachmentService:# The service may already be gone or stopped; the key is# useless without the env, and the row must still go.warning = f'Key could not be revoked on {attachment.service_app.name}: {exc}'+ if details.get('datasource_uid'):+ from app.services.grafana_provisioner import GrafanaError, GrafanaProvisioner+ try:+ (admin or GrafanaProvisioner.for_app(attachment.application)).delete_datasource(+ details['datasource_uid'])+ except GrafanaError as exc:+ warning = f'Data source could not be removed from Grafana: {exc}'for env_key in details.get('env_keys') or []:EnvService.delete_env_var(attachment.app_id, env_key, user_id=user_id)if details.get('secret_name'):diff --git a/backend/app/services/grafana_provisioner.py b/backend/app/services/grafana_provisioner.pynew file mode 100644index 00000000..89ba5322--- /dev/null+++ b/backend/app/services/grafana_provisioner.py@@ -0,0 +1,86 @@+"""Wire an installed Grafana to Prometheus / Loki (plan 86 §A4).++Attaching ``metrics`` (Prometheus) or ``logs`` (Loki) to a Grafana install+creates the data source through Grafana's HTTP API, at the loopback port its+template publishes, with the admin credentials recorded at install. A metrics+attach also imports one bundled dashboard of the panel's own fleet metrics.+Data sources get a fixed uid per attached service, so re-attaching updates in+place and detaching deletes exactly what was created.++Grafana reaches the data source by container name over the shared service+network (the attachment makes Grafana join it on its next deploy).+"""+import base64+import json+import os+import urllib.error+import urllib.request+from typing import Dict, Optional++DASHBOARD_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)),+ 'data', 'grafana', 'serverkit-overview.json')+DATASOURCE_TYPES = {'metrics': 'prometheus', 'logs': 'loki'}+++class GrafanaError(Exception):+ pass+++def datasource_uid(kind: str, service_name: str) -> str:+ return f'serverkit-{kind}-{service_name}'[:40]+++class GrafanaProvisioner:+ def __init__(self, base_url: str, user: str, password: str):+ self.base_url = base_url.rstrip('/')+ token = base64.b64encode(f'{user}:{password}'.encode()).decode()+ self._auth = f'Basic {token}'++ @classmethod+ def for_app(cls, grafana_app) -> 'GrafanaProvisioner':+ from app.services.service_connection_service import _install_variables+ variables = _install_variables(grafana_app)+ port = variables.get('HTTP_PORT') or (str(grafana_app.port) if grafana_app.port else '')+ if not port or not variables.get('ADMIN_PASSWORD'):+ raise GrafanaError('the Grafana install has no recorded port or admin password')+ return cls(f'http://127.0.0.1:{port}', variables.get('ADMIN_USER') or 'admin',+ variables['ADMIN_PASSWORD'])++ def _call(self, method: str, path: str, body: Optional[Dict] = None, ok=(200,)):+ data = json.dumps(body).encode() if body is not None else None+ req = urllib.request.Request(f'{self.base_url}{path}', data=data, method=method,+ headers={'Authorization': self._auth,+ 'Content-Type': 'application/json'})+ try:+ with urllib.request.urlopen(req, timeout=20) as resp:+ raw = resp.read()+ return resp.status, (json.loads(raw) if raw else {})+ except urllib.error.HTTPError as exc:+ if exc.code in ok:+ return exc.code, {}+ detail = exc.read().decode(errors='replace')[:200]+ raise GrafanaError(f'{method} {path}: HTTP {exc.code} {detail}') from exc+ except OSError as exc:+ raise GrafanaError(f'Grafana is not reachable at {self.base_url}: {exc}') from exc++ def upsert_datasource(self, kind: str, service_name: str, url: str) -> str:+ uid = datasource_uid(kind, service_name)+ body = {'uid': uid, 'name': f'{service_name} ({DATASOURCE_TYPES[kind]})',+ 'type': DATASOURCE_TYPES[kind], 'access': 'proxy', 'url': url,+ 'isDefault': False}+ status, _ = self._call('GET', f'/api/datasources/uid/{uid}', ok=(200, 404))+ if status == 404:+ self._call('POST', '/api/datasources', body)+ else:+ self._call('PUT', f'/api/datasources/uid/{uid}', body)+ return uid++ def delete_datasource(self, uid: str) -> None:+ self._call('DELETE', f'/api/datasources/uid/{uid}', ok=(200, 404))++ def import_dashboard(self, datasource: str) -> None:+ with open(DASHBOARD_PATH, encoding='utf-8') as fh:+ dashboard = json.loads(fh.read().replace('${DS_UID}', datasource))+ self._call('POST', '/api/dashboards/db',+ {'dashboard': dashboard, 'overwrite': True,+ 'message': 'Imported by ServerKit'})diff --git a/backend/app/services/service_connection_service.py b/backend/app/services/service_connection_service.pyindex 4dd0cb35..093f798c 100644--- a/backend/app/services/service_connection_service.py+++ b/backend/app/services/service_connection_service.py@@ -190,6 +190,14 @@ class ServiceConnectionService:return Truereturn False+ @classmethod+ def has_attachments(cls, app) -> bool:+ """An app with attachments reaches its services by name (a Grafana data+ source, a bucket endpoint) even when no env reference names them."""+ from app.models.app_attachment import AppAttachment+ return AppAttachment.query.filter_by(app_id=app.id).first() is not None+@classmethoddef needs_shared_network(cls, app) -> bool:- return cls.is_connectable(app) or cls.consumes_services(app)+ return (cls.is_connectable(app) or cls.consumes_services(app)+ or cls.has_attachments(app))diff --git a/backend/templates/loki.yaml b/backend/templates/loki.yamlindex 100f20e5..d7728304 100644--- a/backend/templates/loki.yaml+++ b/backend/templates/loki.yaml@@ -17,6 +17,12 @@ variables:default: "3100"hidden: true+# What an app (Grafana) gets through `fromService`, over the shared network.+connection:+ port: 3100+ properties:+ url: "http://{host}:{port}"+ports:- port: 3100protocol: tcpdiff --git a/backend/tests/test_app_attachments.py b/backend/tests/test_app_attachments.pyindex a69a188c..64ec02c9 100644--- a/backend/tests/test_app_attachments.py+++ b/backend/tests/test_app_attachments.py@@ -255,3 +255,74 @@ def test_api_rejects_an_unknown_kind(installed, client, auth_headers):ok = client.post(f'/api/v1/apps/{web.id}/attachments/cache',json={'service_app_id': cache.id}, headers=auth_headers)assert ok.status_code == 201 and ok.get_json()['attachment']['env_keys'] == ['REDIS_URL']+++# ── Grafana data sources (plan 86 §A4) ────────────────────────────────────────++class FakeGrafana:+ def __init__(self):+ self.calls = []++ def upsert_datasource(self, kind, service_name, url):+ self.calls.append(('upsert', kind, service_name, url))+ return f'serverkit-{kind}-{service_name}'++ def import_dashboard(self, uid):+ self.calls.append(('dashboard', uid))++ def delete_datasource(self, uid):+ self.calls.append(('delete', uid))+++def test_grafana_gets_metrics_and_logs_kinds_and_others_do_not(installed):+ graf, web = installed('graf', 'grafana'), installed('web')+ assert AppAttachmentService.kinds_for(graf) == ['cache', 'storage', 'queue', 'metrics', 'logs']+ assert AppAttachmentService.kinds_for(web) == ['cache', 'storage', 'queue']+++def test_metrics_attach_creates_a_data_source_and_the_dashboard(installed):+ graf, prom = installed('graf', 'grafana'), installed('prom', 'prometheus')+ fake = FakeGrafana()+ row, created = AppAttachmentService.attach_grafana_source(graf, prom, 'metrics', grafana=fake)+ assert created+ assert fake.calls == [('upsert', 'metrics', 'prom', 'http://prom:9090'),+ ('dashboard', 'serverkit-metrics-prom')]+ assert AppAttachmentService.detach(row, admin=fake) is None+ assert fake.calls[-1] == ('delete', 'serverkit-metrics-prom')+++def test_logs_attach_adds_loki_without_a_dashboard(installed):+ graf, loki = installed('graf', 'grafana'), installed('logs1', 'loki')+ fake = FakeGrafana()+ AppAttachmentService.attach_grafana_source(graf, loki, 'logs', grafana=fake)+ assert fake.calls == [('upsert', 'logs', 'logs1', 'http://logs1:3100')]++[email protected]('consumer,service,kind,match', [+ ('web', 'prometheus', 'metrics', 'Only a Grafana'),+ ('grafana', 'loki', 'metrics', 'cannot be used as metrics'),+ ('grafana', 'redis', 'logs', 'cannot be used as logs'),+])+def test_grafana_kinds_refuse_the_wrong_pair(installed, consumer, service, kind, match):+ target = installed('target', consumer if consumer != 'web' else None)+ svc_app = installed('svc', service)+ with pytest.raises(AttachmentError, match=match):+ AppAttachmentService.attach_grafana_source(target, svc_app, kind, grafana=FakeGrafana())+++def test_an_attached_app_joins_the_shared_network(installed):+ from app.services.service_connection_service import ServiceConnectionService+ graf, prom = installed('graf', 'grafana'), installed('prom', 'prometheus')+ assert not ServiceConnectionService.needs_shared_network(graf)+ AppAttachmentService.attach_grafana_source(graf, prom, 'metrics', grafana=FakeGrafana())+ assert ServiceConnectionService.needs_shared_network(graf)+++def test_the_bundled_dashboard_charts_the_metrics_the_panel_exports():+ import re+ from app.services.grafana_provisioner import DASHBOARD_PATH+ exported = {'serverkit_cpu_percent', 'serverkit_memory_percent', 'serverkit_disk_percent',+ 'serverkit_containers_running', 'serverkit_server_up'}+ with open(DASHBOARD_PATH, encoding='utf-8') as fh:+ used = set(re.findall(r'serverkit_[a-z_]+', fh.read()))+ assert used and used <= exported, used - exporteddiff --git a/backend/tests/test_real_grafana_attach_docker.py b/backend/tests/test_real_grafana_attach_docker.pynew file mode 100644index 00000000..8e192e48--- /dev/null+++ b/backend/tests/test_real_grafana_attach_docker.py@@ -0,0 +1,148 @@+"""Attach Prometheus to a REAL Grafana (plan 86 §A4).++Both bundled templates are rendered with the panel's rules and started with+their managed overrides. Attaching creates the data source and the bundled+dashboard through Grafana's API; after the redeploy that puts Grafana on the+shared network, Grafana's own health check on that data source passes. A+detach removes the data source again.++ SERVERKIT_DOCKER_BUILDS=1 pytest tests -m docker_builds+"""+import base64+import json+import os+import shutil+import socket+import subprocess+import time+import urllib.error+import urllib.request+import uuid++import pytest+import yaml++from app import db+from app.services.app_attachment_service import AppAttachmentService+from app.services.compose_env_service import ComposeEnvService+from app.services.template_service import TemplateService+from tests.factories import make_application+++def _docker_ready() -> bool:+ if shutil.which('docker') is None:+ return False+ try:+ return subprocess.run(['docker', 'info'], capture_output=True,+ timeout=20).returncode == 0+ except (OSError, subprocess.TimeoutExpired):+ return False+++pytestmark = [+ pytest.mark.docker_builds,+ pytest.mark.skipif(os.environ.get('SERVERKIT_DOCKER_BUILDS') != '1',+ reason='docker-builds leg; opt in with SERVERKIT_DOCKER_BUILDS=1'),+ pytest.mark.skipif(not _docker_ready(), reason='docker daemon not reachable'),+]+++def _free_port():+ with socket.socket() as s:+ s.bind(('127.0.0.1', 0))+ return s.getsockname()[1]+++def _install(tmp_path, installed, template_id, name, variables, drop_ports=False):+ template = TemplateService.get_template(template_id)['template']+ variables = dict(variables, APP_NAME=name)+ for var in template.get('variables', []):+ variables.setdefault(var['name'], TemplateService.generate_value(var))+ variables.update(TemplateService.collect_magic_variables(template))+ root = tmp_path / name+ root.mkdir()+ rendered = TemplateService._render_compose_and_files(template, variables, str(root))+ compose = yaml.safe_load(rendered['compose_content'])+ if drop_ports:+ for svc in compose['services'].values():+ svc.pop('ports', None)+ (root / 'docker-compose.yml').write_text(yaml.safe_dump(compose))+ for f in rendered['files']:+ with open(f['path'], 'w', newline='\n') as fh:+ fh.write(f['content'])+ (root / '.serverkit-template.json').write_text(json.dumps(+ {'template_id': template_id, 'variables': variables}))+ row = make_application(db, name=name, root_path=str(root))+ installed[str(row.id)] = {'template_id': template_id}+ return row, variables+++def _up(row):+ ComposeEnvService.refresh_for_project(row.root_path)+ files = ['-f', os.path.join(row.root_path, 'docker-compose.yml')]+ override = os.path.join(row.root_path, ComposeEnvService.OVERRIDE_NAME)+ if os.path.exists(override):+ files += ['-f', override]+ subprocess.run(['docker', 'compose', '-p', row.name, *files, 'up', '-d'],+ check=True, capture_output=True, timeout=300)+ return files+++def _grafana(port, user, password, path):+ token = base64.b64encode(f'{user}:{password}'.encode()).decode()+ req = urllib.request.Request(f'http://127.0.0.1:{port}{path}',+ headers={'Authorization': f'Basic {token}'})+ with urllib.request.urlopen(req, timeout=10) as resp:+ return json.load(resp)+++def test_grafana_gets_a_working_prometheus_data_source(app, tmp_path, monkeypatch):+ suffix = uuid.uuid4().hex[:8]+ installed = {}+ monkeypatch.setattr(TemplateService, 'get_config', classmethod(+ lambda cls: {'repos': [], 'installed': installed}))+ port = _free_port()+ prom, _ = _install(tmp_path, installed, 'prometheus', f'skc-prom-{suffix}',+ {'HTTP_PORT': '0', 'PANEL_TARGET': 'localhost:9090'}, drop_ports=True)+ graf, gvars = _install(tmp_path, installed, 'grafana', f'skc-graf-{suffix}',+ {'HTTP_PORT': str(port), 'ADMIN_USER': 'admin',+ 'ADMIN_PASSWORD': 'graf-pw-123'})+ projects = []+ try:+ projects.append((prom, _up(prom)))+ projects.append((graf, _up(graf)))+ deadline = time.time() + 120+ while True:+ try:+ _grafana(port, 'admin', 'graf-pw-123', '/api/health')+ break+ except (OSError, urllib.error.URLError):+ assert time.time() < deadline, 'grafana never came up'+ time.sleep(2)++ row, created = AppAttachmentService.attach(graf, prom, 'metrics')+ assert created+ uid = row.details['datasource_uid']+ dash = _grafana(port, 'admin', 'graf-pw-123', '/api/dashboards/uid/serverkit-overview')+ assert dash['dashboard']['title'] == 'ServerKit servers'++ # The attachment puts Grafana on the shared network at its redeploy.+ projects[-1] = (graf, _up(graf))+ deadline = time.time() + 120+ while True:+ try:+ health = _grafana(port, 'admin', 'graf-pw-123', f'/api/datasources/uid/{uid}/health')+ if health.get('status') == 'OK':+ break+ except (OSError, urllib.error.URLError):+ health = None+ assert time.time() < deadline, f'data source never healthy: {health}'+ time.sleep(2)++ assert AppAttachmentService.detach(row) is None+ with pytest.raises(urllib.error.HTTPError):+ _grafana(port, 'admin', 'graf-pw-123', f'/api/datasources/uid/{uid}')+ finally:+ for app_row, files in reversed(projects):+ subprocess.run(['docker', 'compose', '-p', app_row.name, *files, 'down', '-v'],+ capture_output=True, timeout=180)diff --git a/frontend/src/components/service-detail/AttachmentsCard.jsx b/frontend/src/components/service-detail/AttachmentsCard.jsxindex 8cb792e7..77be9bf9 100644--- a/frontend/src/components/service-detail/AttachmentsCard.jsx+++ b/frontend/src/components/service-detail/AttachmentsCard.jsx@@ -18,8 +18,11 @@ const DEFAULT_TEMPLATES = {cache: { id: 'redis', name: 'Redis' },storage: { id: 'garage', name: 'Garage' },queue: { id: 'rabbitmq', name: 'RabbitMQ' },+ metrics: { id: 'prometheus', name: 'Prometheus' },+ logs: { id: 'loki', name: 'Loki' },};-const KINDS = ['cache', 'storage', 'queue'];+// Until the server says otherwise (a Grafana install also takes metrics/logs).+const DEFAULT_KINDS = ['cache', 'storage', 'queue'];// "Attach → Cache / Storage / Queue" (plan 86 §C4): reuse an installed// service or install one, write the env references, then offer a redeploy.@@ -29,6 +32,7 @@ export default function AttachmentsCard({ app }) {const { confirm } = useConfirm();const { isAdmin } = useAuth();const [attachments, setAttachments] = useState(null);+ const [kinds, setKinds] = useState(DEFAULT_KINDS);const [kind, setKind] = useState(null);const [choices, setChoices] = useState(null);const [selected, setSelected] = useState('');@@ -41,17 +45,24 @@ export default function AttachmentsCard({ app }) {cache: t('app.attachments.failureCache', 'Cached data can be stale; invalidate on write, and never keep the only copy in a cache.'),storage: t('app.attachments.failureStorage', 'Objects outlive the app: detaching keeps the bucket and its data.'),queue: t('app.attachments.failureQueue', 'A job can be delivered twice; make jobs safe to run again.'),+ metrics: t('app.attachments.failureMetrics', 'Grafana shows what Prometheus kept: past its retention, history is gone.'),+ logs: t('app.attachments.failureLogs', 'Grafana shows what Loki kept: past its retention, logs are gone.'),}[k]);const kindLabel = (k) => ({cache: t('app.attachments.kindCache', 'Cache'),storage: t('app.attachments.kindStorage', 'Storage'),queue: t('app.attachments.kindQueue', 'Queue'),+ metrics: t('app.attachments.kindMetrics', 'Metrics'),+ logs: t('app.attachments.kindLogs', 'Logs'),}[k]);const load = useCallback(() => {api.getAppAttachments(app.id)- .then((data) => setAttachments(data.attachments || []))+ .then((data) => {+ setAttachments(data.attachments || []);+ if (data.kinds?.length) setKinds(data.kinds);+ }).catch(() => setAttachments([]));}, [app.id]);@@ -136,7 +147,7 @@ export default function AttachmentsCard({ app }) {}const attachedKinds = new Set((attachments || []).map((a) => a.kind));- const open = KINDS.filter((k) => !attachedKinds.has(k));+ const open = kinds.filter((k) => !attachedKinds.has(k));return (<div className="overview-tab__card overview-tab__card--full attachments">@@ -178,7 +189,10 @@ export default function AttachmentsCard({ app }) {<span className="attachments__kind">{kindLabel(row.kind)}</span><span className="attachments__service">{row.service_name}</span><code className="attachments__env">- {row.kind === 'storage' ? `S3_BUCKET=${row.bucket}` : row.env_keys.join(', ')}+ {row.kind === 'storage' && `S3_BUCKET=${row.bucket}`}+ {(row.kind === 'metrics' || row.kind === 'logs')+ && t('app.attachments.grafanaDataSource', 'Grafana data source')}+ {row.env_keys.length > 0 && row.kind !== 'storage' && row.env_keys.join(', ')}</code><Button variant="ghost" size="sm" onClick={() => detach(row)}aria-label={t('app.attachments.detach', 'Detach')}>diff --git a/frontend/src/i18n/locales/en.json b/frontend/src/i18n/locales/en.jsonindex 8d493c87..ffa5a6cd 100644--- a/frontend/src/i18n/locales/en.json+++ b/frontend/src/i18n/locales/en.json@@ -516,13 +516,18 @@"detachTitle": "Detach {{name}}?","empty": "Nothing attached. Attach a cache, object storage or a queue and the app gets its connection settings as environment variables.","failureCache": "Cached data can be stale; invalidate on write, and never keep the only copy in a cache.",+ "failureLogs": "Grafana shows what Loki kept: past its retention, logs are gone.",+ "failureMetrics": "Grafana shows what Prometheus kept: past its retention, history is gone.","failureQueue": "A job can be delivered twice; make jobs safe to run again.","failureStorage": "Objects outlive the app: detaching keeps the bucket and its data.",+ "grafanaDataSource": "Grafana data source","installAndAttach": "Install {{template}} and attach","installFailed": "The install failed.","installNoId": "Installed, but the new service could not be found. Attach it from the list.","installing": "Installing…","kindCache": "Cache",+ "kindLogs": "Logs",+ "kindMetrics": "Metrics","kindQueue": "Queue","kindStorage": "Storage","noneInstalled": "No {{kind}} service is installed yet. ServerKit can install one now.",diff --git a/frontend/src/i18n/locales/es.json b/frontend/src/i18n/locales/es.jsonindex cb33065a..82df9b59 100644--- a/frontend/src/i18n/locales/es.json+++ b/frontend/src/i18n/locales/es.json@@ -6732,7 +6732,12 @@"redeployNow": "Volver a desplegar ahora","redeploying": "Despliegue iniciado.","service": "Servicio",- "title": "Servicios conectados"+ "title": "Servicios conectados",+ "failureLogs": "Grafana muestra lo que Loki conservó: pasado su periodo de retención, los registros desaparecen.",+ "failureMetrics": "Grafana muestra lo que Prometheus conservó: pasado su periodo de retención, el historial desaparece.",+ "grafanaDataSource": "Fuente de datos de Grafana",+ "kindLogs": "Registros",+ "kindMetrics": "Métricas"},"dbInsights": {"cacheHitMysql": "Tasa de aciertos del buffer pool de InnoDB",