Debian Codegraph @

bridge/opencensus/test/bridge_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package test
5
6 import (
7 "testing"
8
9 octrace "go.opencensus.io/trace"
10
11 "go.opentelemetry.io/otel/attribute"
12 ocbridge "go.opentelemetry.io/otel/bridge/opencensus"
13 "go.opentelemetry.io/otel/bridge/opencensus/internal"
14 "go.opentelemetry.io/otel/codes"
15 sdktrace "go.opentelemetry.io/otel/sdk/trace"
16 "go.opentelemetry.io/otel/sdk/trace/tracetest"
17 "go.opentelemetry.io/otel/trace"
18 )
19
20 func TestMixedAPIs(t *testing.T) {
21 sr := tracetest.NewSpanRecorder()
22 tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))
23 tracer := tp.Tracer("mixedapitracer")
24 ocbridge.InstallTraceBridge(ocbridge.WithTracerProvider(tp))
25
26 func() {
27 ctx := t.Context()
28 var ocspan1 *octrace.Span
29 ctx, ocspan1 = octrace.StartSpan(ctx, "OpenCensusSpan1")
30 defer ocspan1.End()
31
32 var otspan1 trace.Span
33 ctx, otspan1 = tracer.Start(ctx, "OpenTelemetrySpan1")
34 defer otspan1.End()
35
36 var ocspan2 *octrace.Span
37 ctx, ocspan2 = octrace.StartSpan(ctx, "OpenCensusSpan2")
38 defer ocspan2.End()
39
40 var otspan2 trace.Span
41 _, otspan2 = tracer.Start(ctx, "OpenTelemetrySpan2")
42 defer otspan2.End()
43 }()
44
45 spans := sr.Ended()
46
47 if len(spans) != 4 {
48 for _, span := range spans {
49 t.Logf("Span: %s", span.Name())
50 }
51 t.Fatalf("Got %d spans, expected %d.", len(spans), 4)
52 }
53
54 var parent trace.SpanContext
55 for i := len(spans) - 1; i >= 0; i-- {
56 // Verify that OpenCensus spans and OpenTelemetry spans have each
57 // other as parents.
58 if psid := spans[i].Parent().SpanID(); psid != parent.SpanID() {
59 t.Errorf("Span %v had parent %v. Expected %v", spans[i].Name(), psid, parent.SpanID())
60 }
61 parent = spans[i].SpanContext()
62 }
63 }
64
65 func TestStartOptions(t *testing.T) {
66 sr := tracetest.NewSpanRecorder()
67 tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))
68 ocbridge.InstallTraceBridge(ocbridge.WithTracerProvider(tp))
69
70 ctx := t.Context()
71 _, span := octrace.StartSpan(ctx, "OpenCensusSpan", octrace.WithSpanKind(octrace.SpanKindClient))
72 span.End()
73
74 spans := sr.Ended()
75
76 if len(spans) != 1 {
77 t.Fatalf("Got %d spans, expected %d", len(spans), 1)
78 }
79
80 if spans[0].SpanKind() != trace.SpanKindClient {
81 t.Errorf("Got span kind %v, expected %d", spans[0].SpanKind(), trace.SpanKindClient)
82 }
83 }
84
85 func TestStartSpanWithRemoteParent(t *testing.T) {
86 sr := tracetest.NewSpanRecorder()
87 tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))
88 ocbridge.InstallTraceBridge(ocbridge.WithTracerProvider(tp))
89 tracer := tp.Tracer("remoteparent")
90
91 ctx := t.Context()
92 ctx, parent := tracer.Start(ctx, "OpenTelemetrySpan1")
93
94 _, span := octrace.StartSpanWithRemoteParent(
95 ctx,
96 "OpenCensusSpan",
97 ocbridge.OTelSpanContextToOC(parent.SpanContext()),
98 )
99 span.End()
100
101 spans := sr.Ended()
102
103 if len(spans) != 1 {
104 t.Fatalf("Got %d spans, expected %d", len(spans), 1)
105 }
106
107 if psid := spans[0].Parent().SpanID(); psid != parent.SpanContext().SpanID() {
108 t.Errorf("Span %v, had parent %v. Expected %d", spans[0].Name(), psid, parent.SpanContext().SpanID())
109 }
110 }
111
112 func TestToFromContext(t *testing.T) {
113 sr := tracetest.NewSpanRecorder()
114 tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))
115 ocbridge.InstallTraceBridge(ocbridge.WithTracerProvider(tp))
116 tracer := tp.Tracer("tofromcontext")
117
118 func() {
119 ctx := t.Context()
120
121 _, otSpan1 := tracer.Start(ctx, "OpenTelemetrySpan1")
122 defer otSpan1.End()
123
124 // Use NewContext instead of the context from Start
125 ctx = octrace.NewContext(ctx, internal.NewSpan(otSpan1))
126
127 ctx, _ = tracer.Start(ctx, "OpenTelemetrySpan2")
128
129 // Get the opentelemetry span using the OpenCensus FromContext, and end it
130 otSpan2 := octrace.FromContext(ctx)
131 defer otSpan2.End()
132 }()
133
134 spans := sr.Ended()
135
136 if len(spans) != 2 {
137 t.Fatalf("Got %d spans, expected %d.", len(spans), 2)
138 }
139
140 var parent trace.SpanContext
141 for i := len(spans) - 1; i >= 0; i-- {
142 // Verify that OpenCensus spans and OpenTelemetry spans have each
143 // other as parents.
144 if psid := spans[i].Parent().SpanID(); psid != parent.SpanID() {
145 t.Errorf("Span %v had parent %v. Expected %v", spans[i].Name(), psid, parent.SpanID())
146 }
147 parent = spans[i].SpanContext()
148 }
149 }
150
151 func TestIsRecordingEvents(t *testing.T) {
152 sr := tracetest.NewSpanRecorder()
153 tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))
154 ocbridge.InstallTraceBridge(ocbridge.WithTracerProvider(tp))
155
156 ctx := t.Context()
157 _, ocspan := octrace.StartSpan(ctx, "OpenCensusSpan1")
158 if !ocspan.IsRecordingEvents() {
159 t.Errorf("Got %v, expected true", ocspan.IsRecordingEvents())
160 }
161 }
162
163 func attrsMap(s []attribute.KeyValue) map[attribute.Key]attribute.Value {
164 m := make(map[attribute.Key]attribute.Value, len(s))
165 for _, a := range s {
166 m[a.Key] = a.Value
167 }
168 return m
169 }
170
171 func TestSetThings(t *testing.T) {
172 sr := tracetest.NewSpanRecorder()
173 tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))
174 ocbridge.InstallTraceBridge(ocbridge.WithTracerProvider(tp))
175
176 ctx := t.Context()
177 _, ocspan := octrace.StartSpan(ctx, "OpenCensusSpan1")
178 ocspan.SetName("span-foo")
179 ocspan.SetStatus(octrace.Status{Code: 1, Message: "foo"})
180 ocspan.AddAttributes(
181 octrace.BoolAttribute("bool", true),
182 octrace.Int64Attribute("int64", 12345),
183 octrace.Float64Attribute("float64", 12.345),
184 octrace.StringAttribute("string", "stringval"),
185 )
186 ocspan.Annotate(
187 []octrace.Attribute{octrace.StringAttribute("string", "annotateval")},
188 "annotate",
189 )
190 ocspan.Annotatef(
191 []octrace.Attribute{
192 octrace.Int64Attribute("int64", 12345),
193 octrace.Float64Attribute("float64", 12.345),
194 },
195 "annotate%d", 67890,
196 )
197 ocspan.AddMessageSendEvent(123, 456, 789)
198 ocspan.AddMessageReceiveEvent(246, 135, 369)
199 ocspan.End()
200
201 spans := sr.Ended()
202
203 if len(spans) != 1 {
204 t.Fatalf("Got %d spans, expected %d.", len(spans), 1)
205 }
206 s := spans[0]
207
208 if s.Name() != "span-foo" {
209 t.Errorf("Got name %v, expected span-foo", s.Name())
210 }
211
212 if s.Status().Code != codes.Error {
213 t.Errorf("Got code %v, expected %v", s.Status().Code, codes.Error)
214 }
215
216 if s.Status().Description != "foo" {
217 t.Errorf("Got code %v, expected foo", s.Status().Description)
218 }
219
220 attrs := attrsMap(s.Attributes())
221 if v := attrs[attribute.Key("bool")]; !v.AsBool() {
222 t.Errorf("Got attributes[bool] %v, expected true", v.AsBool())
223 }
224 if v := attrs[attribute.Key("int64")]; v.AsInt64() != 12345 {
225 t.Errorf("Got attributes[int64] %v, expected 12345", v.AsInt64())
226 }
227 if v := attrs[attribute.Key("float64")]; v.AsFloat64() != 12.345 {
228 t.Errorf("Got attributes[float64] %v, expected 12.345", v.AsFloat64())
229 }
230 if v := attrs[attribute.Key("string")]; v.AsString() != "stringval" {
231 t.Errorf("Got attributes[string] %v, expected stringval", v.AsString())
232 }
233
234 if len(s.Events()) != 4 {
235 t.Fatalf("Got len(events) = %v, expected 4", len(s.Events()))
236 }
237 annotateEvent := s.Events()[0]
238 aeAttrs := attrsMap(annotateEvent.Attributes)
239 annotatefEvent := s.Events()[1]
240 afeAttrs := attrsMap(annotatefEvent.Attributes)
241 sendEvent := s.Events()[2]
242 receiveEvent := s.Events()[3]
243 if v := aeAttrs[attribute.Key("string")]; v.AsString() != "annotateval" {
244 t.Errorf("Got annotateEvent.Attributes[string] = %v, expected annotateval", v.AsString())
245 }
246 if annotateEvent.Name != "annotate" {
247 t.Errorf("Got annotateEvent.Name = %v, expected annotate", annotateEvent.Name)
248 }
249 if v := afeAttrs[attribute.Key("int64")]; v.AsInt64() != 12345 {
250 t.Errorf("Got annotatefEvent.Attributes[int64] = %v, expected 12345", v.AsInt64())
251 }
252 if v := afeAttrs[attribute.Key("float64")]; v.AsFloat64() != 12.345 {
253 t.Errorf("Got annotatefEvent.Attributes[float64] = %v, expected 12.345", v.AsFloat64())
254 }
255 if annotatefEvent.Name != "annotate67890" {
256 t.Errorf("Got annotatefEvent.Name = %v, expected annotate67890", annotatefEvent.Name)
257 }
258 if v := aeAttrs[attribute.Key("string")]; v.AsString() != "annotateval" {
259 t.Errorf("Got annotateEvent.Attributes[string] = %v, expected annotateval", v.AsString())
260 }
261 seAttrs := attrsMap(sendEvent.Attributes)
262 reAttrs := attrsMap(receiveEvent.Attributes)
263 if sendEvent.Name != internal.MessageSendEvent {
264 t.Errorf("Got sendEvent.Name = %v, expected message send", sendEvent.Name)
265 }
266 if v := seAttrs[internal.UncompressedKey]; v.AsInt64() != 456 {
267 t.Errorf("Got sendEvent.Attributes[uncompressedKey] = %v, expected 456", v.AsInt64())
268 }
269 if v := seAttrs[internal.CompressedKey]; v.AsInt64() != 789 {
270 t.Errorf("Got sendEvent.Attributes[compressedKey] = %v, expected 789", v.AsInt64())
271 }
272 if receiveEvent.Name != internal.MessageReceiveEvent {
273 t.Errorf("Got receiveEvent.Name = %v, expected message receive", receiveEvent.Name)
274 }
275 if v := reAttrs[internal.UncompressedKey]; v.AsInt64() != 135 {
276 t.Errorf("Got receiveEvent.Attributes[uncompressedKey] = %v, expected 135", v.AsInt64())
277 }
278 if v := reAttrs[internal.CompressedKey]; v.AsInt64() != 369 {
279 t.Errorf("Got receiveEvent.Attributes[compressedKey] = %v, expected 369", v.AsInt64())
280 }
281 }