-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
jaegerproto_to_traces.go
469 lines (405 loc) · 14.1 KB
/
jaegerproto_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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package jaeger // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger"
import (
"encoding/binary"
"fmt"
"hash/fnv"
"reflect"
"strconv"
"strings"
"github.com/jaegertracing/jaeger/model"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
conventions "go.opentelemetry.io/collector/semconv/v1.16.0"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/idutils"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/occonventions"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/tracetranslator"
)
var blankJaegerProtoSpan = new(model.Span)
// ProtoToTraces converts multiple Jaeger proto batches to internal traces
func ProtoToTraces(batches []*model.Batch) (ptrace.Traces, error) {
traceData := ptrace.NewTraces()
if len(batches) == 0 {
return traceData, nil
}
batches = regroup(batches)
rss := traceData.ResourceSpans()
rss.EnsureCapacity(len(batches))
for _, batch := range batches {
if batch.GetProcess() == nil && len(batch.GetSpans()) == 0 {
continue
}
protoBatchToResourceSpans(*batch, rss.AppendEmpty())
}
return traceData, nil
}
func regroup(batches []*model.Batch) []*model.Batch {
// Re-group batches
// This is needed as there might be a Process within Batch and Span at the same
// time, with the span one taking precedence.
// As we only have it at one level in OpenTelemetry, ResourceSpans, we split
// each batch into potentially multiple other batches, with the sum of their
// processes as the key to a map.
// Step 1) iterate over the batches
// Step 2) for each batch, calculate the batch's process checksum and store
// it on a map, with the checksum as the key and the process as the value
// Step 3) iterate the spans for a batch: if a given span has its own process,
// calculate the checksum for the process and store it on the same map
// Step 4) each entry on the map becomes a ResourceSpan
registry := map[uint64]*model.Batch{}
for _, batch := range batches {
bb := batchForProcess(registry, batch.Process)
for _, span := range batch.Spans {
if span.Process == nil {
bb.Spans = append(bb.Spans, span)
} else {
b := batchForProcess(registry, span.Process)
b.Spans = append(b.Spans, span)
}
}
}
result := make([]*model.Batch, 0, len(registry))
for _, v := range registry {
result = append(result, v)
}
return result
}
func batchForProcess(registry map[uint64]*model.Batch, p *model.Process) *model.Batch {
sum := checksum(p)
batch := registry[sum]
if batch == nil {
batch = &model.Batch{
Process: p,
}
registry[sum] = batch
}
return batch
}
func checksum(process *model.Process) uint64 {
// this will get all the keys and values, plus service name, into this buffer
// this is potentially dangerous, as a batch/span with a big enough processes
// might cause the collector to allocate this extra big information
// for this reason, we hash it as an integer and return it, instead of keeping
// all the hashes for all the processes for all batches in memory
fnvHash := fnv.New64a()
if process != nil {
// this effectively means that all spans from batches with nil processes
// will be grouped together
// this should only ever happen in unit tests
// this implementation never returns an error according to the Hash interface
_ = process.Hash(fnvHash)
}
out := make([]byte, 0, 16)
out = fnvHash.Sum(out)
return binary.BigEndian.Uint64(out)
}
func protoBatchToResourceSpans(batch model.Batch, dest ptrace.ResourceSpans) {
jSpans := batch.GetSpans()
jProcessToInternalResource(batch.GetProcess(), dest.Resource())
if len(jSpans) == 0 {
return
}
jSpansToInternal(jSpans, dest.ScopeSpans())
}
func jProcessToInternalResource(process *model.Process, dest pcommon.Resource) {
if process == nil || process.ServiceName == tracetranslator.ResourceNoServiceName {
return
}
serviceName := process.ServiceName
tags := process.Tags
if serviceName == "" && tags == nil {
return
}
attrs := dest.Attributes()
if serviceName != "" {
attrs.EnsureCapacity(len(tags) + 1)
attrs.PutStr(conventions.AttributeServiceName, serviceName)
} else {
attrs.EnsureCapacity(len(tags))
}
jTagsToInternalAttributes(tags, attrs)
// Handle special keys translations.
translateHostnameAttr(attrs)
translateJaegerVersionAttr(attrs)
}
// translateHostnameAttr translates "hostname" atttribute
func translateHostnameAttr(attrs pcommon.Map) {
hostname, hostnameFound := attrs.Get("hostname")
_, convHostNameFound := attrs.Get(conventions.AttributeHostName)
if hostnameFound && !convHostNameFound {
hostname.CopyTo(attrs.PutEmpty(conventions.AttributeHostName))
attrs.Remove("hostname")
}
}
// translateHostnameAttr translates "jaeger.version" atttribute
func translateJaegerVersionAttr(attrs pcommon.Map) {
jaegerVersion, jaegerVersionFound := attrs.Get("jaeger.version")
_, exporterVersionFound := attrs.Get(occonventions.AttributeExporterVersion)
if jaegerVersionFound && !exporterVersionFound {
attrs.PutStr(occonventions.AttributeExporterVersion, "Jaeger-"+jaegerVersion.Str())
attrs.Remove("jaeger.version")
}
}
type scope struct {
name, version string
}
func jSpansToInternal(spans []*model.Span, dest ptrace.ScopeSpansSlice) {
spansByLibrary := make(map[scope]ptrace.SpanSlice)
for _, span := range spans {
if span == nil || reflect.DeepEqual(span, blankJaegerProtoSpan) {
continue
}
il := getScope(span)
sps, found := spansByLibrary[il]
if !found {
ss := dest.AppendEmpty()
ss.Scope().SetName(il.name)
ss.Scope().SetVersion(il.version)
sps = ss.Spans()
spansByLibrary[il] = sps
}
jSpanToInternal(span, sps.AppendEmpty())
}
}
func jSpanToInternal(span *model.Span, dest ptrace.Span) {
dest.SetTraceID(idutils.UInt64ToTraceID(span.TraceID.High, span.TraceID.Low))
dest.SetSpanID(idutils.UInt64ToSpanID(uint64(span.SpanID)))
dest.SetName(span.OperationName)
dest.SetStartTimestamp(pcommon.NewTimestampFromTime(span.StartTime))
dest.SetEndTimestamp(pcommon.NewTimestampFromTime(span.StartTime.Add(span.Duration)))
parentSpanID := span.ParentSpanID()
if parentSpanID != model.SpanID(0) {
dest.SetParentSpanID(idutils.UInt64ToSpanID(uint64(parentSpanID)))
}
attrs := dest.Attributes()
attrs.EnsureCapacity(len(span.Tags))
jTagsToInternalAttributes(span.Tags, attrs)
if spanKindAttr, ok := attrs.Get(tracetranslator.TagSpanKind); ok {
dest.SetKind(jSpanKindToInternal(spanKindAttr.Str()))
attrs.Remove(tracetranslator.TagSpanKind)
}
setInternalSpanStatus(attrs, dest)
dest.TraceState().FromRaw(getTraceStateFromAttrs(attrs))
// drop the attributes slice if all of them were replaced during translation
if attrs.Len() == 0 {
attrs.Clear()
}
jLogsToSpanEvents(span.Logs, dest.Events())
jReferencesToSpanLinks(span.References, parentSpanID, dest.Links())
}
func jTagsToInternalAttributes(tags []model.KeyValue, dest pcommon.Map) {
for _, tag := range tags {
switch tag.GetVType() {
case model.ValueType_STRING:
dest.PutStr(tag.Key, tag.GetVStr())
case model.ValueType_BOOL:
dest.PutBool(tag.Key, tag.GetVBool())
case model.ValueType_INT64:
dest.PutInt(tag.Key, tag.GetVInt64())
case model.ValueType_FLOAT64:
dest.PutDouble(tag.Key, tag.GetVFloat64())
case model.ValueType_BINARY:
dest.PutEmptyBytes(tag.Key).FromRaw(tag.GetVBinary())
default:
dest.PutStr(tag.Key, fmt.Sprintf("<Unknown Jaeger TagType %q>", tag.GetVType()))
}
}
}
func setInternalSpanStatus(attrs pcommon.Map, span ptrace.Span) {
dest := span.Status()
statusCode := ptrace.StatusCodeUnset
statusMessage := ""
statusExists := false
if errorVal, ok := attrs.Get(tracetranslator.TagError); ok && errorVal.Type() == pcommon.ValueTypeBool {
if errorVal.Bool() {
statusCode = ptrace.StatusCodeError
attrs.Remove(tracetranslator.TagError)
statusExists = true
if desc, ok := extractStatusDescFromAttr(attrs); ok {
statusMessage = desc
} else if descAttr, ok := attrs.Get(tracetranslator.TagHTTPStatusMsg); ok {
statusMessage = descAttr.Str()
}
}
}
if codeAttr, ok := attrs.Get(conventions.OtelStatusCode); ok {
if !statusExists {
// The error tag is the ultimate truth for a Jaeger spans' error
// status. Only parse the otel.status_code tag if the error tag is
// not set to true.
statusExists = true
switch strings.ToUpper(codeAttr.Str()) {
case statusOk:
statusCode = ptrace.StatusCodeOk
case statusError:
statusCode = ptrace.StatusCodeError
}
if desc, ok := extractStatusDescFromAttr(attrs); ok {
statusMessage = desc
}
}
// Regardless of error tag value, remove the otel.status_code tag. The
// otel.status_message tag will have already been removed if
// statusExists is true.
attrs.Remove(conventions.OtelStatusCode)
} else if httpCodeAttr, ok := attrs.Get(conventions.AttributeHTTPStatusCode); !statusExists && ok {
// Fallback to introspecting if this span represents a failed HTTP
// request or response, but again, only do so if the `error` tag was
// not set to true and no explicit status was sent.
if code, err := getStatusCodeFromHTTPStatusAttr(httpCodeAttr, span.Kind()); err == nil {
if code != ptrace.StatusCodeUnset {
statusExists = true
statusCode = code
}
if msgAttr, ok := attrs.Get(tracetranslator.TagHTTPStatusMsg); ok {
statusMessage = msgAttr.Str()
}
}
}
if statusExists {
dest.SetCode(statusCode)
dest.SetMessage(statusMessage)
}
}
// extractStatusDescFromAttr returns the OTel status description from attrs
// along with true if it is set. Otherwise, an empty string and false are
// returned. The OTel status description attribute is deleted from attrs in
// the process.
func extractStatusDescFromAttr(attrs pcommon.Map) (string, bool) {
if msgAttr, ok := attrs.Get(conventions.OtelStatusDescription); ok {
msg := msgAttr.Str()
attrs.Remove(conventions.OtelStatusDescription)
return msg, true
}
return "", false
}
// codeFromAttr returns the integer code value from attrVal. An error is
// returned if the code is not represented by an integer or string value in
// the attrVal or the value is outside the bounds of an int representation.
func codeFromAttr(attrVal pcommon.Value) (int64, error) {
var val int64
switch attrVal.Type() {
case pcommon.ValueTypeInt:
val = attrVal.Int()
case pcommon.ValueTypeStr:
var err error
val, err = strconv.ParseInt(attrVal.Str(), 10, 0)
if err != nil {
return 0, err
}
default:
return 0, fmt.Errorf("%w: %s", errType, attrVal.Type().String())
}
return val, nil
}
func getStatusCodeFromHTTPStatusAttr(attrVal pcommon.Value, kind ptrace.SpanKind) (ptrace.StatusCode, error) {
statusCode, err := codeFromAttr(attrVal)
if err != nil {
return ptrace.StatusCodeUnset, err
}
// For HTTP status codes in the 4xx range span status MUST be left unset
// in case of SpanKind.SERVER and MUST be set to Error in case of SpanKind.CLIENT.
// For HTTP status codes in the 5xx range, as well as any other code the client
// failed to interpret, span status MUST be set to Error.
if statusCode >= 400 && statusCode < 500 {
switch kind {
case ptrace.SpanKindClient:
return ptrace.StatusCodeError, nil
case ptrace.SpanKindServer:
return ptrace.StatusCodeUnset, nil
}
}
return tracetranslator.StatusCodeFromHTTP(statusCode), nil
}
func jSpanKindToInternal(spanKind string) ptrace.SpanKind {
switch spanKind {
case "client":
return ptrace.SpanKindClient
case "server":
return ptrace.SpanKindServer
case "producer":
return ptrace.SpanKindProducer
case "consumer":
return ptrace.SpanKindConsumer
case "internal":
return ptrace.SpanKindInternal
}
return ptrace.SpanKindUnspecified
}
func jLogsToSpanEvents(logs []model.Log, dest ptrace.SpanEventSlice) {
if len(logs) == 0 {
return
}
dest.EnsureCapacity(len(logs))
for i, log := range logs {
var event ptrace.SpanEvent
if dest.Len() > i {
event = dest.At(i)
} else {
event = dest.AppendEmpty()
}
event.SetTimestamp(pcommon.NewTimestampFromTime(log.Timestamp))
if len(log.Fields) == 0 {
continue
}
attrs := event.Attributes()
attrs.EnsureCapacity(len(log.Fields))
jTagsToInternalAttributes(log.Fields, attrs)
if name, ok := attrs.Get(eventNameAttr); ok {
event.SetName(name.Str())
attrs.Remove(eventNameAttr)
}
}
}
// jReferencesToSpanLinks sets internal span links based on jaeger span references skipping excludeParentID
func jReferencesToSpanLinks(refs []model.SpanRef, excludeParentID model.SpanID, dest ptrace.SpanLinkSlice) {
if len(refs) == 0 || len(refs) == 1 && refs[0].SpanID == excludeParentID && refs[0].RefType == model.ChildOf {
return
}
dest.EnsureCapacity(len(refs))
for _, ref := range refs {
if ref.SpanID == excludeParentID && ref.RefType == model.ChildOf {
continue
}
link := dest.AppendEmpty()
link.SetTraceID(idutils.UInt64ToTraceID(ref.TraceID.High, ref.TraceID.Low))
link.SetSpanID(idutils.UInt64ToSpanID(uint64(ref.SpanID)))
link.Attributes().PutStr(conventions.AttributeOpentracingRefType, jRefTypeToAttribute(ref.RefType))
}
}
func getTraceStateFromAttrs(attrs pcommon.Map) string {
traceState := ""
// TODO Bring this inline with solution for jaegertracing/jaeger-client-java #702 once available
if attr, ok := attrs.Get(tracetranslator.TagW3CTraceState); ok {
traceState = attr.Str()
attrs.Remove(tracetranslator.TagW3CTraceState)
}
return traceState
}
func getScope(span *model.Span) scope {
il := scope{}
if libraryName, ok := getAndDeleteTag(span, conventions.AttributeOtelScopeName); ok {
il.name = libraryName
if libraryVersion, ok := getAndDeleteTag(span, conventions.AttributeOtelScopeVersion); ok {
il.version = libraryVersion
}
}
return il
}
func getAndDeleteTag(span *model.Span, key string) (string, bool) {
for i := range span.Tags {
if span.Tags[i].Key == key {
value := span.Tags[i].GetVStr()
span.Tags = append(span.Tags[:i], span.Tags[i+1:]...)
return value, true
}
}
return "", false
}
func jRefTypeToAttribute(ref model.SpanRefType) string {
if ref == model.ChildOf {
return conventions.AttributeOpentracingRefTypeChildOf
}
return conventions.AttributeOpentracingRefTypeFollowsFrom
}