Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit 22bfaed

Browse files
author
Paulo Janotti
authored
Fix duplicated span.kind in Jaeger sender (#346)
* Fix duplicated span.kind in Jaeger sender Fix #329
1 parent f53d802 commit 22bfaed

2 files changed

Lines changed: 83 additions & 2 deletions

File tree

translator/trace/jaegerthrift_to_protospan_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"io/ioutil"
2222
"reflect"
23+
"sort"
2324
"testing"
2425

2526
"github.com/jaegertracing/jaeger/thrift-gen/jaeger"
@@ -30,6 +31,80 @@ import (
3031
"github.com/census-instrumentation/opencensus-service/internal/testutils"
3132
)
3233

34+
func TestJaegerThriftToOCProto_Roundtrip(t *testing.T) {
35+
const numOfFiles = 2
36+
for i := 0; i < numOfFiles; i++ {
37+
// Jaeger binary tags do not round trip from Jaeger -> OCProto -> Jaeger.
38+
// For tests use data without binary tags.
39+
thriftFile := fmt.Sprintf("./testdata/thrift_batch_no_binary_tags_%02d.json", i+1)
40+
wantJBatch := &jaeger.Batch{}
41+
if err := loadFromJSON(thriftFile, wantJBatch); err != nil {
42+
t.Errorf("Failed load Jaeger Thrift from %q: %v", thriftFile, err)
43+
continue
44+
}
45+
46+
// Remove process tag types that are known to not roundtrip (OC Proto has all of these as strings).
47+
cleanTags := make([]*jaeger.Tag, 0, len(wantJBatch.Process.Tags))
48+
for _, jTag := range wantJBatch.Process.Tags {
49+
if jTag.GetVType() == jaeger.TagType_STRING {
50+
cleanTags = append(cleanTags, jTag)
51+
}
52+
}
53+
wantJBatch.Process.Tags = cleanTags
54+
55+
ocBatch, err := JaegerThriftBatchToOCProto(wantJBatch)
56+
if err != nil {
57+
t.Errorf("Failed to read to read Jaeger Thrift from %q: %v", thriftFile, err)
58+
continue
59+
}
60+
61+
gotJBatch, err := OCProtoToJaegerThrift(ocBatch)
62+
if err != nil {
63+
t.Errorf("Failed to translate OC batch to Jaeger Thrift: %v", err)
64+
continue
65+
}
66+
67+
wantSpanCount, gotSpanCount := len(ocBatch.Spans), len(gotJBatch.Spans)
68+
if wantSpanCount != gotSpanCount {
69+
t.Errorf("Different number of spans in the batches on pass #%d (want %d, got %d)", i, wantSpanCount, gotSpanCount)
70+
continue
71+
}
72+
73+
// Sort tags to help with comparison, not only for jaeger.Process but also on each span.
74+
sort.Slice(gotJBatch.Process.Tags, func(i, j int) bool {
75+
return gotJBatch.Process.Tags[i].Key < gotJBatch.Process.Tags[j].Key
76+
})
77+
sort.Slice(wantJBatch.Process.Tags, func(i, j int) bool {
78+
return wantJBatch.Process.Tags[i].Key < wantJBatch.Process.Tags[j].Key
79+
})
80+
var jSpans []*jaeger.Span
81+
jSpans = append(jSpans, gotJBatch.Spans...)
82+
jSpans = append(jSpans, wantJBatch.Spans...)
83+
for _, jSpan := range jSpans {
84+
sort.Slice(jSpan.Tags, func(i, j int) bool {
85+
return jSpan.Tags[i].Key < jSpan.Tags[j].Key
86+
})
87+
sort.Slice(jSpan.Logs, func(i, j int) bool {
88+
sort.Slice(jSpan.Logs[i].Fields, func(k, l int) bool {
89+
return jSpan.Logs[i].Fields[k].Key < jSpan.Logs[i].Fields[l].Key
90+
})
91+
sort.Slice(jSpan.Logs[j].Fields, func(k, l int) bool {
92+
return jSpan.Logs[j].Fields[k].Key < jSpan.Logs[j].Fields[l].Key
93+
})
94+
return jSpan.Logs[i].Timestamp < jSpan.Logs[j].Timestamp
95+
})
96+
}
97+
98+
gjson, _ := json.MarshalIndent(gotJBatch, "", " ")
99+
wjson, _ := json.MarshalIndent(wantJBatch, "", " ")
100+
gjsonStr := testutils.GenerateNormalizedJSON(string(gjson))
101+
wjsonStr := testutils.GenerateNormalizedJSON(string(wjson))
102+
if gjsonStr != wjsonStr {
103+
t.Errorf("OC Proto to Jaeger Thrift failed.\nGot:\n%s\nWant:\n%s\n", gjson, wjson)
104+
}
105+
}
106+
}
107+
33108
func TestJaegerThriftBatchToOCProto(t *testing.T) {
34109
const numOfFiles = 2
35110
for i := 1; i <= 2; i++ {

translator/trace/protospan_to_jaegerthrift.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,14 @@ func ocSpansToJaegerSpans(ocSpans []*tracepb.Span) ([]*jaeger.Span, error) {
208208
Logs: ocTimeEventsToJaegerLogs(ocSpan.TimeEvents),
209209
}
210210

211-
jSpan.Tags = appendJaegerTagFromOCSpanKind(jSpan.Tags, ocSpan.Kind)
211+
if ocSpan.Attributes == nil {
212+
jSpan.Tags = appendJaegerTagFromOCSpanKind(jSpan.Tags, ocSpan.Kind)
213+
} else {
214+
if _, ok := ocSpan.Attributes.AttributeMap["span.kind"]; !ok {
215+
jSpan.Tags = appendJaegerTagFromOCSpanKind(jSpan.Tags, ocSpan.Kind)
216+
}
217+
}
218+
212219
jSpans = append(jSpans, jSpan)
213220
}
214221

@@ -256,7 +263,6 @@ func ocLinksToJaegerReferences(ocSpanLinks *tracepb.Span_Links) ([]*jaeger.SpanR
256263
}
257264

258265
func appendJaegerTagFromOCSpanKind(jTags []*jaeger.Tag, ocSpanKind tracepb.Span_SpanKind) []*jaeger.Tag {
259-
// We could check if the key is already present but it doesn't seem worth at this point.
260266
// TODO: (@pjanotti): Replace any OpenTracing literals by importing github.com/opentracing/opentracing-go/ext?
261267
var tagValue string
262268
switch ocSpanKind {

0 commit comments

Comments
 (0)