@@ -20,6 +20,9 @@ import (
20
20
"fmt"
21
21
"time"
22
22
23
+ "github.com/opentracing/opentracing-go"
24
+ ottag "github.com/opentracing/opentracing-go/ext"
25
+ otlog "github.com/opentracing/opentracing-go/log"
23
26
"github.com/pkg/errors"
24
27
"github.com/uber/jaeger-lib/metrics"
25
28
"go.uber.org/zap"
@@ -130,8 +133,10 @@ func newSpanReader(p SpanReaderParams) *SpanReader {
130
133
131
134
// GetTrace takes a traceID and returns a Trace associated with that traceID
132
135
func (s * SpanReader ) GetTrace (ctx context.Context , traceID model.TraceID ) (* model.Trace , error ) {
136
+ span , ctx := opentracing .StartSpanFromContext (ctx , "GetTrace" )
137
+ defer span .Finish ()
133
138
currentTime := time .Now ()
134
- traces , err := s .multiRead ([]string {traceID .String ()}, currentTime .Add (- s .maxSpanAge ), currentTime )
139
+ traces , err := s .multiRead (ctx , []string {traceID .String ()}, currentTime .Add (- s .maxSpanAge ), currentTime )
135
140
if err != nil {
136
141
return nil , err
137
142
}
@@ -183,13 +188,17 @@ func (s *SpanReader) indicesForTimeRange(indexName string, startTime time.Time,
183
188
184
189
// GetServices returns all services traced by Jaeger, ordered by frequency
185
190
func (s * SpanReader ) GetServices (ctx context.Context ) ([]string , error ) {
191
+ span , ctx := opentracing .StartSpanFromContext (ctx , "GetServices" )
192
+ defer span .Finish ()
186
193
currentTime := time .Now ()
187
194
jaegerIndices := s .indicesForTimeRange (s .serviceIndexPrefix , currentTime .Add (- s .maxSpanAge ), currentTime )
188
195
return s .serviceOperationStorage .getServices (jaegerIndices )
189
196
}
190
197
191
198
// GetOperations returns all operations for a specific service traced by Jaeger
192
199
func (s * SpanReader ) GetOperations (ctx context.Context , service string ) ([]string , error ) {
200
+ span , ctx := opentracing .StartSpanFromContext (ctx , "GetOperations" )
201
+ defer span .Finish ()
193
202
currentTime := time .Now ()
194
203
jaegerIndices := s .indicesForTimeRange (s .serviceIndexPrefix , currentTime .Add (- s .maxSpanAge ), currentTime )
195
204
return s .serviceOperationStorage .getOperations (jaegerIndices , service )
@@ -209,20 +218,27 @@ func bucketToStringArray(buckets []*elastic.AggregationBucketKeyItem) ([]string,
209
218
210
219
// FindTraces retrieves traces that match the traceQuery
211
220
func (s * SpanReader ) FindTraces (ctx context.Context , traceQuery * spanstore.TraceQueryParameters ) ([]* model.Trace , error ) {
221
+ span , ctx := opentracing .StartSpanFromContext (ctx , "FindTraces" )
222
+ defer span .Finish ()
223
+
212
224
if err := validateQuery (traceQuery ); err != nil {
213
225
return nil , err
214
226
}
215
227
if traceQuery .NumTraces == 0 {
216
228
traceQuery .NumTraces = defaultNumTraces
217
229
}
218
- uniqueTraceIDs , err := s .findTraceIDs (traceQuery )
230
+ uniqueTraceIDs , err := s .findTraceIDs (ctx , traceQuery )
219
231
if err != nil {
220
232
return nil , err
221
233
}
222
- return s .multiRead (uniqueTraceIDs , traceQuery .StartTimeMin , traceQuery .StartTimeMax )
234
+ return s .multiRead (ctx , uniqueTraceIDs , traceQuery .StartTimeMin , traceQuery .StartTimeMax )
223
235
}
224
236
225
- func (s * SpanReader ) multiRead (traceIDs []string , startTime , endTime time.Time ) ([]* model.Trace , error ) {
237
+ func (s * SpanReader ) multiRead (ctx context.Context , traceIDs []string , startTime , endTime time.Time ) ([]* model.Trace , error ) {
238
+
239
+ childSpan , _ := opentracing .StartSpanFromContext (ctx , "multiRead" )
240
+ childSpan .LogFields (otlog .Object ("trace_ids" , traceIDs ))
241
+ defer childSpan .Finish ()
226
242
227
243
if len (traceIDs ) == 0 {
228
244
return []* model.Trace {}, nil
@@ -256,6 +272,7 @@ func (s *SpanReader) multiRead(traceIDs []string, startTime, endTime time.Time)
256
272
results , err := s .client .MultiSearch ().Add (searchRequests ... ).Index (indices ... ).Do (s .ctx )
257
273
258
274
if err != nil {
275
+ logErrorToSpan (childSpan , err )
259
276
return nil , err
260
277
}
261
278
@@ -269,6 +286,7 @@ func (s *SpanReader) multiRead(traceIDs []string, startTime, endTime time.Time)
269
286
}
270
287
spans , err := s .collectSpans (result .Hits .Hits )
271
288
if err != nil {
289
+ logErrorToSpan (childSpan , err )
272
290
return nil , err
273
291
}
274
292
lastSpan := spans [len (spans )- 1 ]
@@ -313,7 +331,9 @@ func validateQuery(p *spanstore.TraceQueryParameters) error {
313
331
return nil
314
332
}
315
333
316
- func (s * SpanReader ) findTraceIDs (traceQuery * spanstore.TraceQueryParameters ) ([]string , error ) {
334
+ func (s * SpanReader ) findTraceIDs (ctx context.Context , traceQuery * spanstore.TraceQueryParameters ) ([]string , error ) {
335
+ childSpan , _ := opentracing .StartSpanFromContext (ctx , "findTraceIDs" )
336
+ defer childSpan .Finish ()
317
337
// Below is the JSON body to our HTTP GET request to ElasticSearch. This function creates this.
318
338
// {
319
339
// "size": 0,
@@ -492,3 +512,8 @@ func (s *SpanReader) buildObjectQuery(field string, k string, v string) elastic.
492
512
keyQuery := elastic .NewMatchQuery (keyField , v )
493
513
return elastic .NewBoolQuery ().Must (keyQuery )
494
514
}
515
+
516
+ func logErrorToSpan (span opentracing.Span , err error ) {
517
+ ottag .Error .Set (span , true )
518
+ span .LogFields (otlog .Error (err ))
519
+ }
0 commit comments