|
| 1 | +// Copyright 2019 The OpenZipkin Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package zipkintracer |
| 16 | + |
| 17 | +import ( |
| 18 | + "net/http" |
| 19 | + |
| 20 | + opentracing "github.com/opentracing/opentracing-go" |
| 21 | + "github.com/openzipkin/zipkin-go/model" |
| 22 | + "github.com/openzipkin/zipkin-go/propagation" |
| 23 | + "github.com/openzipkin/zipkin-go/propagation/b3" |
| 24 | +) |
| 25 | + |
| 26 | +// DelegatingCarrier is a flexible carrier interface which can be implemented |
| 27 | +// by types which have a means of storing the trace metadata and already know |
| 28 | +// how to serialize themselves |
| 29 | +type DelegatingCarrier interface { |
| 30 | + State() (model.SpanContext, error) |
| 31 | + SetState(model.SpanContext) error |
| 32 | +} |
| 33 | + |
| 34 | +type textMapPropagator struct { |
| 35 | + tracer *tracerImpl |
| 36 | +} |
| 37 | + |
| 38 | +func (p *textMapPropagator) Inject( |
| 39 | + spanContext opentracing.SpanContext, |
| 40 | + opaqueCarrier interface{}, |
| 41 | +) error { |
| 42 | + sc, ok := spanContext.(SpanContext) |
| 43 | + if !ok { |
| 44 | + return opentracing.ErrInvalidSpanContext |
| 45 | + } |
| 46 | + // native zipkin-go injector |
| 47 | + if injector, ok := opaqueCarrier.(propagation.Injector); ok { |
| 48 | + return injector(model.SpanContext(sc)) |
| 49 | + } |
| 50 | + // fallback to support native opentracing http carrier |
| 51 | + if httpCarrier, ok := opaqueCarrier.(opentracing.HTTPHeadersCarrier); ok { |
| 52 | + req := &http.Request{Header: http.Header(httpCarrier)} |
| 53 | + switch p.tracer.opts.b3InjectOpt { |
| 54 | + case B3InjectSingle: |
| 55 | + return b3.InjectHTTP(req, b3.WithSingleHeaderOnly())(model.SpanContext(sc)) |
| 56 | + case B3InjectBoth: |
| 57 | + return b3.InjectHTTP(req, b3.WithSingleAndMultiHeader())(model.SpanContext(sc)) |
| 58 | + default: |
| 59 | + return b3.InjectHTTP(req)(model.SpanContext(sc)) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return opentracing.ErrInvalidCarrier |
| 64 | +} |
| 65 | + |
| 66 | +func (p *textMapPropagator) Extract( |
| 67 | + opaqueCarrier interface{}, |
| 68 | +) (opentracing.SpanContext, error) { |
| 69 | + if extractor, ok := opaqueCarrier.(propagation.Extractor); ok { |
| 70 | + sc, err := extractor() |
| 71 | + if sc != nil { |
| 72 | + return SpanContext(*sc), err |
| 73 | + } |
| 74 | + return SpanContext{}, err |
| 75 | + } |
| 76 | + if httpCarrier, ok := opaqueCarrier.(opentracing.HTTPHeadersCarrier); ok { |
| 77 | + req := &http.Request{Header: http.Header(httpCarrier)} |
| 78 | + sc, err := b3.ExtractHTTP(req)() |
| 79 | + if sc != nil { |
| 80 | + return SpanContext(*sc), err |
| 81 | + } |
| 82 | + return SpanContext{}, err |
| 83 | + } |
| 84 | + return nil, opentracing.ErrUnsupportedFormat |
| 85 | +} |
| 86 | + |
| 87 | +type accessorPropagator struct { |
| 88 | + tracer *tracerImpl |
| 89 | +} |
| 90 | + |
| 91 | +func (p *accessorPropagator) Inject( |
| 92 | + spanContext opentracing.SpanContext, |
| 93 | + opaqueCarrier interface{}, |
| 94 | +) error { |
| 95 | + dc, ok := opaqueCarrier.(DelegatingCarrier) |
| 96 | + if !ok || dc == nil { |
| 97 | + return opentracing.ErrInvalidCarrier |
| 98 | + } |
| 99 | + sc, ok := spanContext.(SpanContext) |
| 100 | + if !ok { |
| 101 | + return opentracing.ErrInvalidSpanContext |
| 102 | + } |
| 103 | + return dc.SetState(model.SpanContext(sc)) |
| 104 | +} |
| 105 | + |
| 106 | +func (p *accessorPropagator) Extract( |
| 107 | + opaqueCarrier interface{}, |
| 108 | +) (opentracing.SpanContext, error) { |
| 109 | + dc, ok := opaqueCarrier.(DelegatingCarrier) |
| 110 | + if !ok || dc == nil { |
| 111 | + return nil, opentracing.ErrInvalidCarrier |
| 112 | + } |
| 113 | + |
| 114 | + sc, err := dc.State() |
| 115 | + return SpanContext(sc), err |
| 116 | +} |
0 commit comments