1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-04 09:43:23 +02:00
opentelemetry-go/api/propagation/binary_propagator_test.go
Krzesimir Nowak 6b4acf47b8 Reorganize propagation code (shrink PR 381) (#444)
* Rename distributedcontext package to correlation

Correlation is the name we agreed upon.

* Move trace propagators to api/trace

The trace propagators tests had to be moved to a testtrace subpackage
to avoid import cycles between api/trace and internal/trace.

Needed to shut up golint about stutter in trace.TraceContext -
TraceContext is a name of a W3C spec, so this stutter is
expected. It's certainly still better than golint's suggestion of
having trace.Context.

* Rename api/propagators to api/propagation

This package will not contain any propagators in the long run, just
the interface definitions.

Co-authored-by: Joshua MacDonald <jmacd@users.noreply.github.com>
2020-01-28 10:13:46 -08:00

171 lines
4.8 KiB
Go

// 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/api/propagation"
)
func TestExtractSpanContextFromBytes(t *testing.T) {
traceID, _ := core.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
spanID, _ := core.SpanIDFromHex("00f067aa0ba902b7")
propagator := propagation.Binary()
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.Binary()
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)
}
})
}
}