-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathquery.go
More file actions
707 lines (625 loc) · 23.7 KB
/
query.go
File metadata and controls
707 lines (625 loc) · 23.7 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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
package query
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/VictoriaMetrics/VictoriaLogs/lib/logstorage"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/cespare/xxhash/v2"
"github.com/VictoriaMetrics/VictoriaTraces/app/vtselect/traces/tracecommon"
"github.com/VictoriaMetrics/VictoriaTraces/app/vtstorage"
vtstoragecommon "github.com/VictoriaMetrics/VictoriaTraces/app/vtstorage/common"
otelpb "github.com/VictoriaMetrics/VictoriaTraces/lib/protoparser/opentelemetry/pb"
)
// TraceQueryParam is the parameters for querying a batch of traces.
type TraceQueryParam struct {
ServiceName string
SpanName string
Attributes map[string]string
StartTimeMin time.Time
StartTimeMax time.Time
DurationMin time.Duration
DurationMax time.Duration
Limit int
}
// GetServiceNameList returns all unique service names within *traceServiceAndSpanNameLookbehind window.
// todo: cache of recent result.
func GetServiceNameList(ctx context.Context, cp *tracecommon.CommonParams) ([]string, error) {
currentTime := time.Now()
// query: _time:[start, end] *
qStr := "*"
q, err := logstorage.ParseQueryAtTimestamp(qStr, currentTime.UnixNano())
if err != nil {
return nil, fmt.Errorf("cannot parse query [%s]: %s", qStr, err)
}
q.AddTimeFilter(currentTime.Add(-*tracecommon.TraceServiceAndSpanNameLookbehind).UnixNano(), currentTime.UnixNano())
cp.Query = q
qctx := cp.NewQueryContext(ctx)
defer cp.UpdatePerQueryStatsMetrics()
serviceHits, err := vtstorage.GetStreamFieldValues(qctx, otelpb.ResourceAttrServiceName, *tracecommon.TraceMaxServiceNameList)
if err != nil {
return nil, fmt.Errorf("cannot parse query [%s]: %s", qStr, err)
}
serviceList := make([]string, 0, len(serviceHits))
for i := range serviceHits {
serviceList = append(serviceList, serviceHits[i].Value)
}
return serviceList, nil
}
// GetSpanNameList returns all unique span names for a service within *traceServiceAndSpanNameLookbehind window.
// todo: cache of recent result.
func GetSpanNameList(ctx context.Context, cp *tracecommon.CommonParams, serviceName string) ([]string, error) {
currentTime := time.Now()
// query: _time:[start, end] {"resource_attr:service.name"=serviceName}
qStr := fmt.Sprintf("_stream:{%s=%q}", otelpb.ResourceAttrServiceName, serviceName)
q, err := logstorage.ParseQueryAtTimestamp(qStr, currentTime.Unix())
if err != nil {
return nil, fmt.Errorf("cannot parse query [%s]: %s", qStr, err)
}
q.AddTimeFilter(currentTime.Add(-*tracecommon.TraceServiceAndSpanNameLookbehind).UnixNano(), currentTime.UnixNano())
cp.Query = q
qctx := cp.NewQueryContext(ctx)
defer cp.UpdatePerQueryStatsMetrics()
spanNameHits, err := vtstorage.GetStreamFieldValues(qctx, otelpb.NameField, *tracecommon.TraceMaxSpanNameList)
if err != nil {
return nil, fmt.Errorf("get span name hits error: %s", err)
}
spanNameList := make([]string, 0, len(spanNameHits))
for i := range spanNameHits {
spanNameList = append(spanNameList, spanNameHits[i].Value)
}
return spanNameList, nil
}
// GetTrace returns all spans of a trace in []*Row format.
// It searches in the index stream for start_time and end_time.
// If found:
// - search for span in time range [start_time, end_time].
func GetTrace(ctx context.Context, cp *tracecommon.CommonParams, traceID string) ([]*tracecommon.Row, error) {
currentTime := time.Now()
// possible partition
// query: {trace_id_idx="xx"} AND trace_id:traceID
qStr := fmt.Sprintf(
`{%s="%d"} AND %s:=%q | stats min(_time) _time, min(%s) %s, max(%s) %s`,
otelpb.TraceIDIndexStreamName,
xxhash.Sum64String(traceID)%otelpb.TraceIDIndexPartitionCount,
otelpb.TraceIDIndexFieldName,
traceID,
otelpb.TraceIDIndexStartTimeFieldName, otelpb.TraceIDIndexStartTimeFieldName,
otelpb.TraceIDIndexEndTimeFieldName, otelpb.TraceIDIndexEndTimeFieldName,
)
q, err := logstorage.ParseQueryAtTimestamp(qStr, currentTime.UnixNano())
if err != nil {
return nil, fmt.Errorf("cannot unmarshal query=%q: %w", qStr, err)
}
q.AddPipeOffsetLimit(0, 10)
traceStartTime, traceEndTime, err := findTraceIDTimeSplitTimeRange(ctx, q, cp)
if err != nil && errors.Is(err, vtstoragecommon.ErrOutOfRetention) {
// no hit in the retention period, simply returns empty.
return nil, nil
}
if err != nil {
// something wrong when trying to find the trace_id's start and end time.
return nil, fmt.Errorf("cannot find trace_id %q start time: %s", traceID, err)
}
// trace start time found, search in [trace start time, trace start time + *traceMaxDurationWindow] time range.
return findSpansByTraceIDAndTime(ctx, cp, traceID, traceStartTime, traceEndTime)
}
// GetTraceList returns multiple traceIDs and spans of them in []*Row format.
// It searches for traceIDs first, and then search for the spans of these traceIDs.
// To not miss any spans on the edge, it extends both the start time and end time
// by *traceMaxDurationWindow.
//
// e.g.:
// 1. input time range: [00:00, 09:00]
// 2. found 20 trace id, and adjust time range to: [08:00, 09:00]
// 3. find spans on time range: [08:00-traceMaxDurationWindow, 09:00+traceMaxDurationWindow]
func GetTraceList(ctx context.Context, cp *tracecommon.CommonParams, param *TraceQueryParam) ([]string, []*tracecommon.Row, error) {
currentTime := time.Now()
// query 1: * AND filter_conditions | last 1 by (_time) partition by (trace_id) | fields _time, trace_id | sort by (_time) desc
traceIDs, startTime, err := getTraceIDList(ctx, cp, param)
if err != nil {
return nil, nil, fmt.Errorf("get trace id error: %w", err)
}
if len(traceIDs) == 0 {
return nil, nil, nil
}
// query 2: trace_id:in(traceID, traceID, ...)
qStr := fmt.Sprintf(otelpb.TraceIDField+":in(%s)", strings.Join(traceIDs, ","))
q, err := logstorage.ParseQueryAtTimestamp(qStr, currentTime.UnixNano())
if err != nil {
return nil, nil, fmt.Errorf("cannot parse query [%s]: %s", qStr, err)
}
// adjust start time and end time with max duration window to make sure all spans are included.
q.AddTimeFilter(startTime.Add(-*tracecommon.TraceMaxDurationWindow).UnixNano(), param.StartTimeMax.Add(*tracecommon.TraceMaxDurationWindow).UnixNano())
ctxWithCancel, cancel := context.WithCancel(ctx)
defer cancel()
cp.Query = q
qctx := cp.NewQueryContext(ctxWithCancel)
defer cp.UpdatePerQueryStatsMetrics()
// search for trace spans and write to `rows []*Row`
var rowsLock sync.Mutex
var rows []*tracecommon.Row
var missingTimeColumn atomic.Bool
writeBlock := func(_ uint, db *logstorage.DataBlock) {
if missingTimeColumn.Load() {
return
}
columns := db.GetColumns(false)
clonedColumnNames := make([]string, len(columns))
for i, c := range columns {
clonedColumnNames[i] = strings.Clone(c.Name)
}
timestamps, ok := db.GetTimestamps(nil)
if !ok {
missingTimeColumn.Store(true)
cancel()
return
}
for i, timestamp := range timestamps {
fields := make([]logstorage.Field, 0, len(columns))
for j := range columns {
// column could be empty if this span does not contain such field.
// only append non-empty columns.
if columns[j].Values[i] != "" {
fields = append(fields, logstorage.Field{Name: clonedColumnNames[j], Value: strings.Clone(columns[j].Values[i])})
}
}
rowsLock.Lock()
rows = append(rows, &tracecommon.Row{
Timestamp: timestamp,
Fields: fields,
})
rowsLock.Unlock()
}
}
if err = vtstorage.RunQuery(qctx, writeBlock); err != nil {
return nil, nil, err
}
if missingTimeColumn.Load() {
return nil, nil, fmt.Errorf("missing _time column in the result for the query [%s]", q)
}
return traceIDs, rows, nil
}
// getTraceIDList returns traceIDs according to the search params.
// It also returns the earliest start time of these traces, to help reducing the time range for spans search.
func getTraceIDList(ctx context.Context, cp *tracecommon.CommonParams, param *TraceQueryParam) ([]string, time.Time, error) {
currentTime := time.Now()
// query: * AND <filter> | last 1 by (_time) partition by (trace_id) | fields _time, trace_id | sort by (_time) desc
qStr := "* "
if param.ServiceName != "" {
qStr += fmt.Sprintf("AND _stream:{"+otelpb.ResourceAttrServiceName+"=%q} ", param.ServiceName)
}
if param.SpanName != "" {
qStr += fmt.Sprintf("AND _stream:{"+otelpb.NameField+"=%q} ", param.SpanName)
}
if len(param.Attributes) > 0 {
for k, v := range param.Attributes {
if strings.HasPrefix(v, "~") {
// ~ prefix forces regex (e.g. tags={"key":"~value.*"})
qStr += fmt.Sprintf(`AND %q:re(%s) `, k, strconv.Quote(v[1:]))
} else {
qStr += fmt.Sprintf(`AND %q:=%q `, k, v)
}
}
}
if param.DurationMin > 0 {
qStr += fmt.Sprintf("AND "+otelpb.DurationField+":>%d ", param.DurationMin.Nanoseconds())
}
if param.DurationMax > 0 {
qStr += fmt.Sprintf("AND duration:<%d ", param.DurationMax.Nanoseconds())
}
qStr += " | last 1 by (_time) partition by (" + otelpb.TraceIDField + ") | fields _time, " + otelpb.TraceIDField + " | sort by (_time) desc"
q, err := logstorage.ParseQueryAtTimestamp(qStr, currentTime.UnixNano())
if err != nil {
return nil, time.Time{}, fmt.Errorf("cannot parse query [%s]: %s", qStr, err)
}
q.AddPipeOffsetLimit(0, uint64(param.Limit))
// adjust the max start time, because fresh traces may not be completed.
// they should wait for *latencyOffset before being visible.
maxStartTime := time.Now().Add(-*tracecommon.LatencyOffset)
if param.StartTimeMax.After(maxStartTime) {
param.StartTimeMax = maxStartTime
}
traceIDs, maxStartTime, err := findTraceIDsSplitTimeRange(ctx, q, cp, param.StartTimeMin, param.StartTimeMax, param.Limit)
if err != nil {
return nil, time.Time{}, err
}
return traceIDs, maxStartTime, nil
}
// findTraceIDsSplitTimeRange try to search from the nearest time range of the end time.
// if the result already met requirement of `limit`, return.
// otherwise, amplify the time range to 5x and search again, until the start time exceed the input.
func findTraceIDsSplitTimeRange(ctx context.Context, q *logstorage.Query, cp *tracecommon.CommonParams, startTime, endTime time.Time, limit int) ([]string, time.Time, error) {
currentTime := time.Now()
step := time.Minute
currentStartTime := endTime.Add(-step)
var traceIDListLock sync.Mutex
var startTimeLock sync.Mutex
traceIDList := make([]string, 0, limit)
maxStartTimeStr := endTime.Format(time.RFC3339)
cp.Query = q
qctx := cp.NewQueryContext(ctx)
defer cp.UpdatePerQueryStatsMetrics()
writeBlock := func(_ uint, db *logstorage.DataBlock) {
columns := db.GetColumns(false)
clonedColumnNames := make([]string, len(columns))
for i, c := range columns {
clonedColumnNames[i] = strings.Clone(c.Name)
}
for i := range clonedColumnNames {
switch clonedColumnNames[i] {
case "trace_id":
traceIDListLock.Lock()
for _, v := range columns[i].Values {
traceIDList = append(traceIDList, strings.Clone(v))
}
traceIDListLock.Unlock()
case "_time":
startTimeLock.Lock()
for _, v := range columns[i].Values {
if v < maxStartTimeStr {
maxStartTimeStr = strings.Clone(v)
}
}
startTimeLock.Unlock()
}
}
}
for currentStartTime.After(startTime) {
qClone := q.CloneWithTimeFilter(currentTime.UnixNano(), currentStartTime.UnixNano(), endTime.UnixNano())
qctx = qctx.WithQuery(qClone)
if err := vtstorage.RunQuery(qctx, writeBlock); err != nil {
if errors.Is(err, vtstoragecommon.ErrOutOfRetention) {
return nil, time.Time{}, nil
}
return nil, time.Time{}, err
}
// found enough trace_id, return directly
if len(traceIDList) == limit {
maxStartTime, err := time.Parse(time.RFC3339, maxStartTimeStr)
if err != nil {
return nil, maxStartTime, err
}
return checkTraceIDList(traceIDList), maxStartTime, nil
}
// not enough trace_id, clear the result, extend the time range and try again.
traceIDList = traceIDList[:0]
step *= 5
currentStartTime = currentStartTime.Add(-step)
}
// one last try with input time range
if currentStartTime.Before(startTime) {
currentStartTime = startTime
}
qClone := q.CloneWithTimeFilter(currentTime.UnixNano(), currentStartTime.UnixNano(), endTime.UnixNano())
qctx = qctx.WithQuery(qClone)
if err := vtstorage.RunQuery(qctx, writeBlock); err != nil {
return nil, time.Time{}, err
}
maxStartTime, err := time.Parse(time.RFC3339, maxStartTimeStr)
if err != nil {
return nil, maxStartTime, err
}
return checkTraceIDList(traceIDList), maxStartTime, nil
}
// findTraceIDTimeSplitTimeRange try to search from {trace_id_idx_stream="xx"} stream, which contains
// the trace_id and start/end time of this trace. It returns the time range of the trace if found.
//
// If the span with this trace_id never reach VictoriaTraces, the index search will go through the whole time range within
// the retention period, and returns an ErrOutOfRetention.
func findTraceIDTimeSplitTimeRange(ctx context.Context, q *logstorage.Query, cp *tracecommon.CommonParams) (time.Time, time.Time, error) {
var (
traceIDStartTimeStr, traceIDEndTimeStr string
// for compatible with old data
timeStr string
valueLock sync.Mutex
)
ctxWithCancel, cancel := context.WithCancel(ctx)
defer cancel()
cp.Query = q
qctx := cp.NewQueryContext(ctxWithCancel)
defer cp.UpdatePerQueryStatsMetrics()
writeBlock := func(_ uint, db *logstorage.DataBlock) {
rowsCount := db.RowsCount()
if rowsCount == 0 {
return
}
if rowsCount > 1 {
logger.Errorf("BUG: unexpected rowCount during trace ID index search. query: %s", q.String())
}
columns := db.GetColumns(false)
clonedColumnNames := make([]string, len(columns))
for i, c := range columns {
clonedColumnNames[i] = strings.Clone(c.Name)
}
// There should be only a few lines in result, so it's safe to lock the whole block.
valueLock.Lock()
defer valueLock.Unlock()
for _, c := range columns {
switch c.Name {
case "_time":
timeStr = c.Values[len(c.Values)-1]
case otelpb.TraceIDIndexStartTimeFieldName:
for _, v := range c.Values {
if traceIDStartTimeStr == "" || traceIDStartTimeStr > v {
traceIDStartTimeStr = strings.Clone(v)
}
}
case otelpb.TraceIDIndexEndTimeFieldName:
for _, v := range c.Values {
if traceIDEndTimeStr == "" || traceIDEndTimeStr < v {
traceIDEndTimeStr = strings.Clone(v)
}
}
}
}
}
currentTime := time.Now()
startTime := currentTime.Add(-*tracecommon.TraceSearchStep)
endTime := currentTime
for startTime.UnixNano() > 0 {
qq := q.CloneWithTimeFilter(currentTime.UnixNano(), startTime.UnixNano(), endTime.UnixNano())
qctx = qctx.WithQuery(qq)
if err := vtstorage.RunQuery(qctx, writeBlock); err != nil {
// this could be either a ErrOutOfRetention, or a real error.
return time.Time{}, time.Time{}, err
}
// no hit in this time range, continue with step.
if timeStr == "" {
endTime = startTime
startTime = startTime.Add(-*tracecommon.TraceSearchStep)
continue
}
// found result.
if traceIDStartTimeStr == "" || traceIDEndTimeStr == "" {
// this could be the old format index, which records trace ID and the approximate timestamp only.
// to transform this into new format (start time & end time), use [t-traceWindow, t+traceWindow].
// this code should be deprecated in the future.
timestamp, _ := time.Parse(time.RFC3339, timeStr)
return timestamp.Add(-*tracecommon.TraceMaxDurationWindow), timestamp.Add(*tracecommon.TraceMaxDurationWindow), nil
}
traceIDStartTime, _ := strconv.ParseInt(traceIDStartTimeStr, 10, 64)
traceIDEndTime, _ := strconv.ParseInt(traceIDEndTimeStr, 10, 64)
return time.Unix(traceIDStartTime/int64(time.Second), traceIDStartTime%int64(time.Second)), time.Unix(traceIDEndTime/int64(time.Second), traceIDEndTime%int64(time.Second)), nil
}
return time.Time{}, time.Time{}, vtstoragecommon.ErrOutOfRetention
}
// findSpansByTraceIDAndTime search for spans in given time range.
func findSpansByTraceIDAndTime(ctx context.Context, cp *tracecommon.CommonParams, traceID string, startTime, endTime time.Time) ([]*tracecommon.Row, error) {
// query: trace_id:traceID
qStr := fmt.Sprintf(otelpb.TraceIDField+": %q", traceID)
q, err := logstorage.ParseQueryAtTimestamp(qStr, endTime.UnixNano())
if err != nil {
return nil, fmt.Errorf("cannot parse query [%s]: %s", qStr, err)
}
ctxWithCancel, cancel := context.WithCancel(ctx)
defer cancel()
cp.Query = q
qctx := cp.NewQueryContext(ctxWithCancel)
defer cp.UpdatePerQueryStatsMetrics()
// search for trace spans and write to `rows []*Row`
var rowsLock sync.Mutex
var rows []*tracecommon.Row
var missingTimeColumn atomic.Bool
writeBlock := func(_ uint, db *logstorage.DataBlock) {
if missingTimeColumn.Load() {
return
}
columns := db.GetColumns(false)
clonedColumnNames := make([]string, len(columns))
for i, c := range columns {
clonedColumnNames[i] = strings.Clone(c.Name)
}
timestamps, ok := db.GetTimestamps(nil)
if !ok {
missingTimeColumn.Store(true)
cancel()
return
}
for i, timestamp := range timestamps {
fields := make([]logstorage.Field, 0, len(columns))
for j := range columns {
// column could be empty if this span does not contain such field.
// only append non-empty columns.
if columns[j].Values[i] != "" {
fields = append(fields, logstorage.Field{
Name: clonedColumnNames[j],
Value: strings.Clone(columns[j].Values[i]),
})
}
}
rowsLock.Lock()
rows = append(rows, &tracecommon.Row{
Timestamp: timestamp,
Fields: fields,
})
rowsLock.Unlock()
}
}
qq := q.CloneWithTimeFilter(endTime.UnixNano(), startTime.UnixNano(), endTime.UnixNano())
qctx = qctx.WithQuery(qq)
if err = vtstorage.RunQuery(qctx, writeBlock); err != nil {
return nil, err
}
if missingTimeColumn.Load() {
return nil, fmt.Errorf("missing _time column in the result for the query [%s]", qq)
}
return rows, nil
}
// checkTraceIDList removes invalid `trace_id`. It helps prevent query injection.
func checkTraceIDList(traceIDList []string) []string {
result := make([]string, 0, len(traceIDList))
for i := range traceIDList {
if tracecommon.TraceIDRegex.MatchString(traceIDList[i]) {
result = append(result, traceIDList[i])
}
}
return result
}
type ServiceGraphQueryParameters struct {
EndTs time.Time
Lookback time.Duration
}
// GetServiceGraphList returns service dependencies graph edges (parent, child, callCount) in []*Row format.
//
// TODO: currently this function can only handle request from Jaeger dependencies API. Since Tempo provides similar service graph
// feature, it would be great to add support for Tempo service graph API as well.
func GetServiceGraphList(ctx context.Context, cp *tracecommon.CommonParams, param *ServiceGraphQueryParameters) ([]*tracecommon.Row, error) {
// {trace_service_graph_stream="-"} | fields parent, child, callCount | stats by (parent, child) sum(callCount) as callCount
qStr := fmt.Sprintf(`{%s="-"} | fields %s, %s, %s | stats by (%s, %s) sum(%s) as %s`,
otelpb.ServiceGraphStreamName,
otelpb.ServiceGraphParentFieldName,
otelpb.ServiceGraphChildFieldName,
otelpb.ServiceGraphCallCountFieldName,
otelpb.ServiceGraphParentFieldName,
otelpb.ServiceGraphChildFieldName,
otelpb.ServiceGraphCallCountFieldName,
otelpb.ServiceGraphCallCountFieldName,
)
startTime := param.EndTs.Add(-param.Lookback).UnixNano()
endTime := param.EndTs.UnixNano()
q, err := logstorage.ParseQueryAtTimestamp(qStr, endTime)
if err != nil {
return nil, fmt.Errorf("cannot parse query [%s]: %s", qStr, err)
}
q.AddTimeFilter(startTime, endTime)
cp.Query = q
qctx := cp.NewQueryContext(ctx)
var rowsLock sync.Mutex
var rows []*tracecommon.Row
writeBlock := func(_ uint, db *logstorage.DataBlock) {
columns := db.GetColumns(false)
if len(columns) == 0 {
return
}
clonedColumnNames := make([]string, len(columns))
valuesCount := 0
for i, c := range columns {
clonedColumnNames[i] = strings.Clone(c.Name)
if len(c.Values) > valuesCount {
valuesCount = len(c.Values)
}
}
if valuesCount == 0 {
return
}
for i := 0; i < valuesCount; i++ {
fields := make([]logstorage.Field, 0, len(columns))
for j := range columns {
fields = append(
fields,
logstorage.Field{
Name: clonedColumnNames[j],
Value: strings.Clone(columns[j].Values[i]),
},
)
}
rowsLock.Lock()
rows = append(rows, &tracecommon.Row{
Fields: fields,
})
rowsLock.Unlock()
}
}
if err = vtstorage.RunQuery(qctx, writeBlock); err != nil {
return nil, err
}
return rows, nil
}
// GetServiceGraphTimeRange is an internal function used by service graph background task.
// It calculates the service graph relation within the time range in (parent, child, callCount) format for specific tenant.
func GetServiceGraphTimeRange(ctx context.Context, tenantID logstorage.TenantID, startTime, endTime time.Time, limit uint64) ([][]logstorage.Field, error) {
cp := &tracecommon.CommonParams{
TenantIDs: []logstorage.TenantID{tenantID},
}
// (NOT parent_span_id:"") AND (kind:~"2|5") | fields parent_span_id, resource_attr:service.name | rename parent_span_id as span_id, resource_attr:service.name as child
qStrChildSpans := fmt.Sprintf(
`(NOT %s:"") AND (%s:~"%d|%d") | fields %s, %s | rename %s as %s, %s as %s`,
otelpb.ParentSpanIDField, // parent span id not empty means this span is a child span.
otelpb.KindField, // only server(2) and consumer(5) span could be used as a child. It helps reduce the spans it needs to fetch.
otelpb.SpanKind(2),
otelpb.SpanKind(5),
otelpb.ParentSpanIDField,
otelpb.ResourceAttrServiceName,
otelpb.ParentSpanIDField,
otelpb.SpanIDField,
otelpb.ResourceAttrServiceName,
otelpb.ServiceGraphChildFieldName,
)
// (NOT span_id:"") AND (kind:~"3|4") | fields span_id, resource_attr:service.name | rename resource_attr:service.name as parent
qStrParentSpans := fmt.Sprintf(
`(NOT %s:"") AND (%s:~"%d|%d") | fields %s, %s | rename %s as %s`,
otelpb.SpanIDField, // Any span could be a parent span, as long as it has a span ID.
otelpb.KindField, // only client(3) and producer(4) span could be used as a parent. It helps reduce the spans it needs to fetch.
otelpb.SpanKind(3),
otelpb.SpanKind(4),
otelpb.SpanIDField,
otelpb.ResourceAttrServiceName,
otelpb.ResourceAttrServiceName,
otelpb.ServiceGraphParentFieldName,
)
// join by span_id
qStr := fmt.Sprintf(
`%s | join by (%s) (%s) inner | NOT %s:eq_field(%s) | stats by (%s, %s) count() %s`,
qStrChildSpans,
otelpb.SpanIDField,
qStrParentSpans,
otelpb.ServiceGraphParentFieldName,
otelpb.ServiceGraphChildFieldName,
otelpb.ServiceGraphParentFieldName,
otelpb.ServiceGraphChildFieldName,
otelpb.ServiceGraphCallCountFieldName,
)
q, err := logstorage.ParseQueryAtTimestamp(qStr, endTime.UnixNano())
if err != nil {
return nil, fmt.Errorf("cannot parse query [%s]: %s", qStr, err)
}
q.AddTimeFilter(startTime.UnixNano(), endTime.UnixNano())
q.AddPipeOffsetLimit(0, limit)
cp.Query = q
qctx := cp.NewQueryContext(ctx)
defer cp.UpdatePerQueryStatsMetrics()
var rowsLock sync.Mutex
var rows [][]logstorage.Field
writeBlock := func(_ uint, db *logstorage.DataBlock) {
columns := db.GetColumns(false)
if len(columns) == 0 {
return
}
clonedColumnNames := make([]string, len(columns))
valuesCount := 0
for i, c := range columns {
clonedColumnNames[i] = strings.Clone(c.Name)
if len(c.Values) > valuesCount {
valuesCount = len(c.Values)
}
}
if valuesCount == 0 {
return
}
for i := 0; i < valuesCount; i++ {
fields := make([]logstorage.Field, 0, len(columns))
for j := range clonedColumnNames {
fields = append(
fields,
logstorage.Field{
Name: clonedColumnNames[j],
Value: strings.Clone(columns[j].Values[i]),
},
)
}
rowsLock.Lock()
rows = append(rows, fields)
rowsLock.Unlock()
}
}
if err = vtstorage.RunQuery(qctx, writeBlock); err != nil {
return nil, fmt.Errorf("cannot execute query [%s]: %s", qStr, err)
}
return rows, nil
}