Skip to content

Commit

Permalink
Prometheus API set query version
Browse files Browse the repository at this point in the history
  • Loading branch information
alpinskiy committed Nov 20, 2024
1 parent 3258c69 commit d561a83
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
4 changes: 4 additions & 0 deletions internal/api/promql.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func HandleInstantQuery(r *httpRequestHandler) {
q := promql.Query{
Expr: r.FormValue("query"),
Options: promql.Options{
Version: r.version(),
Mode: data_model.InstantQuery,
Compat: true,
TimeNow: time.Now().Unix(),
Expand Down Expand Up @@ -72,6 +73,7 @@ func HandleRangeQuery(r *httpRequestHandler) {
q := promql.Query{
Expr: r.FormValue("query"),
Options: promql.Options{
Version: r.version(),
Compat: true,
Namespace: r.Header.Get("X-StatsHouse-Namespace"),
},
Expand Down Expand Up @@ -138,6 +140,7 @@ func HandlePromSeriesQuery(r *httpRequestHandler) {
End: end,
Expr: expr,
Options: promql.Options{
Version: r.version(),
Version3Start: r.Version3Start.Load(),
Limit: 1000,
Mode: data_model.TagsQuery,
Expand Down Expand Up @@ -212,6 +215,7 @@ func HandlePromLabelValuesQuery(r *httpRequestHandler) {
End: end,
Expr: expr,
Options: promql.Options{
Version: r.version(),
Version3Start: r.Version3Start.Load(),
Limit: 1000,
Mode: data_model.TagsQuery,
Expand Down
19 changes: 12 additions & 7 deletions internal/promql/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,12 @@ func (ng Engine) Exec(ctx context.Context, h Handler, qry Query) (parser.Value,
}

func (ev *evaluator) Run() (parser.Value, func(), error) {
// parse query
if ev.t.Empty() {
return &TimeSeries{Time: []int64{}}, func() {}, nil
}
if e, ok := ev.ast.(*parser.StringLiteral); ok {
return String{T: ev.t.Start, V: e.Val}, func() {}, nil
}
if ev.t.Empty() {
return &TimeSeries{Time: []int64{}}, func() {}, nil
}
// evaluate query
ev.Tracef(ev.ast.String())
if ev.opt.Debug {
Expand Down Expand Up @@ -281,10 +280,12 @@ func (ng Engine) NewEvaluator(ctx context.Context, h Handler, qry Query) (evalua
Location: ng.location,
UTCOffset: ng.utcOffset,
})

if err != nil || ev.t.Empty() {
if err != nil {
return evaluator{}, Error{what: err}
}
if ev.t.Empty() {
return ev, nil
}
// evaluate reduction rules
ev.ars = make(map[parser.Expr]parser.Expr)
stepMin := ev.t.LODs[len(ev.t.LODs)-1].Step
Expand Down Expand Up @@ -1344,7 +1345,11 @@ func (ev *evaluator) getTagValue(metric *format.MetricMetaValue, tagX int, tagV
if err != nil {
return data_model.TagValue{}, err
}
return data_model.NewTagValueM(v), nil
if v != 0 {
return data_model.NewTagValueM(v), nil
} else {
return data_model.NewTagValue("", 0), nil
}
}
if tagX < 0 || len(metric.Tags) <= tagX {
return data_model.TagValue{}, ErrNotFound
Expand Down

0 comments on commit d561a83

Please sign in to comment.