Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | // Copyright (c) 2017 Uber Technologies, Inc. |
| 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 jaeger |
| 16 | |
| 17 | import ( |
| 18 | "github.com/opentracing/opentracing-go" |
| 19 | ) |
| 20 | |
| 21 | // ZipkinSpanFormat is an OpenTracing carrier format constant |
| 22 | const ZipkinSpanFormat = "zipkin-span-format" |
| 23 | |
| 24 | // ExtractableZipkinSpan is a type of Carrier used for integration with Zipkin-aware |
| 25 | // RPC frameworks (like TChannel). It does not support baggage, only trace IDs. |
| 26 | type ExtractableZipkinSpan interface { |
| 27 | TraceID() uint64 |
| 28 | SpanID() uint64 |
| 29 | ParentID() uint64 |
| 30 | Flags() byte |
| 31 | } |
| 32 | |
| 33 | // InjectableZipkinSpan is a type of Carrier used for integration with Zipkin-aware |
| 34 | // RPC frameworks (like TChannel). It does not support baggage, only trace IDs. |
| 35 | type InjectableZipkinSpan interface { |
| 36 | SetTraceID(traceID uint64) |
| 37 | SetSpanID(spanID uint64) |
| 38 | SetParentID(parentID uint64) |
| 39 | SetFlags(flags byte) |
| 40 | } |
| 41 | |
| 42 | type zipkinPropagator struct { |
| 43 | tracer *Tracer |
| 44 | } |
| 45 | |
| 46 | func (p *zipkinPropagator) Inject( |
| 47 | ctx SpanContext, |
| 48 | abstractCarrier interface{}, |
| 49 | ) error { |
| 50 | carrier, ok := abstractCarrier.(InjectableZipkinSpan) |
| 51 | if !ok { |
| 52 | return opentracing.ErrInvalidCarrier |
| 53 | } |
| 54 | |
| 55 | carrier.SetTraceID(ctx.TraceID().Low) // TODO this cannot work with 128bit IDs |
| 56 | carrier.SetSpanID(uint64(ctx.SpanID())) |
| 57 | carrier.SetParentID(uint64(ctx.ParentID())) |
| 58 | carrier.SetFlags(ctx.samplingState.flags()) |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | func (p *zipkinPropagator) Extract(abstractCarrier interface{}) (SpanContext, error) { |
| 63 | carrier, ok := abstractCarrier.(ExtractableZipkinSpan) |
| 64 | if !ok { |
| 65 | return emptyContext, opentracing.ErrInvalidCarrier |
| 66 | } |
| 67 | if carrier.TraceID() == 0 { |
| 68 | return emptyContext, opentracing.ErrSpanContextNotFound |
| 69 | } |
| 70 | var ctx SpanContext |
| 71 | ctx.traceID.Low = carrier.TraceID() |
| 72 | ctx.spanID = SpanID(carrier.SpanID()) |
| 73 | ctx.parentID = SpanID(carrier.ParentID()) |
| 74 | ctx.samplingState = &samplingState{} |
| 75 | ctx.samplingState.setFlags(carrier.Flags()) |
| 76 | return ctx, nil |
| 77 | } |