5
5
"fmt"
6
6
"strings"
7
7
"testing"
8
+ "time"
8
9
9
10
"github.com/Shopify/sarama"
10
11
"github.com/gogo/protobuf/jsonpb"
@@ -21,6 +22,7 @@ func TestJaegerMarshalerCurer(t *testing.T) {
21
22
maxMessageBytes := 1024
22
23
maxAttributeValueSize := 256
23
24
jsonMarshaler := & jsonpb.Marshaler {}
25
+ ts := pcommon .NewTimestampFromTime (time .Now ())
24
26
25
27
td := ptrace .NewTraces ()
26
28
rs := td .ResourceSpans ().AppendEmpty ()
@@ -59,6 +61,38 @@ func TestJaegerMarshalerCurer(t *testing.T) {
59
61
}
60
62
span .Attributes ().Insert ("big-tag" , pcommon .NewValueString (createLongString (maxMessageBytes , "a" )))
61
63
64
+ // Will cure this span by curing the span logs
65
+ span = ils .Spans ().AppendEmpty ()
66
+ span .SetName ("bar" )
67
+ span .SetStartTimestamp (pcommon .Timestamp (101 ))
68
+ span .SetEndTimestamp (pcommon .Timestamp (226 ))
69
+ span .SetTraceID (pcommon .NewTraceID ([16 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 }))
70
+ span .SetSpanID (pcommon .NewSpanID ([8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 }))
71
+ span .Attributes ().Insert ("tag10" , pcommon .NewValueString ("tag10-val" ))
72
+ span .Attributes ().Insert ("tag11" , pcommon .NewValueString ("tag11-val" ))
73
+ // Add events to span
74
+ for i := 0 ; i < 128 ; i ++ {
75
+ se := span .Events ().AppendEmpty ()
76
+ se .SetName (createLongString (1 , "a" ))
77
+ se .SetTimestamp (ts )
78
+ }
79
+
80
+ // Will be unable to cure this span
81
+ span = ils .Spans ().AppendEmpty ()
82
+ span .SetName ("bar" )
83
+ span .SetStartTimestamp (pcommon .Timestamp (102 ))
84
+ span .SetEndTimestamp (pcommon .Timestamp (227 ))
85
+ span .SetTraceID (pcommon .NewTraceID ([16 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 }))
86
+ span .SetSpanID (pcommon .NewSpanID ([8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 }))
87
+ span .Attributes ().Insert ("tag10" , pcommon .NewValueString ("tag10-val" ))
88
+ span .Attributes ().Insert ("tag11" , pcommon .NewValueString ("tag11-val" ))
89
+ // Add events to span
90
+ for i := 0 ; i < 1024 ; i ++ {
91
+ se := span .Events ().AppendEmpty ()
92
+ se .SetName (createLongString (1 , "a" ))
93
+ se .SetTimestamp (ts )
94
+ }
95
+
62
96
batches , err := jaeger .ProtoFromTraces (td )
63
97
require .NoError (t , err )
64
98
@@ -71,7 +105,7 @@ func TestJaegerMarshalerCurer(t *testing.T) {
71
105
jsonByteBuffer0 := new (bytes.Buffer )
72
106
require .NoError (t , jsonMarshaler .Marshal (jsonByteBuffer0 , batches [0 ].Spans [0 ]))
73
107
74
- // Get the marshalled bytes of the 2nd span that cannot be cured. Will be the needed for expected value of the tests
108
+ // Get the marshalled bytes of the 3rd span that cannot be cured. Will be the needed for expected value of the tests
75
109
// depending on whether dropSpans is turned on or not.
76
110
batches [0 ].Spans [2 ].Process = batches [0 ].Process
77
111
jaegerProtoBytes2 , err := batches [0 ].Spans [2 ].Marshal ()
@@ -81,8 +115,19 @@ func TestJaegerMarshalerCurer(t *testing.T) {
81
115
jsonByteBuffer2 := new (bytes.Buffer )
82
116
require .NoError (t , jsonMarshaler .Marshal (jsonByteBuffer2 , batches [0 ].Spans [2 ]))
83
117
118
+ // Get the marshalled bytes of the 5th span that cannot be cured. Will be the needed for expected value of the tests
119
+ // depending on whether dropSpans is turned on or not.
120
+ batches [0 ].Spans [4 ].Process = batches [0 ].Process
121
+ jaegerProtoBytes4 , err := batches [0 ].Spans [4 ].Marshal ()
122
+ require .NoError (t , err )
123
+ require .NotNil (t , jaegerProtoBytes4 )
124
+
125
+ jsonByteBuffer4 := new (bytes.Buffer )
126
+ require .NoError (t , jsonMarshaler .Marshal (jsonByteBuffer4 , batches [0 ].Spans [4 ]))
127
+
84
128
// expected cured spans should be similar to spans that came in as if they were already cured.
85
- // batches[0].Spans[1] when cured will be the same as curedBatches[0].Spans[0]
129
+ // batches[0].Spans[1] when cured will be the same as curedBatches[0].Spans[0] except for the
130
+ // cured attribute.
86
131
curedTd := ptrace .NewTraces ()
87
132
curedRs := curedTd .ResourceSpans ().AppendEmpty ()
88
133
curedRs .Resource ().Attributes ().Insert ("test-key" , pcommon .NewValueString ("test-val" ))
@@ -98,6 +143,42 @@ func TestJaegerMarshalerCurer(t *testing.T) {
98
143
curedSpan .Attributes ().Insert ("big-tag" , pcommon .NewValueString (createLongString (maxAttributeValueSize , "a" )))
99
144
curedSpan .Attributes ().Insert ("big-tag" + truncationTagSuffix , pcommon .NewValueBool (true ))
100
145
146
+ // batches[0].Spans[3] when cured will be the same as curedBatches[0].Spans[1] except for the truncated log events.
147
+ curedSpan = curedIls .Spans ().AppendEmpty ()
148
+ curedSpan .SetName ("bar" )
149
+ curedSpan .SetStartTimestamp (pcommon .Timestamp (101 ))
150
+ curedSpan .SetEndTimestamp (pcommon .Timestamp (226 ))
151
+ curedSpan .SetTraceID (pcommon .NewTraceID ([16 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 }))
152
+ curedSpan .SetSpanID (pcommon .NewSpanID ([8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 }))
153
+ curedSpan .Attributes ().Insert ("tag10" , pcommon .NewValueString ("tag10-val" ))
154
+ curedSpan .Attributes ().Insert ("tag11" , pcommon .NewValueString ("tag11-val" ))
155
+ curedSpan .Attributes ().Insert (spanLogsTruncationTagName , pcommon .NewValueBool (true ))
156
+ // Add events to span
157
+ for i := 0 ; i < 16 ; i ++ {
158
+ se := curedSpan .Events ().AppendEmpty ()
159
+ se .SetName (createLongString (1 , "a" ))
160
+ se .SetTimestamp (ts )
161
+ }
162
+
163
+ // For the jaegerJSONSpanMarshaler{ pbMarshaler: &jsonpb.Marshaler{}}, the marshaled log events
164
+ // are large even for small log messages. So still the same as batches[0].Spans[3] except for the
165
+ // truncated log events.
166
+ curedSpan = curedIls .Spans ().AppendEmpty ()
167
+ curedSpan .SetName ("bar" )
168
+ curedSpan .SetStartTimestamp (pcommon .Timestamp (101 ))
169
+ curedSpan .SetEndTimestamp (pcommon .Timestamp (226 ))
170
+ curedSpan .SetTraceID (pcommon .NewTraceID ([16 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 }))
171
+ curedSpan .SetSpanID (pcommon .NewSpanID ([8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 }))
172
+ curedSpan .Attributes ().Insert ("tag10" , pcommon .NewValueString ("tag10-val" ))
173
+ curedSpan .Attributes ().Insert ("tag11" , pcommon .NewValueString ("tag11-val" ))
174
+ curedSpan .Attributes ().Insert (spanLogsTruncationTagName , pcommon .NewValueBool (true ))
175
+ // Add events to span
176
+ for i := 0 ; i < 4 ; i ++ {
177
+ se := curedSpan .Events ().AppendEmpty ()
178
+ se .SetName (createLongString (1 , "a" ))
179
+ se .SetTimestamp (ts )
180
+ }
181
+
101
182
curedBatches , err := jaeger .ProtoFromTraces (curedTd )
102
183
require .NoError (t , err )
103
184
@@ -109,6 +190,22 @@ func TestJaegerMarshalerCurer(t *testing.T) {
109
190
curedJsonByteBuffer1 := new (bytes.Buffer )
110
191
require .NoError (t , jsonMarshaler .Marshal (curedJsonByteBuffer1 , curedBatches [0 ].Spans [0 ]))
111
192
193
+ curedBatches [0 ].Spans [1 ].Process = curedBatches [0 ].Process
194
+ curedJaegerProtoBytes2 , err := curedBatches [0 ].Spans [1 ].Marshal ()
195
+ require .NoError (t , err )
196
+ require .NotNil (t , curedJaegerProtoBytes2 )
197
+
198
+ curedJsonByteBuffer2 := new (bytes.Buffer )
199
+ require .NoError (t , jsonMarshaler .Marshal (curedJsonByteBuffer2 , curedBatches [0 ].Spans [1 ]))
200
+
201
+ curedBatches [0 ].Spans [2 ].Process = curedBatches [0 ].Process
202
+ curedJaegerProtoBytes3 , err := curedBatches [0 ].Spans [2 ].Marshal ()
203
+ require .NoError (t , err )
204
+ require .NotNil (t , curedJaegerProtoBytes3 )
205
+
206
+ curedJsonByteBuffer3 := new (bytes.Buffer )
207
+ require .NoError (t , jsonMarshaler .Marshal (curedJsonByteBuffer3 , curedBatches [0 ].Spans [2 ]))
208
+
112
209
tests := []struct {
113
210
unmarshaler TracesMarshaler
114
211
encoding string
@@ -126,6 +223,8 @@ func TestJaegerMarshalerCurer(t *testing.T) {
126
223
{Topic : "topic" , Value : sarama .ByteEncoder (jaegerProtoBytes0 ), Key : sarama .ByteEncoder (messageKey )},
127
224
{Topic : "topic" , Value : sarama .ByteEncoder (curedJaegerProtoBytes1 ), Key : sarama .ByteEncoder (messageKey )},
128
225
{Topic : "topic" , Value : sarama .ByteEncoder (jaegerProtoBytes2 ), Key : sarama .ByteEncoder (messageKey )},
226
+ {Topic : "topic" , Value : sarama .ByteEncoder (curedJaegerProtoBytes2 ), Key : sarama .ByteEncoder (messageKey )},
227
+ {Topic : "topic" , Value : sarama .ByteEncoder (jaegerProtoBytes4 ), Key : sarama .ByteEncoder (messageKey )},
129
228
},
130
229
},
131
230
{
@@ -142,6 +241,8 @@ func TestJaegerMarshalerCurer(t *testing.T) {
142
241
{Topic : "topic" , Value : sarama .ByteEncoder (jsonByteBuffer0 .Bytes ()), Key : sarama .ByteEncoder (messageKey )},
143
242
{Topic : "topic" , Value : sarama .ByteEncoder (curedJsonByteBuffer1 .Bytes ()), Key : sarama .ByteEncoder (messageKey )},
144
243
{Topic : "topic" , Value : sarama .ByteEncoder (jsonByteBuffer2 .Bytes ()), Key : sarama .ByteEncoder (messageKey )},
244
+ {Topic : "topic" , Value : sarama .ByteEncoder (curedJsonByteBuffer3 .Bytes ()), Key : sarama .ByteEncoder (messageKey )},
245
+ {Topic : "topic" , Value : sarama .ByteEncoder (jsonByteBuffer4 .Bytes ()), Key : sarama .ByteEncoder (messageKey )},
145
246
},
146
247
},
147
248
{
@@ -157,6 +258,8 @@ func TestJaegerMarshalerCurer(t *testing.T) {
157
258
{Topic : "topic" , Value : sarama .ByteEncoder (jaegerProtoBytes0 ), Key : sarama .ByteEncoder (messageKey )},
158
259
{Topic : "topic" , Value : sarama .ByteEncoder (curedJaegerProtoBytes1 ), Key : sarama .ByteEncoder (messageKey )},
159
260
{Topic : "topic" , Value : sarama .ByteEncoder (jaegerProtoBytes2 ), Key : sarama .ByteEncoder (messageKey )},
261
+ {Topic : "topic" , Value : sarama .ByteEncoder (curedJaegerProtoBytes2 ), Key : sarama .ByteEncoder (messageKey )},
262
+ {Topic : "topic" , Value : sarama .ByteEncoder (jaegerProtoBytes4 ), Key : sarama .ByteEncoder (messageKey )},
160
263
},
161
264
},
162
265
{
@@ -171,6 +274,7 @@ func TestJaegerMarshalerCurer(t *testing.T) {
171
274
messages : []* sarama.ProducerMessage {
172
275
{Topic : "topic" , Value : sarama .ByteEncoder (jaegerProtoBytes0 ), Key : sarama .ByteEncoder (messageKey )},
173
276
{Topic : "topic" , Value : sarama .ByteEncoder (curedJaegerProtoBytes1 ), Key : sarama .ByteEncoder (messageKey )},
277
+ {Topic : "topic" , Value : sarama .ByteEncoder (curedJaegerProtoBytes2 ), Key : sarama .ByteEncoder (messageKey )},
174
278
},
175
279
},
176
280
}
@@ -351,6 +455,34 @@ func TestJaegerMarshalerCurerCureSpansFail(t *testing.T) {
351
455
assert .Nil (t , msg )
352
456
}
353
457
458
+ func TestCutSpanLogsByHalf (t * testing.T ) {
459
+ now := time .Now ()
460
+ jpl1 := jaegerproto.Log {Timestamp : now }
461
+ jpl2 := jaegerproto.Log {Timestamp : now .Add (time .Minute * 2 )}
462
+ jpl3 := jaegerproto.Log {Timestamp : now .Add (time .Minute * 4 )}
463
+ jpl4 := jaegerproto.Log {Timestamp : now .Add (time .Minute * 6 )}
464
+ jpl5 := jaegerproto.Log {Timestamp : now .Add (time .Minute * 8 )}
465
+ jpl6 := jaegerproto.Log {Timestamp : now .Add (time .Minute * 10 )}
466
+ assert .Equal (t ,
467
+ []jaegerproto.Log {jpl1 },
468
+ cutSpanLogsByHalf ([]jaegerproto.Log {jpl1 }))
469
+ assert .Equal (t ,
470
+ []jaegerproto.Log {jpl1 },
471
+ cutSpanLogsByHalf ([]jaegerproto.Log {jpl1 , jpl2 }))
472
+ assert .Equal (t ,
473
+ []jaegerproto.Log {jpl1 , jpl3 },
474
+ cutSpanLogsByHalf ([]jaegerproto.Log {jpl1 , jpl2 , jpl3 }))
475
+ assert .Equal (t ,
476
+ []jaegerproto.Log {jpl1 , jpl3 },
477
+ cutSpanLogsByHalf ([]jaegerproto.Log {jpl1 , jpl2 , jpl3 , jpl4 }))
478
+ assert .Equal (t ,
479
+ []jaegerproto.Log {jpl1 , jpl3 , jpl5 },
480
+ cutSpanLogsByHalf ([]jaegerproto.Log {jpl1 , jpl2 , jpl3 , jpl4 , jpl5 }))
481
+ assert .Equal (t ,
482
+ []jaegerproto.Log {jpl1 , jpl3 , jpl5 },
483
+ cutSpanLogsByHalf ([]jaegerproto.Log {jpl1 , jpl2 , jpl3 , jpl4 , jpl5 , jpl6 }))
484
+ }
485
+
354
486
func createLongString (n int , s string ) string {
355
487
var b strings.Builder
356
488
b .Grow (n * len (s ))
0 commit comments