@@ -65,7 +65,10 @@ func (s shardQuery) RoundTrip(r *http.Request) (*http.Response, error) {
65
65
66
66
// context propagation
67
67
r = r .WithContext (ctx )
68
- reqs , err := s .buildShardedRequests (r )
68
+ subCtx , subCancel := context .WithCancel (ctx )
69
+ defer subCancel ()
70
+
71
+ reqs , err := s .buildShardedRequests (subCtx , r )
69
72
if err != nil {
70
73
return nil , err
71
74
}
@@ -75,14 +78,18 @@ func (s shardQuery) RoundTrip(r *http.Request) (*http.Response, error) {
75
78
if s .cfg .ConcurrentShards > 0 {
76
79
concurrentShards = uint (s .cfg .ConcurrentShards )
77
80
}
78
- wg := boundedwaitgroup .New (concurrentShards )
79
- mtx := sync.Mutex {}
80
81
81
- var overallError error
82
+ var (
83
+ overallError error
84
+
85
+ mtx = sync.Mutex {}
86
+ statusCode = http .StatusNotFound
87
+ statusMsg = "trace not found"
88
+ wg = boundedwaitgroup .New (concurrentShards )
89
+ )
90
+
82
91
combiner := trace .NewCombiner (s .o .MaxBytesPerTrace (userID ))
83
92
_ , _ = combiner .Consume (& tempopb.Trace {}) // The query path returns a non-nil result even if no inputs (which is different than other paths which return nil for no inputs)
84
- statusCode := http .StatusNotFound
85
- statusMsg := "trace not found"
86
93
87
94
for _ , req := range reqs {
88
95
wg .Add (1 )
@@ -97,20 +104,16 @@ func (s shardQuery) RoundTrip(r *http.Request) (*http.Response, error) {
97
104
overallError = rtErr
98
105
}
99
106
100
- if shouldQuit (r .Context (), statusCode , overallError ) {
101
- return
102
- }
103
-
104
- // check http error
105
- if rtErr != nil {
106
- _ = level .Error (s .logger ).Log ("msg" , "error querying proxy target" , "url" , innerR .RequestURI , "err" , rtErr )
107
- overallError = rtErr
107
+ // Check the context of the worker request
108
+ if shouldQuit (innerR .Context (), statusCode , overallError ) {
108
109
return
109
110
}
110
111
111
- // if the status code is anything but happy, save the error and pass it down the line
112
+ // if the status code is anything but happy, save the error and pass it
113
+ // down the line
112
114
if resp .StatusCode != http .StatusOK && resp .StatusCode != http .StatusNotFound {
113
- // todo: if we cancel the parent context here will it shortcircuit the other queries and fail fast?
115
+ defer subCancel ()
116
+
114
117
statusCode = resp .StatusCode
115
118
bytesMsg , readErr := io .ReadAll (resp .Body )
116
119
if readErr != nil {
@@ -129,7 +132,7 @@ func (s shardQuery) RoundTrip(r *http.Request) (*http.Response, error) {
129
132
}
130
133
131
134
// marshal into a trace to combine.
132
- // todo : better define responsibilities between middleware. the parent middleware in frontend.go actually sets the header
135
+ // TODO : better define responsibilities between middleware. the parent middleware in frontend.go actually sets the header
133
136
// which forces the body here to be a proto encoded tempopb.Trace{}
134
137
traceResp := & tempopb.TraceByIDResponse {}
135
138
rtErr = proto .Unmarshal (buff , traceResp )
@@ -202,9 +205,8 @@ func (s shardQuery) RoundTrip(r *http.Request) (*http.Response, error) {
202
205
203
206
// buildShardedRequests returns a slice of requests sharded on the precalculated
204
207
// block boundaries
205
- func (s * shardQuery ) buildShardedRequests (parent * http.Request ) ([]* http.Request , error ) {
206
- ctx := parent .Context ()
207
- userID , err := user .ExtractOrgID (ctx )
208
+ func (s * shardQuery ) buildShardedRequests (ctx context.Context , parent * http.Request ) ([]* http.Request , error ) {
209
+ userID , err := user .ExtractOrgID (parent .Context ())
208
210
if err != nil {
209
211
return nil , err
210
212
}
@@ -237,6 +239,7 @@ func shouldQuit(ctx context.Context, statusCode int, err error) bool {
237
239
if err != nil {
238
240
return true
239
241
}
242
+
240
243
if ctx .Err () != nil {
241
244
return true
242
245
}
@@ -245,9 +248,5 @@ func shouldQuit(ctx context.Context, statusCode int, err error) bool {
245
248
return true
246
249
}
247
250
248
- if statusCode / 100 == 5 { // bail on any 5xx's
249
- return true
250
- }
251
-
252
- return false
251
+ return statusCode / 100 == 5 // bail on any 5xx's
253
252
}
0 commit comments