Debian Codegraph @

exporters/otlp/otlptrace/internal/tracetransform/span.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package tracetransform // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal/tracetransform"
5
6 import (
7 "math"
8
9 tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
10
11 "go.opentelemetry.io/otel/attribute"
12 "go.opentelemetry.io/otel/codes"
13 "go.opentelemetry.io/otel/sdk/instrumentation"
14 tracesdk "go.opentelemetry.io/otel/sdk/trace"
15 "go.opentelemetry.io/otel/trace"
16 )
17
18 // Spans transforms a slice of OpenTelemetry spans into a slice of OTLP
19 // ResourceSpans.
20 func Spans(sdl []tracesdk.ReadOnlySpan) []*tracepb.ResourceSpans {
21 if len(sdl) == 0 {
22 return nil
23 }
24
25 rsm := make(map[attribute.Distinct]*tracepb.ResourceSpans)
26
27 type key struct {
28 r attribute.Distinct
29 is instrumentation.Scope
30 }
31 ssm := make(map[key]*tracepb.ScopeSpans)
32
33 var resources int
34 for _, sd := range sdl {
35 if sd == nil {
36 continue
37 }
38
39 rKey := sd.Resource().Equivalent()
40 k := key{
41 r: rKey,
42 is: sd.InstrumentationScope(),
43 }
44 scopeSpan, iOk := ssm[k]
45 if !iOk {
46 // Either the resource or instrumentation scope were unknown.
47 scopeSpan = &tracepb.ScopeSpans{
48 Scope: InstrumentationScope(sd.InstrumentationScope()),
49 Spans: []*tracepb.Span{},
50 SchemaUrl: sd.InstrumentationScope().SchemaURL,
51 }
52 }
53 scopeSpan.Spans = append(scopeSpan.Spans, span(sd))
54 ssm[k] = scopeSpan
55
56 rs, rOk := rsm[rKey]
57 if !rOk {
58 resources++
59 // The resource was unknown.
60 rs = &tracepb.ResourceSpans{
61 Resource: Resource(sd.Resource()),
62 ScopeSpans: []*tracepb.ScopeSpans{scopeSpan},
63 SchemaUrl: sd.Resource().SchemaURL(),
64 }
65 rsm[rKey] = rs
66 continue
67 }
68
69 // The resource has been seen before. Check if the instrumentation
70 // library lookup was unknown because if so we need to add it to the
71 // ResourceSpans. Otherwise, the instrumentation library has already
72 // been seen and the append we did above will be included it in the
73 // ScopeSpans reference.
74 if !iOk {
75 rs.ScopeSpans = append(rs.ScopeSpans, scopeSpan)
76 }
77 }
78
79 // Transform the categorized map into a slice
80 rss := make([]*tracepb.ResourceSpans, 0, resources)
81 for _, rs := range rsm {
82 rss = append(rss, rs)
83 }
84 return rss
85 }
86
87 // span transforms a Span into an OTLP span.
88 func span(sd tracesdk.ReadOnlySpan) *tracepb.Span {
89 if sd == nil {
90 return nil
91 }
92
93 tid := sd.SpanContext().TraceID()
94 sid := sd.SpanContext().SpanID()
95
96 s := &tracepb.Span{
97 TraceId: tid[:],
98 SpanId: sid[:],
99 TraceState: sd.SpanContext().TraceState().String(),
100 Status: status(sd.Status().Code, sd.Status().Description),
101 StartTimeUnixNano: uint64(max(0, sd.StartTime().UnixNano())), // nolint:gosec // Overflow checked.
102 EndTimeUnixNano: uint64(max(0, sd.EndTime().UnixNano())), // nolint:gosec // Overflow checked.
103 Links: links(sd.Links()),
104 Kind: spanKind(sd.SpanKind()),
105 Name: sd.Name(),
106 Attributes: KeyValues(sd.Attributes()),
107 Events: spanEvents(sd.Events()),
108 DroppedAttributesCount: clampUint32(sd.DroppedAttributes()),
109 DroppedEventsCount: clampUint32(sd.DroppedEvents()),
110 DroppedLinksCount: clampUint32(sd.DroppedLinks()),
111 }
112
113 if psid := sd.Parent().SpanID(); psid.IsValid() {
114 s.ParentSpanId = psid[:]
115 }
116 s.Flags = buildSpanFlagsWith(sd.SpanContext().TraceFlags(), sd.Parent())
117
118 return s
119 }
120
121 func clampUint32(v int) uint32 {
122 if v < 0 {
123 return 0
124 }
125 if int64(v) > math.MaxUint32 {
126 return math.MaxUint32
127 }
128 return uint32(v) // nolint: gosec // Overflow/Underflow checked.
129 }
130
131 // status transform a span code and message into an OTLP span status.
132 func status(status codes.Code, message string) *tracepb.Status {
133 var c tracepb.Status_StatusCode
134 switch status {
135 case codes.Ok:
136 c = tracepb.Status_STATUS_CODE_OK
137 case codes.Error:
138 c = tracepb.Status_STATUS_CODE_ERROR
139 default:
140 c = tracepb.Status_STATUS_CODE_UNSET
141 }
142 return &tracepb.Status{
143 Code: c,
144 Message: message,
145 }
146 }
147
148 // links transforms span Links to OTLP span links.
149 func links(links []tracesdk.Link) []*tracepb.Span_Link {
150 if len(links) == 0 {
151 return nil
152 }
153
154 sl := make([]*tracepb.Span_Link, 0, len(links))
155 for _, otLink := range links {
156 // This redefinition is necessary to prevent otLink.*ID[:] copies
157 // being reused -- in short we need a new otLink per iteration.
158
159 tid := otLink.SpanContext.TraceID()
160 sid := otLink.SpanContext.SpanID()
161
162 flags := buildSpanFlagsWith(otLink.SpanContext.TraceFlags(), otLink.SpanContext)
163
164 sl = append(sl, &tracepb.Span_Link{
165 TraceId: tid[:],
166 SpanId: sid[:],
167 Attributes: KeyValues(otLink.Attributes),
168 DroppedAttributesCount: clampUint32(otLink.DroppedAttributeCount),
169 Flags: flags,
170 })
171 }
172 return sl
173 }
174
175 func buildSpanFlagsWith(tf trace.TraceFlags, parent trace.SpanContext) uint32 {
176 // Lower 8 bits are the W3C TraceFlags; always indicate that we know whether the parent is remote
177 flags := uint32(tf) | uint32(tracepb.SpanFlags_SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK)
178 // Set the parent-is-remote bit when applicable
179 if parent.IsRemote() {
180 flags |= uint32(tracepb.SpanFlags_SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK)
181 }
182
183 return flags // nolint:gosec // Flags is a bitmask and can't be negative
184 }
185
186 // spanEvents transforms span Events to an OTLP span events.
187 func spanEvents(es []tracesdk.Event) []*tracepb.Span_Event {
188 if len(es) == 0 {
189 return nil
190 }
191
192 events := make([]*tracepb.Span_Event, len(es))
193 // Transform message events
194 for i := range es {
195 events[i] = &tracepb.Span_Event{
196 Name: es[i].Name,
197 TimeUnixNano: uint64(max(0, es[i].Time.UnixNano())), // nolint:gosec // Overflow checked.
198 Attributes: KeyValues(es[i].Attributes),
199 DroppedAttributesCount: clampUint32(es[i].DroppedAttributeCount),
200 }
201 }
202 return events
203 }
204
205 // spanKind transforms a SpanKind to an OTLP span kind.
206 func spanKind(kind trace.SpanKind) tracepb.Span_SpanKind {
207 switch kind {
208 case trace.SpanKindInternal:
209 return tracepb.Span_SPAN_KIND_INTERNAL
210 case trace.SpanKindClient:
211 return tracepb.Span_SPAN_KIND_CLIENT
212 case trace.SpanKindServer:
213 return tracepb.Span_SPAN_KIND_SERVER
214 case trace.SpanKindProducer:
215 return tracepb.Span_SPAN_KIND_PRODUCER
216 case trace.SpanKindConsumer:
217 return tracepb.Span_SPAN_KIND_CONSUMER
218 default:
219 return tracepb.Span_SPAN_KIND_UNSPECIFIED
220 }
221 }