fix: Use a single HTTP header for zap trace context
parent
9149c6508d
commit
48f5afb33e
|
@ -15,8 +15,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
traceIDKey = "__trace_id__"
|
traceHTTPHeader = "Zap-Trace-Span"
|
||||||
spanIDKey = "__span_id__"
|
|
||||||
|
|
||||||
logTraceIDKey = "ot_trace_id"
|
logTraceIDKey = "ot_trace_id"
|
||||||
logSpanIDKey = "ot_span_id"
|
logSpanIDKey = "ot_span_id"
|
||||||
|
@ -78,15 +77,13 @@ func (t *Tracer) Inject(sm opentracing.SpanContext, format interface{}, carrier
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("carrier must be an opentracing.TextMapWriter for text map format, got %T", carrier)
|
return fmt.Errorf("carrier must be an opentracing.TextMapWriter for text map format, got %T", carrier)
|
||||||
}
|
}
|
||||||
injectTextMapWriter(ctx, w)
|
return injectTextMapWriter(ctx, w)
|
||||||
return nil
|
|
||||||
case opentracing.HTTPHeaders:
|
case opentracing.HTTPHeaders:
|
||||||
w, ok := carrier.(opentracing.TextMapWriter)
|
w, ok := carrier.(opentracing.TextMapWriter)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("carrier must be an opentracing.TextMapWriter for http header format, got %T", carrier)
|
return fmt.Errorf("carrier must be an opentracing.TextMapWriter for http header format, got %T", carrier)
|
||||||
}
|
}
|
||||||
injectTextMapWriter(ctx, w)
|
return injectTextMapWriter(ctx, w)
|
||||||
return nil
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unsupported format %v", format)
|
return fmt.Errorf("unsupported format %v", format)
|
||||||
}
|
}
|
||||||
|
@ -126,33 +123,24 @@ func (t *Tracer) Extract(format interface{}, carrier interface{}) (opentracing.S
|
||||||
return ctx, err
|
return ctx, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func injectTextMapWriter(ctx SpanContext, w opentracing.TextMapWriter) {
|
func injectTextMapWriter(ctx SpanContext, w opentracing.TextMapWriter) error {
|
||||||
ctx.ForeachBaggageItem(func(k, v string) bool {
|
data, err := json.Marshal(ctx)
|
||||||
w.Set(k, v)
|
if err != nil {
|
||||||
return true
|
return err
|
||||||
})
|
}
|
||||||
w.Set(traceIDKey, ctx.traceID.String())
|
w.Set(traceHTTPHeader, string(data))
|
||||||
w.Set(spanIDKey, ctx.spanID.String())
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractTextMapReader(ctx *SpanContext, r opentracing.TextMapReader) error {
|
func extractTextMapReader(ctx *SpanContext, r opentracing.TextMapReader) error {
|
||||||
return r.ForeachKey(func(k, v string) error {
|
var data []byte
|
||||||
switch k {
|
r.ForeachKey(func(k, v string) error {
|
||||||
case traceIDKey:
|
if k == traceHTTPHeader {
|
||||||
err := ctx.traceID.DecodeFromString(v)
|
data = []byte(v)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case spanIDKey:
|
|
||||||
err := ctx.spanID.DecodeFromString(v)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
ctx.baggage[k] = v
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
return json.Unmarshal(data, ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Span implements opentracing.Span, all Spans must be created using the Tracer.
|
// Span implements opentracing.Span, all Spans must be created using the Tracer.
|
||||||
|
@ -290,9 +278,9 @@ func (c SpanContext) ForeachBaggageItem(handler func(k, v string) bool) {
|
||||||
|
|
||||||
func (c SpanContext) MarshalJSON() ([]byte, error) {
|
func (c SpanContext) MarshalJSON() ([]byte, error) {
|
||||||
raw := struct {
|
raw := struct {
|
||||||
TraceID platform.ID
|
TraceID platform.ID `json:"trace_id"`
|
||||||
SpanID platform.ID
|
SpanID platform.ID `json:"span_id"`
|
||||||
Baggage map[string]string
|
Baggage map[string]string `json:"baggage"`
|
||||||
}{
|
}{
|
||||||
TraceID: c.traceID,
|
TraceID: c.traceID,
|
||||||
SpanID: c.spanID,
|
SpanID: c.spanID,
|
||||||
|
@ -303,9 +291,9 @@ func (c SpanContext) MarshalJSON() ([]byte, error) {
|
||||||
|
|
||||||
func (c *SpanContext) UnmarshalJSON(data []byte) error {
|
func (c *SpanContext) UnmarshalJSON(data []byte) error {
|
||||||
raw := struct {
|
raw := struct {
|
||||||
TraceID platform.ID
|
TraceID platform.ID `json:"trace_id"`
|
||||||
SpanID platform.ID
|
SpanID platform.ID `json:"span_id"`
|
||||||
Baggage map[string]string
|
Baggage map[string]string `json:"baggage"`
|
||||||
}{
|
}{
|
||||||
TraceID: c.traceID,
|
TraceID: c.traceID,
|
||||||
SpanID: c.spanID,
|
SpanID: c.spanID,
|
||||||
|
|
Loading…
Reference in New Issue