Debian Codegraph @

pdata/ptrace/traces_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package ptrace
5
6 import (
7 "testing"
8 "time"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12
13 "go.opentelemetry.io/collector/pdata/internal"
14 "go.opentelemetry.io/collector/pdata/pcommon"
15 )
16
17 func TestSpanCount(t *testing.T) {
18 traces := NewTraces()
19 assert.Equal(t, 0, traces.SpanCount())
20
21 rs := traces.ResourceSpans().AppendEmpty()
22 assert.Equal(t, 0, traces.SpanCount())
23
24 ils := rs.ScopeSpans().AppendEmpty()
25 assert.Equal(t, 0, traces.SpanCount())
26
27 ils.Spans().AppendEmpty()
28 assert.Equal(t, 1, traces.SpanCount())
29
30 rms := traces.ResourceSpans()
31 rms.EnsureCapacity(3)
32 rms.AppendEmpty().ScopeSpans().AppendEmpty()
33 ilss := rms.AppendEmpty().ScopeSpans().AppendEmpty().Spans()
34 for range 5 {
35 ilss.AppendEmpty()
36 }
37 // 5 + 1 (from rms.At(0) initialized first)
38 assert.Equal(t, 6, traces.SpanCount())
39 }
40
41 func TestSpanCountWithEmpty(t *testing.T) {
42 assert.Equal(t, 0, newTraces(&internal.ExportTraceServiceRequest{
43 ResourceSpans: []*internal.ResourceSpans{{}},
44 }, new(internal.State)).SpanCount())
45 assert.Equal(t, 0, newTraces(&internal.ExportTraceServiceRequest{
46 ResourceSpans: []*internal.ResourceSpans{
47 {
48 ScopeSpans: []*internal.ScopeSpans{{}},
49 },
50 },
51 }, new(internal.State)).SpanCount())
52 assert.Equal(t, 1, newTraces(&internal.ExportTraceServiceRequest{
53 ResourceSpans: []*internal.ResourceSpans{
54 {
55 ScopeSpans: []*internal.ScopeSpans{
56 {
57 Spans: []*internal.Span{{}},
58 },
59 },
60 },
61 },
62 }, new(internal.State)).SpanCount())
63 }
64
65 func TestTracesCopyTo(t *testing.T) {
66 td := generateTestTraces()
67 tracesCopy := NewTraces()
68 td.CopyTo(tracesCopy)
69 assert.Equal(t, td, tracesCopy)
70 }
71
72 func TestReadOnlyTracesInvalidUsage(t *testing.T) {
73 td := NewTraces()
74 assert.False(t, td.IsReadOnly())
75 res := td.ResourceSpans().AppendEmpty().Resource()
76 res.Attributes().PutStr("k1", "v1")
77 td.MarkReadOnly()
78 assert.True(t, td.IsReadOnly())
79 assert.Panics(t, func() { res.Attributes().PutStr("k2", "v2") })
80 }
81
82 func BenchmarkTracesUsage(b *testing.B) {
83 td := generateTestTraces()
84 ts := pcommon.NewTimestampFromTime(time.Now())
85
86 b.ReportAllocs()
87
88 for b.Loop() {
89 for i := 0; i < td.ResourceSpans().Len(); i++ {
90 rs := td.ResourceSpans().At(i)
91 res := rs.Resource()
92 res.Attributes().PutStr("foo", "bar")
93 v, ok := res.Attributes().Get("foo")
94 assert.True(b, ok)
95 assert.Equal(b, "bar", v.Str())
96 v.SetStr("new-bar")
97 assert.Equal(b, "new-bar", v.Str())
98 res.Attributes().Remove("foo")
99 for j := 0; j < rs.ScopeSpans().Len(); j++ {
100 iss := rs.ScopeSpans().At(j)
101 iss.Scope().SetName("new_test_name")
102 assert.Equal(b, "new_test_name", iss.Scope().Name())
103 for k := 0; k < iss.Spans().Len(); k++ {
104 s := iss.Spans().At(k)
105 s.SetName("new_span")
106 assert.Equal(b, "new_span", s.Name())
107 s.SetStartTimestamp(ts)
108 assert.Equal(b, ts, s.StartTimestamp())
109 s.SetEndTimestamp(ts)
110 assert.Equal(b, ts, s.EndTimestamp())
111 s.SetTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
112 assert.Equal(b, pcommon.TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}), s.TraceID())
113 s.SetSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})
114 assert.Equal(b, pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}), s.SpanID())
115 }
116 s := iss.Spans().AppendEmpty()
117 s.SetName("another_span")
118 s.SetStartTimestamp(ts)
119 s.SetEndTimestamp(ts)
120 s.SetTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
121 s.SetParentSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})
122 s.SetSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})
123 s.Attributes().PutStr("foo1", "bar1")
124 s.Attributes().PutStr("foo2", "bar2")
125 iss.Spans().RemoveIf(func(lr Span) bool {
126 return lr.Name() == "another_span"
127 })
128 }
129 }
130 }
131 }
132
133 func BenchmarkTracesMarshalJSON(b *testing.B) {
134 td := generateTestTraces()
135 encoder := &JSONMarshaler{}
136
137 b.ReportAllocs()
138
139 for b.Loop() {
140 jsonBuf, err := encoder.MarshalTraces(td)
141 require.NoError(b, err)
142 require.NotNil(b, jsonBuf)
143 }
144 }