-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
traces_to_jaegerproto.go
422 lines (369 loc) · 11.6 KB
/
traces_to_jaegerproto.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package jaeger // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger"
import (
"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/tracetranslator"
)
// ProtoFromTraces translates internal trace data into the Jaeger Proto for GRPC.
// Returns slice of translated Jaeger batches and error if translation failed.
func ProtoFromTraces(td ptrace.Traces) []*model.Batch {
resourceSpans := td.ResourceSpans()
if resourceSpans.Len() == 0 {
return nil
}
batches := make([]*model.Batch, 0, resourceSpans.Len())
for i := 0; i < resourceSpans.Len(); i++ {
rs := resourceSpans.At(i)
batch := resourceSpansToJaegerProto(rs)
if batch != nil {
batches = append(batches, batch)
}
}
return batches
}
func resourceSpansToJaegerProto(rs ptrace.ResourceSpans) *model.Batch {
resource := rs.Resource()
ilss := rs.ScopeSpans()
if resource.Attributes().Len() == 0 && ilss.Len() == 0 {
return nil
}
batch := &model.Batch{
Process: resourceToJaegerProtoProcess(resource),
}
if ilss.Len() == 0 {
return batch
}
// Approximate the number of the spans as the number of the spans in the first
// instrumentation library info.
jSpans := make([]*model.Span, 0, ilss.At(0).Spans().Len())
for i := 0; i < ilss.Len(); i++ {
ils := ilss.At(i)
spans := ils.Spans()
for j := 0; j < spans.Len(); j++ {
span := spans.At(j)
jSpan := spanToJaegerProto(span, ils.Scope())
if jSpan != nil {
jSpans = append(jSpans, jSpan)
}
}
}
batch.Spans = jSpans
return batch
}
func resourceToJaegerProtoProcess(resource pcommon.Resource) *model.Process {
process := &model.Process{}
attrs := resource.Attributes()
if attrs.Len() == 0 {
process.ServiceName = tracetranslator.ResourceNoServiceName
return process
}
attrsCount := attrs.Len()
if serviceName, ok := attrs.Get(conventions.AttributeServiceName); ok {
process.ServiceName = serviceName.Str()
attrsCount--
}
if attrsCount == 0 {
return process
}
tags := make([]model.KeyValue, 0, attrsCount)
process.Tags = appendTagsFromResourceAttributes(tags, attrs)
return process
}
func appendTagsFromResourceAttributes(dest []model.KeyValue, attrs pcommon.Map) []model.KeyValue {
if attrs.Len() == 0 {
return dest
}
attrs.Range(func(key string, attr pcommon.Value) bool {
if key == conventions.AttributeServiceName {
return true
}
dest = append(dest, attributeToJaegerProtoTag(key, attr))
return true
})
return dest
}
func appendTagsFromAttributes(dest []model.KeyValue, attrs pcommon.Map) []model.KeyValue {
if attrs.Len() == 0 {
return dest
}
attrs.Range(func(key string, attr pcommon.Value) bool {
dest = append(dest, attributeToJaegerProtoTag(key, attr))
return true
})
return dest
}
func attributeToJaegerProtoTag(key string, attr pcommon.Value) model.KeyValue {
tag := model.KeyValue{Key: key}
switch attr.Type() {
case pcommon.ValueTypeStr:
tag.VType = model.ValueType_STRING
tag.VStr = attr.Str()
case pcommon.ValueTypeInt:
tag.VType = model.ValueType_INT64
tag.VInt64 = attr.Int()
case pcommon.ValueTypeBool:
tag.VType = model.ValueType_BOOL
tag.VBool = attr.Bool()
case pcommon.ValueTypeDouble:
tag.VType = model.ValueType_FLOAT64
tag.VFloat64 = attr.Double()
case pcommon.ValueTypeBytes:
tag.VType = model.ValueType_BINARY
tag.VBinary = attr.Bytes().AsRaw()
case pcommon.ValueTypeMap, pcommon.ValueTypeSlice:
tag.VType = model.ValueType_STRING
tag.VStr = attr.AsString()
}
return tag
}
func spanToJaegerProto(span ptrace.Span, libraryTags pcommon.InstrumentationScope) *model.Span {
traceID := traceIDToJaegerProto(span.TraceID())
jReferences := makeJaegerProtoReferences(span.Links(), spanIDToJaegerProto(span.ParentSpanID()), traceID)
startTime := span.StartTimestamp().AsTime()
return &model.Span{
TraceID: traceID,
SpanID: spanIDToJaegerProto(span.SpanID()),
OperationName: span.Name(),
References: jReferences,
StartTime: startTime,
Duration: span.EndTimestamp().AsTime().Sub(startTime),
Tags: getJaegerProtoSpanTags(span, libraryTags),
Logs: spanEventsToJaegerProtoLogs(span.Events()),
}
}
func getJaegerProtoSpanTags(span ptrace.Span, scope pcommon.InstrumentationScope) []model.KeyValue {
var spanKindTag, statusCodeTag, errorTag, statusMsgTag model.KeyValue
var spanKindTagFound, statusCodeTagFound, errorTagFound, statusMsgTagFound bool
libraryTags, libraryTagsFound := getTagsFromInstrumentationLibrary(scope)
tagsCount := span.Attributes().Len() + len(libraryTags)
spanKindTag, spanKindTagFound = getTagFromSpanKind(span.Kind())
if spanKindTagFound {
tagsCount++
}
status := span.Status()
statusCodeTag, statusCodeTagFound = getTagFromStatusCode(status.Code())
if statusCodeTagFound {
tagsCount++
}
errorTag, errorTagFound = getErrorTagFromStatusCode(status.Code())
if errorTagFound {
tagsCount++
}
statusMsgTag, statusMsgTagFound = getTagFromStatusMsg(status.Message())
if statusMsgTagFound {
tagsCount++
}
traceStateTags, traceStateTagsFound := getTagsFromTraceState(span.TraceState().AsRaw())
if traceStateTagsFound {
tagsCount += len(traceStateTags)
}
if tagsCount == 0 {
return nil
}
tags := make([]model.KeyValue, 0, tagsCount)
if libraryTagsFound {
tags = append(tags, libraryTags...)
}
tags = appendTagsFromAttributes(tags, span.Attributes())
if spanKindTagFound {
tags = append(tags, spanKindTag)
}
if statusCodeTagFound {
tags = append(tags, statusCodeTag)
}
if errorTagFound {
tags = append(tags, errorTag)
}
if statusMsgTagFound {
tags = append(tags, statusMsgTag)
}
if traceStateTagsFound {
tags = append(tags, traceStateTags...)
}
return tags
}
func traceIDToJaegerProto(traceID pcommon.TraceID) model.TraceID {
traceIDHigh, traceIDLow := idutils.TraceIDToUInt64Pair(traceID)
return model.TraceID{
Low: traceIDLow,
High: traceIDHigh,
}
}
func spanIDToJaegerProto(spanID pcommon.SpanID) model.SpanID {
return model.SpanID(idutils.SpanIDToUInt64(spanID))
}
// makeJaegerProtoReferences constructs jaeger span references based on parent span ID and span links.
// The parent span ID is used to add a CHILD_OF reference, _unless_ it is referenced from one of the links.
func makeJaegerProtoReferences(links ptrace.SpanLinkSlice, parentSpanID model.SpanID, traceID model.TraceID) []model.SpanRef {
refsCount := links.Len()
if parentSpanID != 0 {
refsCount++
}
if refsCount == 0 {
return nil
}
refs := make([]model.SpanRef, 0, refsCount)
// Put parent span ID at the first place because usually backends look for it
// as the first CHILD_OF item in the model.SpanRef slice.
if parentSpanID != 0 {
refs = append(refs, model.SpanRef{
TraceID: traceID,
SpanID: parentSpanID,
RefType: model.SpanRefType_CHILD_OF,
})
}
for i := 0; i < links.Len(); i++ {
link := links.At(i)
linkTraceID := traceIDToJaegerProto(link.TraceID())
linkSpanID := spanIDToJaegerProto(link.SpanID())
linkRefType := refTypeFromLink(link)
if parentSpanID != 0 && linkTraceID == traceID && linkSpanID == parentSpanID {
// We already added a reference to this span, but maybe with the wrong type, so override.
refs[0].RefType = linkRefType
continue
}
refs = append(refs, model.SpanRef{
TraceID: traceIDToJaegerProto(link.TraceID()),
SpanID: spanIDToJaegerProto(link.SpanID()),
RefType: refTypeFromLink(link),
})
}
return refs
}
func spanEventsToJaegerProtoLogs(events ptrace.SpanEventSlice) []model.Log {
if events.Len() == 0 {
return nil
}
logs := make([]model.Log, 0, events.Len())
for i := 0; i < events.Len(); i++ {
event := events.At(i)
fields := make([]model.KeyValue, 0, event.Attributes().Len()+1)
_, eventAttrFound := event.Attributes().Get(eventNameAttr)
if event.Name() != "" && !eventAttrFound {
fields = append(fields, model.KeyValue{
Key: eventNameAttr,
VType: model.ValueType_STRING,
VStr: event.Name(),
})
}
fields = appendTagsFromAttributes(fields, event.Attributes())
logs = append(logs, model.Log{
Timestamp: event.Timestamp().AsTime(),
Fields: fields,
})
}
return logs
}
func getTagFromSpanKind(spanKind ptrace.SpanKind) (model.KeyValue, bool) {
var tagStr string
switch spanKind {
case ptrace.SpanKindClient:
tagStr = string(tracetranslator.OpenTracingSpanKindClient)
case ptrace.SpanKindServer:
tagStr = string(tracetranslator.OpenTracingSpanKindServer)
case ptrace.SpanKindProducer:
tagStr = string(tracetranslator.OpenTracingSpanKindProducer)
case ptrace.SpanKindConsumer:
tagStr = string(tracetranslator.OpenTracingSpanKindConsumer)
case ptrace.SpanKindInternal:
tagStr = string(tracetranslator.OpenTracingSpanKindInternal)
default:
return model.KeyValue{}, false
}
return model.KeyValue{
Key: tracetranslator.TagSpanKind,
VType: model.ValueType_STRING,
VStr: tagStr,
}, true
}
func getTagFromStatusCode(statusCode ptrace.StatusCode) (model.KeyValue, bool) {
switch statusCode {
case ptrace.StatusCodeError:
return model.KeyValue{
Key: conventions.OtelStatusCode,
VType: model.ValueType_STRING,
VStr: statusError,
}, true
case ptrace.StatusCodeOk:
return model.KeyValue{
Key: conventions.OtelStatusCode,
VType: model.ValueType_STRING,
VStr: statusOk,
}, true
}
return model.KeyValue{}, false
}
func getErrorTagFromStatusCode(statusCode ptrace.StatusCode) (model.KeyValue, bool) {
if statusCode == ptrace.StatusCodeError {
return model.KeyValue{
Key: tracetranslator.TagError,
VBool: true,
VType: model.ValueType_BOOL,
}, true
}
return model.KeyValue{}, false
}
func getTagFromStatusMsg(statusMsg string) (model.KeyValue, bool) {
if statusMsg == "" {
return model.KeyValue{}, false
}
return model.KeyValue{
Key: conventions.OtelStatusDescription,
VStr: statusMsg,
VType: model.ValueType_STRING,
}, true
}
func getTagsFromTraceState(traceState string) ([]model.KeyValue, bool) {
var keyValues []model.KeyValue
exists := traceState != ""
if exists {
// TODO Bring this inline with solution for jaegertracing/jaeger-client-java #702 once available
kv := model.KeyValue{
Key: tracetranslator.TagW3CTraceState,
VStr: traceState,
VType: model.ValueType_STRING,
}
keyValues = append(keyValues, kv)
}
return keyValues, exists
}
func getTagsFromInstrumentationLibrary(il pcommon.InstrumentationScope) ([]model.KeyValue, bool) {
var keyValues []model.KeyValue
if ilName := il.Name(); ilName != "" {
kv := model.KeyValue{
Key: conventions.AttributeOtelScopeName,
VStr: ilName,
VType: model.ValueType_STRING,
}
keyValues = append(keyValues, kv)
}
if ilVersion := il.Version(); ilVersion != "" {
kv := model.KeyValue{
Key: conventions.AttributeOtelScopeVersion,
VStr: ilVersion,
VType: model.ValueType_STRING,
}
keyValues = append(keyValues, kv)
}
return keyValues, true
}
func refTypeFromLink(link ptrace.SpanLink) model.SpanRefType {
refTypeAttr, ok := link.Attributes().Get(conventions.AttributeOpentracingRefType)
if !ok {
return model.SpanRefType_FOLLOWS_FROM
}
return strToJRefType(refTypeAttr.Str())
}
func strToJRefType(attr string) model.SpanRefType {
if attr == conventions.AttributeOpentracingRefTypeChildOf {
return model.ChildOf
}
// There are only 2 types of SpanRefType we assume that everything
// that's not a model.ChildOf is a model.FollowsFrom
return model.FollowsFrom
}