feat(outbox): lab6 persist W3C trace context with attachment jobs

This commit is contained in:
Голиков Maximillian Антон
2026-11-16 16:12:21 +03:00
parent 472b4761d6
commit 2716119a20
5 changed files with 65 additions and 8 deletions
+15 -8
View File
@@ -8,6 +8,7 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"high-loud-apps-labs/internal/domain"
"high-loud-apps-labs/internal/telemetry"
)
type OutboxRepo struct {
@@ -39,14 +40,17 @@ func (r *OutboxRepo) Enqueue(ctx context.Context, tx domain.Tx, e *domain.Outbox
e.CreatedAt = time.Now().UTC()
}
if e.Headers == nil {
e.Headers = telemetry.TraceHeaders(ctx)
}
const q = `
INSERT INTO outbox
(id, aggregate_type, aggregate_id, event_type, payload, attempts, created_at, published_at)
VALUES ($1::uuid, $2, $3::uuid, $4, $5::jsonb, $6, $7, $8)
(id, aggregate_type, aggregate_id, event_type, payload, attempts, created_at, published_at, headers)
VALUES ($1::uuid, $2, $3::uuid, $4, $5::jsonb, $6, $7, $8, $9::jsonb)
`
if err := tx.Exec(ctx, q,
e.ID, e.AggregateType, e.AggregateID, e.EventType,
string(e.Payload), e.Attempts, e.CreatedAt, e.PublishedAt,
string(e.Payload), e.Attempts, e.CreatedAt, e.PublishedAt, e.Headers,
); err != nil {
return err
}
@@ -66,7 +70,7 @@ func (r *OutboxRepo) ClaimBatch(ctx context.Context, limit int) ([]domain.Outbox
SET attempts = attempts + 1
FROM claimed c
WHERE o.id = c.id
RETURNING o.id, o.aggregate_type, o.aggregate_id, o.event_type, o.payload, o.attempts, o.created_at, o.published_at
RETURNING o.id, o.aggregate_type, o.aggregate_id, o.event_type, o.payload, o.attempts, o.created_at, o.published_at, o.headers
`
rows, err := r.pool.Query(ctx, q, limit)
if err != nil {
@@ -79,7 +83,7 @@ func (r *OutboxRepo) ClaimBatch(ctx context.Context, limit int) ([]domain.Outbox
var e domain.OutboxEntry
if err := rows.Scan(
&e.ID, &e.AggregateType, &e.AggregateID, &e.EventType,
&e.Payload, &e.Attempts, &e.CreatedAt, &e.PublishedAt,
&e.Payload, &e.Attempts, &e.CreatedAt, &e.PublishedAt, &e.Headers,
); err != nil {
return nil, mapError(err)
}
@@ -92,14 +96,17 @@ func (r *OutboxRepo) EnqueueDirect(ctx context.Context, e *domain.OutboxEntry) e
if e.ID == "" {
e.ID = uuid.NewString()
}
if e.Headers == nil {
e.Headers = telemetry.TraceHeaders(ctx)
}
const q = `
INSERT INTO outbox
(id, aggregate_type, aggregate_id, event_type, payload, attempts, created_at, published_at)
VALUES ($1::uuid, $2, $3::uuid, $4, $5::jsonb, $6, $7, $8)
(id, aggregate_type, aggregate_id, event_type, payload, attempts, created_at, published_at, headers)
VALUES ($1::uuid, $2, $3::uuid, $4, $5::jsonb, $6, $7, $8, $9::jsonb)
`
_, err := r.pool.Exec(ctx, q,
e.ID, e.AggregateType, e.AggregateID, e.EventType,
string(e.Payload), e.Attempts, e.CreatedAt, e.PublishedAt,
string(e.Payload), e.Attempts, e.CreatedAt, e.PublishedAt, e.Headers,
)
return mapError(err)
}