Skip to content

Commit aa63600

Browse files
committed
populate stats
1 parent 0ed7047 commit aa63600

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

internal/cortex/frontend/transport/handler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/prometheus/client_golang/prometheus"
2222
"github.com/prometheus/client_golang/prometheus/promauto"
2323
"github.com/thanos-io/thanos/internal/cortex/frontend/transport/utils"
24+
"github.com/thanos-io/thanos/internal/cortex/querier/queryrange"
2425
querier_stats "github.com/thanos-io/thanos/internal/cortex/querier/stats"
2526
"github.com/thanos-io/thanos/internal/cortex/tenant"
2627
"github.com/thanos-io/thanos/internal/cortex/util"
@@ -212,6 +213,8 @@ func (f *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
212213
f.reportSlowQuery(r, hs, queryString, queryResponseTime)
213214
}
214215
if f.cfg.QueryStatsEnabled {
216+
stats.FetchedChunkBytes = queryrange.GetQueryBytesFetchedFromHeader(resp.Header)
217+
stats.FetchedSeriesCount = queryrange.GetQuerySeriesFetchedFromHeader(resp.Header)
215218
f.reportQueryStats(r, queryString, queryResponseTime, stats)
216219
}
217220
}

internal/cortex/querier/queryrange/query_bytes_fetched.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
package queryrange
55

66
import (
7+
"net/http"
78
"strconv"
89
)
910

1011
// QueryBytesFetchedHeaderName is the http header name of number of bytes fetched by a query from m3readcoord.
1112
// This name is compatible with M3 and rule manager code
1213
const QueryBytesFetchedHeaderName = "M3-Fetched-Bytes-Estimate"
14+
const QuerySeriesFetchedHeaderName = "M3-Fetched-Series-Estimate"
1315

1416
func sumQueryBytesFetched(responses ...Response) uint64 {
1517
var result uint64
@@ -52,3 +54,25 @@ func QueryBytesFetchedHttpHeaderValue(response Response) []string {
5254
}
5355
return result
5456
}
57+
58+
func getHeaderValue(hdr http.Header, key string) uint64 {
59+
if val, ok := hdr[key]; ok {
60+
if len(val) != 1 {
61+
return 0
62+
}
63+
n, err := strconv.ParseUint(val[0], 10, 64)
64+
if err != nil {
65+
return 0
66+
}
67+
return n
68+
}
69+
return 0
70+
}
71+
72+
func GetQueryBytesFetchedFromHeader(hdr http.Header) uint64 {
73+
return getHeaderValue(hdr, QueryBytesFetchedHeaderName)
74+
}
75+
76+
func GetQuerySeriesFetchedFromHeader(hdr http.Header) uint64 {
77+
return getHeaderValue(hdr, QuerySeriesFetchedHeaderName)
78+
}

internal/cortex/querier/queryrange/query_range.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ func (prometheusCodec) EncodeResponse(ctx context.Context, res Response) (*http.
483483
} else if res.(*PrometheusResponse).Data.SeriesStatsCounter != nil {
484484
// Pantheon code path
485485
httpHeader[QueryBytesFetchedHeaderName] = []string{strconv.FormatInt(res.(*PrometheusResponse).Data.SeriesStatsCounter.Bytes, 10)}
486+
httpHeader[QuerySeriesFetchedHeaderName] = []string{strconv.FormatInt(res.(*PrometheusResponse).Data.SeriesStatsCounter.Series, 10)}
486487
}
487488
resp := http.Response{
488489
Header: httpHeader,

0 commit comments

Comments
 (0)