База lab7: контракт 1.7.0 и реализация lab1–lab6

Перенесены cmd/internal/config, compose.yaml, Dockerfile, скрипты, student-tests и evidence из mtusi-messenger (master, lab6). Skeleton lab7 (openapi/infrastructure 1.7.0, trust-matrix.md, security/THREAT_MODEL.md, infra/openbao, tests/security.py) сохранён; make test-security добавлен в Makefile.
This commit is contained in:
Vladislav Kunetskiy
2026-11-09 13:03:49 +03:00
parent 20afc2c790
commit 3d216fbd50
479 changed files with 84512 additions and 50 deletions
+96
View File
@@ -0,0 +1,96 @@
#!/usr/bin/env python3
"""Isolated collector outage under load with labeled probes and alert timeline."""
import argparse
import json
import os
import subprocess
import time
import urllib.parse
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
for key in ('HTTP_PROXY','HTTPS_PROXY','ALL_PROXY','http_proxy','https_proxy','all_proxy'):
os.environ.pop(key, None)
OPENER = urllib.request.build_opener(urllib.request.ProxyHandler({}))
def query(expr):
url = 'http://localhost:9090/api/v1/query?' + urllib.parse.urlencode({'query': expr})
with OPENER.open(url, timeout=10) as response:
result = json.loads(response.read()).get('data', {}).get('result', [])
return result[0]['value'][1] if result else 'MISSING'
def alerts():
with OPENER.open('http://localhost:9090/api/v1/alerts', timeout=10) as response:
data = json.loads(response.read()).get('data', {}).get('alerts', [])
return [(a['labels'].get('alertname', ''), a['state']) for a in data]
def compose(*args, check=True):
return subprocess.run(['docker','compose','--env-file','.env',*args], cwd=ROOT,
capture_output=True, text=True, check=check)
def sample(label):
print(label,
'up=' + query('up{job="otel-collector"}'),
'api_p99=' + query('histogram_quantile(0.99, sum by (le) (rate(http_server_request_duration_seconds_bucket[5m])))'),
'api_error_5m=' + query('slo:api_error_ratio:5m'),
'queue_logs=' + query('max(otelcol_exporter_queue_size{data_type="logs"})'),
'queue_traces=' + query('max(otelcol_exporter_queue_size{data_type="traces"})'),
'alerts=' + json.dumps(alerts()), flush=True)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--duration', type=int, default=300)
args = parser.parse_args()
print('UTC', time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()), flush=True)
print('HEAD', subprocess.run(['git','rev-parse','HEAD'], cwd=ROOT, capture_output=True, text=True, check=True).stdout.strip(), flush=True)
print('pre_alerts=', json.dumps(alerts()), flush=True)
load_path = ROOT/'evidence/lab6/checks/collector-down-load.json'
load_path.unlink(missing_ok=True)
load_duration = args.duration + 300 + 60
load = subprocess.Popen([
'python3', str(ROOT/'scripts/lab6_load.py'), '--base-url', 'http://localhost:8080',
'--rate', '0.2', '--duration', str(load_duration), '--upload-ratio', '0.2',
'--deadline', '60', '--output', str(load_path),
], cwd=ROOT, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
time.sleep(10)
print('$ docker compose ps -a', flush=True)
print(compose('ps','-a').stdout, flush=True)
print('$ docker compose stop otel-collector', flush=True)
print(compose('stop','otel-collector').stdout.strip(), flush=True)
started = time.monotonic()
while time.monotonic() - started < args.duration:
time.sleep(30)
sample('down t=%ss' % int(time.monotonic() - started))
print('$ docker compose ps -a (down)', flush=True)
print(compose('ps','-a').stdout, flush=True)
print('$ docker compose start otel-collector', flush=True)
print(compose('start','otel-collector').stdout.strip(), flush=True)
started = time.monotonic()
while time.monotonic() - started < 300:
time.sleep(30)
sample('recovery t=%ss' % int(time.monotonic() - started))
current = alerts()
if not any(name in ('TelemetryCollectorDown', 'CollectorDroppedTelemetry') for name, _ in current):
print('telemetry alerts inactive', flush=True)
break
try:
load.wait(timeout=load_duration + 120)
except subprocess.TimeoutExpired:
load.kill()
load.wait()
print('load_exit=', load.returncode, flush=True)
if load_path.exists():
data = json.loads(load_path.read_text())
print('load_summary=', json.dumps({k: data.get(k) for k in ('counters','failures','failures_by_reason','duration_s')}), flush=True)
print('$ docker compose ps -a (final)', flush=True)
print(compose('ps','-a').stdout, flush=True)
if __name__ == '__main__':
main()