forked from mtusi2026/mtusi2026lab6
feat(outbox): lab6 persist W3C trace context with attachment jobs
This commit is contained in:
@@ -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("отсутствующий или повреждённый контекст дал валидного родителя")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user