Lab 6: track - 1

This commit is contained in:
2026-11-23 14:54:48 +03:00
parent 470d9d6f1e
commit a271ddd156
39 changed files with 3141 additions and 15 deletions
+54
View File
@@ -0,0 +1,54 @@
"""Grafana proxy helper for student observability verification (reads local secret, never prints it)."""
import base64
import json
from pathlib import Path
import time
import urllib.parse
import urllib.request
ROOT = Path(__file__).resolve().parents[1]
class GrafanaClient:
def __init__(self, base_url='http://localhost:3000'):
self.base = base_url.rstrip('/')
pw = (ROOT / 'secrets/grafana_admin_password').read_text(encoding='utf-8').strip()
auth = base64.b64encode(('admin:' + pw).encode('utf-8')).decode('ascii')
self.headers = {'Authorization': 'Basic ' + auth, 'Accept': 'application/json'}
def get_json(self, path, timeout=10):
req = urllib.request.Request(self.base + path, headers=self.headers, method='GET')
with urllib.request.urlopen(req, timeout=timeout) as resp:
return json.loads(resp.read().decode('utf-8'))
def wait_ready(self, timeout=90):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
try:
health = self.get_json('/api/health', timeout=3)
req = urllib.request.Request(self.base + '/api/datasources/proxy/uid/tempo/ready', headers=self.headers)
with urllib.request.urlopen(req, timeout=3) as r:
tempo_ok = (r.code == 200)
if health.get('database') == 'ok' and tempo_ok:
return
except Exception:
pass
time.sleep(1.5)
raise RuntimeError('Grafana or Tempo datasource did not become ready within timeout')
def prom_query(self, expr):
q = urllib.parse.urlencode({'query': expr})
return self.get_json(f'/api/datasources/proxy/uid/prometheus/api/v1/query?{q}')
def prom_rules(self):
return self.get_json('/api/datasources/proxy/uid/prometheus/api/v1/rules')
def loki_query_range(self, query, start_ns, end_ns, limit=100):
q = urllib.parse.urlencode({'query': query, 'start': str(start_ns), 'end': str(end_ns), 'limit': str(limit)})
return self.get_json(f'/api/datasources/proxy/uid/loki/loki/api/v1/query_range?{q}')
def tempo_search(self, q, start_s, end_s, limit=20):
params = urllib.parse.urlencode({'q': q, 'start': str(int(start_s)), 'end': str(int(end_s)), 'limit': str(limit)})
return self.get_json(f'/api/datasources/proxy/uid/tempo/api/search?{params}')
def tempo_trace(self, trace_id):
return self.get_json(f'/api/datasources/proxy/uid/tempo/api/traces/{trace_id}')