forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreq.go
More file actions
466 lines (408 loc) · 13.6 KB
/
req.go
File metadata and controls
466 lines (408 loc) · 13.6 KB
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package test
import (
crand "crypto/rand"
"encoding/json"
"fmt"
"math/rand"
"testing"
"time"
"github.com/gogo/protobuf/proto"
"github.com/grafana/tempo/pkg/tempopb"
v1_common "github.com/grafana/tempo/pkg/tempopb/common/v1"
v1_resource "github.com/grafana/tempo/pkg/tempopb/resource/v1"
v1_trace "github.com/grafana/tempo/pkg/tempopb/trace/v1"
"github.com/grafana/tempo/tempodb/backend"
"github.com/stretchr/testify/require"
)
func MakeAttribute(key, value string) *v1_common.KeyValue {
return &v1_common.KeyValue{
Key: key,
Value: &v1_common.AnyValue{
Value: &v1_common.AnyValue_StringValue{
StringValue: value,
},
},
}
}
func MakeSpan(traceID []byte) *v1_trace.Span {
now := time.Now()
startTime := uint64(now.UnixNano())
endTime := uint64(now.Add(time.Second).UnixNano())
return makeSpanWithAttributeCount(traceID, rand.Int()%10+1, startTime, endTime) // nolint:gosec // G404: Use of weak random number generator
}
func MakeSpanWithTimeWindow(traceID []byte, startTime uint64, endTime uint64) *v1_trace.Span {
return makeSpanWithAttributeCount(traceID, rand.Int()%10+1, startTime, endTime) // nolint:gosec // G404: Use of weak random number generator
}
func makeSpanWithAttributeCount(traceID []byte, count int, startTime uint64, endTime uint64) *v1_trace.Span {
attributes := make([]*v1_common.KeyValue, 0, count)
for range count {
attributes = append(attributes, &v1_common.KeyValue{
Key: RandomString(),
Value: &v1_common.AnyValue{Value: &v1_common.AnyValue_StringValue{StringValue: RandomString()}},
})
}
s := &v1_trace.Span{
Name: "test",
TraceId: traceID,
SpanId: make([]byte, 8),
ParentSpanId: make([]byte, 8),
Kind: v1_trace.Span_SPAN_KIND_CLIENT,
Status: &v1_trace.Status{
Code: 1,
Message: "OK",
},
StartTimeUnixNano: startTime,
EndTimeUnixNano: endTime,
Attributes: attributes,
DroppedLinksCount: rand.Uint32(), // nolint:gosec // G404: Use of weak random number generator
DroppedAttributesCount: rand.Uint32(), // nolint:gosec // G404: Use of weak random number generator
}
_, err := crand.Read(s.SpanId)
if err != nil {
panic(err)
}
// add link
if rand.Intn(5) == 0 { // nolint:gosec // G404: Use of weak random number generator
s.Links = append(s.Links, &v1_trace.Span_Link{
TraceId: traceID,
SpanId: make([]byte, 8),
TraceState: "state",
Attributes: []*v1_common.KeyValue{
{
Key: "linkkey",
Value: &v1_common.AnyValue{
Value: &v1_common.AnyValue_StringValue{
StringValue: "linkvalue",
},
},
},
},
})
}
// add attr
if rand.Intn(2) == 0 { // nolint:gosec // G404: Use of weak random number generator
s.Attributes = append(s.Attributes, &v1_common.KeyValue{
Key: "key",
Value: &v1_common.AnyValue{
Value: &v1_common.AnyValue_StringValue{
StringValue: "value",
},
},
})
}
// add event
if rand.Intn(3) == 0 { // nolint:gosec // G404: Use of weak random number generator
s.Events = append(s.Events, &v1_trace.Span_Event{
TimeUnixNano: s.StartTimeUnixNano + uint64(rand.Intn(1*1000*1000)), // 1ms
Name: "event",
DroppedAttributesCount: rand.Uint32(), // nolint:gosec // G404: Use of weak random number generator
Attributes: []*v1_common.KeyValue{
{
Key: "eventkey",
Value: &v1_common.AnyValue{
Value: &v1_common.AnyValue_StringValue{
StringValue: "eventvalue",
},
},
},
},
})
}
return s
}
func MakeBatch(spans int, traceID []byte) *v1_trace.ResourceSpans {
return makeBatchWithTimeRange(spans, traceID, nil, nil)
}
func MakeBatchWithAttributes(spans int, traceID []byte, resAttributes []*v1_common.KeyValue) *v1_trace.ResourceSpans {
return makeBatchWithTimeRange(spans, traceID, nil, resAttributes)
}
type batchTimeRange struct {
start uint64
end uint64
}
func makeBatchWithTimeRange(spans int, traceID []byte, timeRange *batchTimeRange, resAttributes []*v1_common.KeyValue) *v1_trace.ResourceSpans {
traceID = ValidTraceID(traceID)
batch := &v1_trace.ResourceSpans{
Resource: &v1_resource.Resource{
Attributes: []*v1_common.KeyValue{
{
Key: "random.res.attr",
Value: &v1_common.AnyValue{
Value: &v1_common.AnyValue_StringValue{
StringValue: RandomString(),
},
},
},
{
Key: "service.name",
Value: &v1_common.AnyValue{
Value: &v1_common.AnyValue_StringValue{
StringValue: "test-service",
},
},
},
},
},
}
if resAttributes != nil {
batch.Resource.Attributes = append(batch.Resource.Attributes, resAttributes...)
}
var (
ss *v1_trace.ScopeSpans
ssCount int
)
for range spans {
// occasionally make a new ss
if ss == nil || rand.Int()%3 == 0 { // nolint:gosec // G404: Use of weak random number generator
ssCount++
ss = &v1_trace.ScopeSpans{
Scope: &v1_common.InstrumentationScope{
Name: "super library",
Version: fmt.Sprintf("1.0.%d", ssCount),
},
}
batch.ScopeSpans = append(batch.ScopeSpans, ss)
}
if timeRange == nil {
ss.Spans = append(ss.Spans, MakeSpan(traceID))
} else {
ss.Spans = append(ss.Spans, MakeSpanWithTimeWindow(traceID, timeRange.start, timeRange.end))
}
}
return batch
}
func MakeTrace(requests int, traceID []byte) *tempopb.Trace {
traceID = ValidTraceID(traceID)
trace := &tempopb.Trace{
ResourceSpans: make([]*v1_trace.ResourceSpans, 0),
}
for i := 0; i < requests; i++ {
trace.ResourceSpans = append(trace.ResourceSpans, MakeBatch(rand.Int()%20+1, traceID)) // nolint:gosec // G404: Use of weak random number generator
}
return trace
}
func MakeTraceWithTimeRange(requests int, traceID []byte, startTime, endTime uint64) *tempopb.Trace {
traceID = ValidTraceID(traceID)
trace := &tempopb.Trace{
ResourceSpans: make([]*v1_trace.ResourceSpans, 0),
}
for range requests {
timeRange := &batchTimeRange{start: startTime, end: endTime}
trace.ResourceSpans = append(trace.ResourceSpans, makeBatchWithTimeRange(rand.Int()%20+1, traceID, timeRange, nil)) // nolint:gosec // G404: Use of weak random number generator
}
return trace
}
func MakeTraceWithSpanCount(requests int, spansEach int, traceID []byte) *tempopb.Trace {
trace := &tempopb.Trace{
ResourceSpans: make([]*v1_trace.ResourceSpans, 0),
}
for range requests {
trace.ResourceSpans = append(trace.ResourceSpans, MakeBatch(spansEach, traceID))
}
return trace
}
var (
dedicatedColumnsResource = backend.DedicatedColumns{
{Scope: "resource", Name: "dedicated.resource.1", Type: "string"},
{Scope: "resource", Name: "dedicated.resource.2", Type: "string"},
{Scope: "resource", Name: "dedicated.resource.3", Type: "string"},
{Scope: "resource", Name: "dedicated.resource.4", Type: "string"},
{Scope: "resource", Name: "dedicated.resource.5", Type: "string"},
{Scope: "resource", Name: "dedicated.resource.6", Type: "int"},
{Scope: "resource", Name: "dedicated.resource.7", Type: "int"},
}
dedicatedColumnsSpan = backend.DedicatedColumns{
{Scope: "span", Name: "dedicated.span.1", Type: "string"},
{Scope: "span", Name: "dedicated.span.2", Type: "string"},
{Scope: "span", Name: "dedicated.span.3", Type: "string"},
{Scope: "span", Name: "dedicated.span.4", Type: "string"},
{Scope: "span", Name: "dedicated.span.5", Type: "string"},
{Scope: "span", Name: "dedicated.span.6", Type: "int"},
{Scope: "span", Name: "dedicated.span.7", Type: "int"},
}
dedicatedColumnsEvent = backend.DedicatedColumns{
{Scope: "event", Name: "dedicated.event.1", Type: "string"},
{Scope: "event", Name: "dedicated.event.2", Type: "string"},
}
)
// AddDedicatedAttributes adds resource and span attributes to a trace that are stored in dedicated
// columns when a backend.BlockMeta is created with the column assignments from MakeDedicatedColumns.
func AddDedicatedAttributes(trace *tempopb.Trace) *tempopb.Trace {
makeVal := func(typ backend.DedicatedColumnType, scope backend.DedicatedColumnScope, i int) *v1_common.AnyValue {
if typ == backend.DedicatedColumnTypeInt {
return &v1_common.AnyValue{
Value: &v1_common.AnyValue_IntValue{
IntValue: int64(i + 1),
},
}
}
return &v1_common.AnyValue{
Value: &v1_common.AnyValue_StringValue{
StringValue: fmt.Sprintf("dedicated-%s-attr-value-%d", scope, i+1),
},
}
}
spanAttrs := make([]*v1_common.KeyValue, 0, len(dedicatedColumnsSpan))
for i, c := range dedicatedColumnsSpan {
spanAttrs = append(spanAttrs, &v1_common.KeyValue{
Key: c.Name,
Value: makeVal(c.Type, c.Scope, i),
})
}
resourceAttrs := make([]*v1_common.KeyValue, 0, len(dedicatedColumnsResource))
for i, c := range dedicatedColumnsResource {
resourceAttrs = append(resourceAttrs, &v1_common.KeyValue{
Key: c.Name,
Value: makeVal(c.Type, c.Scope, i),
})
}
eventAttrs := make([]*v1_common.KeyValue, 0, len(dedicatedColumnsEvent))
for i, c := range dedicatedColumnsEvent {
eventAttrs = append(eventAttrs, &v1_common.KeyValue{
Key: c.Name,
Value: makeVal(c.Type, c.Scope, i),
})
}
for _, batch := range trace.ResourceSpans {
attr := make([]*v1_common.KeyValue, 0, len(resourceAttrs)+len(batch.Resource.Attributes))
attr = append(attr, resourceAttrs...)
batch.Resource.Attributes = append(attr, batch.Resource.Attributes...)
for _, ss := range batch.ScopeSpans {
for _, span := range ss.Spans {
attr = make([]*v1_common.KeyValue, 0, len(spanAttrs)+len(span.Attributes))
attr = append(attr, spanAttrs...)
span.Attributes = append(attr, span.Attributes...)
for _, e := range span.Events {
attr = make([]*v1_common.KeyValue, 0, len(eventAttrs)+len(e.Attributes))
attr = append(attr, eventAttrs...)
e.Attributes = append(attr, e.Attributes...)
}
}
}
}
return trace
}
func MakeReqWithMultipleTraceWithSpanCount(spanCounts []int, traceIDs [][]byte) *tempopb.Trace {
if len(spanCounts) != len(traceIDs) {
panic("spanCounts and traceIDs lengths do not match")
}
trace := &tempopb.Trace{
ResourceSpans: make([]*v1_trace.ResourceSpans, 0),
}
for index, traceID := range traceIDs {
traceID = ValidTraceID(traceID)
trace.ResourceSpans = append(trace.ResourceSpans, MakeBatch(spanCounts[index], traceID))
}
return trace
}
// MakeDedicatedColumns creates a dedicated column assignment that matches the attributes
// generated by AddDedicatedAttributes.
func MakeDedicatedColumns() backend.DedicatedColumns {
columns := make(backend.DedicatedColumns, 0, len(dedicatedColumnsResource)+len(dedicatedColumnsSpan)+len(dedicatedColumnsEvent))
columns = append(columns, dedicatedColumnsResource...)
columns = append(columns, dedicatedColumnsSpan...)
columns = append(columns, dedicatedColumnsEvent...)
return columns
}
func ValidTraceID(traceID []byte) []byte {
if len(traceID) == 0 {
traceID = make([]byte, 16)
_, err := crand.Read(traceID)
if err != nil {
panic(err)
}
}
for len(traceID) < 16 {
traceID = append(traceID, 0)
}
return traceID
}
func RandomString() string {
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
s := make([]rune, 10)
for i := range s {
s[i] = letters[rand.Intn(len(letters))] // nolint:gosec // G404: Use of weak random number generator
}
return string(s)
}
func TracesEqual(t *testing.T, t1 *tempopb.Trace, t2 *tempopb.Trace) {
if !proto.Equal(t1, t2) {
wantJSON, _ := json.MarshalIndent(t1, "", " ")
gotJSON, _ := json.MarshalIndent(t2, "", " ")
require.Equal(t, string(wantJSON), string(gotJSON))
}
}
func MakeTraceWithTags(traceID []byte, service string, intValue int64) *tempopb.Trace {
now := time.Now()
traceID = ValidTraceID(traceID)
trace := &tempopb.Trace{
ResourceSpans: make([]*v1_trace.ResourceSpans, 0),
}
attributes := make([]*v1_common.KeyValue, 0, 2)
attributes = append(attributes, &v1_common.KeyValue{
Key: "stringTag",
Value: &v1_common.AnyValue{Value: &v1_common.AnyValue_StringValue{StringValue: "value1"}},
})
attributes = append(attributes, &v1_common.KeyValue{
Key: "intTag",
Value: &v1_common.AnyValue{Value: &v1_common.AnyValue_IntValue{IntValue: intValue}},
})
trace.ResourceSpans = append(trace.ResourceSpans, &v1_trace.ResourceSpans{
Resource: &v1_resource.Resource{
Attributes: []*v1_common.KeyValue{
{
Key: "service.name",
Value: &v1_common.AnyValue{
Value: &v1_common.AnyValue_StringValue{
StringValue: service,
},
},
},
{
Key: "other",
Value: &v1_common.AnyValue{
Value: &v1_common.AnyValue_StringValue{
StringValue: "other-value",
},
},
},
},
},
ScopeSpans: []*v1_trace.ScopeSpans{
{
Spans: []*v1_trace.Span{
{
Name: "test",
TraceId: traceID,
SpanId: make([]byte, 8),
ParentSpanId: make([]byte, 8),
Kind: v1_trace.Span_SPAN_KIND_CLIENT,
Status: &v1_trace.Status{
Code: 1,
Message: "OK",
},
StartTimeUnixNano: uint64(now.UnixNano()),
EndTimeUnixNano: uint64(now.Add(time.Second).UnixNano()),
Attributes: attributes,
DroppedLinksCount: rand.Uint32(),
DroppedAttributesCount: rand.Uint32(),
},
},
},
},
})
return trace
}
func MakePushBytesRequest(t testing.TB, requests int, traceID []byte, startTime, endTime uint64) *tempopb.PushBytesRequest {
traceID = ValidTraceID(traceID)
trace := MakeTraceWithTimeRange(requests, traceID, startTime, endTime)
b, err := proto.Marshal(trace)
require.NoError(t, err)
req := &tempopb.PushBytesRequest{
Traces: make([]tempopb.PreallocBytes, 0),
Ids: make([][]byte, 0),
}
req.Traces = append(req.Traces, tempopb.PreallocBytes{Slice: b})
req.Ids = append(req.Ids, traceID)
return req
}