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
+1
View File
@@ -22,6 +22,7 @@ type OutboxEntry struct {
AggregateID string
EventType string
Payload []byte
Headers map[string]string
CreatedAt time.Time
PublishedAt *time.Time
Attempts int
+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)
}
+18
View File
@@ -0,0 +1,18 @@
package telemetry
import (
"context"
"go.opentelemetry.io/otel/propagation"
)
// TraceHeaders переносит только W3C Trace Context через границу задания.
func TraceHeaders(ctx context.Context) map[string]string {
headers := propagation.MapCarrier{}
propagation.TraceContext{}.Inject(ctx, headers)
return headers
}
func ExtractTrace(ctx context.Context, headers map[string]string) context.Context {
return propagation.TraceContext{}.Extract(ctx, propagation.MapCarrier(headers))
}
@@ -0,0 +1,26 @@
package telemetry
import (
"context"
"testing"
"go.opentelemetry.io/otel/trace"
)
func TestOutboxTraceHeaders(t *testing.T) {
input := map[string]string{
"traceparent": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
"tracestate": "lab=node1",
"baggage": "private=example",
}
ctx := ExtractTrace(context.Background(), input)
got := TraceHeaders(ctx)
if len(got) != 2 || got["traceparent"] != input["traceparent"] || got["tracestate"] != input["tracestate"] {
t.Fatalf("неожиданные заголовки outbox: %v", got)
}
for _, headers := range []map[string]string{nil, {"traceparent": "broken"}} {
if trace.SpanContextFromContext(ExtractTrace(context.Background(), headers)).IsValid() {
t.Fatal("отсутствующий или повреждённый контекст дал валидного родителя")
}
}
}
@@ -0,0 +1,5 @@
-- +goose Up
ALTER TABLE outbox ADD COLUMN headers JSONB NOT NULL DEFAULT '{}';
-- +goose Down
ALTER TABLE outbox DROP COLUMN headers;