go.undefinedlabs.com/scopeagent@v0.4.2/tracer/propagation.go (about) 1 package tracer 2 3 import ( 4 "github.com/google/uuid" 5 opentracing "github.com/opentracing/opentracing-go" 6 ) 7 8 type accessorPropagator struct { 9 tracer *tracerImpl 10 } 11 12 // DelegatingCarrier is a flexible carrier interface which can be implemented 13 // by types which have a means of storing the trace metadata and already know 14 // how to serialize themselves (for example, protocol buffers). 15 type DelegatingCarrier interface { 16 SetState(traceID uuid.UUID, spanID uint64, sampled bool) 17 State() (traceID uuid.UUID, spanID uint64, sampled bool) 18 SetBaggageItem(key, value string) 19 GetBaggage(func(key, value string)) 20 } 21 22 func (p *accessorPropagator) Inject( 23 spanContext opentracing.SpanContext, 24 carrier interface{}, 25 ) error { 26 dc, ok := carrier.(DelegatingCarrier) 27 if !ok || dc == nil { 28 return opentracing.ErrInvalidCarrier 29 } 30 sc, ok := spanContext.(SpanContext) 31 if !ok { 32 return opentracing.ErrInvalidSpanContext 33 } 34 dc.SetState(sc.TraceID, sc.SpanID, sc.Sampled) 35 for k, v := range sc.Baggage { 36 dc.SetBaggageItem(k, v) 37 } 38 return nil 39 } 40 41 func (p *accessorPropagator) Extract( 42 carrier interface{}, 43 ) (opentracing.SpanContext, error) { 44 dc, ok := carrier.(DelegatingCarrier) 45 if !ok || dc == nil { 46 return nil, opentracing.ErrInvalidCarrier 47 } 48 49 traceID, spanID, sampled := dc.State() 50 sc := SpanContext{ 51 TraceID: traceID, 52 SpanID: spanID, 53 Sampled: sampled, 54 Baggage: nil, 55 } 56 dc.GetBaggage(func(k, v string) { 57 if sc.Baggage == nil { 58 sc.Baggage = map[string]string{} 59 } 60 sc.Baggage[k] = v 61 }) 62 63 return sc, nil 64 }