Skip to content
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

querier: Adds additional stats field in query api #4176

Merged
merged 6 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ We use _breaking :warning:_ to mark changes that are not backward compatible (re

- [#4107](https://github.com/thanos-io/thanos/pull/4107) Store: `LabelNames` and `LabelValues` now support label matchers.
- [#4171](https://github.com/thanos-io/thanos/pull/4171) Docker: Busybox image updated to latest (1.33.1)

- [#4176](https://github.com/thanos-io/thanos/pull/4176) Query API: Adds optional `Stats param` to return stats for query APIs
### Fixed
-
### Changed
Expand Down
21 changes: 17 additions & 4 deletions pkg/api/query/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/storage"

"github.com/prometheus/prometheus/util/stats"
"github.com/thanos-io/thanos/pkg/api"
"github.com/thanos-io/thanos/pkg/exemplars"
"github.com/thanos-io/thanos/pkg/exemplars/exemplarspb"
Expand All @@ -68,6 +69,7 @@ const (
MatcherParam = "match[]"
StoreMatcherParam = "storeMatch[]"
Step = "step"
Stats = "stats"
)

// QueryAPI is an API used by Thanos Querier.
Expand Down Expand Up @@ -189,9 +191,9 @@ func (qapi *QueryAPI) Register(r *route.Router, tracer opentracing.Tracer, logge
}

type queryData struct {
ResultType parser.ValueType `json:"resultType"`
Result parser.Value `json:"result"`

ResultType parser.ValueType `json:"resultType"`
Result parser.Value `json:"result"`
Stats *stats.QueryStats `json:"stats,omitempty"`
// Additional Thanos Response field.
Warnings []error `json:"warnings,omitempty"`
}
Expand Down Expand Up @@ -283,7 +285,6 @@ func (qapi *QueryAPI) parseStep(r *http.Request, defaultRangeQueryStep time.Dura
}
return defaultRangeQueryStep, nil
}

// Default step is used this way to make it consistent with UI.
d := time.Duration(math.Max(float64(rangeSeconds/250), float64(defaultRangeQueryStep/time.Second))) * time.Second
return d, nil
Expand Down Expand Up @@ -364,9 +365,15 @@ func (qapi *QueryAPI) query(r *http.Request) (interface{}, []error, *api.ApiErro
return nil, nil, &api.ApiError{Typ: api.ErrorExec, Err: res.Err}
}

// Optional stats field in response if parameter "stats" is not empty.
var qs *stats.QueryStats
if r.FormValue(Stats) != "" {
qs = stats.NewQueryStats(qry.Stats())
}
return &queryData{
ResultType: res.Value.Type(),
Result: res.Value,
Stats: qs,
}, res.Warnings, nil
}

Expand Down Expand Up @@ -478,9 +485,15 @@ func (qapi *QueryAPI) queryRange(r *http.Request) (interface{}, []error, *api.Ap
return nil, nil, &api.ApiError{Typ: api.ErrorExec, Err: res.Err}
}

// Optional stats field in response if parameter "stats" is not empty.
var qs *stats.QueryStats
if r.FormValue(Stats) != "" {
qs = stats.NewQueryStats(qry.Stats())
}
return &queryData{
ResultType: res.Value.Type(),
Result: res.Value,
Stats: qs,
}, res.Warnings, nil
}

Expand Down
45 changes: 41 additions & 4 deletions pkg/api/query/v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/tsdb"
"github.com/prometheus/prometheus/tsdb/tsdbutil"
"github.com/prometheus/prometheus/util/stats"

"github.com/thanos-io/thanos/pkg/compact"

Expand Down Expand Up @@ -72,8 +73,16 @@ type endpointTestCase struct {
response interface{}
errType baseAPI.ErrorType
}
type responeCompareFunction func(interface{}, interface{}) bool

func testEndpoint(t *testing.T, test endpointTestCase, name string) bool {
// Checks if both responses have Stats present or not.
func lookupStats(a interface{}, b interface{}) bool {
ra := a.(*queryData)
rb := b.(*queryData)
return (ra.Stats == nil && rb.Stats == nil) || (ra.Stats != nil && rb.Stats != nil)
}

func testEndpoint(t *testing.T, test endpointTestCase, name string, responseCompareFunc responeCompareFunction) bool {
return t.Run(name, func(t *testing.T) {
// Build a context with the correct request params.
ctx := context.Background()
Expand Down Expand Up @@ -115,7 +124,7 @@ func testEndpoint(t *testing.T, test endpointTestCase, name string) bool {
t.Fatalf("Expected error of type %q but got none", test.errType)
}

if !reflect.DeepEqual(resp, test.response) {
if !responseCompareFunc(resp, test.response) {
t.Fatalf("Response does not match, expected:\n%+v\ngot:\n%+v", test.response, resp)
}
})
Expand Down Expand Up @@ -597,7 +606,35 @@ func TestQueryEndpoints(t *testing.T) {
}

for i, test := range tests {
if ok := testEndpoint(t, test, fmt.Sprintf("#%d %s", i, test.query.Encode())); !ok {
if ok := testEndpoint(t, test, fmt.Sprintf("#%d %s", i, test.query.Encode()), reflect.DeepEqual); !ok {
return
}
}

tests = []endpointTestCase{
{
endpoint: api.query,
query: url.Values{
"query": []string{"2"},
"time": []string{"123.4"},
},
response: &queryData{},
},
{
endpoint: api.query,
query: url.Values{
"query": []string{"2"},
"time": []string{"123.4"},
"stats": []string{"true"},
},
response: &queryData{
Stats: &stats.QueryStats{},
},
},
}

for i, test := range tests {
if ok := testEndpoint(t, test, fmt.Sprintf("#%d %s", i, test.query.Encode()), lookupStats); !ok {
return
}
}
Expand Down Expand Up @@ -1158,7 +1195,7 @@ func TestMetadataEndpoints(t *testing.T) {
}

for i, test := range tests {
if ok := testEndpoint(t, test, strings.TrimSpace(fmt.Sprintf("#%d %s", i, test.query.Encode()))); !ok {
if ok := testEndpoint(t, test, strings.TrimSpace(fmt.Sprintf("#%d %s", i, test.query.Encode())), reflect.DeepEqual); !ok {
return
}
}
Expand Down