Debian Codegraph @

sdk/trace/tracetest/span.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package tracetest // import "go.opentelemetry.io/otel/sdk/trace/tracetest"
5
6 import (
7 "time"
8
9 "go.opentelemetry.io/otel/attribute"
10 "go.opentelemetry.io/otel/sdk/instrumentation"
11 "go.opentelemetry.io/otel/sdk/resource"
12 tracesdk "go.opentelemetry.io/otel/sdk/trace"
13 "go.opentelemetry.io/otel/trace"
14 )
15
16 // SpanStubs is a slice of SpanStub use for testing an SDK.
17 type SpanStubs []SpanStub
18
19 // SpanStubsFromReadOnlySpans returns SpanStubs populated from ro.
20 func SpanStubsFromReadOnlySpans(ro []tracesdk.ReadOnlySpan) SpanStubs {
21 if len(ro) == 0 {
22 return nil
23 }
24
25 s := make(SpanStubs, 0, len(ro))
26 for _, r := range ro {
27 s = append(s, SpanStubFromReadOnlySpan(r))
28 }
29
30 return s
31 }
32
33 // Snapshots returns s as a slice of ReadOnlySpans.
34 func (s SpanStubs) Snapshots() []tracesdk.ReadOnlySpan {
35 if len(s) == 0 {
36 return nil
37 }
38
39 ro := make([]tracesdk.ReadOnlySpan, len(s))
40 for i := range s {
41 ro[i] = s[i].Snapshot()
42 }
43 return ro
44 }
45
46 // SpanStub is a stand-in for a Span.
47 type SpanStub struct {
48 Name string
49 SpanContext trace.SpanContext
50 Parent trace.SpanContext
51 SpanKind trace.SpanKind
52 StartTime time.Time
53 EndTime time.Time
54 Attributes []attribute.KeyValue
55 Events []tracesdk.Event
56 Links []tracesdk.Link
57 Status tracesdk.Status
58 DroppedAttributes int
59 DroppedEvents int
60 DroppedLinks int
61 ChildSpanCount int
62 Resource *resource.Resource
63 InstrumentationScope instrumentation.Scope
64
65 // Deprecated: use InstrumentationScope instead.
66 InstrumentationLibrary instrumentation.Library //nolint:staticcheck // This method needs to be define for backwards compatibility
67 }
68
69 // SpanStubFromReadOnlySpan returns a SpanStub populated from ro.
70 func SpanStubFromReadOnlySpan(ro tracesdk.ReadOnlySpan) SpanStub {
71 if ro == nil {
72 return SpanStub{}
73 }
74
75 return SpanStub{
76 Name: ro.Name(),
77 SpanContext: ro.SpanContext(),
78 Parent: ro.Parent(),
79 SpanKind: ro.SpanKind(),
80 StartTime: ro.StartTime(),
81 EndTime: ro.EndTime(),
82 Attributes: ro.Attributes(),
83 Events: ro.Events(),
84 Links: ro.Links(),
85 Status: ro.Status(),
86 DroppedAttributes: ro.DroppedAttributes(),
87 DroppedEvents: ro.DroppedEvents(),
88 DroppedLinks: ro.DroppedLinks(),
89 ChildSpanCount: ro.ChildSpanCount(),
90 Resource: ro.Resource(),
91 InstrumentationScope: ro.InstrumentationScope(),
92 InstrumentationLibrary: ro.InstrumentationScope(),
93 }
94 }
95
96 // Snapshot returns a read-only copy of the SpanStub.
97 func (s SpanStub) Snapshot() tracesdk.ReadOnlySpan {
98 scopeOrLibrary := s.InstrumentationScope
99 if scopeOrLibrary.Name == "" && scopeOrLibrary.Version == "" && scopeOrLibrary.SchemaURL == "" {
100 scopeOrLibrary = s.InstrumentationLibrary
101 }
102
103 return spanSnapshot{
104 name: s.Name,
105 spanContext: s.SpanContext,
106 parent: s.Parent,
107 spanKind: s.SpanKind,
108 startTime: s.StartTime,
109 endTime: s.EndTime,
110 attributes: s.Attributes,
111 events: s.Events,
112 links: s.Links,
113 status: s.Status,
114 droppedAttributes: s.DroppedAttributes,
115 droppedEvents: s.DroppedEvents,
116 droppedLinks: s.DroppedLinks,
117 childSpanCount: s.ChildSpanCount,
118 resource: s.Resource,
119 instrumentationScope: scopeOrLibrary,
120 }
121 }
122
123 type spanSnapshot struct {
124 // Embed the interface to implement the private method.
125 tracesdk.ReadOnlySpan
126
127 name string
128 spanContext trace.SpanContext
129 parent trace.SpanContext
130 spanKind trace.SpanKind
131 startTime time.Time
132 endTime time.Time
133 attributes []attribute.KeyValue
134 events []tracesdk.Event
135 links []tracesdk.Link
136 status tracesdk.Status
137 droppedAttributes int
138 droppedEvents int
139 droppedLinks int
140 childSpanCount int
141 resource *resource.Resource
142 instrumentationScope instrumentation.Scope
143 }
144
145 func (s spanSnapshot) Name() string { return s.name }
146 func (s spanSnapshot) SpanContext() trace.SpanContext { return s.spanContext }
147 func (s spanSnapshot) Parent() trace.SpanContext { return s.parent }
148 func (s spanSnapshot) SpanKind() trace.SpanKind { return s.spanKind }
149 func (s spanSnapshot) StartTime() time.Time { return s.startTime }
150 func (s spanSnapshot) EndTime() time.Time { return s.endTime }
151 func (s spanSnapshot) Attributes() []attribute.KeyValue { return s.attributes }
152 func (s spanSnapshot) Links() []tracesdk.Link { return s.links }
153 func (s spanSnapshot) Events() []tracesdk.Event { return s.events }
154 func (s spanSnapshot) Status() tracesdk.Status { return s.status }
155 func (s spanSnapshot) DroppedAttributes() int { return s.droppedAttributes }
156 func (s spanSnapshot) DroppedLinks() int { return s.droppedLinks }
157 func (s spanSnapshot) DroppedEvents() int { return s.droppedEvents }
158 func (s spanSnapshot) ChildSpanCount() int { return s.childSpanCount }
159 func (s spanSnapshot) Resource() *resource.Resource { return s.resource }
160 func (s spanSnapshot) InstrumentationScope() instrumentation.Scope {
161 return s.instrumentationScope
162 }
163
164 func (s spanSnapshot) InstrumentationLibrary() instrumentation.Library { //nolint:staticcheck // This method needs to be define for backwards compatibility
165 return s.instrumentationScope
166 }