Skip to content

Commit

Permalink
Preserve tracing context
Browse files Browse the repository at this point in the history
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
  • Loading branch information
kakkoyun committed Jun 17, 2020
1 parent 662adc8 commit 0c88266
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
7 changes: 5 additions & 2 deletions pkg/query/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ func (q *querier) Select(_ bool, hints *storage.SelectHints, ms ...*labels.Match
}

// The querier has a context but it gets cancelled, as soon as query evaluation is completed, by the engine.
ctx, cancel := context.WithTimeout(context.Background(), q.selectTimeout)
// We want to prevent this from happening for the async storea API calls we make while preserving tracing context.
ctx := tracing.ContextWithTracer(context.Background(), tracing.TracerFromContext(q.ctx))
ctx = opentracing.ContextWithSpan(ctx, opentracing.SpanFromContext(q.ctx))
ctx, cancel := context.WithTimeout(ctx, q.selectTimeout)
span, ctx := tracing.StartSpan(ctx, "querier_select", opentracing.Tags{
"minTime": hints.Start,
"maxTime": hints.End,
Expand All @@ -209,7 +212,7 @@ func (q *querier) Select(_ bool, hints *storage.SelectHints, ms ...*labels.Match
defer close(promise)

var err error
tracing.DoInSpan(ctx, "querier_select_ismyturn", func(ctx context.Context) {
tracing.DoInSpan(ctx, "querier_select_gate_ismyturn", func(ctx context.Context) {
err = q.selectGate.Start(ctx)
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/tracing/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type tripperware struct {
}

func (t *tripperware) RoundTrip(r *http.Request) (*http.Response, error) {
tracer := tracerFromContext(r.Context())
tracer := TracerFromContext(r.Context())
if tracer == nil {
// No tracer, programmatic mistake.
level.Warn(t.logger).Log("msg", "Tracer not found in context.")
Expand Down
5 changes: 3 additions & 2 deletions pkg/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func ContextWithTracer(ctx context.Context, tracer opentracing.Tracer) context.C
return context.WithValue(ctx, tracerKey, tracer)
}

func tracerFromContext(ctx context.Context) opentracing.Tracer {
// TracerFromContext extracts opentracing.Tracer from the given context.
func TracerFromContext(ctx context.Context) opentracing.Tracer {
val := ctx.Value(tracerKey)
if sp, ok := val.(opentracing.Tracer); ok {
return sp
Expand All @@ -40,7 +41,7 @@ func tracerFromContext(ctx context.Context) opentracing.Tracer {
// StartSpan starts and returns span with `operationName` and hooking as child to a span found within given context if any.
// It uses opentracing.Tracer propagated in context. If no found, it uses noop tracer without notification.
func StartSpan(ctx context.Context, operationName string, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
tracer := tracerFromContext(ctx)
tracer := TracerFromContext(ctx)
if tracer == nil {
// No tracing found, return noop span.
return opentracing.NoopTracer{}.StartSpan(operationName), ctx
Expand Down

0 comments on commit 0c88266

Please sign in to comment.