2019-10-08 19:22:15 +02:00
|
|
|
// Copyright 2019, OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-01-28 20:13:46 +02:00
|
|
|
package trace
|
2019-10-08 19:22:15 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
2020-01-28 20:13:46 +02:00
|
|
|
"go.opentelemetry.io/otel/api/correlation"
|
|
|
|
"go.opentelemetry.io/otel/api/propagation"
|
2019-10-08 19:22:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
B3SingleHeader = "X-B3"
|
|
|
|
B3DebugFlagHeader = "X-B3-Flags"
|
|
|
|
B3TraceIDHeader = "X-B3-TraceId"
|
|
|
|
B3SpanIDHeader = "X-B3-SpanId"
|
|
|
|
B3SampledHeader = "X-B3-Sampled"
|
|
|
|
B3ParentSpanIDHeader = "X-B3-ParentSpanId"
|
|
|
|
)
|
|
|
|
|
2019-12-03 23:52:04 +02:00
|
|
|
// B3 propagator serializes core.SpanContext to/from B3 Headers.
|
2019-10-08 19:22:15 +02:00
|
|
|
// This propagator supports both version of B3 headers,
|
|
|
|
// 1. Single Header :
|
|
|
|
// X-B3: {TraceId}-{SpanId}-{SamplingState}-{ParentSpanId}
|
|
|
|
// 2. Multiple Headers:
|
|
|
|
// X-B3-TraceId: {TraceId}
|
|
|
|
// X-B3-ParentSpanId: {ParentSpanId}
|
|
|
|
// X-B3-SpanId: {SpanId}
|
|
|
|
// X-B3-Sampled: {SamplingState}
|
|
|
|
// X-B3-Flags: {DebugFlag}
|
|
|
|
//
|
2019-10-16 19:24:38 +02:00
|
|
|
// If SingleHeader is set to true then X-B3 header is used to inject and extract. Otherwise,
|
2019-10-08 19:22:15 +02:00
|
|
|
// separate headers are used to inject and extract.
|
2019-12-03 23:52:04 +02:00
|
|
|
type B3 struct {
|
2019-10-16 19:24:38 +02:00
|
|
|
SingleHeader bool
|
2019-10-08 19:22:15 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 20:13:46 +02:00
|
|
|
var _ propagation.TextFormat = B3{}
|
2019-10-16 19:24:38 +02:00
|
|
|
|
2020-01-28 20:13:46 +02:00
|
|
|
func (b3 B3) Inject(ctx context.Context, supplier propagation.Supplier) {
|
|
|
|
sc := SpanFromContext(ctx).SpanContext()
|
2019-10-08 19:22:15 +02:00
|
|
|
if sc.IsValid() {
|
2019-10-16 19:24:38 +02:00
|
|
|
if b3.SingleHeader {
|
2019-10-08 19:22:15 +02:00
|
|
|
sampled := sc.TraceFlags & core.TraceFlagsSampled
|
|
|
|
supplier.Set(B3SingleHeader,
|
2019-10-23 08:01:33 +02:00
|
|
|
fmt.Sprintf("%s-%.16x-%.1d", sc.TraceIDString(), sc.SpanID, sampled))
|
2019-10-08 19:22:15 +02:00
|
|
|
} else {
|
2019-10-23 08:01:33 +02:00
|
|
|
supplier.Set(B3TraceIDHeader, sc.TraceIDString())
|
2019-10-08 19:22:15 +02:00
|
|
|
supplier.Set(B3SpanIDHeader,
|
|
|
|
fmt.Sprintf("%.16x", sc.SpanID))
|
|
|
|
|
|
|
|
var sampled string
|
|
|
|
if sc.IsSampled() {
|
|
|
|
sampled = "1"
|
|
|
|
} else {
|
|
|
|
sampled = "0"
|
|
|
|
}
|
|
|
|
supplier.Set(B3SampledHeader, sampled)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract retrieves B3 Headers from the supplier
|
2020-01-28 20:13:46 +02:00
|
|
|
func (b3 B3) Extract(ctx context.Context, supplier propagation.Supplier) (core.SpanContext, correlation.Map) {
|
2019-10-16 19:24:38 +02:00
|
|
|
if b3.SingleHeader {
|
2020-01-28 20:13:46 +02:00
|
|
|
return b3.extractSingleHeader(supplier), correlation.NewEmptyMap()
|
2019-10-08 19:22:15 +02:00
|
|
|
}
|
2020-01-28 20:13:46 +02:00
|
|
|
return b3.extract(supplier), correlation.NewEmptyMap()
|
2019-10-08 19:22:15 +02:00
|
|
|
}
|
|
|
|
|
2019-12-03 23:52:04 +02:00
|
|
|
func (b3 B3) GetAllKeys() []string {
|
2019-10-16 19:24:38 +02:00
|
|
|
if b3.SingleHeader {
|
2019-10-08 19:22:15 +02:00
|
|
|
return []string{B3SingleHeader}
|
|
|
|
}
|
|
|
|
return []string{B3TraceIDHeader, B3SpanIDHeader, B3SampledHeader}
|
|
|
|
}
|
|
|
|
|
2020-01-28 20:13:46 +02:00
|
|
|
func (b3 B3) extract(supplier propagation.Supplier) core.SpanContext {
|
2019-10-28 19:05:06 +02:00
|
|
|
tid, err := core.TraceIDFromHex(supplier.Get(B3TraceIDHeader))
|
|
|
|
if err != nil {
|
2019-10-08 19:22:15 +02:00
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
2019-10-28 19:05:06 +02:00
|
|
|
sid, err := core.SpanIDFromHex(supplier.Get(B3SpanIDHeader))
|
|
|
|
if err != nil {
|
2019-10-08 19:22:15 +02:00
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
sampled, ok := b3.extractSampledState(supplier.Get(B3SampledHeader))
|
|
|
|
if !ok {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
debug, ok := b3.extracDebugFlag(supplier.Get(B3DebugFlagHeader))
|
|
|
|
if !ok {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
if debug == core.TraceFlagsSampled {
|
|
|
|
sampled = core.TraceFlagsSampled
|
|
|
|
}
|
|
|
|
|
|
|
|
sc := core.SpanContext{
|
|
|
|
TraceID: tid,
|
|
|
|
SpanID: sid,
|
|
|
|
TraceFlags: sampled,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sc.IsValid() {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
return sc
|
|
|
|
}
|
|
|
|
|
2020-01-28 20:13:46 +02:00
|
|
|
func (b3 B3) extractSingleHeader(supplier propagation.Supplier) core.SpanContext {
|
2019-10-08 19:22:15 +02:00
|
|
|
h := supplier.Get(B3SingleHeader)
|
|
|
|
if h == "" || h == "0" {
|
|
|
|
core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
sc := core.SpanContext{}
|
|
|
|
parts := strings.Split(h, "-")
|
|
|
|
l := len(parts)
|
|
|
|
if l > 4 {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
if l < 2 {
|
|
|
|
return core.EmptySpanContext()
|
2019-10-16 19:24:38 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 19:05:06 +02:00
|
|
|
var err error
|
|
|
|
sc.TraceID, err = core.TraceIDFromHex(parts[0])
|
|
|
|
if err != nil {
|
2019-10-16 19:24:38 +02:00
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
|
2019-10-28 19:05:06 +02:00
|
|
|
sc.SpanID, err = core.SpanIDFromHex(parts[1])
|
|
|
|
if err != nil {
|
2019-10-16 19:24:38 +02:00
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
if l > 2 {
|
2019-10-28 19:05:06 +02:00
|
|
|
var ok bool
|
2019-10-16 19:24:38 +02:00
|
|
|
sc.TraceFlags, ok = b3.extractSampledState(parts[2])
|
2019-10-08 19:22:15 +02:00
|
|
|
if !ok {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
2019-10-16 19:24:38 +02:00
|
|
|
}
|
|
|
|
if l == 4 {
|
2019-10-28 19:05:06 +02:00
|
|
|
_, err = core.SpanIDFromHex(parts[3])
|
|
|
|
if err != nil {
|
2019-10-08 19:22:15 +02:00
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sc.IsValid() {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
return sc
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractSampledState parses the value of the X-B3-Sampled b3Header.
|
2019-12-03 23:52:04 +02:00
|
|
|
func (b3 B3) extractSampledState(sampled string) (flag byte, ok bool) {
|
2019-10-08 19:22:15 +02:00
|
|
|
switch sampled {
|
|
|
|
case "", "0":
|
|
|
|
return 0, true
|
|
|
|
case "1":
|
|
|
|
return core.TraceFlagsSampled, true
|
|
|
|
case "true":
|
2019-10-16 19:24:38 +02:00
|
|
|
if !b3.SingleHeader {
|
2019-10-08 19:22:15 +02:00
|
|
|
return core.TraceFlagsSampled, true
|
|
|
|
}
|
|
|
|
case "d":
|
2019-10-16 19:24:38 +02:00
|
|
|
if b3.SingleHeader {
|
2019-10-08 19:22:15 +02:00
|
|
|
return core.TraceFlagsSampled, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// extracDebugFlag parses the value of the X-B3-Sampled b3Header.
|
2019-12-03 23:52:04 +02:00
|
|
|
func (b3 B3) extracDebugFlag(debug string) (flag byte, ok bool) {
|
2019-10-08 19:22:15 +02:00
|
|
|
switch debug {
|
|
|
|
case "", "0":
|
|
|
|
return 0, true
|
|
|
|
case "1":
|
|
|
|
return core.TraceFlagsSampled, true
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|