Debian Codegraph @

pdata/ptrace/doc_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package ptrace_test
5
6 import (
7 "fmt"
8 "strconv"
9
10 "go.opentelemetry.io/collector/pdata/pcommon"
11 "go.opentelemetry.io/collector/pdata/ptrace"
12 )
13
14 func ExampleNewTraces() {
15 traces := ptrace.NewTraces()
16
17 resourceSpans := traces.ResourceSpans().AppendEmpty()
18
19 resourceSpans.Resource().Attributes().PutStr("service.name", "my-service")
20 resourceSpans.Resource().Attributes().PutStr("service.version", "1.0.0")
21
22 scopeSpans := resourceSpans.ScopeSpans().AppendEmpty()
23 scopeSpans.Scope().SetName("my-instrumentation-library")
24 scopeSpans.Scope().SetVersion("1.0.0")
25
26 span := scopeSpans.Spans().AppendEmpty()
27 span.SetName("my-operation")
28 span.SetKind(ptrace.SpanKindServer)
29 span.SetStartTimestamp(pcommon.Timestamp(1640995200000000000)) // 2022-01-01 00:00:00 UTC
30 span.SetEndTimestamp(pcommon.Timestamp(1640995200100000000)) // 2022-01-01 00:00:00.1 UTC
31
32 span.Attributes().PutStr("http.method", "GET")
33 span.Attributes().PutStr("http.url", "/api/v1/users")
34 span.Attributes().PutInt("http.status_code", 200)
35
36 fmt.Printf("Resource spans count: %d\n", traces.ResourceSpans().Len())
37 fmt.Printf("Spans count: %d\n", scopeSpans.Spans().Len())
38 fmt.Printf("Span name: %s\n", span.Name())
39 // Output:
40 // Resource spans count: 1
41 // Spans count: 1
42 // Span name: my-operation
43 }
44
45 func ExampleSpan_SetTraceID() {
46 traces := ptrace.NewTraces()
47 resourceSpans := traces.ResourceSpans().AppendEmpty()
48 scopeSpans := resourceSpans.ScopeSpans().AppendEmpty()
49 span := scopeSpans.Spans().AppendEmpty()
50
51 traceID := pcommon.TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
52 spanID := pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})
53 parentSpanID := pcommon.SpanID([8]byte{9, 10, 11, 12, 13, 14, 15, 16})
54
55 span.SetTraceID(traceID)
56 span.SetSpanID(spanID)
57 span.SetParentSpanID(parentSpanID)
58 span.SetName("child-operation")
59
60 fmt.Printf("TraceID: %s\n", span.TraceID())
61 fmt.Printf("SpanID: %s\n", span.SpanID())
62 fmt.Printf("ParentSpanID: %s\n", span.ParentSpanID())
63 // Output:
64 // TraceID: 0102030405060708090a0b0c0d0e0f10
65 // SpanID: 0102030405060708
66 // ParentSpanID: 090a0b0c0d0e0f10
67 }
68
69 func ExampleSpan_Events() {
70 traces := ptrace.NewTraces()
71 resourceSpans := traces.ResourceSpans().AppendEmpty()
72 scopeSpans := resourceSpans.ScopeSpans().AppendEmpty()
73 span := scopeSpans.Spans().AppendEmpty()
74
75 span.SetName("database-query")
76
77 event1 := span.Events().AppendEmpty()
78 event1.SetName("query.start")
79 event1.SetTimestamp(pcommon.Timestamp(1640995200000000000))
80 event1.Attributes().PutStr("query", "SELECT * FROM users")
81
82 event2 := span.Events().AppendEmpty()
83 event2.SetName("query.end")
84 event2.SetTimestamp(pcommon.Timestamp(1640995200050000000))
85 event2.Attributes().PutInt("rows_returned", 42)
86
87 fmt.Printf("Events count: %d\n", span.Events().Len())
88 fmt.Printf("First event name: %s\n", span.Events().At(0).Name())
89 fmt.Printf("Second event name: %s\n", span.Events().At(1).Name())
90 // Output:
91 // Events count: 2
92 // First event name: query.start
93 // Second event name: query.end
94 }
95
96 func ExampleSpan_Status() {
97 traces := ptrace.NewTraces()
98 resourceSpans := traces.ResourceSpans().AppendEmpty()
99 scopeSpans := resourceSpans.ScopeSpans().AppendEmpty()
100 span := scopeSpans.Spans().AppendEmpty()
101
102 span.SetName("failed-operation")
103
104 status := span.Status()
105 status.SetCode(ptrace.StatusCodeError)
106 status.SetMessage("Connection timeout")
107
108 fmt.Printf("Status code: %s\n", status.Code())
109 fmt.Printf("Status message: %s\n", status.Message())
110 // Output:
111 // Status code: Error
112 // Status message: Connection timeout
113 }
114
115 func ExampleSpanKind() {
116 traces := ptrace.NewTraces()
117 resourceSpans := traces.ResourceSpans().AppendEmpty()
118 scopeSpans := resourceSpans.ScopeSpans().AppendEmpty()
119
120 // Different span kinds
121 kinds := []ptrace.SpanKind{
122 ptrace.SpanKindUnspecified,
123 ptrace.SpanKindInternal,
124 ptrace.SpanKindServer,
125 ptrace.SpanKindClient,
126 ptrace.SpanKindProducer,
127 ptrace.SpanKindConsumer,
128 }
129
130 for i, kind := range kinds {
131 span := scopeSpans.Spans().AppendEmpty()
132 span.SetName("operation-" + strconv.Itoa(i))
133 span.SetKind(kind)
134 span.SetStartTimestamp(pcommon.Timestamp(1640995200000000000))
135 span.SetEndTimestamp(pcommon.Timestamp(1640995200100000000))
136 }
137
138 fmt.Printf("Total spans: %d\n", scopeSpans.Spans().Len())
139 fmt.Printf("First span kind: %s\n", scopeSpans.Spans().At(0).Kind())
140 fmt.Printf("Server span kind: %s\n", scopeSpans.Spans().At(2).Kind())
141 fmt.Printf("Client span kind: %s\n", scopeSpans.Spans().At(3).Kind())
142 // Output:
143 // Total spans: 6
144 // First span kind: Unspecified
145 // Server span kind: Server
146 // Client span kind: Client
147 }
148
149 func ExampleSpan_Links() {
150 traces := ptrace.NewTraces()
151 resourceSpans := traces.ResourceSpans().AppendEmpty()
152 scopeSpans := resourceSpans.ScopeSpans().AppendEmpty()
153 span := scopeSpans.Spans().AppendEmpty()
154
155 span.SetName("memlimit-processor")
156 span.SetKind(ptrace.SpanKindInternal)
157
158 // Add links to other spans
159 link1 := span.Links().AppendEmpty()
160 link1.SetTraceID(pcommon.TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}))
161 link1.SetSpanID(pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))
162 link1.TraceState().FromRaw("rojo=00f067aa0ba902b7,congo=t61rcWkgMzE")
163 link1.Attributes().PutStr("link.type", "follows_from")
164 link1.SetFlags(0x01)
165
166 link2 := span.Links().AppendEmpty()
167 link2.SetTraceID(pcommon.TraceID([16]byte{16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}))
168 link2.SetSpanID(pcommon.SpanID([8]byte{8, 7, 6, 5, 4, 3, 2, 1}))
169 link2.Attributes().PutStr("link.type", "child_of")
170 link2.SetDroppedAttributesCount(2)
171
172 span.SetDroppedLinksCount(1)
173
174 fmt.Printf("Links count: %d\n", span.Links().Len())
175 fmt.Printf("First link trace state: %s\n", link1.TraceState().AsRaw())
176 fmt.Printf("First link flags: %d\n", link1.Flags())
177 fmt.Printf("Dropped links count: %d\n", span.DroppedLinksCount())
178 // Output:
179 // Links count: 2
180 // First link trace state: rojo=00f067aa0ba902b7,congo=t61rcWkgMzE
181 // First link flags: 1
182 // Dropped links count: 1
183 }
184
185 func ExampleSpan_TraceState() {
186 traces := ptrace.NewTraces()
187 resourceSpans := traces.ResourceSpans().AppendEmpty()
188 scopeSpans := resourceSpans.ScopeSpans().AppendEmpty()
189 span := scopeSpans.Spans().AppendEmpty()
190
191 span.SetName("traced-operation")
192
193 // Set trace state (W3C Trace Context)
194 span.TraceState().FromRaw("rojo=00f067aa0ba902b7,congo=t61rcWkgMzE")
195
196 fmt.Printf("Trace state: %s\n", span.TraceState().AsRaw())
197 // Output:
198 // Trace state: rojo=00f067aa0ba902b7,congo=t61rcWkgMzE
199 }
200
201 func ExampleSpan_Flags() {
202 traces := ptrace.NewTraces()
203 resourceSpans := traces.ResourceSpans().AppendEmpty()
204 scopeSpans := resourceSpans.ScopeSpans().AppendEmpty()
205 span := scopeSpans.Spans().AppendEmpty()
206
207 span.SetName("sampled-span")
208
209 // Set trace flags (W3C Trace Context)
210 span.SetFlags(0x01) // Sampled flag
211
212 fmt.Printf("Span flags: %d\n", span.Flags())
213 // Output:
214 // Span flags: 1
215 }
216
217 func ExampleStatusCode() {
218 traces := ptrace.NewTraces()
219 resourceSpans := traces.ResourceSpans().AppendEmpty()
220 scopeSpans := resourceSpans.ScopeSpans().AppendEmpty()
221
222 // Different status codes
223 statuses := []struct {
224 code ptrace.StatusCode
225 msg string
226 name string
227 }{
228 {ptrace.StatusCodeUnset, "", "unset"},
229 {ptrace.StatusCodeOk, "Success", "ok"},
230 {ptrace.StatusCodeError, "Internal server error", "error"},
231 }
232
233 for _, s := range statuses {
234 span := scopeSpans.Spans().AppendEmpty()
235 span.SetName("operation-" + s.name)
236 status := span.Status()
237 status.SetCode(s.code)
238 status.SetMessage(s.msg)
239 }
240
241 fmt.Printf("Total spans: %d\n", scopeSpans.Spans().Len())
242 fmt.Printf("Unset status: %s\n", scopeSpans.Spans().At(0).Status().Code())
243 fmt.Printf("Ok status: %s\n", scopeSpans.Spans().At(1).Status().Code())
244 fmt.Printf("Error status: %s\n", scopeSpans.Spans().At(2).Status().Code())
245 fmt.Printf("Error message: %s\n", scopeSpans.Spans().At(2).Status().Message())
246 // Output:
247 // Total spans: 3
248 // Unset status: Unset
249 // Ok status: Ok
250 // Error status: Error
251 // Error message: Internal server error
252 }
253
254 func ExampleSpan_DroppedAttributesCount() {
255 traces := ptrace.NewTraces()
256 resourceSpans := traces.ResourceSpans().AppendEmpty()
257 scopeSpans := resourceSpans.ScopeSpans().AppendEmpty()
258 span := scopeSpans.Spans().AppendEmpty()
259
260 span.SetName("span-with-dropped-data")
261
262 // Add some attributes and events
263 span.Attributes().PutStr("key1", "value1")
264 span.Attributes().PutStr("key2", "value2")
265
266 event := span.Events().AppendEmpty()
267 event.SetName("event1")
268 event.SetTimestamp(pcommon.Timestamp(1640995200000000000))
269
270 // Set dropped counts
271 span.SetDroppedAttributesCount(5)
272 span.SetDroppedEventsCount(3)
273 span.SetDroppedLinksCount(2)
274
275 fmt.Printf("Current attributes: %d\n", span.Attributes().Len())
276 fmt.Printf("Dropped attributes: %d\n", span.DroppedAttributesCount())
277 fmt.Printf("Current events: %d\n", span.Events().Len())
278 fmt.Printf("Dropped events: %d\n", span.DroppedEventsCount())
279 fmt.Printf("Dropped links: %d\n", span.DroppedLinksCount())
280 // Output:
281 // Current attributes: 2
282 // Dropped attributes: 5
283 // Current events: 1
284 // Dropped events: 3
285 // Dropped links: 2
286 }