Skip to content

QueryFrontend: Return server side performance information on server-timing header #3685

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

Merged
merged 2 commits into from
Jan 26, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [ENHANCEMENT] Prometheus upgraded. #3739
* Avoid unnecessary `runtime.GC()` during compactions.
* Prevent compaction loop in TSDB on data gap.
* [ENHANCEMENT] Return server side performance metrics for query-frontend (using Server-timing header). #3685
* [BUGFIX] HA Tracker: don't track as error in the `cortex_kv_request_duration_seconds` metric a CAS operation intentionally aborted. #3745

## 1.7.0 in progress
Expand Down
12 changes: 12 additions & 0 deletions integration/query_frontend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@ func runQueryFrontendTest(t *testing.T, cfg queryFrontendTestConfig) {
assert.Equal(t, model.Time(1595846750806), matrix[0].Values[2].Timestamp)
}

// No need to repeat the test on Server-Timing header for each user.
if userID == 0 && cfg.queryStatsEnabled {
res, _, err := c.QueryRaw("{instance=~\"hello.*\"}")
require.NoError(t, err)
require.Regexp(t, "querier_wall_time;dur=[0-9.]*, response_time;dur=[0-9.]*$", res.Header.Values("Server-Timing")[0])
}

// In this test we do ensure that the /series start/end time is ignored and Cortex
// always returns series in ingesters memory. No need to repeat it for each user.
if userID == 0 {
Expand Down Expand Up @@ -332,6 +339,11 @@ func runQueryFrontendTest(t *testing.T, cfg queryFrontendTestConfig) {
if cfg.testMissingMetricName {
extra++
}

if cfg.queryStatsEnabled {
extra++
}

require.NoError(t, queryFrontend.WaitSumMetrics(e2e.Equals(numUsers*numQueriesPerUser+extra), "cortex_query_frontend_queries_total"))

// The number of received request is greater then the query requests because include
Expand Down
20 changes: 20 additions & 0 deletions pkg/frontend/transport/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"

Expand All @@ -27,6 +28,7 @@ import (
const (
// StatusClientClosedRequest is the status code for when a client request cancellation of an http request
StatusClientClosedRequest = 499
ServiceTimingHeaderName = "Server-Timing"
)

var (
Expand Down Expand Up @@ -114,6 +116,10 @@ func (f *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
hs[h] = vs
}

if f.cfg.QueryStatsEnabled {
writeServiceTimingHeader(queryResponseTime, hs, stats)
}

w.WriteHeader(resp.StatusCode)
// we don't check for copy error as there is no much we can do at this point
_, _ = io.Copy(w, resp.Body)
Expand Down Expand Up @@ -201,3 +207,17 @@ func writeError(w http.ResponseWriter, err error) {
}
server.WriteError(w, err)
}

func writeServiceTimingHeader(queryResponseTime time.Duration, headers http.Header, stats *querier_stats.Stats) {
if stats != nil {
parts := make([]string, 0)
parts = append(parts, statsValue("querier_wall_time", stats.LoadWallTime()))
parts = append(parts, statsValue("response_time", queryResponseTime))
headers.Set(ServiceTimingHeaderName, strings.Join(parts, ", "))
}
}

func statsValue(name string, d time.Duration) string {
durationInMs := strconv.FormatFloat(float64(d)/float64(time.Millisecond), 'f', -1, 64)
return name + ";dur=" + durationInMs
}