forked from mtusi2026/mtusi2026lab6
feat(rabbitmq): lab6 propagate trace context from publisher to worker
This commit is contained in:
@@ -7,6 +7,11 @@ import (
|
||||
"time"
|
||||
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
type Publisher interface {
|
||||
@@ -15,11 +20,10 @@ type Publisher interface {
|
||||
}
|
||||
|
||||
type RabbitPublisher struct {
|
||||
url string
|
||||
mu sync.Mutex
|
||||
conn *amqp.Connection
|
||||
ch *amqp.Channel
|
||||
confirms chan amqp.Confirmation
|
||||
url string
|
||||
mu sync.Mutex
|
||||
conn *amqp.Connection
|
||||
ch *amqp.Channel
|
||||
}
|
||||
|
||||
func NewRabbitPublisher(url string) (*RabbitPublisher, error) {
|
||||
@@ -45,7 +49,6 @@ func (p *RabbitPublisher) connect() error {
|
||||
if err := p.ch.Confirm(false); err != nil {
|
||||
return fmt.Errorf("включение режима confirm: %w", err)
|
||||
}
|
||||
p.confirms = p.ch.NotifyPublish(make(chan amqp.Confirmation, 1))
|
||||
|
||||
err = p.ch.ExchangeDeclare("attachments", "direct", true, false, false, false, nil)
|
||||
if err != nil {
|
||||
@@ -65,7 +68,19 @@ func (p *RabbitPublisher) connect() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *RabbitPublisher) Publish(ctx context.Context, routingKey string, body []byte) error {
|
||||
func (p *RabbitPublisher) Publish(ctx context.Context, routingKey string, body []byte) (publishErr error) {
|
||||
ctx, span := telemetry.Tracer("broker").Start(ctx, "attachments publish",
|
||||
trace.WithSpanKind(trace.SpanKindProducer), trace.WithAttributes(
|
||||
attribute.String("messaging.system", "rabbitmq"),
|
||||
attribute.String("messaging.destination.name", "attachments"),
|
||||
))
|
||||
defer func() {
|
||||
if publishErr != nil {
|
||||
span.SetStatus(codes.Error, "публикация не подтверждена")
|
||||
}
|
||||
span.End()
|
||||
}()
|
||||
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
@@ -75,7 +90,12 @@ func (p *RabbitPublisher) Publish(ctx context.Context, routingKey string, body [
|
||||
}
|
||||
}
|
||||
|
||||
err := p.ch.PublishWithContext(ctx, "attachments", routingKey, false, false, amqp.Publishing{
|
||||
headers := amqp.Table{}
|
||||
for key, value := range telemetry.TraceHeaders(ctx) {
|
||||
headers[key] = value
|
||||
}
|
||||
confirmation, err := p.ch.PublishWithDeferredConfirmWithContext(ctx, "attachments", routingKey, false, false, amqp.Publishing{
|
||||
Headers: headers,
|
||||
ContentType: "application/json",
|
||||
DeliveryMode: amqp.Persistent,
|
||||
Body: body,
|
||||
@@ -84,17 +104,16 @@ func (p *RabbitPublisher) Publish(ctx context.Context, routingKey string, body [
|
||||
return err
|
||||
}
|
||||
|
||||
select {
|
||||
case confirm := <-p.confirms:
|
||||
if !confirm.Ack {
|
||||
return fmt.Errorf("сообщение отклонено брокером")
|
||||
}
|
||||
return nil
|
||||
case <-time.After(5 * time.Second):
|
||||
return fmt.Errorf("таймаут ожидания подтверждения от rabbitmq")
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
waitCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
acked, err := confirmation.WaitContext(waitCtx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ожидание подтверждения RabbitMQ: %w", err)
|
||||
}
|
||||
if !acked {
|
||||
return fmt.Errorf("сообщение отклонено брокером")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *RabbitPublisher) Close() error {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"high-loud-apps-labs/internal/broker"
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
type Publisher struct {
|
||||
@@ -54,7 +55,7 @@ func (p *Publisher) tick(ctx context.Context) {
|
||||
|
||||
var successIDs []string
|
||||
for _, e := range entries {
|
||||
err := p.broker.Publish(ctx, "process", e.Payload)
|
||||
err := p.broker.Publish(telemetry.ExtractTrace(ctx, e.Headers), "process", e.Payload)
|
||||
if err != nil {
|
||||
log.Printf("outbox: ошибка публикации %s: %v", e.ID, err)
|
||||
break // Прерываем пачку
|
||||
|
||||
Reference in New Issue
Block a user