Debian Codegraph @

bridge/opencensus/internal/tracer_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package internal_test
5
6 import (
7 "context"
8 "testing"
9
10 octrace "go.opencensus.io/trace"
11
12 "go.opentelemetry.io/otel/bridge/opencensus/internal"
13 "go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel"
14 "go.opentelemetry.io/otel/bridge/opencensus/internal/otel2oc"
15 "go.opentelemetry.io/otel/trace"
16 "go.opentelemetry.io/otel/trace/embedded"
17 "go.opentelemetry.io/otel/trace/noop"
18 )
19
20 type handler struct{ err error }
21
22 func (h *handler) Handle(e error) { h.err = e }
23
24 func withHandler() (*handler, func()) {
25 h := new(handler)
26 original := internal.Handle
27 internal.Handle = h.Handle
28 return h, func() { internal.Handle = original }
29 }
30
31 type tracer struct {
32 embedded.Tracer
33
34 ctx context.Context
35 name string
36 opts []trace.SpanStartOption
37 }
38
39 func (t *tracer) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
40 t.ctx, t.name, t.opts = ctx, name, opts
41 sub := noop.NewTracerProvider().Tracer("testing")
42 return sub.Start(ctx, name, opts...)
43 }
44
45 type ctxKey string
46
47 func TestTracerStartSpan(t *testing.T) {
48 h, restore := withHandler()
49 defer restore()
50
51 otelTracer := &tracer{}
52 ocTracer := internal.NewTracer(otelTracer)
53
54 ctx := context.WithValue(t.Context(), ctxKey("key"), "value")
55 name := "testing span"
56 ocTracer.StartSpan(ctx, name, octrace.WithSpanKind(octrace.SpanKindClient))
57 if h.err != nil {
58 t.Fatalf("OC tracer.StartSpan errored: %v", h.err)
59 }
60
61 if otelTracer.ctx != ctx {
62 t.Error("OTel tracer.Start called with wrong context")
63 }
64 if otelTracer.name != name {
65 t.Error("OTel tracer.Start called with wrong name")
66 }
67 sk := trace.SpanKindClient
68 c := trace.NewSpanStartConfig(otelTracer.opts...)
69 if c.SpanKind() != sk {
70 t.Errorf("OTel tracer.Start called with wrong options: %#v", c)
71 }
72 }
73
74 func TestTracerStartSpanReportsErrors(t *testing.T) {
75 h, restore := withHandler()
76 defer restore()
77
78 ocTracer := internal.NewTracer(&tracer{})
79 ocTracer.StartSpan(t.Context(), "", octrace.WithSampler(octrace.AlwaysSample()))
80 if h.err == nil {
81 t.Error("OC tracer.StartSpan no error when converting Sampler")
82 }
83 }
84
85 func TestTracerStartSpanWithRemoteParent(t *testing.T) {
86 otelTracer := new(tracer)
87 ocTracer := internal.NewTracer(otelTracer)
88 sc := octrace.SpanContext{TraceID: [16]byte{1}, SpanID: [8]byte{1}}
89 converted := oc2otel.SpanContext(sc).WithRemote(true)
90
91 ocTracer.StartSpanWithRemoteParent(t.Context(), "", sc)
92
93 got := trace.SpanContextFromContext(otelTracer.ctx)
94 if !got.Equal(converted) {
95 t.Error("tracer.StartSpanWithRemoteParent failed to set remote parent")
96 }
97 }
98
99 func TestTracerFromContext(t *testing.T) {
100 sc := trace.NewSpanContext(trace.SpanContextConfig{
101 TraceID: [16]byte{1},
102 SpanID: [8]byte{1},
103 })
104 ctx := trace.ContextWithSpanContext(t.Context(), sc)
105
106 tracer := noop.NewTracerProvider().Tracer("TestTracerFromContext")
107 // Test using the fact that the No-Op span will propagate a span context .
108 ctx, _ = tracer.Start(ctx, "test")
109
110 got := internal.NewTracer(tracer).FromContext(ctx).SpanContext()
111 // Do not test the conversion, only the propagation.
112 want := otel2oc.SpanContext(sc)
113 if got != want {
114 t.Errorf("tracer.FromContext returned wrong context: %#v", got)
115 }
116 }
117
118 func TestTracerNewContext(t *testing.T) {
119 sc := trace.NewSpanContext(trace.SpanContextConfig{
120 TraceID: [16]byte{1},
121 SpanID: [8]byte{1},
122 })
123 ctx := trace.ContextWithSpanContext(t.Context(), sc)
124
125 tracer := noop.NewTracerProvider().Tracer("TestTracerNewContext")
126 // Test using the fact that the No-Op span will propagate a span context .
127 _, s := tracer.Start(ctx, "test")
128
129 ocTracer := internal.NewTracer(tracer)
130 ctx = ocTracer.NewContext(t.Context(), internal.NewSpan(s))
131 got := trace.SpanContextFromContext(ctx)
132
133 if !got.Equal(sc) {
134 t.Error("tracer.NewContext did not attach Span to context")
135 }
136 }
137
138 type differentSpan struct {
139 octrace.SpanInterface
140 }
141
142 func (*differentSpan) String() string { return "testing span" }
143
144 func TestTracerNewContextErrors(t *testing.T) {
145 h, restore := withHandler()
146 defer restore()
147
148 ocTracer := internal.NewTracer(&tracer{})
149 ocSpan := octrace.NewSpan(&differentSpan{})
150 ocTracer.NewContext(t.Context(), ocSpan)
151 if h.err == nil {
152 t.Error("tracer.NewContext did not error for unrecognized span")
153 }
154 }