/
oc_to_traces.go
438 lines (378 loc) · 14.7 KB
/
oc_to_traces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright 2020 OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internaldata
import (
"strings"
occommon "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
ocresource "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
octrace "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
"github.com/golang/protobuf/ptypes"
"github.com/open-telemetry/opentelemetry-collector/consumer/consumerdata"
"github.com/open-telemetry/opentelemetry-collector/internal"
"github.com/open-telemetry/opentelemetry-collector/internal/data"
"github.com/open-telemetry/opentelemetry-collector/translator/conventions"
tracetranslator "github.com/open-telemetry/opentelemetry-collector/translator/trace"
)
// OCToTraceData converts OC data format to TraceData.
func OCToTraceData(td consumerdata.TraceData) data.TraceData {
traceData := data.NewTraceData()
if td.Node == nil && td.Resource == nil && len(td.Spans) == 0 {
return traceData
}
if len(td.Spans) == 0 {
// At least one of the td.Node or td.Resource is not nil. Set the resource and return.
traceData.SetResourceSpans(data.NewResourceSpansSlice(1))
ocNodeResourceToInternal(td.Node, td.Resource, traceData.ResourceSpans().Get(0))
return traceData
}
// We may need to split OC spans into several ResourceSpans. OC Spans can have a
// Resource field inside them set to nil which indicates they use the Resource
// specified in "td.Resource", or they can have the Resource field inside them set
// to non-nil which indicates they have overridden Resource field and "td.Resource"
// does not apply to those spans.
//
// Each OC Span that has its own Resource field set to non-nil must be placed in a
// separate ResourceSpans instance, containing only that span. All other OC Spans
// that have nil Resource field must be placed in one other ResourceSpans instance,
// which will gets its Resource field from "td.Resource".
//
// We will end up with with one or more ResourceSpans like this:
//
// ResourceSpans ResourceSpans ResourceSpans
// +-----+-----+---+-----+ +------------+ +------------+
// |Span1|Span2|...|SpanM| |Span | |Span | ...
// +-----+-----+---+-----+ +------------+ +------------+
// Count the number of spans that have nil Resource and need to be combined
// in one slice.
combinedSpanCount := 0
distinctResourceCount := 0
for _, ocSpan := range td.Spans {
if ocSpan == nil {
// Skip nil spans.
continue
}
if ocSpan.Resource == nil {
combinedSpanCount++
} else {
distinctResourceCount++
}
}
// Total number of resources is equal to:
// 1 (for all spans with nil resource) + numSpansWithResource (distinctResourceCount).
traceData.SetResourceSpans(data.NewResourceSpansSlice(distinctResourceCount + 1))
rs0 := traceData.ResourceSpans().Get(0)
ocNodeResourceToInternal(td.Node, td.Resource, rs0)
// Allocate a slice for spans that need to be combined into first ResourceSpans.
rs0.SetInstrumentationLibrarySpans(data.NewInstrumentationLibrarySpansSlice(1))
ils0 := rs0.InstrumentationLibrarySpans().Get(0)
ils0.SetSpans(data.NewSpanSlice(combinedSpanCount))
combinedSpans := ils0.Spans()
// Now do the span translation and place them in appropriate ResourceSpans
// instances.
// Index to next available slot in "combinedSpans" slice.
combinedSpanIdx := 0
// First resourcespan is used for the default resource, so start with 1.
resourceSpanIdx := 1
for _, ocSpan := range td.Spans {
if ocSpan == nil {
// Skip nil spans.
continue
}
if ocSpan.Resource == nil {
// Add the span to the "combinedSpans". combinedSpans length is equal
// to combinedSpanCount. The loop above that calculates combinedSpanCount
// has exact same conditions as we have here in this loop.
ocSpanToInternal(combinedSpans.Get(combinedSpanIdx), ocSpan)
combinedSpanIdx++
} else {
// This span has a different Resource and must be placed in a different
// ResourceSpans instance. Create a separate ResourceSpans item just for this span.
ocSpanToResourceSpans(ocSpan, td.Node, traceData.ResourceSpans().Get(resourceSpanIdx))
resourceSpanIdx++
}
}
return traceData
}
func ocSpanToResourceSpans(ocSpan *octrace.Span, node *occommon.Node, out data.ResourceSpans) {
ocNodeResourceToInternal(node, ocSpan.Resource, out)
out.SetInstrumentationLibrarySpans(data.NewInstrumentationLibrarySpansSlice(1))
ils0 := out.InstrumentationLibrarySpans().Get(0)
ils0.SetSpans(data.NewSpanSlice(1))
ocSpanToInternal(ils0.Spans().Get(0), ocSpan)
}
func ocSpanToInternal(dest data.Span, src *octrace.Span) {
events, droppedEventCount := ocEventsToInternal(src.TimeEvents)
links, droppedLinkCount := ocLinksToInternal(src.Links)
// Note that ocSpanKindToInternal must be called before ocAttrsToInternal
// since it may modify src.Attributes (remove the attribute which represents the
// span kind).
kind := ocSpanKindToInternal(src.Kind, src.Attributes)
attrs, droppedAttrs := ocAttrsToInternal(src.Attributes)
dest.SetTraceID(data.NewTraceID(src.TraceId))
dest.SetSpanID(data.NewSpanID(src.SpanId))
dest.SetTraceState(ocTraceStateToInternal(src.Tracestate))
dest.SetParentSpanID(data.NewSpanID(src.ParentSpanId))
dest.SetName(src.Name.GetValue())
dest.SetKind(kind)
dest.SetStartTime(internal.TimestampToUnixNano(src.StartTime))
dest.SetEndTime(internal.TimestampToUnixNano(src.EndTime))
dest.SetAttributes(attrs)
dest.SetDroppedAttributesCount(droppedAttrs)
dest.SetEvents(events)
dest.SetDroppedEventsCount(droppedEventCount)
dest.SetLinks(links)
dest.SetDroppedLinksCount(droppedLinkCount)
dest.SetStatus(ocStatusToInternal(src.Status))
}
func ocStatusToInternal(ocStatus *octrace.Status) data.SpanStatus {
if ocStatus == nil {
return data.SpanStatus{}
}
ss := data.NewSpanStatus()
ss.SetCode(data.StatusCode(ocStatus.Code))
ss.SetMessage(ocStatus.Message)
return ss
}
// Convert tracestate to W3C format. See the https://w3c.github.io/trace-context/
func ocTraceStateToInternal(ocTracestate *octrace.Span_Tracestate) data.TraceState {
if ocTracestate == nil {
return ""
}
var sb strings.Builder
for i, entry := range ocTracestate.Entries {
sb.Grow(1 + len(entry.Key) + 1 + len(entry.Value))
if i > 0 {
sb.WriteString(",")
}
sb.WriteString(entry.Key)
sb.WriteString("=")
sb.WriteString(entry.Value)
}
return data.TraceState(sb.String())
}
func ocAttrsToInternal(ocAttrs *octrace.Span_Attributes) (data.AttributeMap, uint32) {
if ocAttrs == nil {
return data.NewAttributeMap(nil), 0
}
attrCount := len(ocAttrs.AttributeMap)
attrMap := make(data.AttributesMap, attrCount)
if attrCount > 0 {
values := data.NewAttributeValueSlice(attrCount)
i := 0
for key, ocAttr := range ocAttrs.AttributeMap {
switch attribValue := ocAttr.Value.(type) {
case *octrace.AttributeValue_StringValue:
values[i].SetString(attribValue.StringValue.GetValue())
case *octrace.AttributeValue_IntValue:
values[i].SetInt(attribValue.IntValue)
case *octrace.AttributeValue_BoolValue:
values[i].SetBool(attribValue.BoolValue)
case *octrace.AttributeValue_DoubleValue:
values[i].SetDouble(attribValue.DoubleValue)
default:
str := "<Unknown OpenCensus attribute value type>"
values[i].SetString(str)
}
attrMap[key] = values[i]
i++
}
}
return data.NewAttributeMap(attrMap), uint32(ocAttrs.DroppedAttributesCount)
}
func ocSpanKindToInternal(ocKind octrace.Span_SpanKind, ocAttrs *octrace.Span_Attributes) data.SpanKind {
switch ocKind {
case octrace.Span_SERVER:
return data.SpanKindSERVER
case octrace.Span_CLIENT:
return data.SpanKindCLIENT
case octrace.Span_SPAN_KIND_UNSPECIFIED:
// Span kind field is unspecified, check if TagSpanKind attribute is set.
// This can happen if span kind had no equivalent in OC, so we could represent it in
// the SpanKind. In that case the span kind may be a special attribute TagSpanKind.
if ocAttrs != nil {
kindAttr := ocAttrs.AttributeMap[tracetranslator.TagSpanKind]
if kindAttr != nil {
strVal, ok := kindAttr.Value.(*octrace.AttributeValue_StringValue)
if ok && strVal != nil {
var otlpKind data.SpanKind
switch tracetranslator.OpenTracingSpanKind(strVal.StringValue.GetValue()) {
case tracetranslator.OpenTracingSpanKindConsumer:
otlpKind = data.SpanKindCONSUMER
case tracetranslator.OpenTracingSpanKindProducer:
otlpKind = data.SpanKindPRODUCER
default:
return data.SpanKindUNSPECIFIED
}
delete(ocAttrs.AttributeMap, tracetranslator.TagSpanKind)
return otlpKind
}
}
}
return data.SpanKindUNSPECIFIED
default:
return data.SpanKindUNSPECIFIED
}
}
func ocEventsToInternal(ocEvents *octrace.Span_TimeEvents) (data.SpanEventSlice, uint32) {
if ocEvents == nil {
return data.NewSpanEventSlice(0), 0
}
droppedCount := uint32(ocEvents.DroppedMessageEventsCount + ocEvents.DroppedAnnotationsCount)
if len(ocEvents.TimeEvent) == 0 {
return data.NewSpanEventSlice(0), droppedCount
}
events := data.NewSpanEventSlice(len(ocEvents.TimeEvent))
i := 0
for _, ocEvent := range ocEvents.TimeEvent {
if ocEvent == nil {
// Skip nil source events.
continue
}
event := events.Get(i)
i++
event.SetTimestamp(internal.TimestampToUnixNano(ocEvent.Time))
switch teValue := ocEvent.Value.(type) {
case *octrace.Span_TimeEvent_Annotation_:
if teValue.Annotation != nil {
event.SetName(teValue.Annotation.Description.GetValue())
attrs, droppedAttrs := ocAttrsToInternal(teValue.Annotation.Attributes)
event.SetAttributes(attrs)
event.SetDroppedAttributesCount(droppedAttrs)
}
case *octrace.Span_TimeEvent_MessageEvent_:
attrs, droppedAttrs := ocMessageEventToInternalAttrs(teValue.MessageEvent)
event.SetAttributes(attrs)
event.SetDroppedAttributesCount(droppedAttrs)
default:
event.SetName("An unknown OpenCensus TimeEvent type was detected when translating")
}
}
// Truncate the slice to only include populated items.
events.Resize(0, i)
return events, droppedCount
}
func ocLinksToInternal(ocLinks *octrace.Span_Links) (data.SpanLinkSlice, uint32) {
if ocLinks == nil {
return data.NewSpanLinkSlice(0), 0
}
if len(ocLinks.Link) == 0 {
return data.NewSpanLinkSlice(0), uint32(ocLinks.DroppedLinksCount)
}
links := data.NewSpanLinkSlice(len(ocLinks.Link))
i := 0
for _, ocLink := range ocLinks.Link {
if ocLink == nil {
continue
}
link := links.Get(i)
i++
link.SetTraceID(data.NewTraceID(ocLink.TraceId))
link.SetSpanID(data.NewSpanID(ocLink.SpanId))
link.SetTraceState(ocTraceStateToInternal(ocLink.Tracestate))
attr, droppedAttr := ocAttrsToInternal(ocLink.Attributes)
link.SetAttributes(attr)
link.SetDroppedAttributesCount(droppedAttr)
}
// Truncate the slice to only include populated items.
links.Resize(0, i)
return links, uint32(ocLinks.DroppedLinksCount)
}
func ocMessageEventToInternalAttrs(msgEvent *octrace.Span_TimeEvent_MessageEvent) (data.AttributeMap, uint32) {
if msgEvent == nil {
return data.NewAttributeMap(nil), 0
}
attrs := data.NewAttributeValueSlice(4)
attrs[0].SetString(msgEvent.Type.String())
attrs[1].SetInt(int64(msgEvent.Id))
attrs[2].SetInt(int64(msgEvent.UncompressedSize))
attrs[3].SetInt(int64(msgEvent.CompressedSize))
return data.NewAttributeMap(data.AttributesMap{
conventions.OCTimeEventMessageEventType: attrs[0],
conventions.OCTimeEventMessageEventID: attrs[1],
conventions.OCTimeEventMessageEventUSize: attrs[2],
conventions.OCTimeEventMessageEventCSize: attrs[3],
}), 0
}
func ocNodeResourceToInternal(ocNode *occommon.Node, ocResource *ocresource.Resource, out data.ResourceSpans) {
// Number of special fields in the Node. See the code below that deals with special fields.
const specialNodeAttrCount = 7
// Number of special fields in the Resource.
const specialResourceAttrCount = 1
// Calculate maximum total number of attributes. It is OK if we are a bit higher than
// the exact number since this is only needed for capacity reservation.
maxTotalAttrCount := 0
if ocNode != nil {
maxTotalAttrCount += len(ocNode.Attributes) + specialNodeAttrCount
}
if ocResource != nil {
maxTotalAttrCount += len(ocResource.Labels) + specialResourceAttrCount
}
// Create a map where we will place all attributes from the Node and Resource.
attrs := make(data.AttributesMap, maxTotalAttrCount)
if ocNode != nil {
// Copy all Attributes.
for k, v := range ocNode.Attributes {
attrs[k] = data.NewAttributeValueString(v)
}
// Add all special fields.
if ocNode.ServiceInfo != nil {
if ocNode.ServiceInfo.Name != "" {
attrs[conventions.AttributeServiceName] = data.NewAttributeValueString(
ocNode.ServiceInfo.Name)
}
}
if ocNode.Identifier != nil {
if ocNode.Identifier.StartTimestamp != nil {
attrs[conventions.OCAttributeProcessStartTime] = data.NewAttributeValueString(
ptypes.TimestampString(ocNode.Identifier.StartTimestamp))
}
if ocNode.Identifier.HostName != "" {
attrs[conventions.AttributeHostHostname] = data.NewAttributeValueString(
ocNode.Identifier.HostName)
}
if ocNode.Identifier.Pid != 0 {
attrs[conventions.OCAttributeProcessID] = data.NewAttributeValueInt(int64(ocNode.Identifier.Pid))
}
}
if ocNode.LibraryInfo != nil {
if ocNode.LibraryInfo.CoreLibraryVersion != "" {
attrs[conventions.AttributeLibraryVersion] = data.NewAttributeValueString(
ocNode.LibraryInfo.CoreLibraryVersion)
}
if ocNode.LibraryInfo.ExporterVersion != "" {
attrs[conventions.OCAttributeExporterVersion] = data.NewAttributeValueString(
ocNode.LibraryInfo.ExporterVersion)
}
if ocNode.LibraryInfo.Language != occommon.LibraryInfo_LANGUAGE_UNSPECIFIED {
attrs[conventions.AttributeLibraryLanguage] = data.NewAttributeValueString(
ocNode.LibraryInfo.Language.String())
}
}
}
if ocResource != nil {
// Copy resource Labels.
for k, v := range ocResource.Labels {
attrs[k] = data.NewAttributeValueString(v)
}
// Add special fields.
if ocResource.Type != "" {
attrs[conventions.OCAttributeResourceType] = data.NewAttributeValueString(ocResource.Type)
}
}
if len(attrs) != 0 {
// TODO: Re-evaluate if we want to construct a map first, or we can construct directly
// a slice of AttributeKeyValue.
out.Resource().SetAttributes(data.NewAttributeMap(attrs))
}
}