feat: Instrument durable API-to-worker telemetry (U2)

This commit is contained in:
Самохвалов Андрей Ремович
2026-10-02 15:09:55 +03:00
parent 26f091c9b4
commit 8eab283f6f
14 changed files with 355 additions and 36 deletions
+18 -7
View File
@@ -13,6 +13,8 @@ from uuid import UUID, uuid4
from fastapi import HTTPException, Request
from minio import Minio
from minio.error import S3Error
from opentelemetry import trace
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
from PIL import Image, UnidentifiedImageError
from urllib3 import PoolManager, Retry, Timeout
@@ -25,6 +27,8 @@ MAX_BYTES = 10 * 1024 * 1024
MAX_PIXELS = 40_000_000
Image.MAX_IMAGE_PIXELS = MAX_PIXELS
logger = logging.getLogger(__name__)
tracer = trace.get_tracer(__name__)
trace_context = TraceContextTextMapPropagator()
def secret(name: str) -> str:
@@ -120,17 +124,24 @@ def persist_attachment(chat_id: str, actor_id: str, filename: str, content_type:
try:
stream.seek(0)
client = storage()
client.put_object(BUCKET, staging_key, stream, size, content_type=content_type)
with tracer.start_as_current_span("s3.stage_upload", record_exception=False, set_status_on_exception=False):
client.put_object(BUCKET, staging_key, stream, size, content_type=content_type)
with connection() as conn:
if not renew_upload(conn, attachment_id):
raise HTTPException(503)
verify_staging(client, staging_key, size, sha256, content_type)
with tracer.start_as_current_span("s3.verify_staging", record_exception=False, set_status_on_exception=False):
verify_staging(client, staging_key, size, sha256, content_type)
carrier = {}
trace_context.inject(carrier)
with connection() as conn:
row, _job_id = queue_attachment(
conn, attachment_id=attachment_id, chat_id=UUID(chat_id), owner_id=UUID(actor_id),
filename=filename, staging_key=staging_key, object_key=object_key,
content_type=content_type, size_bytes=size, sha256=sha256,
)
with tracer.start_as_current_span("db.queue_attachment", record_exception=False,
set_status_on_exception=False):
row, _job_id = queue_attachment(
conn, attachment_id=attachment_id, chat_id=UUID(chat_id), owner_id=UUID(actor_id),
filename=filename, staging_key=staging_key, object_key=object_key,
content_type=content_type, size_bytes=size, sha256=sha256,
traceparent=carrier.get("traceparent"), tracestate=carrier.get("tracestate"),
)
except HTTPException:
raise
except Exception as exc: