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

Add bytes fetched in query frontend #37

Merged
merged 7 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 47 additions & 0 deletions internal/cortex/querier/queryrange/query_bytes_fetched.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) The Cortex Authors.
// Licensed under the Apache License 2.0.

package queryrange

import (
"strconv"
)

const QueryBytesFetchedHeaderName = "M3-Fetched-Bytes-Estimate"
yuchen-db marked this conversation as resolved.
Show resolved Hide resolved

func sumQueryBytesFetched(responses ...Response) int {
yuchen-db marked this conversation as resolved.
Show resolved Hide resolved
result := 0
for _, resp := range responses {
for _, hdr := range resp.GetHeaders() {
if hdr.GetName() == QueryBytesFetchedHeaderName {
for _, v := range hdr.Values {
Copy link
Collaborator

@jnyi jnyi May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious why a header will have an array of values?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n, err := strconv.Atoi(v)
if err != nil {
continue
}
result += n
}
break
}
}
}
return result
}

func QueryBytesFetchedPrometheusResponseHeaders(responses ...Response) []*PrometheusResponseHeader {
return []*PrometheusResponseHeader{{
Name: QueryBytesFetchedHeaderName,
Values: []string{strconv.Itoa(sumQueryBytesFetched(responses...))},
}}
}

func QueryBytesFetchedHttpHeaderValue(response Response) []string {
result := []string{}
for _, hdr := range response.GetHeaders() {
if hdr.GetName() == QueryBytesFetchedHeaderName {
result = hdr.GetValues()
break
}
}
return result
}
9 changes: 5 additions & 4 deletions internal/cortex/querier/queryrange/query_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ func (prometheusCodec) MergeResponse(_ Request, responses ...Response) (Response
Analysis: AnalyzesMerge(analyzes...),
},
}

response.Headers = QueryBytesFetchedPrometheusResponseHeaders(responses...)
if len(resultsCacheGenNumberHeaderValues) != 0 {
response.Headers = []*PrometheusResponseHeader{{
response.Headers = append(response.Headers, &PrometheusResponseHeader{
Name: ResultsCacheGenNumberHeaderName,
Values: resultsCacheGenNumberHeaderValues,
}}
})
}

return &response, nil
Expand Down Expand Up @@ -449,7 +449,8 @@ func (prometheusCodec) EncodeResponse(ctx context.Context, res Response) (*http.

resp := http.Response{
Header: http.Header{
"Content-Type": []string{"application/json"},
"Content-Type": []string{"application/json"},
QueryBytesFetchedHeaderName: QueryBytesFetchedHttpHeaderValue(res),
},
Body: io.NopCloser(bytes.NewBuffer(b)),
StatusCode: http.StatusOK,
Expand Down
5 changes: 4 additions & 1 deletion pkg/queryfrontend/queryinstant_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (c queryInstantCodec) MergeResponse(req queryrange.Request, responses ...qu
Analysis: queryrange.AnalyzesMerge(analyzes...),
Stats: queryrange.StatsMerge(responses),
},
Headers: queryrange.QueryBytesFetchedPrometheusResponseHeaders(responses...),
}
default:
v, err := vectorMerge(req, promResponses)
Expand All @@ -96,6 +97,7 @@ func (c queryInstantCodec) MergeResponse(req queryrange.Request, responses ...qu
Analysis: queryrange.AnalyzesMerge(analyzes...),
Stats: queryrange.StatsMerge(responses),
},
Headers: queryrange.QueryBytesFetchedPrometheusResponseHeaders(responses...),
}
}

Expand Down Expand Up @@ -248,7 +250,8 @@ func (c queryInstantCodec) EncodeResponse(ctx context.Context, res queryrange.Re

resp := http.Response{
Header: http.Header{
"Content-Type": []string{"application/json"},
"Content-Type": []string{"application/json"},
queryrange.QueryBytesFetchedHeaderName: queryrange.QueryBytesFetchedHttpHeaderValue(res),
},
Body: io.NopCloser(bytes.NewBuffer(b)),
StatusCode: http.StatusOK,
Expand Down
Loading