1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-14 10:13:10 +02:00
opentelemetry-go/api/trace/span_context_test.go
Tyler Yahn c506e99b01
Fix B3 propagator and add tests (#882)
* Correct B3 propagators and add tests

* Break up external integration and internal unit tests

* Add changes to Changelog.

* Update Changelog with PR number

* Fix lint issues

* Update trace flags

Add a new "not sampled" mask to complement the existing "sampled" one.

Rename `FlagsUnused` to `FlagsUnset`.

Add documentation for each of the flags to help understand their
purpose.

* Update extractSingle to support unset sampling

* Update existing tests to appropriately use FlagsUnset

* Remove bogus debug flag test

The B3 specification states "Debug is encoded as `X-B3-Flags: 1`. Absent
or any other values can be ignored", so testing of other values should
not result in an error.

* B3 Extract now supports parsing both headers

Remove test cases that would fail if the fallback header format was
expected to not be used.

* Feedback

* Switch to bitmask inject encoding field

Add the B3Encoding and valid HTTP based values. Change the B3 propagator
to use these bitmask fields to specify the inject encoding it will
propagate.

* Add comments

* Migrate B3 integration tests to existing testtrace

* Update comment

* Benchmark invalid B3 injects as well

* Update trace flags

Add a FlagsDebug and FlagsDeferred to track the B3 trace state.

Add helper methods to the SpanContext to check the debug and deferred
bit of the trace flags.

Update SpanContext.IsSampled to return if the sampling decision is to
sample rather than if the sample bit is set. This means that if the
debug bit is also set it will return true.

* Revert SpanContext.IsSampled back

* Add comment to b3 test data generation

* Update Changelog

* Fix trace flag name in Changelog

* Fix Changelog formatting

* Update Changelog

* Remove valid check at start of B3 injectg

This check makes sample only headers not propagate.

* Update B3 inject integration tests

Use the passed SpanContext and check directly the span ID.

* Update B3 integration tests

Run update checked SpanID to match sent.

Add tests to validate sample only transmissions and debug flag support.

* Rename injectTest parentSc to sc

This is no longer the parent.

* Update GetAllKeys for B3

* Un-Export the B3 headers

The B3SingleHeader name will conflict with the upcoming change to prefix
the SingleHeader encoding with "B3". There are a few options to address
this conflict, but in the end we do not need to be exporting these
values. They are duplicates of the OpenZipkin package and users should
use those.

* Rename B3 encodings and move support method to B3Encoding

Include a `B3` prefix to scope the encoding names.

Move the related support method to the B3Encoding itself, instead of the
B3 propagator.

Add tests to provide a sanity check for encoding bitmasks.

* Update span_context_test tests

Update test name to better describe how unused bits have no affect on
the sampling decision. Include the inverse of this test as well: not
sampled but has unused bits.

* Use named const for Single Header decoding widths

* Update api/trace/b3_propagator.go

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
2020-07-07 16:38:52 -07:00

257 lines
6.4 KiB
Go

// Copyright The 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 trace_test
import (
"testing"
"go.opentelemetry.io/otel/api/trace"
)
func TestIsValid(t *testing.T) {
for _, testcase := range []struct {
name string
tid trace.ID
sid trace.SpanID
want bool
}{
{
name: "SpanContext.IsValid() returns true if sc has both an Trace ID and Span ID",
tid: [16]byte{1},
sid: [8]byte{42},
want: true,
}, {
name: "SpanContext.IsValid() returns false if sc has neither an Trace ID nor Span ID",
tid: trace.ID([16]byte{}),
sid: [8]byte{},
want: false,
}, {
name: "SpanContext.IsValid() returns false if sc has a Span ID but not a Trace ID",
tid: trace.ID([16]byte{}),
sid: [8]byte{42},
want: false,
}, {
name: "SpanContext.IsValid() returns false if sc has a Trace ID but not a Span ID",
tid: trace.ID([16]byte{1}),
sid: [8]byte{},
want: false,
},
} {
t.Run(testcase.name, func(t *testing.T) {
sc := trace.SpanContext{
TraceID: testcase.tid,
SpanID: testcase.sid,
}
have := sc.IsValid()
if have != testcase.want {
t.Errorf("Want: %v, but have: %v", testcase.want, have)
}
})
}
}
func TestIsValidFromHex(t *testing.T) {
for _, testcase := range []struct {
name string
hex string
tid trace.ID
valid bool
}{
{
name: "Valid TraceID",
tid: trace.ID([16]byte{128, 241, 152, 238, 86, 52, 59, 168, 100, 254, 139, 42, 87, 211, 239, 247}),
hex: "80f198ee56343ba864fe8b2a57d3eff7",
valid: true,
}, {
name: "Invalid TraceID with invalid length",
hex: "80f198ee56343ba864fe8b2a57d3eff",
valid: false,
}, {
name: "Invalid TraceID with invalid char",
hex: "80f198ee56343ba864fe8b2a57d3efg7",
valid: false,
}, {
name: "Invalid TraceID with uppercase",
hex: "80f198ee56343ba864fe8b2a57d3efF7",
valid: false,
},
} {
t.Run(testcase.name, func(t *testing.T) {
tid, err := trace.IDFromHex(testcase.hex)
if testcase.valid && err != nil {
t.Errorf("Expected TraceID %s to be valid but end with error %s", testcase.hex, err.Error())
}
if !testcase.valid && err == nil {
t.Errorf("Expected TraceID %s to be invalid but end no error", testcase.hex)
}
if tid != testcase.tid {
t.Errorf("Want: %v, but have: %v", testcase.tid, tid)
}
})
}
}
func TestHasTraceID(t *testing.T) {
for _, testcase := range []struct {
name string
tid trace.ID
want bool
}{
{
name: "SpanContext.HasTraceID() returns true if both Low and High are nonzero",
tid: trace.ID([16]byte{1}),
want: true,
}, {
name: "SpanContext.HasTraceID() returns false if neither Low nor High are nonzero",
tid: trace.ID{},
want: false,
},
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (sc SpanContext) HasTraceID() bool{}
sc := trace.SpanContext{TraceID: testcase.tid}
have := sc.HasTraceID()
if have != testcase.want {
t.Errorf("Want: %v, but have: %v", testcase.want, have)
}
})
}
}
func TestHasSpanID(t *testing.T) {
for _, testcase := range []struct {
name string
sc trace.SpanContext
want bool
}{
{
name: "SpanContext.HasSpanID() returns true if self.SpanID != 0",
sc: trace.SpanContext{SpanID: [8]byte{42}},
want: true,
}, {
name: "SpanContext.HasSpanID() returns false if self.SpanID == 0",
sc: trace.SpanContext{},
want: false,
},
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (sc SpanContext) HasSpanID() bool {}
have := testcase.sc.HasSpanID()
if have != testcase.want {
t.Errorf("Want: %v, but have: %v", testcase.want, have)
}
})
}
}
func TestSpanContextIsSampled(t *testing.T) {
for _, testcase := range []struct {
name string
sc trace.SpanContext
want bool
}{
{
name: "sampled",
sc: trace.SpanContext{
TraceID: trace.ID([16]byte{1}),
TraceFlags: trace.FlagsSampled,
},
want: true,
}, {
name: "unused bits are ignored, still not sampled",
sc: trace.SpanContext{
TraceID: trace.ID([16]byte{1}),
TraceFlags: ^trace.FlagsSampled,
},
want: false,
}, {
name: "unused bits are ignored, still sampled",
sc: trace.SpanContext{
TraceID: trace.ID([16]byte{1}),
TraceFlags: trace.FlagsSampled | ^trace.FlagsSampled,
},
want: true,
}, {
name: "not sampled/default",
sc: trace.SpanContext{TraceID: trace.ID{}},
want: false,
},
} {
t.Run(testcase.name, func(t *testing.T) {
have := testcase.sc.IsSampled()
if have != testcase.want {
t.Errorf("Want: %v, but have: %v", testcase.want, have)
}
})
}
}
func TestStringTraceID(t *testing.T) {
for _, testcase := range []struct {
name string
tid trace.ID
want string
}{
{
name: "TraceID.String returns string representation of self.TraceID values > 0",
tid: trace.ID([16]byte{255}),
want: "ff000000000000000000000000000000",
},
{
name: "TraceID.String returns string representation of self.TraceID values == 0",
tid: trace.ID([16]byte{}),
want: "00000000000000000000000000000000",
},
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (t TraceID) String() string {}
have := testcase.tid.String()
if have != testcase.want {
t.Errorf("Want: %s, but have: %s", testcase.want, have)
}
})
}
}
func TestStringSpanID(t *testing.T) {
for _, testcase := range []struct {
name string
sid trace.SpanID
want string
}{
{
name: "SpanID.String returns string representation of self.SpanID values > 0",
sid: trace.SpanID([8]byte{255}),
want: "ff00000000000000",
},
{
name: "SpanID.String returns string representation of self.SpanID values == 0",
sid: trace.SpanID([8]byte{}),
want: "0000000000000000",
},
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (t TraceID) String() string {}
have := testcase.sid.String()
if have != testcase.want {
t.Errorf("Want: %s, but have: %s", testcase.want, have)
}
})
}
}