@@ -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.\n Got:\n %s\n Want:\n %s\n " , gjson , wjson )
104+ }
105+ }
106+ }
107+
33108func TestJaegerThriftBatchToOCProto (t * testing.T ) {
34109 const numOfFiles = 2
35110 for i := 1 ; i <= 2 ; i ++ {
0 commit comments