forked from mtusi2026/mtusi2026lab6
ЛР6: OTel SDK, Collector, Prometheus/Loki/Tempo, Grafana, alerts, dashboard, тесты инцидентов
This commit is contained in:
+104
@@ -0,0 +1,104 @@
|
||||
"""RabbitMQ: publish с publisher confirm, consume с ручным ack.
|
||||
|
||||
Topology:
|
||||
exchange: lab5 (direct, durable)
|
||||
routing_key: attachment.process.v1 → queue attachment.process.v1 (durable)
|
||||
DLX: lab5.dlx (direct)
|
||||
routing_key: attachment.failed → queue attachment.failed (durable)
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
|
||||
import pika
|
||||
|
||||
from . import config
|
||||
|
||||
log = logging.getLogger("broker")
|
||||
|
||||
EXCHANGE = "lab5"
|
||||
DLX = "lab5.dlx"
|
||||
QUEUE = "attachment.process.v1"
|
||||
FAILED_QUEUE = "attachment.failed"
|
||||
|
||||
_lock = threading.Lock()
|
||||
_channel = None
|
||||
|
||||
|
||||
def _connect():
|
||||
params = pika.URLParameters(config.BROKER_URL)
|
||||
params.heartbeat = 30
|
||||
params.blocked_connection_timeout = 30
|
||||
return pika.BlockingConnection(params)
|
||||
|
||||
|
||||
def get_channel():
|
||||
"""Один канал на процесс; worker также использует для consume."""
|
||||
global _channel
|
||||
if _channel is None or _channel.is_closed:
|
||||
with _lock:
|
||||
if _channel is None or _channel.is_closed:
|
||||
conn = _connect()
|
||||
ch = conn.channel()
|
||||
ch.exchange_declare(EXCHANGE, exchange_type="direct", durable=True)
|
||||
ch.exchange_declare(DLX, exchange_type="direct", durable=True)
|
||||
ch.queue_declare(
|
||||
QUEUE, durable=True,
|
||||
arguments={"x-dead-letter-exchange": DLX,
|
||||
"x-dead-letter-routing-key": "attachment.failed"})
|
||||
ch.queue_declare(FAILED_QUEUE, durable=True)
|
||||
ch.queue_bind(QUEUE, EXCHANGE, routing_key=QUEUE)
|
||||
ch.queue_bind(FAILED_QUEUE, DLX, routing_key="attachment.failed")
|
||||
_channel = ch
|
||||
return _channel
|
||||
|
||||
|
||||
def publish(job_payload: dict) -> None:
|
||||
"""Publish с publisher confirm + W3C trace context в headers.
|
||||
|
||||
Заголовки `traceparent`/`tracestate` создаёт OTel propagator из текущего span.
|
||||
Worker их извлечёт и создаст child span — сквозная трасса API → broker → worker.
|
||||
"""
|
||||
from . import telemetry
|
||||
ch = get_channel()
|
||||
ch.confirm_delivery()
|
||||
body = json.dumps(job_payload, separators=(",", ":")).encode("utf-8")
|
||||
headers = {}
|
||||
telemetry.inject_headers(headers)
|
||||
ch.basic_publish(
|
||||
exchange=EXCHANGE,
|
||||
routing_key=QUEUE,
|
||||
body=body,
|
||||
properties=pika.BasicProperties(
|
||||
content_type="application/json",
|
||||
delivery_mode=2,
|
||||
message_id=job_payload["job_id"],
|
||||
headers=headers or None,
|
||||
),
|
||||
mandatory=True,
|
||||
)
|
||||
|
||||
|
||||
def declare_only() -> None:
|
||||
"""Идемпотентное создание topology (при старте API и worker)."""
|
||||
get_channel()
|
||||
|
||||
|
||||
def wait_ready(timeout=30) -> bool:
|
||||
"""Проверка брокера для /health/ready."""
|
||||
deadline = time.monotonic() + timeout
|
||||
last = None
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
conn = _connect()
|
||||
try:
|
||||
conn.channel().queue_declare(QUEUE, passive=True)
|
||||
return True
|
||||
finally:
|
||||
conn.close()
|
||||
except Exception as exc:
|
||||
last = exc
|
||||
time.sleep(0.5)
|
||||
log.warning("broker not ready: %s", type(last).__name__ if last else "timeout")
|
||||
return False
|
||||
Reference in New Issue
Block a user