/
jaegerproto_to_traces.go
362 lines (306 loc) · 10.5 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
// Copyright The 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 jaeger
import (
"encoding/base64"
"fmt"
"math"
"reflect"
"strconv"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/thrift-gen/jaeger"
"go.opentelemetry.io/collector/consumer/pdata"
otlptrace "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/trace/v1"
"go.opentelemetry.io/collector/translator/conventions"
tracetranslator "go.opentelemetry.io/collector/translator/trace"
)
var blankJaegerProtoSpan = new(jaeger.Span)
// ProtoBatchesToInternalTraces converts multiple Jaeger proto batches to internal traces
func ProtoBatchesToInternalTraces(batches []*model.Batch) pdata.Traces {
traceData := pdata.NewTraces()
if len(batches) == 0 {
return traceData
}
rss := traceData.ResourceSpans()
rss.Resize(len(batches))
i := 0
for _, batch := range batches {
if batch.GetProcess() == nil && len(batch.GetSpans()) == 0 {
continue
}
protoBatchToResourceSpans(*batch, rss.At(i))
i++
}
// reduce traceData.ResourceSpans slice if some batched were skipped
if i < len(batches) {
rss.Resize(i)
}
return traceData
}
// ProtoBatchToInternalTraces converts Jeager proto batch to internal traces
func ProtoBatchToInternalTraces(batch model.Batch) pdata.Traces {
traceData := pdata.NewTraces()
if batch.GetProcess() == nil && len(batch.GetSpans()) == 0 {
return traceData
}
rss := traceData.ResourceSpans()
rss.Resize(1)
protoBatchToResourceSpans(batch, rss.At(0))
return traceData
}
func protoBatchToResourceSpans(batch model.Batch, dest pdata.ResourceSpans) {
jSpans := batch.GetSpans()
jProcessToInternalResource(batch.GetProcess(), dest.Resource())
if len(jSpans) == 0 {
return
}
ilss := dest.InstrumentationLibrarySpans()
ilss.Resize(1)
jSpansToInternal(jSpans, ilss.At(0).Spans())
}
func jProcessToInternalResource(process *model.Process, dest pdata.Resource) {
if process == nil || process.ServiceName == tracetranslator.ResourceNotSet {
return
}
dest.InitEmpty()
serviceName := process.GetServiceName()
if serviceName == tracetranslator.ResourceNoAttrs {
return
}
tags := process.GetTags()
if serviceName == "" && tags == nil {
return
}
attrs := dest.Attributes()
if serviceName != "" {
attrs.InitEmptyWithCapacity(len(tags) + 1)
attrs.UpsertString(conventions.AttributeServiceName, serviceName)
} else {
attrs.InitEmptyWithCapacity(len(tags))
}
jTagsToInternalAttributes(tags, attrs)
// Handle special keys translations.
translateHostnameAttr(attrs)
translateJaegerVersionAttr(attrs)
}
// translateHostnameAttr translates "hostname" atttribute
func translateHostnameAttr(attrs pdata.AttributeMap) {
hostname, hostnameFound := attrs.Get("hostname")
_, convHostnameFound := attrs.Get(conventions.AttributeHostHostname)
if hostnameFound && !convHostnameFound {
attrs.Insert(conventions.AttributeHostHostname, hostname)
attrs.Delete("hostname")
}
}
// translateHostnameAttr translates "jaeger.version" atttribute
func translateJaegerVersionAttr(attrs pdata.AttributeMap) {
jaegerVersion, jaegerVersionFound := attrs.Get("jaeger.version")
_, exporterVersionFound := attrs.Get(conventions.OCAttributeExporterVersion)
if jaegerVersionFound && !exporterVersionFound {
attrs.InsertString(conventions.OCAttributeExporterVersion, "Jaeger-"+jaegerVersion.StringVal())
attrs.Delete("jaeger.version")
}
}
func jSpansToInternal(spans []*model.Span, dest pdata.SpanSlice) {
if len(spans) == 0 {
return
}
dest.Resize(len(spans))
i := 0
for _, span := range spans {
if span == nil || reflect.DeepEqual(span, blankJaegerProtoSpan) {
continue
}
jSpanToInternal(span, dest.At(i))
i++
}
if i < len(spans) {
dest.Resize(i)
}
}
func jSpanToInternal(span *model.Span, dest pdata.Span) {
dest.SetTraceID(pdata.TraceID(tracetranslator.UInt64ToByteTraceID(span.TraceID.High, span.TraceID.Low)))
dest.SetSpanID(pdata.SpanID(tracetranslator.UInt64ToByteSpanID(uint64(span.SpanID))))
dest.SetName(span.OperationName)
dest.SetStartTime(pdata.TimestampUnixNano(uint64(span.StartTime.UnixNano())))
dest.SetEndTime(pdata.TimestampUnixNano(uint64(span.StartTime.Add(span.Duration).UnixNano())))
parentSpanID := span.ParentSpanID()
if parentSpanID != model.SpanID(0) {
dest.SetParentSpanID(pdata.SpanID(tracetranslator.UInt64ToByteSpanID(uint64(parentSpanID))))
}
attrs := dest.Attributes()
attrs.InitEmptyWithCapacity(len(span.Tags))
jTagsToInternalAttributes(span.Tags, attrs)
setInternalSpanStatus(attrs, dest.Status())
if spanKindAttr, ok := attrs.Get(tracetranslator.TagSpanKind); ok {
dest.SetKind(jSpanKindToInternal(spanKindAttr.StringVal()))
attrs.Delete(tracetranslator.TagSpanKind)
}
dest.SetTraceState(getTraceStateFromAttrs(attrs))
// drop the attributes slice if all of them were replaced during translation
if attrs.Len() == 0 {
attrs.InitFromMap(nil)
}
jLogsToSpanEvents(span.Logs, dest.Events())
jReferencesToSpanLinks(span.References, parentSpanID, dest.Links())
}
func jTagsToInternalAttributes(tags []model.KeyValue, dest pdata.AttributeMap) {
for _, tag := range tags {
switch tag.GetVType() {
case model.ValueType_STRING:
dest.UpsertString(tag.Key, tag.GetVStr())
case model.ValueType_BOOL:
dest.UpsertBool(tag.Key, tag.GetVBool())
case model.ValueType_INT64:
dest.UpsertInt(tag.Key, tag.GetVInt64())
case model.ValueType_FLOAT64:
dest.UpsertDouble(tag.Key, tag.GetVFloat64())
case model.ValueType_BINARY:
dest.UpsertString(tag.Key, base64.StdEncoding.EncodeToString(tag.GetVBinary()))
default:
dest.UpsertString(tag.Key, fmt.Sprintf("<Unknown Jaeger TagType %q>", tag.GetVType()))
}
}
}
func setInternalSpanStatus(attrs pdata.AttributeMap, dest pdata.SpanStatus) {
statusCode := pdata.StatusCode(otlptrace.Status_Ok)
statusMessage := ""
statusExists := false
if errorVal, ok := attrs.Get(tracetranslator.TagError); ok {
if errorVal.BoolVal() {
statusCode = pdata.StatusCode(otlptrace.Status_UnknownError)
attrs.Delete(tracetranslator.TagError)
statusExists = true
}
}
if codeAttr, ok := attrs.Get(tracetranslator.TagStatusCode); ok {
statusExists = true
if code, err := getStatusCodeFromAttr(codeAttr); err == nil {
statusCode = code
attrs.Delete(tracetranslator.TagStatusCode)
}
if msgAttr, ok := attrs.Get(tracetranslator.TagStatusMsg); ok {
statusMessage = msgAttr.StringVal()
attrs.Delete(tracetranslator.TagStatusMsg)
}
} else if httpCodeAttr, ok := attrs.Get(tracetranslator.TagHTTPStatusCode); ok {
statusExists = true
if code, err := getStatusCodeFromHTTPStatusAttr(httpCodeAttr); err == nil {
// Do not set status code to OK in case it was set to Unknown based on "error" tag
if code != pdata.StatusCode(otlptrace.Status_Ok) {
statusCode = code
}
if msgAttr, ok := attrs.Get(tracetranslator.TagHTTPStatusMsg); ok {
statusMessage = msgAttr.StringVal()
}
}
}
if statusExists {
dest.InitEmpty()
dest.SetCode(statusCode)
dest.SetMessage(statusMessage)
}
}
func getStatusCodeFromAttr(attrVal pdata.AttributeValue) (pdata.StatusCode, error) {
var codeVal int64
switch attrVal.Type() {
case pdata.AttributeValueINT:
codeVal = attrVal.IntVal()
case pdata.AttributeValueSTRING:
i, err := strconv.Atoi(attrVal.StringVal())
if err != nil {
return pdata.StatusCode(0), err
}
codeVal = int64(i)
default:
return pdata.StatusCode(0), fmt.Errorf("invalid status code attribute type: %s", attrVal.Type().String())
}
if codeVal > math.MaxInt32 || codeVal < math.MinInt32 {
return pdata.StatusCode(0), fmt.Errorf("invalid status code value: %d", codeVal)
}
return pdata.StatusCode(codeVal), nil
}
func getStatusCodeFromHTTPStatusAttr(attrVal pdata.AttributeValue) (pdata.StatusCode, error) {
statusCode, err := getStatusCodeFromAttr(attrVal)
if err != nil {
return pdata.StatusCode(0), err
}
// TODO: Introduce and use new HTTP -> OTLP code translator instead
return pdata.StatusCode(tracetranslator.OCStatusCodeFromHTTP(int32(statusCode))), nil
}
func jSpanKindToInternal(spanKind string) pdata.SpanKind {
switch spanKind {
case "client":
return pdata.SpanKindCLIENT
case "server":
return pdata.SpanKindSERVER
case "producer":
return pdata.SpanKindPRODUCER
case "consumer":
return pdata.SpanKindCONSUMER
case "internal":
return pdata.SpanKindINTERNAL
}
return pdata.SpanKindUNSPECIFIED
}
func jLogsToSpanEvents(logs []model.Log, dest pdata.SpanEventSlice) {
if len(logs) == 0 {
return
}
dest.Resize(len(logs))
for i, log := range logs {
event := dest.At(i)
event.SetTimestamp(pdata.TimestampUnixNano(uint64(log.Timestamp.UnixNano())))
if len(log.Fields) == 0 {
continue
}
attrs := event.Attributes()
attrs.InitEmptyWithCapacity(len(log.Fields))
jTagsToInternalAttributes(log.Fields, attrs)
if name, ok := attrs.Get(tracetranslator.TagMessage); ok {
event.SetName(name.StringVal())
attrs.Delete(tracetranslator.TagMessage)
}
}
}
// jReferencesToSpanLinks sets internal span links based on jaeger span references skipping excludeParentID
func jReferencesToSpanLinks(refs []model.SpanRef, excludeParentID model.SpanID, dest pdata.SpanLinkSlice) {
if len(refs) == 0 || len(refs) == 1 && refs[0].SpanID == excludeParentID && refs[0].RefType == model.ChildOf {
return
}
dest.Resize(len(refs))
i := 0
for _, ref := range refs {
link := dest.At(i)
if ref.SpanID == excludeParentID && ref.RefType == model.ChildOf {
continue
}
link.SetTraceID(pdata.NewTraceID(tracetranslator.UInt64ToByteTraceID(ref.TraceID.High, ref.TraceID.Low)))
link.SetSpanID(pdata.NewSpanID(tracetranslator.UInt64ToByteSpanID(uint64(ref.SpanID))))
i++
}
// Reduce slice size in case if excludeParentID was skipped
if i < len(refs) {
dest.Resize(i)
}
}
func getTraceStateFromAttrs(attrs pdata.AttributeMap) pdata.TraceState {
traceState := pdata.TraceStateEmpty
// TODO Bring this inline with solution for jaegertracing/jaeger-client-java #702 once available
if attr, ok := attrs.Get(tracetranslator.TagW3CTraceState); ok {
traceState = pdata.TraceState(attr.StringVal())
attrs.Delete(tracetranslator.TagW3CTraceState)
}
return traceState
}