-
Notifications
You must be signed in to change notification settings - Fork 65
Tracing: enable tracing for main query execution steps #544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pedro-stanaka
wants to merge
3
commits into
thanos-io:main
Choose a base branch
from
pedro-stanaka:feat/tracing-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
"github.com/thanos-io/promql-engine/query" | ||
engstorage "github.com/thanos-io/promql-engine/storage" | ||
promstorage "github.com/thanos-io/promql-engine/storage/prometheus" | ||
"github.com/thanos-io/promql-engine/tracing" | ||
|
||
"github.com/efficientgo/core/errors" | ||
"github.com/prometheus/client_golang/prometheus" | ||
|
@@ -50,6 +51,16 @@ const ( | |
stepsBatch = 10 | ||
) | ||
|
||
func (q QueryType) String() string { | ||
switch q { | ||
case InstantQuery: | ||
return "InstantQuery" | ||
case RangeQuery: | ||
return "RangeQuery" | ||
} | ||
|
||
return "Unknown" | ||
} | ||
func IsUnimplemented(err error) bool { | ||
return errors.Is(err, parse.ErrNotSupportedExpr) || errors.Is(err, parse.ErrNotImplemented) | ||
} | ||
|
@@ -237,14 +248,21 @@ type Engine struct { | |
} | ||
|
||
func (e *Engine) MakeInstantQuery(ctx context.Context, q storage.Queryable, opts *QueryOpts, qs string, ts time.Time) (promql.Query, error) { | ||
span, ctx := tracing.StartSpanFromContext(ctx, "engine.MakeInstantQuery") | ||
defer span.Finish() | ||
span.SetTag("query", qs) | ||
span.SetTag("timestamp", ts.Unix()) | ||
|
||
idx, err := e.activeQueryTracker.Insert(ctx, qs) | ||
if err != nil { | ||
tracing.LogError(span, err) | ||
return nil, err | ||
} | ||
defer e.activeQueryTracker.Delete(idx) | ||
|
||
expr, err := parser.NewParser(qs, parser.WithFunctions(e.functions)).ParseExpr() | ||
if err != nil { | ||
tracing.LogError(span, err) | ||
return nil, err | ||
} | ||
// determine sorting order before optimizers run, we do this by looking for "sort" | ||
|
@@ -254,23 +272,29 @@ func (e *Engine) MakeInstantQuery(ctx context.Context, q storage.Queryable, opts | |
|
||
qOpts := e.makeQueryOpts(ts, ts, 0, opts) | ||
if qOpts.StepsBatch > 64 { | ||
return nil, ErrStepsBatchTooLarge | ||
err := ErrStepsBatchTooLarge | ||
tracing.LogError(span, err) | ||
return nil, err | ||
} | ||
|
||
planOpts := logicalplan.PlanOptions{ | ||
DisableDuplicateLabelCheck: e.disableDuplicateLabelChecks, | ||
} | ||
|
||
lplan, warns := logicalplan.NewFromAST(expr, qOpts, planOpts).Optimize(e.getLogicalOptimizers(opts)) | ||
|
||
ctx = warnings.NewContext(ctx) | ||
defer func() { warns.Merge(warnings.FromContext(ctx)) }() | ||
|
||
scanners, err := e.storageScanners(q, qOpts, lplan) | ||
if err != nil { | ||
tracing.LogError(span, err) | ||
return nil, errors.Wrap(err, "creating storage scanners") | ||
} | ||
|
||
ctx = warnings.NewContext(ctx) | ||
defer func() { warns.Merge(warnings.FromContext(ctx)) }() | ||
exec, err := execution.New(ctx, lplan.Root(), scanners, qOpts) | ||
if err != nil { | ||
tracing.LogError(span, err) | ||
return nil, err | ||
} | ||
e.metrics.totalQueries.Inc() | ||
|
@@ -336,39 +360,56 @@ func (e *Engine) MakeInstantQueryFromPlan(ctx context.Context, q storage.Queryab | |
} | ||
|
||
func (e *Engine) MakeRangeQuery(ctx context.Context, q storage.Queryable, opts *QueryOpts, qs string, start, end time.Time, step time.Duration) (promql.Query, error) { | ||
span, ctx := tracing.StartSpanFromContext(ctx, "engine.MakeRangeQuery") | ||
defer span.Finish() | ||
span.SetTag("query", qs) | ||
span.SetTag("start", start.Unix()) | ||
span.SetTag("end", end.Unix()) | ||
span.SetTag("step", step.String()) | ||
|
||
idx, err := e.activeQueryTracker.Insert(ctx, qs) | ||
if err != nil { | ||
tracing.LogError(span, err) | ||
return nil, err | ||
} | ||
defer e.activeQueryTracker.Delete(idx) | ||
|
||
expr, err := parser.NewParser(qs, parser.WithFunctions(e.functions)).ParseExpr() | ||
if err != nil { | ||
tracing.LogError(span, err) | ||
return nil, err | ||
} | ||
|
||
// Use same check as Prometheus for range queries. | ||
if expr.Type() != parser.ValueTypeVector && expr.Type() != parser.ValueTypeScalar { | ||
return nil, errors.Newf("invalid expression type %q for range query, must be Scalar or instant Vector", parser.DocumentedType(expr.Type())) | ||
err := errors.Newf("invalid expression type %q for range query, must be Scalar or instant Vector", parser.DocumentedType(expr.Type())) | ||
tracing.LogError(span, err) | ||
return nil, err | ||
} | ||
qOpts := e.makeQueryOpts(start, end, step, opts) | ||
if qOpts.StepsBatch > 64 { | ||
return nil, ErrStepsBatchTooLarge | ||
err := ErrStepsBatchTooLarge | ||
tracing.LogError(span, err) | ||
return nil, err | ||
} | ||
planOpts := logicalplan.PlanOptions{ | ||
DisableDuplicateLabelCheck: e.disableDuplicateLabelChecks, | ||
} | ||
|
||
lplan, warns := logicalplan.NewFromAST(expr, qOpts, planOpts).Optimize(e.getLogicalOptimizers(opts)) | ||
|
||
ctx = warnings.NewContext(ctx) | ||
defer func() { warns.Merge(warnings.FromContext(ctx)) }() | ||
|
||
scnrs, err := e.storageScanners(q, qOpts, lplan) | ||
if err != nil { | ||
tracing.LogError(span, err) | ||
return nil, errors.Wrap(err, "creating storage scanners") | ||
} | ||
|
||
exec, err := execution.New(ctx, lplan.Root(), scnrs, qOpts) | ||
if err != nil { | ||
tracing.LogError(span, err) | ||
return nil, err | ||
} | ||
e.metrics.totalQueries.Inc() | ||
|
@@ -528,8 +569,21 @@ type compatibilityQuery struct { | |
} | ||
|
||
func (q *compatibilityQuery) Exec(ctx context.Context) (ret *promql.Result) { | ||
span, ctx := tracing.StartSpanFromContext(ctx, "compatibilityQuery.Exec") | ||
defer span.Finish() | ||
span.SetTag("query_type", q.t.String()) | ||
span.SetTag("query_string", q.String()) | ||
if q.t == RangeQuery { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Should we just set start=end and step=0 for instant query? |
||
span.SetTag("start", q.start) | ||
span.SetTag("end", q.end) | ||
span.SetTag("step", q.step) | ||
} else { | ||
span.SetTag("timestamp", q.ts) | ||
} | ||
|
||
idx, err := q.engine.activeQueryTracker.Insert(ctx, q.String()) | ||
if err != nil { | ||
tracing.LogError(span, err) | ||
return &promql.Result{Err: err} | ||
} | ||
defer q.engine.activeQueryTracker.Delete(idx) | ||
|
@@ -557,23 +611,32 @@ func (q *compatibilityQuery) Exec(ctx context.Context) (ret *promql.Result) { | |
defer cancel() | ||
q.cancel = cancel | ||
|
||
seriesSpan := tracing.ChildSpan(span, "get_series") | ||
resultSeries, err := q.Query.exec.Series(ctx) | ||
seriesSpan.Finish() | ||
if err != nil { | ||
tracing.LogError(span, err) | ||
return newErrResult(ret, err) | ||
} | ||
|
||
series := make([]promql.Series, len(resultSeries)) | ||
for i, s := range resultSeries { | ||
series[i].Metric = s | ||
} | ||
|
||
samplesSpan := tracing.ChildSpan(span, "collect_samples") | ||
loop: | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
tracing.LogError(samplesSpan, ctx.Err()) | ||
samplesSpan.Finish() | ||
return newErrResult(ret, ctx.Err()) | ||
default: | ||
r, err := q.Query.exec.Next(ctx) | ||
if err != nil { | ||
tracing.LogError(samplesSpan, err) | ||
samplesSpan.Finish() | ||
return newErrResult(ret, err) | ||
} | ||
if r == nil { | ||
|
@@ -610,6 +673,10 @@ loop: | |
q.Query.exec.GetPool().PutVectors(r) | ||
} | ||
} | ||
samplesSpan.Finish() | ||
|
||
resultSpan := tracing.ChildSpan(span, "prepare_result") | ||
defer resultSpan.Finish() | ||
|
||
// For range Query we expect always a Matrix value type. | ||
if q.t == RangeQuery { | ||
|
@@ -623,7 +690,9 @@ loop: | |
sort.Sort(matrix) | ||
ret.Value = matrix | ||
if matrix.ContainsSameLabelset() { | ||
return newErrResult(ret, extlabels.ErrDuplicateLabelSet) | ||
err := extlabels.ErrDuplicateLabelSet | ||
tracing.LogError(resultSpan, err) | ||
return newErrResult(ret, err) | ||
} | ||
return ret | ||
} | ||
|
@@ -657,7 +726,9 @@ loop: | |
} | ||
sort.Slice(vector, q.resultSort.comparer(&vector)) | ||
if vector.ContainsSameLabelset() { | ||
return newErrResult(ret, extlabels.ErrDuplicateLabelSet) | ||
err := extlabels.ErrDuplicateLabelSet | ||
tracing.LogError(resultSpan, err) | ||
return newErrResult(ret, err) | ||
} | ||
result = vector | ||
case parser.ValueTypeScalar: | ||
|
@@ -667,7 +738,9 @@ loop: | |
} | ||
result = promql.Scalar{V: v, T: q.ts.UnixMilli()} | ||
default: | ||
panic(errors.Newf("new.Engine.exec: unexpected expression type %q", q.plan.Root().ReturnType())) | ||
err := errors.Newf("new.Engine.exec: unexpected expression type %q", q.plan.Root().ReturnType()) | ||
tracing.LogError(resultSpan, err) | ||
panic(err) | ||
} | ||
|
||
ret.Value = result | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we expect the engine to take a lot of time before query execution phase? Does these spans add value?