2020-11-16 20:35:25 +02:00
|
|
|
// 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.
|
|
|
|
|
2021-08-06 17:44:07 +02:00
|
|
|
package oc2otel
|
2020-11-16 20:35:25 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
octrace "go.opencensus.io/trace"
|
2021-08-06 17:44:07 +02:00
|
|
|
"go.opencensus.io/trace/tracestate"
|
2020-11-16 20:35:25 +02:00
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
)
|
|
|
|
|
2021-08-06 17:44:07 +02:00
|
|
|
func TestSpanContextConversion(t *testing.T) {
|
2020-11-16 20:35:25 +02:00
|
|
|
for _, tc := range []struct {
|
|
|
|
description string
|
|
|
|
input octrace.SpanContext
|
|
|
|
expected trace.SpanContext
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "empty",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "sampled",
|
|
|
|
input: octrace.SpanContext{
|
|
|
|
TraceID: octrace.TraceID([16]byte{1}),
|
|
|
|
SpanID: octrace.SpanID([8]byte{2}),
|
|
|
|
TraceOptions: octrace.TraceOptions(0x1),
|
|
|
|
},
|
2021-03-09 18:17:29 +02:00
|
|
|
expected: trace.NewSpanContext(trace.SpanContextConfig{
|
2020-11-16 20:35:25 +02:00
|
|
|
TraceID: trace.TraceID([16]byte{1}),
|
|
|
|
SpanID: trace.SpanID([8]byte{2}),
|
|
|
|
TraceFlags: trace.FlagsSampled,
|
2021-03-09 18:17:29 +02:00
|
|
|
}),
|
2020-11-16 20:35:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "not sampled",
|
|
|
|
input: octrace.SpanContext{
|
|
|
|
TraceID: octrace.TraceID([16]byte{1}),
|
|
|
|
SpanID: octrace.SpanID([8]byte{2}),
|
|
|
|
TraceOptions: octrace.TraceOptions(0),
|
|
|
|
},
|
2021-03-09 18:17:29 +02:00
|
|
|
expected: trace.NewSpanContext(trace.SpanContextConfig{
|
2020-11-16 20:35:25 +02:00
|
|
|
TraceID: trace.TraceID([16]byte{1}),
|
|
|
|
SpanID: trace.SpanID([8]byte{2}),
|
2021-03-09 18:17:29 +02:00
|
|
|
}),
|
2020-11-16 20:35:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "trace state is ignored",
|
|
|
|
input: octrace.SpanContext{
|
|
|
|
TraceID: octrace.TraceID([16]byte{1}),
|
|
|
|
SpanID: octrace.SpanID([8]byte{2}),
|
|
|
|
Tracestate: &tracestate.Tracestate{},
|
|
|
|
},
|
2021-03-09 18:17:29 +02:00
|
|
|
expected: trace.NewSpanContext(trace.SpanContextConfig{
|
2020-11-16 20:35:25 +02:00
|
|
|
TraceID: trace.TraceID([16]byte{1}),
|
|
|
|
SpanID: trace.SpanID([8]byte{2}),
|
2021-03-09 18:17:29 +02:00
|
|
|
}),
|
2020-11-16 20:35:25 +02:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
2021-08-06 17:44:07 +02:00
|
|
|
output := SpanContext(tc.input)
|
2021-03-09 18:17:29 +02:00
|
|
|
if !output.Equal(tc.expected) {
|
2023-04-12 02:28:13 +02:00
|
|
|
t.Fatalf("Got %+v spancontext, expected %+v.", output, tc.expected)
|
2020-11-16 20:35:25 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|