forked from mtusi2026/mtusi2026lab6
feat: ЛР6 - наблюдаемость OpenTelemetry, Grafana и исследование цены наблюдаемости
This commit is contained in:
+26
-4
@@ -17,6 +17,7 @@ import botocore.exceptions
|
||||
from PIL import Image
|
||||
|
||||
from app.auth import generate_session_token, hash_password, verify_password
|
||||
from app.telemetry import inject_trace_context, set_db_pool_active
|
||||
from app.config import (
|
||||
CACHE_URL,
|
||||
DATABASE_URL,
|
||||
@@ -98,9 +99,24 @@ class PostgresStorage:
|
||||
async def acquire_conn(self, timeout: float = 2.0):
|
||||
if self.pg_pool is None:
|
||||
raise asyncpg.InterfaceError("Пул подключений к PostgreSQL не инициализирован")
|
||||
async with asyncio.timeout(3.0):
|
||||
async with self.pg_pool.acquire(timeout=timeout) as conn:
|
||||
yield conn
|
||||
try:
|
||||
holders = getattr(self.pg_pool, "_holders", [])
|
||||
free = getattr(self.pg_pool, "_free", [])
|
||||
set_db_pool_active(len(holders) - len(free) + 1)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
async with asyncio.timeout(3.0):
|
||||
async with self.pg_pool.acquire(timeout=timeout) as conn:
|
||||
yield conn
|
||||
finally:
|
||||
if self.pg_pool is not None:
|
||||
try:
|
||||
holders = getattr(self.pg_pool, "_holders", [])
|
||||
free = getattr(self.pg_pool, "_free", [])
|
||||
set_db_pool_active(len(holders) - len(free))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
async def connect(self) -> None:
|
||||
logger.info("Подключение к PostgreSQL: %s", DATABASE_URL.split("@")[-1])
|
||||
@@ -809,7 +825,9 @@ class PostgresStorage:
|
||||
|
||||
await asyncio.to_thread(_put_to_s3)
|
||||
|
||||
# 9. Атомарное сохранение в PostgreSQL: attachments + outbox_jobs
|
||||
carrier: dict[str, str] = {}
|
||||
inject_trace_context(carrier)
|
||||
|
||||
job_payload = {
|
||||
"schema_version": 1,
|
||||
"job_id": str(job_id),
|
||||
@@ -819,6 +837,10 @@ class PostgresStorage:
|
||||
"attempt": 1,
|
||||
"created_at": now_iso,
|
||||
}
|
||||
if "traceparent" in carrier:
|
||||
job_payload["traceparent"] = carrier["traceparent"]
|
||||
if "tracestate" in carrier:
|
||||
job_payload["tracestate"] = carrier["tracestate"]
|
||||
|
||||
try:
|
||||
async with self.acquire_conn() as conn:
|
||||
|
||||
Reference in New Issue
Block a user