forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bytes fetched in query frontend (#37)
- Loading branch information
Showing
4 changed files
with
88 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) The Cortex Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package queryrange | ||
|
||
import ( | ||
"strconv" | ||
) | ||
|
||
// QueryBytesFetchedHeaderName is the http header name of number of bytes fetched by a query from m3readcoord. | ||
// This name is compatible with M3 and rule manager code | ||
const QueryBytesFetchedHeaderName = "M3-Fetched-Bytes-Estimate" | ||
|
||
func sumQueryBytesFetched(responses ...Response) uint64 { | ||
var result uint64 | ||
result = 0 | ||
for _, resp := range responses { | ||
for _, hdr := range resp.GetHeaders() { | ||
if hdr.GetName() == QueryBytesFetchedHeaderName { | ||
for _, v := range hdr.GetValues() { | ||
n, err := strconv.ParseUint(v, 10, 64) | ||
if err != nil { | ||
continue | ||
} | ||
result += n | ||
} | ||
break | ||
} | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func QueryBytesFetchedPrometheusResponseHeaders(responses ...Response) []*PrometheusResponseHeader { | ||
n := sumQueryBytesFetched(responses...) | ||
if n == 0 { | ||
return nil | ||
} | ||
return []*PrometheusResponseHeader{{ | ||
Name: QueryBytesFetchedHeaderName, | ||
Values: []string{strconv.FormatUint(n, 10)}, | ||
}} | ||
} | ||
|
||
func QueryBytesFetchedHttpHeaderValue(response Response) []string { | ||
var result []string | ||
for _, hdr := range response.GetHeaders() { | ||
if hdr.GetName() == QueryBytesFetchedHeaderName { | ||
result = hdr.GetValues() | ||
break | ||
} | ||
} | ||
return result | ||
} |
20 changes: 20 additions & 0 deletions
20
internal/cortex/querier/queryrange/query_bytes_fetched_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) The Cortex Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package queryrange | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestQueryBytesFetchedPrometheusResponseHeaders(t *testing.T) { | ||
resp1 := PrometheusResponse{Headers: []*PrometheusResponseHeader{&PrometheusResponseHeader{Name: "M3-Fetched-Bytes-Estimate", Values: []string{"100"}}}} | ||
resp2 := PrometheusResponse{Headers: []*PrometheusResponseHeader{&PrometheusResponseHeader{Name: "M3-Fetched-Bytes-Estimate", Values: []string{"1000"}}}} | ||
resp3 := PrometheusResponse{} | ||
hdrs := QueryBytesFetchedPrometheusResponseHeaders(&resp1, &resp2, &resp3) | ||
expected := []*PrometheusResponseHeader{{Name: QueryBytesFetchedHeaderName, | ||
Values: []string{"1100"}}} | ||
require.Equal(t, hdrs, expected) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters