Files
mtusi2026lab6/app/storage.py
T
Artu-RitoandClaude Sonnet 5 6c50a63743 Лаба 6: OpenTelemetry + Grafana наблюдаемость (оценка 4)
Переиспользован стек lab5. API/worker шлют OTLP (traces/metrics/logs) в
otel-collector -> Prometheus/Loki/Tempo; Grafana с provisioning датасорсов,
дашборда и alert rules в Git. W3C trace context переносится через поле
traceparent в задании очереди (API producer span -> worker consumer span
в одном trace, подтверждено прямым запросом к Tempo). Три контролируемых
инцидента прогнаны и задокументированы (worker down -> queue lag + alert,
DB down отличим от медленного handler по error-span, telemetry backend down
не роняет API/Collector). В процессе найдены и исправлены 4 реальных бага
(middleware после старта приложения, detached span context, замерзающая
метрика очереди при падении её единственного источника, неатомарный
XACK+XDEL) — см. REPORT.md и evidence/.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 01:59:33 +03:00

64 lines
2.0 KiB
Python

"""Private S3-compatible object storage for attachment bytes (MinIO behind the scenes).
boto3 is synchronous; calls are pushed to a thread so the event loop is not
blocked by S3 I/O. Object keys are server-generated UUIDs, never the
user-supplied filename (filename is display-only, see contracts/README.md).
"""
import asyncio
import io
import uuid
import boto3
from botocore.client import Config
from botocore.exceptions import ClientError
_client = None
_bucket: str | None = None
def init(endpoint: str, region: str, access_key: str, secret_key: str, bucket: str) -> None:
global _client, _bucket
_bucket = bucket
_client = boto3.client(
"s3",
endpoint_url=endpoint,
region_name=region,
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
config=Config(signature_version="s3v4", retries={"max_attempts": 2}),
)
async def ensure_bucket() -> None:
"""The API identity only has Read/Write/List on this one bucket, not
bucket-admin rights, so CreateBucket is not an option. SeaweedFS (like S3)
auto-creates the bucket on the first successful PutObject, so writing an
empty marker object both creates the bucket and proves write access."""
await asyncio.to_thread(_client.put_object, Bucket=_bucket, Key=".bootstrap", Body=b"")
async def ping() -> bool:
try:
await asyncio.to_thread(_client.head_object, Bucket=_bucket, Key=".bootstrap")
return True
except Exception: # noqa: BLE001 - readiness probe
return False
def new_object_key() -> str:
return f"attachments/{uuid.uuid4().hex}"
async def put_object(key: str, data: bytes, content_type: str) -> None:
await asyncio.to_thread(
_client.put_object, Bucket=_bucket, Key=key, Body=data, ContentType=content_type
)
async def get_object(key: str) -> bytes:
def _get():
resp = _client.get_object(Bucket=_bucket, Key=key)
return resp["Body"].read()
return await asyncio.to_thread(_get)