2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-07-19 22:29:42 +02:00
|
|
|
//
|
|
|
|
// 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-11-07 00:13:31 +02:00
|
|
|
package trace
|
2019-07-19 22:29:42 +02:00
|
|
|
|
|
|
|
import (
|
2021-06-17 17:48:34 +02:00
|
|
|
"bytes"
|
2021-07-22 20:39:27 +02:00
|
|
|
"context"
|
2019-07-19 22:29:42 +02:00
|
|
|
"testing"
|
2020-10-21 23:30:28 +02:00
|
|
|
|
2021-11-13 18:35:04 +02:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-07-22 20:39:27 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2019-07-19 22:29:42 +02:00
|
|
|
)
|
|
|
|
|
2021-06-17 17:48:34 +02:00
|
|
|
func TestSpanContextIsValid(t *testing.T) {
|
2019-08-05 18:58:51 +02:00
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
2020-10-09 04:58:56 +02:00
|
|
|
tid TraceID
|
|
|
|
sid SpanID
|
2019-08-05 18:58:51 +02:00
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
2019-08-22 20:40:15 +02:00
|
|
|
name: "SpanContext.IsValid() returns true if sc has both an Trace ID and Span ID",
|
2019-10-28 19:05:06 +02:00
|
|
|
tid: [16]byte{1},
|
|
|
|
sid: [8]byte{42},
|
2019-08-05 18:58:51 +02:00
|
|
|
want: true,
|
|
|
|
}, {
|
2019-08-22 20:40:15 +02:00
|
|
|
name: "SpanContext.IsValid() returns false if sc has neither an Trace ID nor Span ID",
|
2020-10-09 04:58:56 +02:00
|
|
|
tid: TraceID([16]byte{}),
|
2019-10-28 19:05:06 +02:00
|
|
|
sid: [8]byte{},
|
2019-08-05 18:58:51 +02:00
|
|
|
want: false,
|
|
|
|
}, {
|
2019-08-22 20:40:15 +02:00
|
|
|
name: "SpanContext.IsValid() returns false if sc has a Span ID but not a Trace ID",
|
2020-10-09 04:58:56 +02:00
|
|
|
tid: TraceID([16]byte{}),
|
2019-10-28 19:05:06 +02:00
|
|
|
sid: [8]byte{42},
|
2019-08-05 18:58:51 +02:00
|
|
|
want: false,
|
2019-08-22 20:40:15 +02:00
|
|
|
}, {
|
|
|
|
name: "SpanContext.IsValid() returns false if sc has a Trace ID but not a Span ID",
|
2020-10-09 04:58:56 +02:00
|
|
|
tid: TraceID([16]byte{1}),
|
2019-10-28 19:05:06 +02:00
|
|
|
sid: [8]byte{},
|
2019-08-22 20:40:15 +02:00
|
|
|
want: false,
|
2019-08-05 18:58:51 +02:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
2020-10-09 04:58:56 +02:00
|
|
|
sc := SpanContext{
|
2021-03-09 18:17:29 +02:00
|
|
|
traceID: testcase.tid,
|
|
|
|
spanID: testcase.sid,
|
2019-08-05 18:58:51 +02:00
|
|
|
}
|
|
|
|
have := sc.IsValid()
|
|
|
|
if have != testcase.want {
|
|
|
|
t.Errorf("Want: %v, but have: %v", testcase.want, have)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:48:34 +02:00
|
|
|
func TestSpanContextEqual(t *testing.T) {
|
|
|
|
a := SpanContext{
|
|
|
|
traceID: [16]byte{1},
|
|
|
|
spanID: [8]byte{42},
|
|
|
|
}
|
|
|
|
|
|
|
|
b := SpanContext{
|
|
|
|
traceID: [16]byte{1},
|
|
|
|
spanID: [8]byte{42},
|
|
|
|
}
|
|
|
|
|
|
|
|
c := SpanContext{
|
|
|
|
traceID: [16]byte{2},
|
|
|
|
spanID: [8]byte{42},
|
|
|
|
}
|
|
|
|
|
2022-05-19 22:15:07 +02:00
|
|
|
if !a.Equal(b) {
|
2021-06-17 17:48:34 +02:00
|
|
|
t.Error("Want: true, but have: false")
|
|
|
|
}
|
|
|
|
|
2022-05-19 22:15:07 +02:00
|
|
|
if a.Equal(c) {
|
2021-06-17 17:48:34 +02:00
|
|
|
t.Error("Want: false, but have: true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpanContextIsSampled(t *testing.T) {
|
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
|
|
|
tf TraceFlags
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "SpanContext.IsSampled() returns false if sc is not sampled",
|
|
|
|
want: false,
|
|
|
|
}, {
|
|
|
|
name: "SpanContext.IsSampled() returns true if sc is sampled",
|
|
|
|
tf: FlagsSampled,
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
|
|
|
sc := SpanContext{
|
|
|
|
traceFlags: testcase.tf,
|
|
|
|
}
|
|
|
|
|
|
|
|
have := sc.IsSampled()
|
|
|
|
|
|
|
|
if have != testcase.want {
|
|
|
|
t.Errorf("Want: %v, but have: %v", testcase.want, have)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpanContextIsRemote(t *testing.T) {
|
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
|
|
|
remote bool
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "SpanContext.IsRemote() returns false if sc is not remote",
|
|
|
|
want: false,
|
|
|
|
}, {
|
|
|
|
name: "SpanContext.IsRemote() returns true if sc is remote",
|
|
|
|
remote: true,
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
|
|
|
sc := SpanContext{
|
|
|
|
remote: testcase.remote,
|
|
|
|
}
|
|
|
|
|
|
|
|
have := sc.IsRemote()
|
|
|
|
|
|
|
|
if have != testcase.want {
|
|
|
|
t.Errorf("Want: %v, but have: %v", testcase.want, have)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpanContextMarshalJSON(t *testing.T) {
|
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
|
|
|
tid TraceID
|
|
|
|
sid SpanID
|
|
|
|
tstate TraceState
|
|
|
|
tflags TraceFlags
|
|
|
|
isRemote bool
|
|
|
|
want []byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "SpanContext.MarshalJSON() returns json with partial data",
|
|
|
|
tid: [16]byte{1},
|
|
|
|
sid: [8]byte{42},
|
|
|
|
want: []byte(`{"TraceID":"01000000000000000000000000000000","SpanID":"2a00000000000000","TraceFlags":"00","TraceState":"","Remote":false}`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SpanContext.MarshalJSON() returns json with full data",
|
|
|
|
tid: [16]byte{1},
|
|
|
|
sid: [8]byte{42},
|
|
|
|
tflags: FlagsSampled,
|
|
|
|
isRemote: true,
|
|
|
|
tstate: TraceState{list: []member{
|
|
|
|
{Key: "foo", Value: "1"},
|
|
|
|
}},
|
|
|
|
want: []byte(`{"TraceID":"01000000000000000000000000000000","SpanID":"2a00000000000000","TraceFlags":"01","TraceState":"foo=1","Remote":true}`),
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
|
|
|
sc := SpanContext{
|
|
|
|
traceID: testcase.tid,
|
|
|
|
spanID: testcase.sid,
|
|
|
|
traceFlags: testcase.tflags,
|
|
|
|
traceState: testcase.tstate,
|
|
|
|
remote: testcase.isRemote,
|
|
|
|
}
|
|
|
|
have, err := sc.MarshalJSON()
|
|
|
|
if err != nil {
|
2021-10-11 20:47:32 +02:00
|
|
|
t.Errorf("Marshaling failed: %v", err)
|
2021-06-17 17:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(have, testcase.want) {
|
|
|
|
t.Errorf("Want: %v, but have: %v", string(testcase.want), string(have))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpanIDFromHex(t *testing.T) {
|
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
|
|
|
hex string
|
|
|
|
sid SpanID
|
|
|
|
valid bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Valid SpanID",
|
|
|
|
sid: SpanID([8]byte{42}),
|
|
|
|
hex: "2a00000000000000",
|
|
|
|
valid: true,
|
|
|
|
}, {
|
|
|
|
name: "Invalid SpanID with invalid length",
|
|
|
|
hex: "80f198ee56343ba",
|
|
|
|
valid: false,
|
|
|
|
}, {
|
|
|
|
name: "Invalid SpanID with invalid char",
|
|
|
|
hex: "80f198ee563433g7",
|
|
|
|
valid: false,
|
|
|
|
}, {
|
|
|
|
name: "Invalid SpanID with uppercase",
|
|
|
|
hex: "80f198ee53ba86F7",
|
|
|
|
valid: false,
|
|
|
|
}, {
|
|
|
|
name: "Invalid SpanID with zero value",
|
|
|
|
hex: "0000000000000000",
|
|
|
|
valid: false,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
|
|
|
sid, err := SpanIDFromHex(testcase.hex)
|
|
|
|
|
|
|
|
if testcase.valid && err != nil {
|
|
|
|
t.Errorf("Expected SpanID %s to be valid but end with error %s", testcase.hex, err.Error())
|
|
|
|
} else if !testcase.valid && err == nil {
|
|
|
|
t.Errorf("Expected SpanID %s to be invalid but end no error", testcase.hex)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sid != testcase.sid {
|
|
|
|
t.Errorf("Want: %v, but have: %v", testcase.sid, sid)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 08:01:33 +02:00
|
|
|
func TestIsValidFromHex(t *testing.T) {
|
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
|
|
|
hex string
|
2020-10-09 04:58:56 +02:00
|
|
|
tid TraceID
|
2019-10-23 08:01:33 +02:00
|
|
|
valid bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Valid TraceID",
|
2020-10-09 04:58:56 +02:00
|
|
|
tid: TraceID([16]byte{128, 241, 152, 238, 86, 52, 59, 168, 100, 254, 139, 42, 87, 211, 239, 247}),
|
2019-10-23 08:01:33 +02:00
|
|
|
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,
|
2021-06-17 17:48:34 +02:00
|
|
|
}, {
|
|
|
|
name: "Invalid TraceID with zero value",
|
|
|
|
hex: "00000000000000000000000000000000",
|
|
|
|
valid: false,
|
2019-10-23 08:01:33 +02:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
2020-10-09 04:58:56 +02:00
|
|
|
tid, err := TraceIDFromHex(testcase.hex)
|
2019-10-23 08:01:33 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:48:34 +02:00
|
|
|
func TestSpanContextHasTraceID(t *testing.T) {
|
2019-07-19 22:29:42 +02:00
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
2020-10-09 04:58:56 +02:00
|
|
|
tid TraceID
|
2019-07-19 22:29:42 +02:00
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
2019-08-22 20:40:15 +02:00
|
|
|
name: "SpanContext.HasTraceID() returns true if both Low and High are nonzero",
|
2020-10-09 04:58:56 +02:00
|
|
|
tid: TraceID([16]byte{1}),
|
2019-07-19 22:29:42 +02:00
|
|
|
want: true,
|
|
|
|
}, {
|
2019-08-22 20:40:15 +02:00
|
|
|
name: "SpanContext.HasTraceID() returns false if neither Low nor High are nonzero",
|
2020-10-09 04:58:56 +02:00
|
|
|
tid: TraceID{},
|
2019-07-19 22:29:42 +02:00
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
|
|
|
//proto: func (sc SpanContext) HasTraceID() bool{}
|
2021-03-09 18:17:29 +02:00
|
|
|
sc := SpanContext{traceID: testcase.tid}
|
2019-07-19 22:29:42 +02:00
|
|
|
have := sc.HasTraceID()
|
|
|
|
if have != testcase.want {
|
|
|
|
t.Errorf("Want: %v, but have: %v", testcase.want, have)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:48:34 +02:00
|
|
|
func TestSpanContextHasSpanID(t *testing.T) {
|
2019-07-19 22:29:42 +02:00
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
2020-10-09 04:58:56 +02:00
|
|
|
sc SpanContext
|
2019-07-19 22:29:42 +02:00
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
2019-08-22 20:40:15 +02:00
|
|
|
name: "SpanContext.HasSpanID() returns true if self.SpanID != 0",
|
2021-03-09 18:17:29 +02:00
|
|
|
sc: SpanContext{spanID: [8]byte{42}},
|
2019-07-19 22:29:42 +02:00
|
|
|
want: true,
|
|
|
|
}, {
|
2019-08-22 20:40:15 +02:00
|
|
|
name: "SpanContext.HasSpanID() returns false if self.SpanID == 0",
|
2020-10-09 04:58:56 +02:00
|
|
|
sc: SpanContext{},
|
2019-07-19 22:29:42 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 19:21:42 +02:00
|
|
|
func TestTraceFlagsIsSampled(t *testing.T) {
|
2019-07-19 22:29:42 +02:00
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
2021-04-05 19:21:42 +02:00
|
|
|
tf TraceFlags
|
2020-04-17 00:42:48 +02:00
|
|
|
want bool
|
2019-07-19 22:29:42 +02:00
|
|
|
}{
|
|
|
|
{
|
2020-04-17 00:42:48 +02:00
|
|
|
name: "sampled",
|
2021-04-05 19:21:42 +02:00
|
|
|
tf: FlagsSampled,
|
2020-04-17 00:42:48 +02:00
|
|
|
want: true,
|
2019-07-19 22:29:42 +02:00
|
|
|
}, {
|
2020-07-08 01:38:52 +02:00
|
|
|
name: "unused bits are ignored, still not sampled",
|
2021-04-05 19:21:42 +02:00
|
|
|
tf: ^FlagsSampled,
|
2020-07-08 01:38:52 +02:00
|
|
|
want: false,
|
|
|
|
}, {
|
|
|
|
name: "unused bits are ignored, still sampled",
|
2021-04-05 19:21:42 +02:00
|
|
|
tf: FlagsSampled | ^FlagsSampled,
|
2020-04-17 00:42:48 +02:00
|
|
|
want: true,
|
|
|
|
}, {
|
|
|
|
name: "not sampled/default",
|
|
|
|
want: false,
|
2019-07-19 22:29:42 +02:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
2021-04-05 19:21:42 +02:00
|
|
|
have := testcase.tf.IsSampled()
|
|
|
|
if have != testcase.want {
|
|
|
|
t.Errorf("Want: %v, but have: %v", testcase.want, have)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTraceFlagsWithSampled(t *testing.T) {
|
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
|
|
|
start TraceFlags
|
|
|
|
sample bool
|
|
|
|
want TraceFlags
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "sampled unchanged",
|
|
|
|
start: FlagsSampled,
|
|
|
|
want: FlagsSampled,
|
|
|
|
sample: true,
|
|
|
|
}, {
|
|
|
|
name: "become sampled",
|
|
|
|
want: FlagsSampled,
|
|
|
|
sample: true,
|
|
|
|
}, {
|
|
|
|
name: "unused bits are ignored, still not sampled",
|
|
|
|
start: ^FlagsSampled,
|
|
|
|
want: ^FlagsSampled,
|
|
|
|
sample: false,
|
|
|
|
}, {
|
|
|
|
name: "unused bits are ignored, becomes sampled",
|
|
|
|
start: ^FlagsSampled,
|
|
|
|
want: FlagsSampled | ^FlagsSampled,
|
|
|
|
sample: true,
|
|
|
|
}, {
|
|
|
|
name: "not sampled/default",
|
|
|
|
sample: false,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
|
|
|
have := testcase.start.WithSampled(testcase.sample)
|
2019-07-19 22:29:42 +02:00
|
|
|
if have != testcase.want {
|
2020-04-17 00:42:48 +02:00
|
|
|
t.Errorf("Want: %v, but have: %v", testcase.want, have)
|
2019-07-19 22:29:42 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 00:42:48 +02:00
|
|
|
func TestStringTraceID(t *testing.T) {
|
2019-07-19 22:29:42 +02:00
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
2020-10-09 04:58:56 +02:00
|
|
|
tid TraceID
|
2019-07-19 22:29:42 +02:00
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
2020-04-17 00:42:48 +02:00
|
|
|
name: "TraceID.String returns string representation of self.TraceID values > 0",
|
2020-10-09 04:58:56 +02:00
|
|
|
tid: TraceID([16]byte{255}),
|
2020-04-17 00:42:48 +02:00
|
|
|
want: "ff000000000000000000000000000000",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "TraceID.String returns string representation of self.TraceID values == 0",
|
2020-10-09 04:58:56 +02:00
|
|
|
tid: TraceID([16]byte{}),
|
2020-04-17 00:42:48 +02:00
|
|
|
want: "00000000000000000000000000000000",
|
2019-07-19 22:29:42 +02:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
2020-04-17 00:42:48 +02:00
|
|
|
//proto: func (t TraceID) String() string {}
|
|
|
|
have := testcase.tid.String()
|
2019-07-19 22:29:42 +02:00
|
|
|
if have != testcase.want {
|
|
|
|
t.Errorf("Want: %s, but have: %s", testcase.want, have)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 19:42:47 +02:00
|
|
|
|
2020-04-17 00:42:48 +02:00
|
|
|
func TestStringSpanID(t *testing.T) {
|
2019-08-02 19:42:47 +02:00
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
2020-10-09 04:58:56 +02:00
|
|
|
sid SpanID
|
2020-04-17 00:42:48 +02:00
|
|
|
want string
|
2019-08-02 19:42:47 +02:00
|
|
|
}{
|
|
|
|
{
|
2020-04-17 00:42:48 +02:00
|
|
|
name: "SpanID.String returns string representation of self.SpanID values > 0",
|
2020-10-09 04:58:56 +02:00
|
|
|
sid: SpanID([8]byte{255}),
|
2020-04-17 00:42:48 +02:00
|
|
|
want: "ff00000000000000",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SpanID.String returns string representation of self.SpanID values == 0",
|
2020-10-09 04:58:56 +02:00
|
|
|
sid: SpanID([8]byte{}),
|
2020-04-17 00:42:48 +02:00
|
|
|
want: "0000000000000000",
|
2019-08-02 19:42:47 +02:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
2020-04-17 00:42:48 +02:00
|
|
|
//proto: func (t TraceID) String() string {}
|
|
|
|
have := testcase.sid.String()
|
2019-08-02 19:42:47 +02:00
|
|
|
if have != testcase.want {
|
2020-04-17 00:42:48 +02:00
|
|
|
t.Errorf("Want: %s, but have: %s", testcase.want, have)
|
2019-08-02 19:42:47 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 04:58:56 +02:00
|
|
|
|
|
|
|
func TestValidateSpanKind(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in SpanKind
|
|
|
|
want SpanKind
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
SpanKindUnspecified,
|
|
|
|
SpanKindInternal,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
|
|
|
|
SpanKindInternal,
|
|
|
|
SpanKindInternal,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
|
|
|
|
SpanKindServer,
|
|
|
|
SpanKindServer,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
|
|
|
|
SpanKindClient,
|
|
|
|
SpanKindClient,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SpanKindProducer,
|
|
|
|
SpanKindProducer,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SpanKindConsumer,
|
|
|
|
SpanKindConsumer,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
if got := ValidateSpanKind(test.in); got != test.want {
|
|
|
|
t.Errorf("ValidateSpanKind(%#v) = %#v, want %#v", test.in, got, test.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpanKindString(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in SpanKind
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
SpanKindUnspecified,
|
|
|
|
"unspecified",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
|
|
|
|
SpanKindInternal,
|
|
|
|
"internal",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
|
|
|
|
SpanKindServer,
|
|
|
|
"server",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
|
|
|
|
SpanKindClient,
|
|
|
|
"client",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SpanKindProducer,
|
|
|
|
"producer",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SpanKindConsumer,
|
|
|
|
"consumer",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
if got := test.in.String(); got != test.want {
|
|
|
|
t.Errorf("%#v.String() = %#v, want %#v", test.in, got, test.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 23:30:28 +02:00
|
|
|
|
2020-12-21 23:11:48 +02:00
|
|
|
func assertSpanContextEqual(got SpanContext, want SpanContext) bool {
|
2021-03-09 18:17:29 +02:00
|
|
|
return got.spanID == want.spanID &&
|
|
|
|
got.traceID == want.traceID &&
|
|
|
|
got.traceFlags == want.traceFlags &&
|
2021-03-18 18:05:37 +02:00
|
|
|
got.remote == want.remote &&
|
2021-05-24 16:53:26 +02:00
|
|
|
got.traceState.String() == want.traceState.String()
|
2020-12-21 23:11:48 +02:00
|
|
|
}
|
|
|
|
|
2021-03-09 18:17:29 +02:00
|
|
|
func TestNewSpanContext(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
config SpanContextConfig
|
|
|
|
expectedSpanContext SpanContext
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Complete SpanContext",
|
|
|
|
config: SpanContextConfig{
|
|
|
|
TraceID: TraceID([16]byte{1}),
|
|
|
|
SpanID: SpanID([8]byte{42}),
|
|
|
|
TraceFlags: 0x1,
|
2021-05-24 16:53:26 +02:00
|
|
|
TraceState: TraceState{list: []member{
|
|
|
|
{"foo", "bar"},
|
2021-03-09 18:17:29 +02:00
|
|
|
}},
|
|
|
|
},
|
|
|
|
expectedSpanContext: SpanContext{
|
|
|
|
traceID: TraceID([16]byte{1}),
|
|
|
|
spanID: SpanID([8]byte{42}),
|
|
|
|
traceFlags: 0x1,
|
2021-05-24 16:53:26 +02:00
|
|
|
traceState: TraceState{list: []member{
|
|
|
|
{"foo", "bar"},
|
2021-03-09 18:17:29 +02:00
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Empty SpanContext",
|
|
|
|
config: SpanContextConfig{},
|
|
|
|
expectedSpanContext: SpanContext{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Partial SpanContext",
|
|
|
|
config: SpanContextConfig{
|
|
|
|
TraceID: TraceID([16]byte{1}),
|
|
|
|
SpanID: SpanID([8]byte{42}),
|
|
|
|
},
|
|
|
|
expectedSpanContext: SpanContext{
|
|
|
|
traceID: TraceID([16]byte{1}),
|
|
|
|
spanID: SpanID([8]byte{42}),
|
|
|
|
traceFlags: 0x0,
|
|
|
|
traceState: TraceState{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
sctx := NewSpanContext(tc.config)
|
|
|
|
if !assertSpanContextEqual(sctx, tc.expectedSpanContext) {
|
|
|
|
t.Fatalf("%s: Unexpected context created: %s", tc.name, cmp.Diff(sctx, tc.expectedSpanContext))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpanContextDerivation(t *testing.T) {
|
|
|
|
from := SpanContext{}
|
|
|
|
to := SpanContext{traceID: TraceID([16]byte{1})}
|
|
|
|
|
|
|
|
modified := from.WithTraceID(to.TraceID())
|
|
|
|
if !assertSpanContextEqual(modified, to) {
|
|
|
|
t.Fatalf("WithTraceID: Unexpected context created: %s", cmp.Diff(modified, to))
|
|
|
|
}
|
|
|
|
|
|
|
|
from = to
|
|
|
|
to.spanID = SpanID([8]byte{42})
|
|
|
|
|
|
|
|
modified = from.WithSpanID(to.SpanID())
|
|
|
|
if !assertSpanContextEqual(modified, to) {
|
|
|
|
t.Fatalf("WithSpanID: Unexpected context created: %s", cmp.Diff(modified, to))
|
|
|
|
}
|
|
|
|
|
|
|
|
from = to
|
|
|
|
to.traceFlags = 0x13
|
|
|
|
|
|
|
|
modified = from.WithTraceFlags(to.TraceFlags())
|
|
|
|
if !assertSpanContextEqual(modified, to) {
|
|
|
|
t.Fatalf("WithTraceFlags: Unexpected context created: %s", cmp.Diff(modified, to))
|
|
|
|
}
|
|
|
|
|
|
|
|
from = to
|
2021-05-24 16:53:26 +02:00
|
|
|
to.traceState = TraceState{list: []member{{"foo", "bar"}}}
|
2021-03-09 18:17:29 +02:00
|
|
|
|
|
|
|
modified = from.WithTraceState(to.TraceState())
|
|
|
|
if !assertSpanContextEqual(modified, to) {
|
|
|
|
t.Fatalf("WithTraceState: Unexpected context created: %s", cmp.Diff(modified, to))
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 20:39:27 +02:00
|
|
|
|
|
|
|
func TestLinkFromContext(t *testing.T) {
|
|
|
|
k1v1 := attribute.String("key1", "value1")
|
|
|
|
spanCtx := SpanContext{traceID: TraceID([16]byte{1}), remote: true}
|
|
|
|
|
|
|
|
receiverCtx := ContextWithRemoteSpanContext(context.Background(), spanCtx)
|
|
|
|
link := LinkFromContext(receiverCtx, k1v1)
|
|
|
|
|
|
|
|
if !assertSpanContextEqual(link.SpanContext, spanCtx) {
|
|
|
|
t.Fatalf("LinkFromContext: Unexpected context created: %s", cmp.Diff(link.SpanContext, spanCtx))
|
|
|
|
}
|
|
|
|
assert.Equal(t, link.Attributes[0], k1v1)
|
|
|
|
}
|