Skip to content

Commit

Permalink
Merge branch 'main' into update-prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
alanprot authored Jun 10, 2024
2 parents e1f54b6 + b72f7da commit 3914a08
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 63 deletions.
27 changes: 21 additions & 6 deletions pkg/api/query/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/thanos-io/thanos/pkg/query"
"github.com/thanos-io/thanos/pkg/store/labelpb"
"github.com/thanos-io/thanos/pkg/store/storepb/prompb"
"github.com/thanos-io/thanos/pkg/tracing"
)

type GRPCAPI struct {
Expand Down Expand Up @@ -93,13 +94,20 @@ func (g *GRPCAPI) Query(request *querypb.QueryRequest, server querypb.Query_Quer
query.NoopSeriesStatsReporter,
)

qry, err := g.getQueryForEngine(ctx, request, queryable, maxResolution)
if err != nil {
var qry promql.Query
if err := tracing.DoInSpanWithErr(ctx, "instant_query_create", func(ctx context.Context) error {
var err error
qry, err = g.getQueryForEngine(ctx, request, queryable, maxResolution)
return err
}); err != nil {
return err
}
defer qry.Close()

result := qry.Exec(ctx)
var result *promql.Result
tracing.DoInSpan(ctx, "range_query_exec", func(ctx context.Context) {
result = qry.Exec(ctx)
})
if result.Err != nil {
return status.Error(codes.Aborted, result.Err.Error())
}
Expand Down Expand Up @@ -206,13 +214,20 @@ func (g *GRPCAPI) QueryRange(request *querypb.QueryRangeRequest, srv querypb.Que
query.NoopSeriesStatsReporter,
)

qry, err := g.getRangeQueryForEngine(ctx, request, queryable)
if err != nil {
var qry promql.Query
if err := tracing.DoInSpanWithErr(ctx, "range_query_create", func(ctx context.Context) error {
var err error
qry, err = g.getRangeQueryForEngine(ctx, request, queryable)
return err
}); err != nil {
return err
}
defer qry.Close()

result := qry.Exec(ctx)
var result *promql.Result
tracing.DoInSpan(ctx, "range_query_exec", func(ctx context.Context) {
result = qry.Exec(ctx)
})
if result.Err != nil {
return status.Error(codes.Aborted, result.Err.Error())
}
Expand Down
118 changes: 61 additions & 57 deletions pkg/api/query/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,45 +668,48 @@ func (qapi *QueryAPI) query(r *http.Request) (interface{}, []error, *api.ApiErro
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err}, func() {}
}

// We are starting promQL tracing span here, because we have no control over promQL code.
span, ctx := tracing.StartSpan(ctx, "promql_instant_query")
defer span.Finish()

var seriesStats []storepb.SeriesStatsCounter
qry, err := engine.NewInstantQuery(
ctx,
qapi.queryableCreate(
enableDedup,
replicaLabels,
storeDebugMatchers,
maxSourceResolution,
enablePartialResponse,
false,
shardInfo,
query.NewAggregateStatsReporter(&seriesStats),
),
promql.NewPrometheusQueryOpts(false, lookbackDelta),
queryStr,
ts,
var (
qry promql.Query
seriesStats []storepb.SeriesStatsCounter
)

if err != nil {
if err := tracing.DoInSpanWithErr(ctx, "instant_query_create", func(ctx context.Context) error {
var err error
qry, err = engine.NewInstantQuery(
ctx,
qapi.queryableCreate(
enableDedup,
replicaLabels,
storeDebugMatchers,
maxSourceResolution,
enablePartialResponse,
false,
shardInfo,
query.NewAggregateStatsReporter(&seriesStats),
),
promql.NewPrometheusQueryOpts(false, lookbackDelta),
queryStr,
ts,
)
return err
}); err != nil {
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err}, func() {}
}
res := qry.Exec(ctx)

analysis, err := qapi.parseQueryAnalyzeParam(r, qry)
if err != nil {
return nil, nil, apiErr, func() {}
}

tracing.DoInSpan(ctx, "query_gate_ismyturn", func(ctx context.Context) {
err = qapi.gate.Start(ctx)
})
if err != nil {
if err := tracing.DoInSpanWithErr(ctx, "query_gate_ismyturn", qapi.gate.Start); err != nil {
return nil, nil, &api.ApiError{Typ: api.ErrorExec, Err: err}, qry.Close
}
defer qapi.gate.Done()
beforeRange := time.Now()

var res *promql.Result
tracing.DoInSpan(ctx, "instant_query_exec", func(ctx context.Context) {
res = qry.Exec(ctx)
})
if res.Err != nil {
switch res.Err.(type) {
case promql.ErrQueryCanceled:
Expand Down Expand Up @@ -966,48 +969,49 @@ func (qapi *QueryAPI) queryRange(r *http.Request) (interface{}, []error, *api.Ap
// Record the query range requested.
qapi.queryRangeHist.Observe(end.Sub(start).Seconds())

// We are starting promQL tracing span here, because we have no control over promQL code.
span, ctx := tracing.StartSpan(ctx, "promql_range_query")
defer span.Finish()

var seriesStats []storepb.SeriesStatsCounter
qry, err := engine.NewRangeQuery(
ctx,
qapi.queryableCreate(
enableDedup,
replicaLabels,
storeDebugMatchers,
maxSourceResolution,
enablePartialResponse,
false,
shardInfo,
query.NewAggregateStatsReporter(&seriesStats),
),
promql.NewPrometheusQueryOpts(false, lookbackDelta),
queryStr,
start,
end,
step,
var (
qry promql.Query
seriesStats []storepb.SeriesStatsCounter
)
if err != nil {
if err := tracing.DoInSpanWithErr(ctx, "range_query_create", func(ctx context.Context) error {
var err error
qry, err = engine.NewRangeQuery(
ctx,
qapi.queryableCreate(
enableDedup,
replicaLabels,
storeDebugMatchers,
maxSourceResolution,
enablePartialResponse,
false,
shardInfo,
query.NewAggregateStatsReporter(&seriesStats),
),
promql.NewPrometheusQueryOpts(false, lookbackDelta),
queryStr,
start,
end,
step,
)
return err
}); err != nil {
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err}, func() {}
}

res := qry.Exec(ctx)

analysis, err := qapi.parseQueryAnalyzeParam(r, qry)
if err != nil {
return nil, nil, apiErr, func() {}
}

tracing.DoInSpan(ctx, "query_gate_ismyturn", func(ctx context.Context) {
err = qapi.gate.Start(ctx)
})
if err != nil {
if err := tracing.DoInSpanWithErr(ctx, "query_gate_ismyturn", qapi.gate.Start); err != nil {
return nil, nil, &api.ApiError{Typ: api.ErrorExec, Err: err}, qry.Close
}
defer qapi.gate.Done()

var res *promql.Result
tracing.DoInSpan(ctx, "range_query_exec", func(ctx context.Context) {
res = qry.Exec(ctx)

})
beforeRange := time.Now()
if res.Err != nil {
switch res.Err.(type) {
Expand Down

0 comments on commit 3914a08

Please sign in to comment.