| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package oc2otel |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"go.opencensus.io/plugin/ochttp/propagation/tracecontext" |
| 11 |
octrace "go.opencensus.io/trace" |
| 12 |
"go.opencensus.io/trace/tracestate" |
| 13 |
|
| 14 |
"go.opentelemetry.io/otel/bridge/opencensus/internal/otel2oc" |
| 15 |
"go.opentelemetry.io/otel/trace" |
| 16 |
) |
| 17 |
|
| 18 |
func TestSpanContextConversion(t *testing.T) { |
| 19 |
tsOc, _ := tracestate.New(nil, |
| 20 |
tracestate.Entry{Key: "key1", Value: "value1"}, |
| 21 |
tracestate.Entry{Key: "key2", Value: "value2"}, |
| 22 |
) |
| 23 |
tsOtel := trace.TraceState{} |
| 24 |
tsOtel, _ = tsOtel.Insert("key2", "value2") |
| 25 |
tsOtel, _ = tsOtel.Insert("key1", "value1") |
| 26 |
|
| 27 |
httpFormatOc := &tracecontext.HTTPFormat{} |
| 28 |
|
| 29 |
for _, tc := range []struct { |
| 30 |
description string |
| 31 |
input octrace.SpanContext |
| 32 |
expected trace.SpanContext |
| 33 |
expectedTracestate string |
| 34 |
}{ |
| 35 |
{ |
| 36 |
description: "empty", |
| 37 |
}, |
| 38 |
{ |
| 39 |
description: "sampled", |
| 40 |
input: octrace.SpanContext{ |
| 41 |
TraceID: octrace.TraceID([16]byte{1}), |
| 42 |
SpanID: octrace.SpanID([8]byte{2}), |
| 43 |
TraceOptions: octrace.TraceOptions(0x1), |
| 44 |
}, |
| 45 |
expected: trace.NewSpanContext(trace.SpanContextConfig{ |
| 46 |
TraceID: trace.TraceID([16]byte{1}), |
| 47 |
SpanID: trace.SpanID([8]byte{2}), |
| 48 |
TraceFlags: trace.FlagsSampled, |
| 49 |
}), |
| 50 |
}, |
| 51 |
{ |
| 52 |
description: "not sampled", |
| 53 |
input: octrace.SpanContext{ |
| 54 |
TraceID: octrace.TraceID([16]byte{1}), |
| 55 |
SpanID: octrace.SpanID([8]byte{2}), |
| 56 |
TraceOptions: octrace.TraceOptions(0), |
| 57 |
}, |
| 58 |
expected: trace.NewSpanContext(trace.SpanContextConfig{ |
| 59 |
TraceID: trace.TraceID([16]byte{1}), |
| 60 |
SpanID: trace.SpanID([8]byte{2}), |
| 61 |
}), |
| 62 |
}, |
| 63 |
{ |
| 64 |
description: "trace state should be propagated", |
| 65 |
input: octrace.SpanContext{ |
| 66 |
TraceID: octrace.TraceID([16]byte{1}), |
| 67 |
SpanID: octrace.SpanID([8]byte{2}), |
| 68 |
Tracestate: tsOc, |
| 69 |
}, |
| 70 |
expected: trace.NewSpanContext(trace.SpanContextConfig{ |
| 71 |
TraceID: trace.TraceID([16]byte{1}), |
| 72 |
SpanID: trace.SpanID([8]byte{2}), |
| 73 |
TraceState: tsOtel, |
| 74 |
}), |
| 75 |
expectedTracestate: "key1=value1,key2=value2", |
| 76 |
}, |
| 77 |
} { |
| 78 |
t.Run(tc.description, func(t *testing.T) { |
| 79 |
output := SpanContext(tc.input) |
| 80 |
assert.Equal(t, tc.expected, output) |
| 81 |
|
| 82 |
// Ensure the otel tracestate and oc tracestate has the same header output |
| 83 |
_, ts := httpFormatOc.SpanContextToHeaders(tc.input) |
| 84 |
assert.Equal(t, tc.expectedTracestate, ts) |
| 85 |
assert.Equal(t, tc.expectedTracestate, tc.expected.TraceState().String()) |
| 86 |
|
| 87 |
// The reverse conversion should yield the original input |
| 88 |
input := otel2oc.SpanContext(output) |
| 89 |
assert.Equal(t, tc.input, input) |
| 90 |
}) |
| 91 |
} |
| 92 |
} |