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/b3_propagator_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

325 lines
7.6 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
import (
"testing"
"github.com/stretchr/testify/assert"
)
var (
traceID = ID{0, 0, 0, 0, 0, 0, 0, 0x7b, 0, 0, 0, 0, 0, 0, 0x1, 0xc8}
traceIDStr = "000000000000007b00000000000001c8"
spanID = SpanID{0, 0, 0, 0, 0, 0, 0, 0x7b}
spanIDStr = "000000000000007b"
)
func TestExtractMultiple(t *testing.T) {
tests := []struct {
traceID string
spanID string
parentSpanID string
sampled string
flags string
expected SpanContext
err error
}{
{
"", "", "", "0", "",
SpanContext{},
nil,
},
{
"", "", "", "", "",
SpanContext{TraceFlags: FlagsDeferred},
nil,
},
{
"", "", "", "1", "",
SpanContext{TraceFlags: FlagsSampled},
nil,
},
{
"", "", "", "", "1",
SpanContext{TraceFlags: FlagsDeferred | FlagsDebug},
nil,
},
{
"", "", "", "0", "1",
SpanContext{TraceFlags: FlagsDebug},
nil,
},
{
"", "", "", "1", "1",
SpanContext{TraceFlags: FlagsSampled | FlagsDebug},
nil,
},
{
traceIDStr, spanIDStr, "", "", "",
SpanContext{TraceID: traceID, SpanID: spanID, TraceFlags: FlagsDeferred},
nil,
},
{
traceIDStr, spanIDStr, "", "0", "",
SpanContext{TraceID: traceID, SpanID: spanID},
nil,
},
// Ensure backwards compatibility.
{
traceIDStr, spanIDStr, "", "false", "",
SpanContext{TraceID: traceID, SpanID: spanID},
nil,
},
{
traceIDStr, spanIDStr, "", "1", "",
SpanContext{TraceID: traceID, SpanID: spanID, TraceFlags: FlagsSampled},
nil,
},
// Ensure backwards compatibility.
{
traceIDStr, spanIDStr, "", "true", "",
SpanContext{TraceID: traceID, SpanID: spanID, TraceFlags: FlagsSampled},
nil,
},
{
traceIDStr, spanIDStr, "", "a", "",
empty,
errInvalidSampledHeader,
},
{
traceIDStr, spanIDStr, "", "1", "1",
SpanContext{TraceID: traceID, SpanID: spanID, TraceFlags: FlagsSampled | FlagsDebug},
nil,
},
// Invalid flags are discarded.
{
traceIDStr, spanIDStr, "", "1", "invalid",
SpanContext{TraceID: traceID, SpanID: spanID, TraceFlags: FlagsSampled},
nil,
},
// Support short trace IDs.
{
"00000000000001c8", spanIDStr, "", "0", "",
SpanContext{
TraceID: ID{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1, 0xc8},
SpanID: spanID,
},
nil,
},
{
"00000000000001c", spanIDStr, "", "0", "",
empty,
errInvalidTraceIDHeader,
},
{
"00000000000001c80", spanIDStr, "", "0", "",
empty,
errInvalidTraceIDHeader,
},
{
traceIDStr[:len(traceIDStr)-2], spanIDStr, "", "0", "",
empty,
errInvalidTraceIDHeader,
},
{
traceIDStr + "0", spanIDStr, "", "0", "",
empty,
errInvalidTraceIDHeader,
},
{
traceIDStr, "00000000000001c", "", "0", "",
empty,
errInvalidSpanIDHeader,
},
{
traceIDStr, "00000000000001c80", "", "0", "",
empty,
errInvalidSpanIDHeader,
},
{
traceIDStr, "", "", "0", "",
empty,
errInvalidScope,
},
{
"", spanIDStr, "", "0", "",
empty,
errInvalidScope,
},
{
"", "", spanIDStr, "0", "",
empty,
errInvalidScopeParent,
},
{
traceIDStr, spanIDStr, "00000000000001c8", "0", "",
SpanContext{TraceID: traceID, SpanID: spanID},
nil,
},
{
traceIDStr, spanIDStr, "00000000000001c", "0", "",
empty,
errInvalidParentSpanIDHeader,
},
{
traceIDStr, spanIDStr, "00000000000001c80", "0", "",
empty,
errInvalidParentSpanIDHeader,
},
}
for _, test := range tests {
actual, err := extractMultiple(
test.traceID,
test.spanID,
test.parentSpanID,
test.sampled,
test.flags,
)
info := []interface{}{
"trace ID: %q, span ID: %q, parent span ID: %q, sampled: %q, flags: %q",
test.traceID,
test.spanID,
test.parentSpanID,
test.sampled,
test.flags,
}
if !assert.Equal(t, test.err, err, info...) {
continue
}
assert.Equal(t, test.expected, actual, info...)
}
}
func TestExtractSingle(t *testing.T) {
tests := []struct {
header string
expected SpanContext
err error
}{
{"0", SpanContext{}, nil},
{"1", SpanContext{TraceFlags: FlagsSampled}, nil},
{"d", SpanContext{TraceFlags: FlagsDebug}, nil},
{"a", empty, errInvalidSampledByte},
{"3", empty, errInvalidSampledByte},
{"000000000000007b", empty, errInvalidScope},
{"000000000000007b00000000000001c8", empty, errInvalidScope},
// Support short trace IDs.
{
"00000000000001c8-000000000000007b",
SpanContext{
TraceID: ID{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1, 0xc8},
SpanID: spanID,
TraceFlags: FlagsDeferred,
},
nil,
},
{
"000000000000007b00000000000001c8-000000000000007b",
SpanContext{
TraceID: traceID,
SpanID: spanID,
TraceFlags: FlagsDeferred,
},
nil,
},
{
"000000000000007b00000000000001c8-000000000000007b-",
empty,
errInvalidSampledByte,
},
{
"000000000000007b00000000000001c8-000000000000007b-3",
empty,
errInvalidSampledByte,
},
{
"000000000000007b00000000000001c8-000000000000007b-00000000000001c8",
empty,
errInvalidScopeParentSingle,
},
{
"000000000000007b00000000000001c8-000000000000007b-1",
SpanContext{TraceID: traceID, SpanID: spanID, TraceFlags: FlagsSampled},
nil,
},
// ParentSpanID is discarded, but should still restult in a parsable
// header.
{
"000000000000007b00000000000001c8-000000000000007b-1-00000000000001c8",
SpanContext{TraceID: traceID, SpanID: spanID, TraceFlags: FlagsSampled},
nil,
},
{
"000000000000007b00000000000001c8-000000000000007b-1-00000000000001c",
empty,
errInvalidParentSpanIDValue,
},
{"", empty, errEmptyContext},
}
for _, test := range tests {
actual, err := extractSingle(test.header)
if !assert.Equal(t, test.err, err, "header: %s", test.header) {
continue
}
assert.Equal(t, test.expected, actual, "header: %s", test.header)
}
}
func TestB3EncodingOperations(t *testing.T) {
encodings := []B3Encoding{
B3MultipleHeader,
B3SingleHeader,
B3Unspecified,
}
// Test for overflow (or something really unexpected).
for i, e := range encodings {
for j := i + 1; j < i+len(encodings); j++ {
o := encodings[j%len(encodings)]
assert.False(t, e == o, "%v == %v", e, o)
}
}
// B3Unspecified is a special case, it supports only itself, but is
// supported by everything.
assert.True(t, B3Unspecified.supports(B3Unspecified))
for _, e := range encodings[:len(encodings)-1] {
assert.False(t, B3Unspecified.supports(e), e)
assert.True(t, e.supports(B3Unspecified), e)
}
// Skip the special case for B3Unspecified.
for i, e := range encodings[:len(encodings)-1] {
// Everything should support itself.
assert.True(t, e.supports(e))
for j := i + 1; j < i+len(encodings); j++ {
o := encodings[j%len(encodings)]
// Any "or" combination should be supportive of an operand.
assert.True(t, (e | o).supports(e), "(%[0]v|%[1]v).supports(%[0]v)", e, o)
// Bitmasks should be unique.
assert.False(t, o.supports(e), "%v.supports(%v)", o, e)
}
}
// B3Encoding.supports should be more inclusive than equality.
all := ^B3Unspecified
for _, e := range encodings {
assert.True(t, all.supports(e))
}
}