-
Notifications
You must be signed in to change notification settings - Fork 816
Add protobuf codec for query range and instant query responses #5527
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
Changes from 30 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
b1d4793
replace json data conversion with protobuf for querier handler
afhassan c54f8f5
add stats to PrometheusResponse created from query data
afhassan cabd489
add conversion from query data to PrometheusInstantQueryResponse in q…
afhassan b843075
remove endpoints not used by query api and add it as a handler for /q…
afhassan f1eddd9
add snappy compression
afhassan 855375f
improve code readability in querier handler
afhassan 6884a45
add Query and QueryRange handler functions directly to router
afhassan f83b731
add scalar and string result types to instant query handler
afhassan 112312b
reuse time parsing functions for querier handler
afhassan 9717483
improve querier handler code readability
afhassan 581847a
reuse thanos api struct definitions for querier handler
afhassan a6e8e04
return json response for unsharded querier requests
afhassan 45c85a7
remove header copying in codec for querier handler
afhassan 7af52db
change instant query context cancelled unit test to test new querier …
afhassan ad7e03c
handle empty response errors
afhassan 5d7ada2
add config to specify compression type for querier handler response
afhassan 661b078
change unit tests to work with new querier handler
afhassan 78b12ab
fix time parsing in querier handler
afhassan a55d23a
add unit tests for querier handler
afhassan 3d35da3
add feature flag for querier handler
afhassan 9f77e36
refactor querier unit tests
afhassan 17f325c
Merge branch 'cortexproject:master' into master
afhassan 1c7397e
add protobuf codec to reuse prometheus handler for querier
afhassan 8c4dafe
Merge remote-tracking branch 'upstream/master'
afhassan a466d86
refactor to use unified protobuf struct for both range and instant qu…
afhassan 018c931
fix issue with sample pointers
afhassan dea9806
Merge remote-tracking branch 'upstream/master'
afhassan 0afa97d
fix protobuf tests to expect peakSamples
afhassan 4da5cb0
refactor protobuf config logic
afhassan a917d28
refactor protobuf codec
afhassan 2f1b315
add config validation
afhassan 6a457e7
remove snappy compression tests
afhassan 7898191
fix linter formatting
afhassan 9b6ef8c
fix protobuf empty slice decoding
afhassan 4901a28
add integration test for protobuf codec
afhassan 0a7d807
add gzip compression to integration test
afhassan 8c9eaf0
Merge remote-tracking branch 'upstream/master'
afhassan 0f6e8bd
fix recompile proto files
afhassan 7730ad2
update config docs
afhassan 5d8658e
add changelog for protobuf codec
afhassan 91e1366
make gzip default compression
afhassan 390e370
add protobuf codec to experimental features
afhassan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,159 @@ | ||
package codec | ||
|
||
import ( | ||
"github.com/cortexproject/cortex/pkg/querier/tripperware" | ||
"github.com/prometheus/prometheus/promql" | ||
"github.com/cortexproject/cortex/pkg/cortexpb" | ||
"github.com/prometheus/prometheus/util/stats" | ||
jsoniter "github.com/json-iterator/go" | ||
"github.com/prometheus/common/model" | ||
"github.com/prometheus/prometheus/web/api/v1" | ||
"github.com/gogo/protobuf/proto" | ||
) | ||
type ProtobufCodec struct{} | ||
|
||
func (p ProtobufCodec) ContentType() v1.MIMEType { | ||
return v1.MIMEType{Type: "application", SubType: "x-protobuf"} | ||
} | ||
|
||
func (p ProtobufCodec) CanEncode(resp *v1.Response) bool { | ||
// Errors are parsed by default json codec | ||
if resp.Error != "" || resp.Data == nil { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (p ProtobufCodec) Encode(resp *v1.Response) ([]byte, error) { | ||
prometheusQueryResponse, err := createPrometheusQueryResponse(resp) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
b, err := proto.Marshal(prometheusQueryResponse) | ||
return b, err | ||
} | ||
|
||
func createPrometheusQueryResponse(resp *v1.Response) (*tripperware.PrometheusResponse, error) { | ||
var data = resp.Data.(*v1.QueryData) | ||
|
||
var queryResult tripperware.PrometheusQueryResult | ||
switch string(data.ResultType) { | ||
case model.ValMatrix.String(): | ||
queryResult.Result = &tripperware.PrometheusQueryResult_Matrix{ | ||
Matrix: &tripperware.Matrix{ | ||
SampleStreams: *getMatrixSampleStreams(data), | ||
}, | ||
} | ||
case model.ValVector.String(): | ||
queryResult.Result = &tripperware.PrometheusQueryResult_Vector{ | ||
Vector: &tripperware.Vector{ | ||
Samples: *getVectorSamples(data), | ||
}, | ||
} | ||
default: | ||
json := jsoniter.ConfigCompatibleWithStandardLibrary | ||
rawBytes, err := json.Marshal(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
queryResult.Result = &tripperware.PrometheusQueryResult_RawBytes{RawBytes: rawBytes} | ||
} | ||
|
||
var stats *tripperware.PrometheusResponseStats | ||
if data.Stats != nil { | ||
builtin := data.Stats.Builtin() | ||
stats = &tripperware.PrometheusResponseStats{Samples: getStats(&builtin)} | ||
} | ||
|
||
return &tripperware.PrometheusResponse{ | ||
Status: string(resp.Status), | ||
Data: tripperware.PrometheusData{ | ||
ResultType: string(data.ResultType), | ||
Result: queryResult, | ||
Stats: stats, | ||
}, | ||
ErrorType: string(resp.ErrorType), | ||
Error: resp.Error, | ||
Warnings: resp.Warnings, | ||
}, nil | ||
} | ||
|
||
func getMatrixSampleStreams(data *v1.QueryData) *[]tripperware.SampleStream { | ||
sampleStreamsLen := len(data.Result.(promql.Matrix)) | ||
sampleStreams := make([]tripperware.SampleStream, sampleStreamsLen) | ||
|
||
for i := 0; i < sampleStreamsLen; i++ { | ||
labelsLen := len(data.Result.(promql.Matrix)[i].Metric) | ||
var labels []cortexpb.LabelAdapter | ||
if labelsLen > 0 { | ||
labels = make([]cortexpb.LabelAdapter, labelsLen) | ||
for j := 0; j < labelsLen; j++ { | ||
labels[j] = cortexpb.LabelAdapter{ | ||
Name: data.Result.(promql.Matrix)[i].Metric[j].Name, | ||
Value: data.Result.(promql.Matrix)[i].Metric[j].Value, | ||
} | ||
} | ||
} | ||
|
||
samplesLen := len(data.Result.(promql.Matrix)[i].Floats) | ||
var samples []cortexpb.Sample | ||
if samplesLen > 0 { | ||
samples = make([]cortexpb.Sample, samplesLen) | ||
for j := 0; j < samplesLen; j++ { | ||
samples[j] = cortexpb.Sample{ | ||
Value: data.Result.(promql.Matrix)[i].Floats[j].F, | ||
TimestampMs: data.Result.(promql.Matrix)[i].Floats[j].T, | ||
} | ||
} | ||
} | ||
sampleStreams[i] = tripperware.SampleStream{Labels: labels, Samples: samples} | ||
} | ||
return &sampleStreams | ||
} | ||
|
||
func getVectorSamples(data *v1.QueryData) *[]tripperware.Sample { | ||
vectorSamplesLen := len(data.Result.(promql.Vector)) | ||
vectorSamples := make([]tripperware.Sample, vectorSamplesLen) | ||
|
||
for i := 0; i < vectorSamplesLen; i++ { | ||
labelsLen := len(data.Result.(promql.Vector)[i].Metric) | ||
var labels []cortexpb.LabelAdapter | ||
if labelsLen > 0 { | ||
labels = make([]cortexpb.LabelAdapter, labelsLen) | ||
for j := 0; j < labelsLen; j++ { | ||
labels[j] = cortexpb.LabelAdapter{ | ||
Name: data.Result.(promql.Vector)[i].Metric[j].Name, | ||
Value: data.Result.(promql.Vector)[i].Metric[j].Value, | ||
} | ||
} | ||
} | ||
|
||
vectorSamples[i] = tripperware.Sample{ | ||
Labels: labels, | ||
Sample: &cortexpb.Sample{ | ||
TimestampMs: data.Result.(promql.Vector)[i].T, | ||
Value: data.Result.(promql.Vector)[i].F, | ||
}, | ||
} | ||
} | ||
return &vectorSamples | ||
} | ||
|
||
func getStats(builtin *stats.BuiltinStats) *tripperware.PrometheusResponseSamplesStats { | ||
queryableSamplesStatsPerStepLen := len(builtin.Samples.TotalQueryableSamplesPerStep) | ||
queryableSamplesStatsPerStep := make([]*tripperware.PrometheusResponseQueryableSamplesStatsPerStep, queryableSamplesStatsPerStepLen) | ||
for i := 0; i < queryableSamplesStatsPerStepLen; i++ { | ||
queryableSamplesStatsPerStep[i] = &tripperware.PrometheusResponseQueryableSamplesStatsPerStep{ | ||
Value: builtin.Samples.TotalQueryableSamplesPerStep[i].V, | ||
TimestampMs: builtin.Samples.TotalQueryableSamplesPerStep[i].T, | ||
} | ||
} | ||
|
||
statSamples := tripperware.PrometheusResponseSamplesStats{ | ||
TotalQueryableSamples: builtin.Samples.TotalQueryableSamples, | ||
TotalQueryableSamplesPerStep: queryableSamplesStatsPerStep, | ||
PeakSamples: int64(builtin.Samples.PeakSamples), | ||
} | ||
yeya24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return &statSamples | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.