You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-08-10 22:31:50 +02:00
Merge propagation and api/propagation into api/propagators (#362)
* Merge propagation Rename and merge propagation and api/propagation to api/propagators. Drop propagator suffix in general such that TextFormatPropagator becomes TextFormat since usage is propagators.TextFormat fixes #311 * Rebase and godoc updates * Revert go mod changes * Replace carrier with supplier in godoc
This commit is contained in:
@@ -1,207 +0,0 @@
|
||||
// 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.
|
||||
|
||||
package propagation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"go.opentelemetry.io/otel/api/trace"
|
||||
|
||||
"go.opentelemetry.io/otel/api/core"
|
||||
dctx "go.opentelemetry.io/otel/api/distributedcontext"
|
||||
apipropagation "go.opentelemetry.io/otel/api/propagation"
|
||||
)
|
||||
|
||||
const (
|
||||
B3SingleHeader = "X-B3"
|
||||
B3DebugFlagHeader = "X-B3-Flags"
|
||||
B3TraceIDHeader = "X-B3-TraceId"
|
||||
B3SpanIDHeader = "X-B3-SpanId"
|
||||
B3SampledHeader = "X-B3-Sampled"
|
||||
B3ParentSpanIDHeader = "X-B3-ParentSpanId"
|
||||
)
|
||||
|
||||
// B3Propagator that facilitates core.SpanContext
|
||||
// propagation using B3 Headers.
|
||||
// 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}
|
||||
//
|
||||
// If SingleHeader is set to true then X-B3 header is used to inject and extract. Otherwise,
|
||||
// separate headers are used to inject and extract.
|
||||
type B3Propagator struct {
|
||||
SingleHeader bool
|
||||
}
|
||||
|
||||
var _ apipropagation.TextFormatPropagator = B3Propagator{}
|
||||
|
||||
func (b3 B3Propagator) Inject(ctx context.Context, supplier apipropagation.Supplier) {
|
||||
sc := trace.CurrentSpan(ctx).SpanContext()
|
||||
if sc.IsValid() {
|
||||
if b3.SingleHeader {
|
||||
sampled := sc.TraceFlags & core.TraceFlagsSampled
|
||||
supplier.Set(B3SingleHeader,
|
||||
fmt.Sprintf("%s-%.16x-%.1d", sc.TraceIDString(), sc.SpanID, sampled))
|
||||
} else {
|
||||
supplier.Set(B3TraceIDHeader, sc.TraceIDString())
|
||||
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
|
||||
func (b3 B3Propagator) Extract(ctx context.Context, supplier apipropagation.Supplier) (core.SpanContext, dctx.Map) {
|
||||
if b3.SingleHeader {
|
||||
return b3.extractSingleHeader(supplier), dctx.NewEmptyMap()
|
||||
}
|
||||
return b3.extract(supplier), dctx.NewEmptyMap()
|
||||
}
|
||||
|
||||
func (b3 B3Propagator) GetAllKeys() []string {
|
||||
if b3.SingleHeader {
|
||||
return []string{B3SingleHeader}
|
||||
}
|
||||
return []string{B3TraceIDHeader, B3SpanIDHeader, B3SampledHeader}
|
||||
}
|
||||
|
||||
func (b3 B3Propagator) extract(supplier apipropagation.Supplier) core.SpanContext {
|
||||
tid, err := core.TraceIDFromHex(supplier.Get(B3TraceIDHeader))
|
||||
if err != nil {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
sid, err := core.SpanIDFromHex(supplier.Get(B3SpanIDHeader))
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
func (b3 B3Propagator) extractSingleHeader(supplier apipropagation.Supplier) core.SpanContext {
|
||||
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()
|
||||
}
|
||||
|
||||
var err error
|
||||
sc.TraceID, err = core.TraceIDFromHex(parts[0])
|
||||
if err != nil {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
|
||||
sc.SpanID, err = core.SpanIDFromHex(parts[1])
|
||||
if err != nil {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
|
||||
if l > 2 {
|
||||
var ok bool
|
||||
sc.TraceFlags, ok = b3.extractSampledState(parts[2])
|
||||
if !ok {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
}
|
||||
if l == 4 {
|
||||
_, err = core.SpanIDFromHex(parts[3])
|
||||
if err != nil {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
}
|
||||
|
||||
if !sc.IsValid() {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
|
||||
return sc
|
||||
}
|
||||
|
||||
// extractSampledState parses the value of the X-B3-Sampled b3Header.
|
||||
func (b3 B3Propagator) extractSampledState(sampled string) (flag byte, ok bool) {
|
||||
switch sampled {
|
||||
case "", "0":
|
||||
return 0, true
|
||||
case "1":
|
||||
return core.TraceFlagsSampled, true
|
||||
case "true":
|
||||
if !b3.SingleHeader {
|
||||
return core.TraceFlagsSampled, true
|
||||
}
|
||||
case "d":
|
||||
if b3.SingleHeader {
|
||||
return core.TraceFlagsSampled, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// extracDebugFlag parses the value of the X-B3-Sampled b3Header.
|
||||
func (b3 B3Propagator) extracDebugFlag(debug string) (flag byte, ok bool) {
|
||||
switch debug {
|
||||
case "", "0":
|
||||
return 0, true
|
||||
case "1":
|
||||
return core.TraceFlagsSampled, true
|
||||
}
|
||||
return 0, false
|
||||
}
|
@@ -1,129 +0,0 @@
|
||||
// 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.
|
||||
|
||||
package propagation_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"go.opentelemetry.io/otel/api/trace"
|
||||
mocktrace "go.opentelemetry.io/otel/internal/trace"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
)
|
||||
|
||||
func BenchmarkExtractB3(b *testing.B) {
|
||||
testGroup := []struct {
|
||||
singleHeader bool
|
||||
name string
|
||||
tests []extractTest
|
||||
}{
|
||||
{
|
||||
singleHeader: false,
|
||||
name: "multiple headers",
|
||||
tests: extractMultipleHeaders,
|
||||
},
|
||||
{
|
||||
singleHeader: true,
|
||||
name: "single headers",
|
||||
tests: extractSingleHeader,
|
||||
},
|
||||
{
|
||||
singleHeader: false,
|
||||
name: "invalid multiple headers",
|
||||
tests: extractInvalidB3MultipleHeaders,
|
||||
},
|
||||
{
|
||||
singleHeader: true,
|
||||
name: "invalid single headers",
|
||||
tests: extractInvalidB3SingleHeader,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tg := range testGroup {
|
||||
propagator := propagation.B3Propagator{tg.singleHeader}
|
||||
for _, tt := range tg.tests {
|
||||
traceBenchmark(tg.name+"/"+tt.name, b, func(b *testing.B) {
|
||||
ctx := context.Background()
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
for h, v := range tt.headers {
|
||||
req.Header.Set(h, v)
|
||||
}
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = propagator.Extract(ctx, req.Header)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkInjectB3(b *testing.B) {
|
||||
var id uint64
|
||||
testGroup := []struct {
|
||||
singleHeader bool
|
||||
name string
|
||||
tests []injectTest
|
||||
}{
|
||||
{
|
||||
singleHeader: false,
|
||||
name: "multiple headers",
|
||||
tests: injectB3MultipleHeader,
|
||||
},
|
||||
{
|
||||
singleHeader: true,
|
||||
name: "single headers",
|
||||
tests: injectB3SingleleHeader,
|
||||
},
|
||||
}
|
||||
|
||||
mockTracer := &mocktrace.MockTracer{
|
||||
Sampled: false,
|
||||
StartSpanID: &id,
|
||||
}
|
||||
|
||||
for _, tg := range testGroup {
|
||||
id = 0
|
||||
propagator := propagation.B3Propagator{tg.singleHeader}
|
||||
for _, tt := range tg.tests {
|
||||
traceBenchmark(tg.name+"/"+tt.name, b, func(b *testing.B) {
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
ctx := context.Background()
|
||||
if tt.parentSc.IsValid() {
|
||||
ctx, _ = mockTracer.Start(ctx, "inject", trace.ChildOf(tt.parentSc))
|
||||
} else {
|
||||
ctx, _ = mockTracer.Start(ctx, "inject")
|
||||
}
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
propagator.Inject(ctx, req.Header)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func traceBenchmark(name string, b *testing.B, fn func(*testing.B)) {
|
||||
b.Run(name, func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
fn(b)
|
||||
})
|
||||
b.Run(name, func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
fn(b)
|
||||
})
|
||||
}
|
@@ -1,545 +0,0 @@
|
||||
// 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.
|
||||
|
||||
package propagation_test
|
||||
|
||||
import (
|
||||
"go.opentelemetry.io/otel/api/core"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
)
|
||||
|
||||
type extractTest struct {
|
||||
name string
|
||||
headers map[string]string
|
||||
wantSc core.SpanContext
|
||||
}
|
||||
|
||||
var extractMultipleHeaders = []extractTest{
|
||||
{
|
||||
name: "sampling state defer",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736",
|
||||
propagation.B3SpanIDHeader: "00f067aa0ba902b7",
|
||||
},
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sampling state deny",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736",
|
||||
propagation.B3SpanIDHeader: "00f067aa0ba902b7",
|
||||
propagation.B3SampledHeader: "0",
|
||||
},
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sampling state accept",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736",
|
||||
propagation.B3SpanIDHeader: "00f067aa0ba902b7",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sampling state as a boolean",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736",
|
||||
propagation.B3SpanIDHeader: "00f067aa0ba902b7",
|
||||
propagation.B3SampledHeader: "true",
|
||||
},
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "debug flag set",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736",
|
||||
propagation.B3SpanIDHeader: "00f067aa0ba902b7",
|
||||
propagation.B3DebugFlagHeader: "1",
|
||||
},
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
{
|
||||
// spec is not clear on the behavior for this case. If debug flag is set
|
||||
// then sampled state should not be set. From that perspective debug
|
||||
// takes precedence. Hence, it is sampled.
|
||||
name: "debug flag set and sampling state is deny",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736",
|
||||
propagation.B3SpanIDHeader: "00f067aa0ba902b7",
|
||||
propagation.B3SampledHeader: "0",
|
||||
propagation.B3DebugFlagHeader: "1",
|
||||
},
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "with parent span id",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736",
|
||||
propagation.B3SpanIDHeader: "00f067aa0ba902b7",
|
||||
propagation.B3SampledHeader: "1",
|
||||
propagation.B3ParentSpanIDHeader: "00f067aa0ba90200",
|
||||
},
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "with only sampled state header",
|
||||
headers: map[string]string{
|
||||
propagation.B3SampledHeader: "0",
|
||||
},
|
||||
wantSc: core.EmptySpanContext(),
|
||||
},
|
||||
}
|
||||
|
||||
var extractSingleHeader = []extractTest{
|
||||
{
|
||||
name: "sampling state defer",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7",
|
||||
},
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sampling state deny",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-0",
|
||||
},
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sampling state accept",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-1",
|
||||
},
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sampling state debug",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-d",
|
||||
},
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "with parent span id",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-1-00000000000000cd",
|
||||
},
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "with only sampling state deny",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "0",
|
||||
},
|
||||
wantSc: core.EmptySpanContext(),
|
||||
},
|
||||
}
|
||||
|
||||
var extractInvalidB3MultipleHeaders = []extractTest{
|
||||
{
|
||||
name: "trace ID length > 32",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "ab00000000000000000000000000000000",
|
||||
propagation.B3SpanIDHeader: "cd00000000000000",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "trace ID length >16 and <32",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "ab0000000000000000000000000000",
|
||||
propagation.B3SpanIDHeader: "cd00000000000000",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "trace ID length <16",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "ab0000000000",
|
||||
propagation.B3SpanIDHeader: "cd00000000000000",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "wrong span ID length",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "ab000000000000000000000000000000",
|
||||
propagation.B3SpanIDHeader: "cd0000000000000000",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "wrong sampled flag length",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "ab000000000000000000000000000000",
|
||||
propagation.B3SpanIDHeader: "cd00000000000000",
|
||||
propagation.B3SampledHeader: "10",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bogus trace ID",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "qw000000000000000000000000000000",
|
||||
propagation.B3SpanIDHeader: "cd00000000000000",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bogus span ID",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "ab000000000000000000000000000000",
|
||||
propagation.B3SpanIDHeader: "qw00000000000000",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bogus sampled flag",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "ab000000000000000000000000000000",
|
||||
propagation.B3SpanIDHeader: "cd00000000000000",
|
||||
propagation.B3SampledHeader: "d",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bogus debug flag (string)",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "ab000000000000000000000000000000",
|
||||
propagation.B3SpanIDHeader: "cd00000000000000",
|
||||
propagation.B3SampledHeader: "1",
|
||||
propagation.B3DebugFlagHeader: "d",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bogus debug flag (number)",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "ab000000000000000000000000000000",
|
||||
propagation.B3SpanIDHeader: "cd00000000000000",
|
||||
propagation.B3SampledHeader: "1",
|
||||
propagation.B3DebugFlagHeader: "10",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "upper case trace ID",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "AB000000000000000000000000000000",
|
||||
propagation.B3SpanIDHeader: "cd00000000000000",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "upper case span ID",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "ab000000000000000000000000000000",
|
||||
propagation.B3SpanIDHeader: "CD00000000000000",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "zero trace ID",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "00000000000000000000000000000000",
|
||||
propagation.B3SpanIDHeader: "cd00000000000000",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "zero span ID",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "ab000000000000000000000000000000",
|
||||
propagation.B3SpanIDHeader: "0000000000000000",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "missing span ID",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "ab000000000000000000000000000000",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "missing trace ID",
|
||||
headers: map[string]string{
|
||||
propagation.B3SpanIDHeader: "cd00000000000000",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "missing trace ID with valid single header",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-1",
|
||||
propagation.B3SpanIDHeader: "cd00000000000000",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sampled header set to 1 but trace ID and span ID are missing",
|
||||
headers: map[string]string{
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var extractInvalidB3SingleHeader = []extractTest{
|
||||
{
|
||||
name: "wrong trace ID length",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "ab00000000000000000000000000000000-cd00000000000000-1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "wrong span ID length",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "ab000000000000000000000000000000-cd0000000000000000-1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "wrong sampled state length",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "00-ab000000000000000000000000000000-cd00000000000000-01",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "wrong parent span ID length",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "ab000000000000000000000000000000-cd00000000000000-1-cd0000000000000000",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bogus trace ID",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "qw000000000000000000000000000000-cd00000000000000-1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bogus span ID",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "ab000000000000000000000000000000-qw00000000000000-1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bogus sampled flag",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "ab000000000000000000000000000000-cd00000000000000-q",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bogus parent span ID",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "ab000000000000000000000000000000-cd00000000000000-1-qw00000000000000",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "upper case trace ID",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "AB000000000000000000000000000000-cd00000000000000-1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "upper case span ID",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "ab000000000000000000000000000000-CD00000000000000-1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "upper case parent span ID",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "ab000000000000000000000000000000-cd00000000000000-1-EF00000000000000",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "zero trace ID and span ID",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "00000000000000000000000000000000-0000000000000000-1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "missing single header with valid separate headers",
|
||||
headers: map[string]string{
|
||||
propagation.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736",
|
||||
propagation.B3SpanIDHeader: "00f067aa0ba902b7",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "upper case span ID with valid separate headers",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "ab000000000000000000000000000000-CD00000000000000-1",
|
||||
propagation.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736",
|
||||
propagation.B3SpanIDHeader: "00f067aa0ba902b7",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "with sampling set to true",
|
||||
headers: map[string]string{
|
||||
propagation.B3SingleHeader: "ab000000000000000000000000000000-cd00000000000000-true",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
type injectTest struct {
|
||||
name string
|
||||
parentSc core.SpanContext
|
||||
wantHeaders map[string]string
|
||||
doNotWantHeaders []string
|
||||
}
|
||||
|
||||
var injectB3MultipleHeader = []injectTest{
|
||||
{
|
||||
name: "valid spancontext, sampled",
|
||||
parentSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
wantHeaders: map[string]string{
|
||||
propagation.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736",
|
||||
propagation.B3SpanIDHeader: "0000000000000001",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
doNotWantHeaders: []string{
|
||||
propagation.B3ParentSpanIDHeader,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid spancontext, not sampled",
|
||||
parentSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
},
|
||||
wantHeaders: map[string]string{
|
||||
propagation.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736",
|
||||
propagation.B3SpanIDHeader: "0000000000000002",
|
||||
propagation.B3SampledHeader: "0",
|
||||
},
|
||||
doNotWantHeaders: []string{
|
||||
propagation.B3ParentSpanIDHeader,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid spancontext, with unsupported bit set in traceflags",
|
||||
parentSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: 0xff,
|
||||
},
|
||||
wantHeaders: map[string]string{
|
||||
propagation.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736",
|
||||
propagation.B3SpanIDHeader: "0000000000000003",
|
||||
propagation.B3SampledHeader: "1",
|
||||
},
|
||||
doNotWantHeaders: []string{
|
||||
propagation.B3ParentSpanIDHeader,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var injectB3SingleleHeader = []injectTest{
|
||||
{
|
||||
name: "valid spancontext, sampled",
|
||||
parentSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
wantHeaders: map[string]string{
|
||||
propagation.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-0000000000000001-1",
|
||||
},
|
||||
doNotWantHeaders: []string{
|
||||
propagation.B3TraceIDHeader,
|
||||
propagation.B3SpanIDHeader,
|
||||
propagation.B3SampledHeader,
|
||||
propagation.B3ParentSpanIDHeader,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid spancontext, not sampled",
|
||||
parentSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
},
|
||||
wantHeaders: map[string]string{
|
||||
propagation.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-0000000000000002-0",
|
||||
},
|
||||
doNotWantHeaders: []string{
|
||||
propagation.B3TraceIDHeader,
|
||||
propagation.B3SpanIDHeader,
|
||||
propagation.B3SampledHeader,
|
||||
propagation.B3ParentSpanIDHeader,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid spancontext, with unsupported bit set in traceflags",
|
||||
parentSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: 0xff,
|
||||
},
|
||||
wantHeaders: map[string]string{
|
||||
propagation.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-0000000000000003-1",
|
||||
},
|
||||
doNotWantHeaders: []string{
|
||||
propagation.B3TraceIDHeader,
|
||||
propagation.B3SpanIDHeader,
|
||||
propagation.B3SampledHeader,
|
||||
propagation.B3ParentSpanIDHeader,
|
||||
},
|
||||
},
|
||||
}
|
@@ -1,155 +0,0 @@
|
||||
// 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.
|
||||
|
||||
package propagation_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
||||
"go.opentelemetry.io/otel/api/trace"
|
||||
mocktrace "go.opentelemetry.io/otel/internal/trace"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
)
|
||||
|
||||
func TestExtractB3(t *testing.T) {
|
||||
testGroup := []struct {
|
||||
singleHeader bool
|
||||
name string
|
||||
tests []extractTest
|
||||
}{
|
||||
{
|
||||
singleHeader: false,
|
||||
name: "multiple headers",
|
||||
tests: extractMultipleHeaders,
|
||||
},
|
||||
{
|
||||
singleHeader: true,
|
||||
name: "single headers",
|
||||
tests: extractSingleHeader,
|
||||
},
|
||||
{
|
||||
singleHeader: false,
|
||||
name: "invalid multiple headers",
|
||||
tests: extractInvalidB3MultipleHeaders,
|
||||
},
|
||||
{
|
||||
singleHeader: true,
|
||||
name: "invalid single headers",
|
||||
tests: extractInvalidB3SingleHeader,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tg := range testGroup {
|
||||
propagator := propagation.B3Propagator{tg.singleHeader}
|
||||
for _, tt := range tg.tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
for h, v := range tt.headers {
|
||||
req.Header.Set(h, v)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
gotSc, _ := propagator.Extract(ctx, req.Header)
|
||||
if diff := cmp.Diff(gotSc, tt.wantSc); diff != "" {
|
||||
t.Errorf("%s: %s: -got +want %s", tg.name, tt.name, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectB3(t *testing.T) {
|
||||
var id uint64
|
||||
testGroup := []struct {
|
||||
singleHeader bool
|
||||
name string
|
||||
tests []injectTest
|
||||
}{
|
||||
{
|
||||
singleHeader: false,
|
||||
name: "multiple headers",
|
||||
tests: injectB3MultipleHeader,
|
||||
},
|
||||
{
|
||||
singleHeader: true,
|
||||
name: "single headers",
|
||||
tests: injectB3SingleleHeader,
|
||||
},
|
||||
}
|
||||
|
||||
mockTracer := &mocktrace.MockTracer{
|
||||
Sampled: false,
|
||||
StartSpanID: &id,
|
||||
}
|
||||
|
||||
for _, tg := range testGroup {
|
||||
id = 0
|
||||
propagator := propagation.B3Propagator{tg.singleHeader}
|
||||
for _, tt := range tg.tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
ctx := context.Background()
|
||||
if tt.parentSc.IsValid() {
|
||||
ctx, _ = mockTracer.Start(ctx, "inject", trace.ChildOf(tt.parentSc))
|
||||
} else {
|
||||
ctx, _ = mockTracer.Start(ctx, "inject")
|
||||
}
|
||||
propagator.Inject(ctx, req.Header)
|
||||
|
||||
for h, v := range tt.wantHeaders {
|
||||
got, want := req.Header.Get(h), v
|
||||
if diff := cmp.Diff(got, want); diff != "" {
|
||||
t.Errorf("%s: %s, header=%s: -got +want %s", tg.name, tt.name, h, diff)
|
||||
}
|
||||
}
|
||||
wantOk := false
|
||||
for _, h := range tt.doNotWantHeaders {
|
||||
v, gotOk := req.Header[h]
|
||||
if diff := cmp.Diff(gotOk, wantOk); diff != "" {
|
||||
t.Errorf("%s: %s, header=%s: -got +want %s, value=%s", tg.name, tt.name, h, diff, v)
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestB3Propagator_GetAllKeys(t *testing.T) {
|
||||
propagator := propagation.B3Propagator{false}
|
||||
want := []string{
|
||||
propagation.B3TraceIDHeader,
|
||||
propagation.B3SpanIDHeader,
|
||||
propagation.B3SampledHeader,
|
||||
}
|
||||
got := propagator.GetAllKeys()
|
||||
if diff := cmp.Diff(got, want); diff != "" {
|
||||
t.Errorf("GetAllKeys: -got +want %s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestB3PropagatorWithSingleHeader_GetAllKeys(t *testing.T) {
|
||||
propagator := propagation.B3Propagator{true}
|
||||
want := []string{
|
||||
propagation.B3SingleHeader,
|
||||
}
|
||||
got := propagator.GetAllKeys()
|
||||
if diff := cmp.Diff(got, want); diff != "" {
|
||||
t.Errorf("GetAllKeys: -got +want %s", diff)
|
||||
}
|
||||
}
|
@@ -1,71 +0,0 @@
|
||||
// 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.
|
||||
|
||||
package propagation
|
||||
|
||||
import (
|
||||
"go.opentelemetry.io/otel/api/core"
|
||||
apipropagation "go.opentelemetry.io/otel/api/propagation"
|
||||
)
|
||||
|
||||
type binaryPropagator struct{}
|
||||
|
||||
var _ apipropagation.BinaryFormatPropagator = binaryPropagator{}
|
||||
|
||||
// BinaryPropagator creates a new propagator. The propagator implements
|
||||
// ToBytes and FromBytes method to transform SpanContext to/from byte array.
|
||||
func BinaryPropagator() apipropagation.BinaryFormatPropagator {
|
||||
return binaryPropagator{}
|
||||
}
|
||||
|
||||
// ToBytes implements ToBytes method of propagation.BinaryFormatPropagator.
|
||||
// It serializes core.SpanContext into a byte array.
|
||||
func (bp binaryPropagator) ToBytes(sc core.SpanContext) []byte {
|
||||
if sc == core.EmptySpanContext() {
|
||||
return nil
|
||||
}
|
||||
var b [29]byte
|
||||
copy(b[2:18], sc.TraceID[:])
|
||||
b[18] = 1
|
||||
copy(b[19:27], sc.SpanID[:])
|
||||
b[27] = 2
|
||||
b[28] = sc.TraceFlags
|
||||
return b[:]
|
||||
}
|
||||
|
||||
// FromBytes implements FromBytes method of propagation.BinaryFormatPropagator.
|
||||
// It de-serializes bytes into core.SpanContext.
|
||||
func (bp binaryPropagator) FromBytes(b []byte) (sc core.SpanContext) {
|
||||
if len(b) == 0 {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
b = b[1:]
|
||||
if len(b) >= 17 && b[0] == 0 {
|
||||
copy(sc.TraceID[:], b[1:17])
|
||||
b = b[17:]
|
||||
} else {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
if len(b) >= 9 && b[0] == 1 {
|
||||
copy(sc.SpanID[:], b[1:9])
|
||||
b = b[9:]
|
||||
}
|
||||
if len(b) >= 2 && b[0] == 2 {
|
||||
sc.TraceFlags = b[1]
|
||||
}
|
||||
if sc.IsValid() {
|
||||
return sc
|
||||
}
|
||||
return core.EmptySpanContext()
|
||||
}
|
@@ -1,170 +0,0 @@
|
||||
// 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.
|
||||
|
||||
package propagation_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
||||
"go.opentelemetry.io/otel/api/core"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
)
|
||||
|
||||
func TestExtractSpanContextFromBytes(t *testing.T) {
|
||||
traceID, _ := core.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
|
||||
spanID, _ := core.SpanIDFromHex("00f067aa0ba902b7")
|
||||
|
||||
propagator := propagation.BinaryPropagator()
|
||||
tests := []struct {
|
||||
name string
|
||||
bytes []byte
|
||||
wantSc core.SpanContext
|
||||
}{
|
||||
{
|
||||
name: "future version of the proto",
|
||||
bytes: []byte{
|
||||
0x02, 0x00, 0x4b, 0xf9, 0x2f, 0x35, 0x77, 0xb3, 0x4d, 0xa6, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36,
|
||||
0x01, 0x00, 0xf0, 0x67, 0xaa, 0x0b, 0xa9, 0x02, 0xb7,
|
||||
0x02, 0x01,
|
||||
},
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "current version with valid SpanContext and with Sampled bit set",
|
||||
bytes: []byte{
|
||||
0x00, 0x00, 0x4b, 0xf9, 0x2f, 0x35, 0x77, 0xb3, 0x4d, 0xa6, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36,
|
||||
0x01, 0x00, 0xf0, 0x67, 0xaa, 0x0b, 0xa9, 0x02, 0xb7,
|
||||
0x02, 0x01,
|
||||
},
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid SpanContext without option",
|
||||
bytes: []byte{
|
||||
0x00, 0x00, 0x4b, 0xf9, 0x2f, 0x35, 0x77, 0xb3, 0x4d, 0xa6, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36,
|
||||
0x01, 0x00, 0xf0, 0x67, 0xaa, 0x0b, 0xa9, 0x02, 0xb7,
|
||||
},
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "zero trace ID",
|
||||
bytes: []byte{
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x02, 0x01,
|
||||
},
|
||||
wantSc: core.EmptySpanContext(),
|
||||
},
|
||||
{
|
||||
name: "zero span ID",
|
||||
bytes: []byte{
|
||||
0x00, 0x00, 0x4b, 0xf9, 0x2f, 0x35, 0x77, 0xb3, 0x4d, 0xa6, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36,
|
||||
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x02, 0x01,
|
||||
},
|
||||
wantSc: core.EmptySpanContext(),
|
||||
},
|
||||
{
|
||||
name: "wrong trace ID field number",
|
||||
bytes: []byte{
|
||||
0x00, 0x01, 0x4b, 0xf9, 0x2f, 0x35, 0x77, 0xb3, 0x4d, 0xa6, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36,
|
||||
0x01, 0x00, 0xf0, 0x67, 0xaa, 0x0b, 0xa9, 0x02, 0xb7,
|
||||
},
|
||||
wantSc: core.EmptySpanContext(),
|
||||
},
|
||||
{
|
||||
name: "short byte array",
|
||||
bytes: []byte{
|
||||
0x00, 0x00, 0x4b, 0xf9, 0x2f, 0x35, 0x77, 0xb3, 0x4d,
|
||||
},
|
||||
wantSc: core.EmptySpanContext(),
|
||||
},
|
||||
{
|
||||
name: "nil byte array",
|
||||
wantSc: core.EmptySpanContext(),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotSc := propagator.FromBytes(tt.bytes)
|
||||
if diff := cmp.Diff(gotSc, tt.wantSc); diff != "" {
|
||||
t.Errorf("Deserialize SpanContext from byte array: %s: -got +want %s", tt.name, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertSpanContextToBytes(t *testing.T) {
|
||||
traceID, _ := core.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
|
||||
spanID, _ := core.SpanIDFromHex("00f067aa0ba902b7")
|
||||
|
||||
propagator := propagation.BinaryPropagator()
|
||||
tests := []struct {
|
||||
name string
|
||||
sc core.SpanContext
|
||||
bytes []byte
|
||||
}{
|
||||
{
|
||||
name: "valid SpanContext, with sampling bit set",
|
||||
sc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
bytes: []byte{
|
||||
0x00, 0x00, 0x4b, 0xf9, 0x2f, 0x35, 0x77, 0xb3, 0x4d, 0xa6, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36,
|
||||
0x01, 0x00, 0xf0, 0x67, 0xaa, 0x0b, 0xa9, 0x02, 0xb7,
|
||||
0x02, 0x01,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid SpanContext, with sampling bit cleared",
|
||||
sc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
},
|
||||
bytes: []byte{
|
||||
0x00, 0x00, 0x4b, 0xf9, 0x2f, 0x35, 0x77, 0xb3, 0x4d, 0xa6, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36,
|
||||
0x01, 0x00, 0xf0, 0x67, 0xaa, 0x0b, 0xa9, 0x02, 0xb7,
|
||||
0x02, 0x00,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid spancontext",
|
||||
sc: core.EmptySpanContext(),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotBytes := propagator.ToBytes(tt.sc)
|
||||
if diff := cmp.Diff(gotBytes, tt.bytes); diff != "" {
|
||||
t.Errorf("Serialize SpanContext to byte array: %s: -got +want %s", tt.name, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@@ -1,16 +0,0 @@
|
||||
// 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.
|
||||
|
||||
// Package propagation contains propagators for different format and carriers.
|
||||
package propagation // import "go.opentelemetry.io/otel/propagation"
|
@@ -1,196 +0,0 @@
|
||||
// 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.
|
||||
|
||||
package propagation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"go.opentelemetry.io/otel/api/core"
|
||||
dctx "go.opentelemetry.io/otel/api/distributedcontext"
|
||||
"go.opentelemetry.io/otel/api/key"
|
||||
apipropagation "go.opentelemetry.io/otel/api/propagation"
|
||||
"go.opentelemetry.io/otel/api/trace"
|
||||
)
|
||||
|
||||
const (
|
||||
supportedVersion = 0
|
||||
maxVersion = 254
|
||||
TraceparentHeader = "Traceparent"
|
||||
CorrelationContextHeader = "Correlation-Context"
|
||||
)
|
||||
|
||||
// TraceContextPropagator propagates SpanContext in W3C TraceContext format.
|
||||
type TraceContextPropagator struct{}
|
||||
|
||||
var _ apipropagation.TextFormatPropagator = TraceContextPropagator{}
|
||||
var traceCtxRegExp = regexp.MustCompile("^[0-9a-f]{2}-[a-f0-9]{32}-[a-f0-9]{16}-[a-f0-9]{2}-?")
|
||||
|
||||
func (hp TraceContextPropagator) Inject(ctx context.Context, supplier apipropagation.Supplier) {
|
||||
sc := trace.CurrentSpan(ctx).SpanContext()
|
||||
if sc.IsValid() {
|
||||
h := fmt.Sprintf("%.2x-%s-%.16x-%.2x",
|
||||
supportedVersion,
|
||||
sc.TraceIDString(),
|
||||
sc.SpanID,
|
||||
sc.TraceFlags&core.TraceFlagsSampled)
|
||||
supplier.Set(TraceparentHeader, h)
|
||||
}
|
||||
|
||||
correlationCtx := dctx.FromContext(ctx)
|
||||
firstIter := true
|
||||
var headerValueBuilder strings.Builder
|
||||
correlationCtx.Foreach(func(kv core.KeyValue) bool {
|
||||
if !firstIter {
|
||||
headerValueBuilder.WriteRune(',')
|
||||
}
|
||||
firstIter = false
|
||||
headerValueBuilder.WriteString(url.QueryEscape(strings.TrimSpace((string)(kv.Key))))
|
||||
headerValueBuilder.WriteRune('=')
|
||||
headerValueBuilder.WriteString(url.QueryEscape(strings.TrimSpace(kv.Value.Emit())))
|
||||
return true
|
||||
})
|
||||
if headerValueBuilder.Len() > 0 {
|
||||
headerString := headerValueBuilder.String()
|
||||
supplier.Set(CorrelationContextHeader, headerString)
|
||||
}
|
||||
}
|
||||
|
||||
func (hp TraceContextPropagator) Extract(
|
||||
ctx context.Context, supplier apipropagation.Supplier,
|
||||
) (core.SpanContext, dctx.Map) {
|
||||
return hp.extractSpanContext(ctx, supplier), hp.extractCorrelationCtx(ctx, supplier)
|
||||
}
|
||||
|
||||
func (hp TraceContextPropagator) extractSpanContext(
|
||||
ctx context.Context, supplier apipropagation.Supplier,
|
||||
) core.SpanContext {
|
||||
h := supplier.Get(TraceparentHeader)
|
||||
if h == "" {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
|
||||
h = strings.Trim(h, "-")
|
||||
if !traceCtxRegExp.MatchString(h) {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
|
||||
sections := strings.Split(h, "-")
|
||||
if len(sections) < 4 {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
|
||||
if len(sections[0]) != 2 {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
ver, err := hex.DecodeString(sections[0])
|
||||
if err != nil {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
version := int(ver[0])
|
||||
if version > maxVersion {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
|
||||
if version == 0 && len(sections) != 4 {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
|
||||
if len(sections[1]) != 32 {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
|
||||
var sc core.SpanContext
|
||||
|
||||
sc.TraceID, err = core.TraceIDFromHex(sections[1][:32])
|
||||
if err != nil {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
|
||||
if len(sections[2]) != 16 {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
sc.SpanID, err = core.SpanIDFromHex(sections[2])
|
||||
if err != nil {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
|
||||
if len(sections[3]) != 2 {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
opts, err := hex.DecodeString(sections[3])
|
||||
if err != nil || len(opts) < 1 || (version == 0 && opts[0] > 2) {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
sc.TraceFlags = opts[0] &^ core.TraceFlagsUnused
|
||||
|
||||
if !sc.IsValid() {
|
||||
return core.EmptySpanContext()
|
||||
}
|
||||
|
||||
return sc
|
||||
}
|
||||
|
||||
func (hp TraceContextPropagator) extractCorrelationCtx(ctx context.Context, supplier apipropagation.Supplier) dctx.Map {
|
||||
correlationContext := supplier.Get(CorrelationContextHeader)
|
||||
if correlationContext == "" {
|
||||
return dctx.NewEmptyMap()
|
||||
}
|
||||
|
||||
contextValues := strings.Split(correlationContext, ",")
|
||||
keyValues := make([]core.KeyValue, 0, len(contextValues))
|
||||
for _, contextValue := range contextValues {
|
||||
valueAndProps := strings.Split(contextValue, ";")
|
||||
if len(valueAndProps) < 1 {
|
||||
continue
|
||||
}
|
||||
nameValue := strings.Split(valueAndProps[0], "=")
|
||||
if len(nameValue) < 2 {
|
||||
continue
|
||||
}
|
||||
name, err := url.QueryUnescape(nameValue[0])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
trimmedName := strings.TrimSpace(name)
|
||||
value, err := url.QueryUnescape(nameValue[1])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
trimmedValue := strings.TrimSpace(value)
|
||||
|
||||
// TODO (skaris): properties defiend https://w3c.github.io/correlation-context/, are currently
|
||||
// just put as part of the value.
|
||||
var trimmedValueWithProps strings.Builder
|
||||
trimmedValueWithProps.WriteString(trimmedValue)
|
||||
for _, prop := range valueAndProps[1:] {
|
||||
trimmedValueWithProps.WriteRune(';')
|
||||
trimmedValueWithProps.WriteString(prop)
|
||||
}
|
||||
|
||||
keyValues = append(keyValues, key.New(trimmedName).String(trimmedValueWithProps.String()))
|
||||
}
|
||||
return dctx.NewMap(dctx.MapUpdate{
|
||||
MultiKV: keyValues,
|
||||
})
|
||||
}
|
||||
|
||||
func (hp TraceContextPropagator) GetAllKeys() []string {
|
||||
return []string{TraceparentHeader, CorrelationContextHeader}
|
||||
}
|
@@ -1,86 +0,0 @@
|
||||
package propagation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"go.opentelemetry.io/otel/api/core"
|
||||
"go.opentelemetry.io/otel/api/trace"
|
||||
mocktrace "go.opentelemetry.io/otel/internal/trace"
|
||||
)
|
||||
|
||||
func BenchmarkInject(b *testing.B) {
|
||||
var t TraceContextPropagator
|
||||
|
||||
injectSubBenchmarks(b, func(ctx context.Context, b *testing.B) {
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
t.Inject(ctx, req.Header)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func injectSubBenchmarks(b *testing.B, fn func(context.Context, *testing.B)) {
|
||||
b.Run("SampledSpanContext", func(b *testing.B) {
|
||||
var id uint64
|
||||
spanID, _ := core.SpanIDFromHex("00f067aa0ba902b7")
|
||||
traceID, _ := core.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
|
||||
|
||||
mockTracer := &mocktrace.MockTracer{
|
||||
Sampled: false,
|
||||
StartSpanID: &id,
|
||||
}
|
||||
b.ReportAllocs()
|
||||
sc := core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
}
|
||||
ctx := context.Background()
|
||||
ctx, _ = mockTracer.Start(ctx, "inject", trace.ChildOf(sc))
|
||||
fn(ctx, b)
|
||||
})
|
||||
|
||||
b.Run("WithoutSpanContext", func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
ctx := context.Background()
|
||||
fn(ctx, b)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkExtract(b *testing.B) {
|
||||
extractSubBenchmarks(b, func(b *testing.B, req *http.Request) {
|
||||
var propagator TraceContextPropagator
|
||||
ctx := context.Background()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
propagator.Extract(ctx, req.Header)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func extractSubBenchmarks(b *testing.B, fn func(*testing.B, *http.Request)) {
|
||||
b.Run("Sampled", func(b *testing.B) {
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
req.Header.Set("traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01")
|
||||
b.ReportAllocs()
|
||||
|
||||
fn(b, req)
|
||||
})
|
||||
|
||||
b.Run("BogusVersion", func(b *testing.B) {
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
req.Header.Set("traceparent", "qw-00000000000000000000000000000000-0000000000000000-01")
|
||||
b.ReportAllocs()
|
||||
fn(b, req)
|
||||
})
|
||||
|
||||
b.Run("FutureAdditionalData", func(b *testing.B) {
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
req.Header.Set("traceparent", "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-09-XYZxsf09")
|
||||
b.ReportAllocs()
|
||||
fn(b, req)
|
||||
})
|
||||
}
|
@@ -1,484 +0,0 @@
|
||||
// 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.
|
||||
|
||||
package propagation_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
||||
"go.opentelemetry.io/otel/api/core"
|
||||
dctx "go.opentelemetry.io/otel/api/distributedcontext"
|
||||
"go.opentelemetry.io/otel/api/key"
|
||||
"go.opentelemetry.io/otel/api/trace"
|
||||
mocktrace "go.opentelemetry.io/otel/internal/trace"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
)
|
||||
|
||||
var (
|
||||
traceID = mustTraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
|
||||
spanID = mustSpanIDFromHex("00f067aa0ba902b7")
|
||||
)
|
||||
|
||||
func mustTraceIDFromHex(s string) (t core.TraceID) {
|
||||
t, _ = core.TraceIDFromHex(s)
|
||||
return
|
||||
}
|
||||
|
||||
func mustSpanIDFromHex(s string) (t core.SpanID) {
|
||||
t, _ = core.SpanIDFromHex(s)
|
||||
return
|
||||
}
|
||||
|
||||
func TestExtractValidTraceContextFromHTTPReq(t *testing.T) {
|
||||
var propagator propagation.TraceContextPropagator
|
||||
tests := []struct {
|
||||
name string
|
||||
header string
|
||||
wantSc core.SpanContext
|
||||
}{
|
||||
{
|
||||
name: "valid w3cHeader",
|
||||
header: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid w3cHeader and sampled",
|
||||
header: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "future version",
|
||||
header: "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "future options with sampled bit set",
|
||||
header: "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-09",
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "future options with sampled bit cleared",
|
||||
header: "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-08",
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "future additional data",
|
||||
header: "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-09-XYZxsf09",
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid b3Header ending in dash",
|
||||
header: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01-",
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "future valid b3Header ending in dash",
|
||||
header: "01-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-09-",
|
||||
wantSc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
req.Header.Set("traceparent", tt.header)
|
||||
|
||||
ctx := context.Background()
|
||||
gotSc, _ := propagator.Extract(ctx, req.Header)
|
||||
if diff := cmp.Diff(gotSc, tt.wantSc); diff != "" {
|
||||
t.Errorf("Extract Tracecontext: %s: -got +want %s", tt.name, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractInvalidTraceContextFromHTTPReq(t *testing.T) {
|
||||
var propagator propagation.TraceContextPropagator
|
||||
wantSc := core.EmptySpanContext()
|
||||
tests := []struct {
|
||||
name string
|
||||
header string
|
||||
}{
|
||||
{
|
||||
name: "wrong version length",
|
||||
header: "0000-00000000000000000000000000000000-0000000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "wrong trace ID length",
|
||||
header: "00-ab00000000000000000000000000000000-cd00000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "wrong span ID length",
|
||||
header: "00-ab000000000000000000000000000000-cd0000000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "wrong trace flag length",
|
||||
header: "00-ab000000000000000000000000000000-cd00000000000000-0100",
|
||||
},
|
||||
{
|
||||
name: "bogus version",
|
||||
header: "qw-00000000000000000000000000000000-0000000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "bogus trace ID",
|
||||
header: "00-qw000000000000000000000000000000-cd00000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "bogus span ID",
|
||||
header: "00-ab000000000000000000000000000000-qw00000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "bogus trace flag",
|
||||
header: "00-ab000000000000000000000000000000-cd00000000000000-qw",
|
||||
},
|
||||
{
|
||||
name: "upper case version",
|
||||
header: "A0-00000000000000000000000000000000-0000000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "upper case trace ID",
|
||||
header: "00-AB000000000000000000000000000000-cd00000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "upper case span ID",
|
||||
header: "00-ab000000000000000000000000000000-CD00000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "upper case trace flag",
|
||||
header: "00-ab000000000000000000000000000000-cd00000000000000-A1",
|
||||
},
|
||||
{
|
||||
name: "zero trace ID and span ID",
|
||||
header: "00-00000000000000000000000000000000-0000000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "trace-flag unused bits set",
|
||||
header: "00-ab000000000000000000000000000000-cd00000000000000-09",
|
||||
},
|
||||
{
|
||||
name: "missing options",
|
||||
header: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7",
|
||||
},
|
||||
{
|
||||
name: "empty options",
|
||||
header: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
req.Header.Set("traceparent", tt.header)
|
||||
|
||||
ctx := context.Background()
|
||||
gotSc, _ := propagator.Extract(ctx, req.Header)
|
||||
if diff := cmp.Diff(gotSc, wantSc); diff != "" {
|
||||
t.Errorf("Extract Tracecontext: %s: -got +want %s", tt.name, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectTraceContextToHTTPReq(t *testing.T) {
|
||||
var id uint64
|
||||
mockTracer := &mocktrace.MockTracer{
|
||||
Sampled: false,
|
||||
StartSpanID: &id,
|
||||
}
|
||||
var propagator propagation.TraceContextPropagator
|
||||
tests := []struct {
|
||||
name string
|
||||
sc core.SpanContext
|
||||
wantHeader string
|
||||
}{
|
||||
{
|
||||
name: "valid spancontext, sampled",
|
||||
sc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: core.TraceFlagsSampled,
|
||||
},
|
||||
wantHeader: "00-4bf92f3577b34da6a3ce929d0e0e4736-0000000000000001-01",
|
||||
},
|
||||
{
|
||||
name: "valid spancontext, not sampled",
|
||||
sc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
},
|
||||
wantHeader: "00-4bf92f3577b34da6a3ce929d0e0e4736-0000000000000002-00",
|
||||
},
|
||||
{
|
||||
name: "valid spancontext, with unsupported bit set in traceflags",
|
||||
sc: core.SpanContext{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: 0xff,
|
||||
},
|
||||
wantHeader: "00-4bf92f3577b34da6a3ce929d0e0e4736-0000000000000003-01",
|
||||
},
|
||||
{
|
||||
name: "invalid spancontext",
|
||||
sc: core.EmptySpanContext(),
|
||||
wantHeader: "",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
ctx := context.Background()
|
||||
if tt.sc.IsValid() {
|
||||
ctx, _ = mockTracer.Start(ctx, "inject", trace.ChildOf(tt.sc))
|
||||
}
|
||||
propagator.Inject(ctx, req.Header)
|
||||
|
||||
gotHeader := req.Header.Get("traceparent")
|
||||
if diff := cmp.Diff(gotHeader, tt.wantHeader); diff != "" {
|
||||
t.Errorf("Extract Tracecontext: %s: -got +want %s", tt.name, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractValidDistributedContextFromHTTPReq(t *testing.T) {
|
||||
propagator := propagation.TraceContextPropagator{}
|
||||
tests := []struct {
|
||||
name string
|
||||
header string
|
||||
wantKVs []core.KeyValue
|
||||
}{
|
||||
{
|
||||
name: "valid w3cHeader",
|
||||
header: "key1=val1,key2=val2",
|
||||
wantKVs: []core.KeyValue{
|
||||
key.New("key1").String("val1"),
|
||||
key.New("key2").String("val2"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid w3cHeader with spaces",
|
||||
header: "key1 = val1, key2 =val2 ",
|
||||
wantKVs: []core.KeyValue{
|
||||
key.New("key1").String("val1"),
|
||||
key.New("key2").String("val2"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid w3cHeader with properties",
|
||||
header: "key1=val1,key2=val2;prop=1",
|
||||
wantKVs: []core.KeyValue{
|
||||
key.New("key1").String("val1"),
|
||||
key.New("key2").String("val2;prop=1"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid header with url-escaped comma",
|
||||
header: "key1=val1,key2=val2%2Cval3",
|
||||
wantKVs: []core.KeyValue{
|
||||
key.New("key1").String("val1"),
|
||||
key.New("key2").String("val2,val3"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid header with an invalid header",
|
||||
header: "key1=val1,key2=val2,a,val3",
|
||||
wantKVs: []core.KeyValue{
|
||||
key.New("key1").String("val1"),
|
||||
key.New("key2").String("val2"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid header with no value",
|
||||
header: "key1=,key2=val2",
|
||||
wantKVs: []core.KeyValue{
|
||||
key.New("key1").String(""),
|
||||
key.New("key2").String("val2"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
req.Header.Set("Correlation-Context", tt.header)
|
||||
|
||||
ctx := context.Background()
|
||||
_, gotCorCtx := propagator.Extract(ctx, req.Header)
|
||||
wantCorCtx := dctx.NewMap(dctx.MapUpdate{MultiKV: tt.wantKVs})
|
||||
if gotCorCtx.Len() != wantCorCtx.Len() {
|
||||
t.Errorf(
|
||||
"Got and Want CorCtx are not the same size %d != %d",
|
||||
gotCorCtx.Len(),
|
||||
wantCorCtx.Len(),
|
||||
)
|
||||
}
|
||||
totalDiff := ""
|
||||
wantCorCtx.Foreach(func(kv core.KeyValue) bool {
|
||||
val, _ := gotCorCtx.Value(kv.Key)
|
||||
diff := cmp.Diff(kv, core.KeyValue{Key: kv.Key, Value: val}, cmp.AllowUnexported(core.Value{}))
|
||||
if diff != "" {
|
||||
totalDiff += diff + "\n"
|
||||
}
|
||||
return true
|
||||
})
|
||||
if totalDiff != "" {
|
||||
t.Errorf("Extract Tracecontext: %s: -got +want %s", tt.name, totalDiff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) {
|
||||
propagator := propagation.TraceContextPropagator{}
|
||||
tests := []struct {
|
||||
name string
|
||||
header string
|
||||
}{
|
||||
{
|
||||
name: "no key values",
|
||||
header: "header1",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
req.Header.Set("Correlation-Context", tt.header)
|
||||
|
||||
ctx := context.Background()
|
||||
_, gotCorCtx := propagator.Extract(ctx, req.Header)
|
||||
if gotCorCtx.Len() != 0 {
|
||||
t.Errorf("Got and Want CorCtx are not the same size %d != %d", gotCorCtx.Len(), 0)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectCorrelationContextToHTTPReq(t *testing.T) {
|
||||
propagator := propagation.TraceContextPropagator{}
|
||||
tests := []struct {
|
||||
name string
|
||||
kvs []core.KeyValue
|
||||
wantInHeader []string
|
||||
wantedLen int
|
||||
}{
|
||||
{
|
||||
name: "two simple values",
|
||||
kvs: []core.KeyValue{
|
||||
key.New("key1").String("val1"),
|
||||
key.New("key2").String("val2"),
|
||||
},
|
||||
wantInHeader: []string{"key1=val1", "key2=val2"},
|
||||
},
|
||||
{
|
||||
name: "two values with escaped chars",
|
||||
kvs: []core.KeyValue{
|
||||
key.New("key1").String("val1,val2"),
|
||||
key.New("key2").String("val3=4"),
|
||||
},
|
||||
wantInHeader: []string{"key1=val1%2Cval2", "key2=val3%3D4"},
|
||||
},
|
||||
{
|
||||
name: "values of non-string types",
|
||||
kvs: []core.KeyValue{
|
||||
key.New("key1").Bool(true),
|
||||
key.New("key2").Int(123),
|
||||
key.New("key3").Int64(123),
|
||||
key.New("key4").Int32(123),
|
||||
key.New("key5").Uint(123),
|
||||
key.New("key6").Uint32(123),
|
||||
key.New("key7").Uint64(123),
|
||||
key.New("key8").Float64(123.567),
|
||||
key.New("key9").Float32(123.567),
|
||||
},
|
||||
wantInHeader: []string{
|
||||
"key1=true",
|
||||
"key2=123",
|
||||
"key3=123",
|
||||
"key4=123",
|
||||
"key5=123",
|
||||
"key6=123",
|
||||
"key7=123",
|
||||
"key8=123.567",
|
||||
"key9=123.567",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
ctx := dctx.WithMap(context.Background(), dctx.NewMap(dctx.MapUpdate{MultiKV: tt.kvs}))
|
||||
propagator.Inject(ctx, req.Header)
|
||||
|
||||
gotHeader := req.Header.Get("Correlation-Context")
|
||||
wantedLen := len(strings.Join(tt.wantInHeader, ","))
|
||||
if wantedLen != len(gotHeader) {
|
||||
t.Errorf(
|
||||
"%s: Inject Correlation-Context incorrect length %d != %d.", tt.name, tt.wantedLen, len(gotHeader),
|
||||
)
|
||||
}
|
||||
for _, inHeader := range tt.wantInHeader {
|
||||
if !strings.Contains(gotHeader, inHeader) {
|
||||
t.Errorf(
|
||||
"%s: Inject Correlation-Context missing part of header: %s in %s", tt.name, inHeader, gotHeader,
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraceContextPropagator_GetAllKeys(t *testing.T) {
|
||||
var propagator propagation.TraceContextPropagator
|
||||
want := []string{"Traceparent", "Correlation-Context"}
|
||||
got := propagator.GetAllKeys()
|
||||
if diff := cmp.Diff(got, want); diff != "" {
|
||||
t.Errorf("GetAllKeys: -got +want %s", diff)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user