Skip to content

Commit be62816

Browse files
annanay25yurishkuro
authored andcommitted
[storage] Emit spans for Elasticsearch backend (#1128)
* [Storage] Emit spans for elastic storage backend Signed-off-by: Annanay <[email protected]> * [Storage] Pass context vis-a-vis span, add tags to span Signed-off-by: Annanay <[email protected]> * [Storage] Edit span tags to add more useful information Signed-off-by: Annanay <[email protected]> * Fix test coverage Signed-off-by: Annanay <[email protected]>
1 parent de8a9ad commit be62816

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

Diff for: plugin/storage/es/spanstore/reader.go

+30-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import (
2020
"fmt"
2121
"time"
2222

23+
"github.com/opentracing/opentracing-go"
24+
ottag "github.com/opentracing/opentracing-go/ext"
25+
otlog "github.com/opentracing/opentracing-go/log"
2326
"github.com/pkg/errors"
2427
"github.com/uber/jaeger-lib/metrics"
2528
"go.uber.org/zap"
@@ -130,8 +133,10 @@ func newSpanReader(p SpanReaderParams) *SpanReader {
130133

131134
// GetTrace takes a traceID and returns a Trace associated with that traceID
132135
func (s *SpanReader) GetTrace(ctx context.Context, traceID model.TraceID) (*model.Trace, error) {
136+
span, ctx := opentracing.StartSpanFromContext(ctx, "GetTrace")
137+
defer span.Finish()
133138
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)
135140
if err != nil {
136141
return nil, err
137142
}
@@ -183,13 +188,17 @@ func (s *SpanReader) indicesForTimeRange(indexName string, startTime time.Time,
183188

184189
// GetServices returns all services traced by Jaeger, ordered by frequency
185190
func (s *SpanReader) GetServices(ctx context.Context) ([]string, error) {
191+
span, ctx := opentracing.StartSpanFromContext(ctx, "GetServices")
192+
defer span.Finish()
186193
currentTime := time.Now()
187194
jaegerIndices := s.indicesForTimeRange(s.serviceIndexPrefix, currentTime.Add(-s.maxSpanAge), currentTime)
188195
return s.serviceOperationStorage.getServices(jaegerIndices)
189196
}
190197

191198
// GetOperations returns all operations for a specific service traced by Jaeger
192199
func (s *SpanReader) GetOperations(ctx context.Context, service string) ([]string, error) {
200+
span, ctx := opentracing.StartSpanFromContext(ctx, "GetOperations")
201+
defer span.Finish()
193202
currentTime := time.Now()
194203
jaegerIndices := s.indicesForTimeRange(s.serviceIndexPrefix, currentTime.Add(-s.maxSpanAge), currentTime)
195204
return s.serviceOperationStorage.getOperations(jaegerIndices, service)
@@ -209,20 +218,27 @@ func bucketToStringArray(buckets []*elastic.AggregationBucketKeyItem) ([]string,
209218

210219
// FindTraces retrieves traces that match the traceQuery
211220
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+
212224
if err := validateQuery(traceQuery); err != nil {
213225
return nil, err
214226
}
215227
if traceQuery.NumTraces == 0 {
216228
traceQuery.NumTraces = defaultNumTraces
217229
}
218-
uniqueTraceIDs, err := s.findTraceIDs(traceQuery)
230+
uniqueTraceIDs, err := s.findTraceIDs(ctx, traceQuery)
219231
if err != nil {
220232
return nil, err
221233
}
222-
return s.multiRead(uniqueTraceIDs, traceQuery.StartTimeMin, traceQuery.StartTimeMax)
234+
return s.multiRead(ctx, uniqueTraceIDs, traceQuery.StartTimeMin, traceQuery.StartTimeMax)
223235
}
224236

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()
226242

227243
if len(traceIDs) == 0 {
228244
return []*model.Trace{}, nil
@@ -256,6 +272,7 @@ func (s *SpanReader) multiRead(traceIDs []string, startTime, endTime time.Time)
256272
results, err := s.client.MultiSearch().Add(searchRequests...).Index(indices...).Do(s.ctx)
257273

258274
if err != nil {
275+
logErrorToSpan(childSpan, err)
259276
return nil, err
260277
}
261278

@@ -269,6 +286,7 @@ func (s *SpanReader) multiRead(traceIDs []string, startTime, endTime time.Time)
269286
}
270287
spans, err := s.collectSpans(result.Hits.Hits)
271288
if err != nil {
289+
logErrorToSpan(childSpan, err)
272290
return nil, err
273291
}
274292
lastSpan := spans[len(spans)-1]
@@ -313,7 +331,9 @@ func validateQuery(p *spanstore.TraceQueryParameters) error {
313331
return nil
314332
}
315333

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()
317337
// Below is the JSON body to our HTTP GET request to ElasticSearch. This function creates this.
318338
// {
319339
// "size": 0,
@@ -492,3 +512,8 @@ func (s *SpanReader) buildObjectQuery(field string, k string, v string) elastic.
492512
keyQuery := elastic.NewMatchQuery(keyField, v)
493513
return elastic.NewBoolQuery().Must(keyQuery)
494514
}
515+
516+
func logErrorToSpan(span opentracing.Span, err error) {
517+
ottag.Error.Set(span, true)
518+
span.LogFields(otlog.Error(err))
519+
}

Diff for: plugin/storage/es/spanstore/reader_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func returnSearchFunc(typ string, r *spanReaderTest) ([]string, error) {
413413
} else if typ == operationsAggregation {
414414
return r.reader.GetOperations(context.Background(), "someService")
415415
} else if typ == traceIDAggregation {
416-
return r.reader.findTraceIDs(&spanstore.TraceQueryParameters{})
416+
return r.reader.findTraceIDs(context.Background(), &spanstore.TraceQueryParameters{})
417417
}
418418
return nil, errors.New("Specify services, operations, traceIDs only")
419419
}

0 commit comments

Comments
 (0)