Implement bounded OTLP observability across API and asynchronous image pipeline

This commit is contained in:
2026-11-15 22:30:03 +03:00
parent 470d9d6f1e
commit 2701db5106
60 changed files with 6118 additions and 49 deletions
+50
View File
@@ -0,0 +1,50 @@
#!/usr/bin/env python3
"""Create a private local secret directory once; never rotate existing volumes implicitly."""
import os
import json
import secrets
from pathlib import Path
root = Path(__file__).resolve().parents[1]
private = root / "secrets"
private.mkdir(mode=0o700, exist_ok=True)
os.chmod(private, 0o700)
password_file = private / "postgres_password"
if not password_file.exists():
password_file.write_text(secrets.token_urlsafe(32))
password = password_file.read_text().strip()
broker_password_file = private / "broker_password"
if not broker_password_file.exists():
broker_password_file.write_text(secrets.token_urlsafe(32))
broker_password = broker_password_file.read_text().strip()
values = {
"database_url": "postgresql://messenger:" + password + "@postgres:5432/messenger",
"cache_url": "redis://cache:6379/0",
"broker_url": "redis://:" + broker_password + "@broker:6379/0",
"broker_config": "bind 0.0.0.0\nprotected-mode yes\nrequirepass " + broker_password + "\nappendonly yes\nappendfsync always\nsave \"\"\ndir /data\nmaxmemory 128mb\nmaxmemory-policy noeviction\n",
"s3_access_key": "app-" + secrets.token_hex(10),
"s3_secret_key": secrets.token_urlsafe(32),
"grafana_password": secrets.token_urlsafe(32),
"s3_admin_access_key": "admin-" + secrets.token_hex(10),
"s3_admin_secret_key": secrets.token_urlsafe(32),
}
for name, value in values.items():
path = private / name
if not path.exists():
path.write_text(value)
config = private / "s3_config"
if not config.exists():
config.write_text(json.dumps({"identities": [
{"name": "bootstrap", "credentials": [{"accessKey": (private / "s3_admin_access_key").read_text(),
"secretKey": (private / "s3_admin_secret_key").read_text()}], "actions": ["Admin", "Read", "Write", "List"]},
{"name": "api", "credentials": [{"accessKey": (private / "s3_access_key").read_text(),
"secretKey": (private / "s3_secret_key").read_text()}], "actions": ["Read:messenger", "Write:messenger", "List:messenger"]}
]}))
# The parent directory is private on the host; files must be readable by non-root API
# through Compose's file-backed secret mount (uid/mode mapping is not portable).
for path in private.iterdir():
os.chmod(path, 0o644)
if not (root / ".env").exists():
(root / ".env").write_text("PUBLIC_PORT=8086\nSESSION_TTL_SECONDS=3600\n")
print("Local configuration ready; existing secret values preserved.")
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose restart postgres cache broker storage s3 api lb dispatcher worker
docker compose up -d --wait --no-recreate
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
python3 scripts/stop_one.py
+22
View File
@@ -0,0 +1,22 @@
"""Map a process alias to its real container, then leave exactly it stopped."""
import json
import os
import subprocess
from pathlib import Path
wanted = os.environ["TARGET_INSTANCE_ID"]
ids = subprocess.check_output(["docker", "compose", "ps", "-q", "api"], text=True).split()
for cid in ids:
result = subprocess.run(
["docker", "exec", cid, "python", "-c",
"import urllib.request; r=urllib.request.urlopen('http://127.0.0.1:8080/health/live'); print(r.headers['X-Instance-Id'])"],
text=True, capture_output=True, check=False,
)
if result.returncode == 0 and result.stdout.strip() == wanted:
subprocess.run(["docker", "stop", "--time", "8", cid], check=True, stdout=subprocess.DEVNULL)
receipt = {"stopped_container": cid, "instance_id": wanted}
Path("evidence/last-stopped.json").write_text(json.dumps(receipt))
print(json.dumps(receipt))
break
else:
raise SystemExit("Target instance not found among this project's API containers")