forked from mtusi2026/mtusi2026lab6
Подключить OpenTelemetry, Collector и Grafana для оценки 5.
API и worker отдают метрики, логи и трассы. Правила SLO записаны, прогон инцидентов ещё впереди.
This commit is contained in:
+27
-13
@@ -8,7 +8,7 @@ import logging
|
||||
|
||||
import aio_pika
|
||||
|
||||
from app import config
|
||||
from app import config, telemetry
|
||||
|
||||
logger = logging.getLogger("messenger.broker")
|
||||
|
||||
@@ -49,19 +49,33 @@ async def _ensure() -> tuple[aio_pika.RobustChannel, aio_pika.Exchange]:
|
||||
return _channel, _exchange
|
||||
|
||||
|
||||
async def publish_job(payload: dict) -> None:
|
||||
async def publish_job(payload: dict, traceparent: str | None = None, tracestate: str | None = None) -> None:
|
||||
channel, exchange = await _ensure()
|
||||
body = json.dumps(payload, separators=(",", ":")).encode("utf-8")
|
||||
await exchange.publish(
|
||||
aio_pika.Message(
|
||||
body=body,
|
||||
content_type="application/json",
|
||||
delivery_mode=aio_pika.DeliveryMode.PERSISTENT,
|
||||
message_id=payload["job_id"],
|
||||
),
|
||||
routing_key=ROUTING_KEY,
|
||||
mandatory=True,
|
||||
)
|
||||
parent = telemetry.remote_context(traceparent, tracestate)
|
||||
with telemetry.span(
|
||||
"attachment.job.publish",
|
||||
context=parent,
|
||||
kind="producer",
|
||||
attributes={
|
||||
"messaging.system": "rabbitmq",
|
||||
"messaging.destination.name": QUEUE,
|
||||
"messaging.operation": "publish",
|
||||
"job.id": payload.get("job_id"),
|
||||
},
|
||||
):
|
||||
headers = telemetry.current_carrier() or None
|
||||
body = json.dumps(payload, separators=(",", ":")).encode("utf-8")
|
||||
await exchange.publish(
|
||||
aio_pika.Message(
|
||||
body=body,
|
||||
content_type="application/json",
|
||||
delivery_mode=aio_pika.DeliveryMode.PERSISTENT,
|
||||
message_id=payload["job_id"],
|
||||
headers=headers,
|
||||
),
|
||||
routing_key=ROUTING_KEY,
|
||||
mandatory=True,
|
||||
)
|
||||
|
||||
|
||||
async def publish_delay(payload: dict, delay_seconds: float) -> None:
|
||||
|
||||
Reference in New Issue
Block a user