Debian Codegraph @

bridge/opencensus/internal/span_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package internal_test
5
6 import (
7 "testing"
8
9 octrace "go.opencensus.io/trace"
10
11 "go.opentelemetry.io/otel/attribute"
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/codes"
16 "go.opentelemetry.io/otel/trace"
17 )
18
19 type span struct {
20 trace.Span
21
22 recording bool
23 ended bool
24 sc trace.SpanContext
25 name string
26 sCode codes.Code
27 sMsg string
28 attrs []attribute.KeyValue
29 eName string
30 eOpts []trace.EventOption
31 links []trace.Link
32 }
33
34 func (s *span) IsRecording() bool { return s.recording }
35 func (s *span) End(...trace.SpanEndOption) { s.ended = true }
36 func (s *span) SpanContext() trace.SpanContext { return s.sc }
37 func (s *span) SetName(n string) { s.name = n }
38 func (s *span) SetStatus(c codes.Code, d string) { s.sCode, s.sMsg = c, d }
39 func (s *span) SetAttributes(a ...attribute.KeyValue) { s.attrs = a }
40 func (s *span) AddEvent(n string, o ...trace.EventOption) { s.eName, s.eOpts = n, o }
41 func (s *span) AddLink(l trace.Link) { s.links = append(s.links, l) }
42
43 func TestSpanIsRecordingEvents(t *testing.T) {
44 s := &span{recording: true}
45 ocS := internal.NewSpan(s)
46 if !ocS.IsRecordingEvents() {
47 t.Errorf("span.IsRecordingEvents() = false, want true")
48 }
49 s.recording = false
50 if ocS.IsRecordingEvents() {
51 t.Errorf("span.IsRecordingEvents() = true, want false")
52 }
53 }
54
55 func TestSpanEnd(t *testing.T) {
56 s := new(span)
57 ocS := internal.NewSpan(s)
58 if s.ended {
59 t.Fatal("new span already ended")
60 }
61
62 ocS.End()
63 if !s.ended {
64 t.Error("span.End() did not end OpenTelemetry span")
65 }
66 }
67
68 func TestSpanSpanContext(t *testing.T) {
69 sc := trace.NewSpanContext(trace.SpanContextConfig{
70 TraceID: [16]byte{1},
71 SpanID: [8]byte{1},
72 })
73 // Do not test the conversion, only that the method is called.
74 converted := otel2oc.SpanContext(sc)
75
76 s := &span{sc: sc}
77 ocS := internal.NewSpan(s)
78 if ocS.SpanContext() != converted {
79 t.Error("span.SpanContext did not use OpenTelemetry SpanContext")
80 }
81 }
82
83 func TestSpanSetName(t *testing.T) {
84 // OpenCensus does not set a name if not recording.
85 s := &span{recording: true}
86 ocS := internal.NewSpan(s)
87 name := "test name"
88 ocS.SetName(name)
89 if s.name != name {
90 t.Error("span.SetName did not set OpenTelemetry span name")
91 }
92 }
93
94 func TestSpanSetStatus(t *testing.T) {
95 // OpenCensus does not set a status if not recording.
96 s := &span{recording: true}
97 ocS := internal.NewSpan(s)
98
99 for _, tt := range []struct {
100 name string
101
102 code int32
103 message string
104
105 wantCode codes.Code
106 }{
107 {
108 name: "with an error code",
109 code: int32(codes.Error),
110 message: "error",
111
112 wantCode: codes.Error,
113 },
114 {
115 name: "with a negative/invalid code",
116 code: -42,
117 message: "error",
118
119 wantCode: codes.Unset,
120 },
121 } {
122 t.Run(tt.name, func(t *testing.T) {
123 status := octrace.Status{Code: tt.code, Message: tt.message}
124 ocS.SetStatus(status)
125
126 if s.sCode != tt.wantCode {
127 t.Errorf(
128 "span.SetStatus failed to set OpenTelemetry status code. Expected %d, got %d",
129 tt.wantCode,
130 s.sCode,
131 )
132 }
133 if s.sMsg != tt.message {
134 t.Errorf(
135 "span.SetStatus failed to set OpenTelemetry status description. Expected %s, got %s",
136 tt.message,
137 s.sMsg,
138 )
139 }
140 })
141 }
142 }
143
144 func TestSpanAddAttributes(t *testing.T) {
145 attrs := []octrace.Attribute{
146 octrace.BoolAttribute("a", true),
147 }
148 // Do not test the conversion, only that the method is called.
149 converted := oc2otel.Attributes(attrs)
150
151 // OpenCensus does not set attributes if not recording.
152 s := &span{recording: true}
153 ocS := internal.NewSpan(s)
154 ocS.AddAttributes(attrs...)
155
156 if len(s.attrs) != len(converted) || s.attrs[0] != converted[0] {
157 t.Error("span.AddAttributes failed to set OpenTelemetry attributes")
158 }
159 }
160
161 func TestSpanAnnotate(t *testing.T) {
162 name := "annotation"
163 attrs := []octrace.Attribute{
164 octrace.BoolAttribute("a", true),
165 }
166 // Do not test the conversion, only that the method is called.
167 want := oc2otel.Attributes(attrs)
168
169 // OpenCensus does not set events if not recording.
170 s := &span{recording: true}
171 ocS := internal.NewSpan(s)
172 ocS.Annotate(attrs, name)
173
174 if s.eName != name {
175 t.Error("span.Annotate did not set event name")
176 }
177
178 config := trace.NewEventConfig(s.eOpts...)
179 got := config.Attributes()
180 if len(want) != len(got) || want[0] != got[0] {
181 t.Error("span.Annotate did not set event options")
182 }
183 }
184
185 func TestSpanAnnotatef(t *testing.T) {
186 format := "annotation %s"
187 attrs := []octrace.Attribute{
188 octrace.BoolAttribute("a", true),
189 }
190 // Do not test the conversion, only that the method is called.
191 want := oc2otel.Attributes(attrs)
192
193 // OpenCensus does not set events if not recording.
194 s := &span{recording: true}
195 ocS := internal.NewSpan(s)
196 ocS.Annotatef(attrs, format, "a")
197
198 if s.eName != "annotation a" {
199 t.Error("span.Annotatef did not set event name")
200 }
201
202 config := trace.NewEventConfig(s.eOpts...)
203 got := config.Attributes()
204 if len(want) != len(got) || want[0] != got[0] {
205 t.Error("span.Annotatef did not set event options")
206 }
207 }
208
209 func TestSpanAddMessageSendEvent(t *testing.T) {
210 var u, c int64 = 1, 2
211
212 // OpenCensus does not set events if not recording.
213 s := &span{recording: true}
214 ocS := internal.NewSpan(s)
215 ocS.AddMessageSendEvent(0, u, c)
216
217 if s.eName != internal.MessageSendEvent {
218 t.Error("span.AddMessageSendEvent did not set event name")
219 }
220
221 config := trace.NewEventConfig(s.eOpts...)
222 got := config.Attributes()
223 if len(got) != 2 {
224 t.Fatalf("span.AddMessageSendEvent set %d attributes, want 2", len(got))
225 }
226
227 want := attribute.KeyValue{Key: internal.UncompressedKey, Value: attribute.Int64Value(u)}
228 if got[0] != want {
229 t.Errorf("span.AddMessageSendEvent wrong uncompressed attribute: %v", got[0])
230 }
231
232 want = attribute.KeyValue{Key: internal.CompressedKey, Value: attribute.Int64Value(c)}
233 if got[1] != want {
234 t.Errorf("span.AddMessageSendEvent wrong compressed attribute: %v", got[1])
235 }
236 }
237
238 func TestSpanAddMessageReceiveEvent(t *testing.T) {
239 var u, c int64 = 3, 4
240
241 // OpenCensus does not set events if not recording.
242 s := &span{recording: true}
243 ocS := internal.NewSpan(s)
244 ocS.AddMessageReceiveEvent(0, u, c)
245
246 if s.eName != internal.MessageReceiveEvent {
247 t.Error("span.AddMessageReceiveEvent did not set event name")
248 }
249
250 config := trace.NewEventConfig(s.eOpts...)
251 got := config.Attributes()
252 if len(got) != 2 {
253 t.Fatalf("span.AddMessageReceiveEvent set %d attributes, want 2", len(got))
254 }
255
256 want := attribute.KeyValue{Key: internal.UncompressedKey, Value: attribute.Int64Value(u)}
257 if got[0] != want {
258 t.Errorf("span.AddMessageReceiveEvent wrong uncompressed attribute: %v", got[0])
259 }
260
261 want = attribute.KeyValue{Key: internal.CompressedKey, Value: attribute.Int64Value(c)}
262 if got[1] != want {
263 t.Errorf("span.AddMessageReceiveEvent wrong compressed attribute: %v", got[1])
264 }
265 }
266
267 func TestSpanAddLinkFails(t *testing.T) {
268 // OpenCensus does not try to set links if not recording.
269 s := &span{recording: true}
270 ocS := internal.NewSpan(s)
271 ocS.AddLink(octrace.Link{})
272 ocS.AddLink(octrace.Link{
273 TraceID: octrace.TraceID([16]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}),
274 SpanID: octrace.SpanID([8]byte{2, 0, 0, 0, 0, 0, 0, 0}),
275 Attributes: map[string]any{
276 "foo": "bar",
277 "number": int64(3),
278 },
279 })
280
281 wantLinks := []trace.Link{
282 {
283 SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
284 TraceFlags: trace.FlagsSampled,
285 }),
286 },
287 {
288 SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
289 TraceID: trace.TraceID([]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}),
290 SpanID: trace.SpanID([]byte{2, 0, 0, 0, 0, 0, 0, 0}),
291 TraceFlags: trace.FlagsSampled,
292 }),
293 Attributes: []attribute.KeyValue{
294 attribute.String("foo", "bar"),
295 attribute.Int64("number", 3),
296 },
297 },
298 }
299
300 if len(s.links) != len(wantLinks) {
301 t.Fatalf("got wrong number of links; want %v, got %v", len(wantLinks), len(s.links))
302 }
303
304 for i, l := range s.links {
305 if !l.SpanContext.Equal(wantLinks[i].SpanContext) {
306 t.Errorf(
307 "link[%v] has the wrong span context; want %+v, got %+v",
308 i,
309 wantLinks[i].SpanContext,
310 l.SpanContext,
311 )
312 }
313 gotAttributeSet := attribute.NewSet(l.Attributes...)
314 wantAttributeSet := attribute.NewSet(wantLinks[i].Attributes...)
315 if !gotAttributeSet.Equals(&wantAttributeSet) {
316 t.Errorf(
317 "link[%v] has the wrong attributes; want %v, got %v",
318 i,
319 wantAttributeSet.Encoded(attribute.DefaultEncoder()),
320 gotAttributeSet.Encoded(attribute.DefaultEncoder()),
321 )
322 }
323 }
324 }
325
326 func TestSpanString(t *testing.T) {
327 sc := trace.NewSpanContext(trace.SpanContextConfig{
328 TraceID: [16]byte{1},
329 SpanID: [8]byte{1},
330 })
331
332 s := &span{sc: sc}
333 ocS := internal.NewSpan(s)
334 if expected := "span 0100000000000000"; ocS.String() != expected {
335 t.Errorf("span.String = %q, not %q", ocS.String(), expected)
336 }
337 }