From 2716119a20b9d66f0c48a18d526481445376e7dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Mon, 16 Nov 2026 16:12:21 +0300 Subject: [PATCH] feat(outbox): lab6 persist W3C trace context with attachment jobs --- backend/internal/domain/outbox.go | 1 + backend/internal/storage/postgres/outbox.go | 23 ++++++++++------ backend/internal/telemetry/propagation.go | 18 +++++++++++++ .../internal/telemetry/propagation_test.go | 26 +++++++++++++++++++ backend/migrations/0010_outbox_headers.sql | 5 ++++ 5 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 backend/internal/telemetry/propagation.go create mode 100644 backend/internal/telemetry/propagation_test.go create mode 100644 backend/migrations/0010_outbox_headers.sql diff --git a/backend/internal/domain/outbox.go b/backend/internal/domain/outbox.go index ecaa511..107c1f4 100644 --- a/backend/internal/domain/outbox.go +++ b/backend/internal/domain/outbox.go @@ -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 diff --git a/backend/internal/storage/postgres/outbox.go b/backend/internal/storage/postgres/outbox.go index 414ba0b..086fa44 100644 --- a/backend/internal/storage/postgres/outbox.go +++ b/backend/internal/storage/postgres/outbox.go @@ -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) } diff --git a/backend/internal/telemetry/propagation.go b/backend/internal/telemetry/propagation.go new file mode 100644 index 0000000..419d0e5 --- /dev/null +++ b/backend/internal/telemetry/propagation.go @@ -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)) +} diff --git a/backend/internal/telemetry/propagation_test.go b/backend/internal/telemetry/propagation_test.go new file mode 100644 index 0000000..94314a3 --- /dev/null +++ b/backend/internal/telemetry/propagation_test.go @@ -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("отсутствующий или повреждённый контекст дал валидного родителя") + } + } +} diff --git a/backend/migrations/0010_outbox_headers.sql b/backend/migrations/0010_outbox_headers.sql new file mode 100644 index 0000000..db17e2f --- /dev/null +++ b/backend/migrations/0010_outbox_headers.sql @@ -0,0 +1,5 @@ +-- +goose Up +ALTER TABLE outbox ADD COLUMN headers JSONB NOT NULL DEFAULT '{}'; + +-- +goose Down +ALTER TABLE outbox DROP COLUMN headers;