Skip to content

Commit f3bc8f9

Browse files
committed
add most_recent query hint
Signed-off-by: Joe Elliott <number101010@gmail.com>
1 parent 3d9fc4d commit f3bc8f9

File tree

13 files changed

+518
-332
lines changed

13 files changed

+518
-332
lines changed

modules/frontend/combiner/search.go

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package combiner
22

33
import (
44
"net/http"
5-
"sort"
6-
"time"
75

86
"github.com/grafana/tempo/pkg/api"
97
"github.com/grafana/tempo/pkg/search"
@@ -40,8 +38,8 @@ func (s *SearchJobResponse) IsMetadata() bool {
4038
var _ GRPCCombiner[*tempopb.SearchResponse] = (*genericCombiner[*tempopb.SearchResponse])(nil)
4139

4240
// NewSearch returns a search combiner
43-
func NewSearch(keepMostRecent int) Combiner {
44-
metadataCombiner := traceql.NewMetadataCombiner(keepMostRecent)
41+
func NewSearch(limit int, keepMostRecent bool) Combiner {
42+
metadataCombiner := traceql.NewMetadataCombiner(limit, keepMostRecent)
4543
diffTraces := map[string]struct{}{}
4644
completedThroughTracker := &ShardCompletionTracker{}
4745

@@ -95,18 +93,25 @@ func NewSearch(keepMostRecent int) Combiner {
9593
Metrics: current.Metrics,
9694
}
9795

98-
completedThroughSeconds := completedThroughTracker.completedThroughSeconds
99-
// if all jobs are completed then let's just return everything the combiner has
100-
if current.Metrics.CompletedJobs == current.Metrics.TotalJobs && current.Metrics.TotalJobs > 0 {
101-
completedThroughSeconds = 1
102-
}
103-
104-
// if we've not completed any shards, then return nothing
105-
if completedThroughSeconds == 0 {
106-
return diff, nil
96+
metadataFn := metadataCombiner.Metadata
97+
if keepMostRecent {
98+
metadataFn = func() []*tempopb.TraceSearchMetadata {
99+
completedThroughSeconds := completedThroughTracker.completedThroughSeconds
100+
// if all jobs are completed then let's just return everything the combiner has
101+
if current.Metrics.CompletedJobs == current.Metrics.TotalJobs && current.Metrics.TotalJobs > 0 {
102+
completedThroughSeconds = 1
103+
}
104+
105+
// if we've not completed any shards, then return nothing
106+
if completedThroughSeconds == 0 {
107+
return nil
108+
}
109+
110+
return metadataCombiner.MetadataAfter(completedThroughSeconds)
111+
}
107112
}
108113

109-
for _, tr := range metadataCombiner.MetadataAfter(completedThroughSeconds) {
114+
for _, tr := range metadataFn() {
110115
// if not in the map, skip. we haven't seen an update
111116
if _, ok := diffTraces[tr.TraceID]; !ok {
112117
continue
@@ -116,35 +121,20 @@ func NewSearch(keepMostRecent int) Combiner {
116121
diff.Traces = append(diff.Traces, tr)
117122
}
118123

119-
sort.Slice(diff.Traces, func(i, j int) bool {
120-
return diff.Traces[i].StartTimeUnixNano > diff.Traces[j].StartTimeUnixNano
121-
})
122-
123124
addRootSpanNotReceivedText(diff.Traces)
124125

125126
return diff, nil
126127
},
127128
// search combiner doesn't use current in the way i would have expected. it only tracks metrics through current and uses the results map for the actual traces.
128129
// should we change this?
129130
quit: func(_ *tempopb.SearchResponse) bool {
130-
// are we tracking a limit at all?
131-
if keepMostRecent <= 0 {
132-
return false
133-
}
134-
135131
completedThroughSeconds := completedThroughTracker.completedThroughSeconds
136132
// have we completed any shards?
137133
if completedThroughSeconds == 0 {
138-
return false
139-
}
140-
141-
// do we have enought?
142-
if metadataCombiner.Count() < keepMostRecent {
143-
return false
134+
completedThroughSeconds = traceql.TimestampNever
144135
}
145136

146-
// is our oldest trace newer than the completedThrough?
147-
return metadataCombiner.OldestTimestampNanos() > uint64(completedThroughSeconds)*uint64(time.Second)
137+
return metadataCombiner.IsCompleteFor(completedThroughSeconds)
148138
},
149139
}
150140
initHTTPCombiner(c, api.HeaderAcceptJSON)
@@ -159,8 +149,8 @@ func addRootSpanNotReceivedText(results []*tempopb.TraceSearchMetadata) {
159149
}
160150
}
161151

162-
func NewTypedSearch(limit int) GRPCCombiner[*tempopb.SearchResponse] {
163-
return NewSearch(limit).(GRPCCombiner[*tempopb.SearchResponse])
152+
func NewTypedSearch(limit int, keepMostRecent bool) GRPCCombiner[*tempopb.SearchResponse] {
153+
return NewSearch(limit, keepMostRecent).(GRPCCombiner[*tempopb.SearchResponse])
164154
}
165155

166156
// ShardCompletionTracker

0 commit comments

Comments
 (0)