diff --git a/pkg/distributor/distributor.go b/pkg/distributor/distributor.go index 6c1600c50b6..c4346df74e3 100644 --- a/pkg/distributor/distributor.go +++ b/pkg/distributor/distributor.go @@ -627,7 +627,7 @@ func (d *Distributor) Push(ctx context.Context, req *cortexpb.WriteRequest) (*co } // Count the total samples, exemplars in, prior to validation or deduplication, for comparison with other metrics. d.incomingSamples.WithLabelValues(userID, sampleMetricTypeFloat).Add(float64(numFloatSamples)) - d.incomingSamples.WithLabelValues(userID, sampleMetricTypeHistogram).Add(float64(numFloatSamples)) + d.incomingSamples.WithLabelValues(userID, sampleMetricTypeHistogram).Add(float64(numHistogramSamples)) d.incomingExemplars.WithLabelValues(userID).Add(float64(numExemplars)) // Count the total number of metadata in. d.incomingMetadata.WithLabelValues(userID).Add(float64(len(req.Metadata))) diff --git a/pkg/querier/tripperware/instantquery/custom.go b/pkg/querier/tripperware/instantquery/custom.go new file mode 100644 index 00000000000..c88f9284e14 --- /dev/null +++ b/pkg/querier/tripperware/instantquery/custom.go @@ -0,0 +1,12 @@ +package instantquery + +func (m *Sample) GetTimestampMs() int64 { + if m != nil { + if m.Sample != nil { + return m.Sample.TimestampMs + } else if m.Histogram != nil { + return m.Histogram.TimestampMs + } + } + return 0 +} diff --git a/pkg/querier/tripperware/instantquery/instant_query.go b/pkg/querier/tripperware/instantquery/instant_query.go index 423e17bab0e..135670ec377 100644 --- a/pkg/querier/tripperware/instantquery/instant_query.go +++ b/pkg/querier/tripperware/instantquery/instant_query.go @@ -10,6 +10,7 @@ import ( "sort" "strings" "time" + "unsafe" jsoniter "github.com/json-iterator/go" "github.com/opentracing/opentracing-go" @@ -344,7 +345,7 @@ func vectorMerge(ctx context.Context, req tripperware.Request, resps []*Promethe if existingSample, ok := output[metric]; !ok { output[metric] = s metrics = append(metrics, metric) // Preserve the order of metric. - } else if existingSample.GetSample().TimestampMs < s.GetSample().TimestampMs { + } else if existingSample.GetTimestampMs() < s.GetTimestampMs() { // Choose the latest sample if we see overlap. output[metric] = s } @@ -366,11 +367,6 @@ func vectorMerge(ctx context.Context, req tripperware.Request, resps []*Promethe return result, nil } - type pair struct { - metric string - s *Sample - } - samples := make([]*pair, 0, len(output)) for k, v := range output { samples = append(samples, &pair{ @@ -379,13 +375,15 @@ func vectorMerge(ctx context.Context, req tripperware.Request, resps []*Promethe }) } + // TODO: What if we have mixed float and histogram samples in the response? + // Then the sorting behavior is undefined. Prometheus doesn't handle it. sort.Slice(samples, func(i, j int) bool { - // Order is determined by vector + // Order is determined by vector. switch sortPlan { case sortByValuesAsc: - return samples[i].s.Sample.Value < samples[j].s.Sample.Value + return getSortValueFromPair(samples, i) < getSortValueFromPair(samples, j) case sortByValuesDesc: - return samples[i].s.Sample.Value > samples[j].s.Sample.Value + return getSortValueFromPair(samples, i) > getSortValueFromPair(samples, j) } return samples[i].metric < samples[j].metric }) @@ -405,6 +403,22 @@ const ( sortByLabels sortPlan = 3 ) +type pair struct { + metric string + s *Sample +} + +// getSortValueFromPair gets the float value used for sorting from samples. +// If float sample, use sample value. If histogram sample, use histogram sum. +// This is the same behavior as Prometheus https://github.com/prometheus/prometheus/blob/v2.53.0/promql/functions.go#L1595. +func getSortValueFromPair(samples []*pair, i int) float64 { + if samples[i].s.Histogram != nil { + return samples[i].s.Histogram.Histogram.Sum + } + // Impossible to have both histogram and sample nil. + return samples[i].s.Sample.Value +} + func sortPlanForQuery(q string) (sortPlan, error) { expr, err := promqlparser.ParseExpr(q) if err != nil { @@ -534,30 +548,65 @@ func decorateWithParamName(err error, field string) error { return fmt.Errorf(errTmpl, field, err) } -// UnmarshalJSON implements json.Unmarshaler. -func (s *Sample) UnmarshalJSON(data []byte) error { - var sample struct { - Metric labels.Labels `json:"metric"` - Value cortexpb.Sample `json:"value"` - } - if err := json.Unmarshal(data, &sample); err != nil { - return err +func init() { + jsoniter.RegisterTypeEncoderFunc("instantquery.Sample", encodeSample, marshalJSONIsEmpty) + jsoniter.RegisterTypeDecoderFunc("instantquery.Sample", decodeSample) +} + +func marshalJSONIsEmpty(ptr unsafe.Pointer) bool { + return false +} + +func decodeSample(ptr unsafe.Pointer, iter *jsoniter.Iterator) { + ss := (*Sample)(ptr) + for field := iter.ReadObject(); field != ""; field = iter.ReadObject() { + switch field { + case "metric": + metricString := iter.ReadAny().ToString() + lbls := labels.Labels{} + if err := json.UnmarshalFromString(metricString, &lbls); err != nil { + iter.ReportError("unmarshal Sample", err.Error()) + return + } + ss.Labels = cortexpb.FromLabelsToLabelAdapters(lbls) + case "value": + ss.Sample = &cortexpb.Sample{} + cortexpb.SampleJsoniterDecode(unsafe.Pointer(ss.Sample), iter) + case "histogram": + ss.Histogram = &tripperware.SampleHistogramPair{} + tripperware.UnmarshalSampleHistogramPairJSON(unsafe.Pointer(ss.Histogram), iter) + default: + iter.ReportError("unmarshal Sample", fmt.Sprint("unexpected key:", field)) + return + } } - s.Labels = cortexpb.FromLabelsToLabelAdapters(sample.Metric) - s.Sample = sample.Value - return nil } -// MarshalJSON implements json.Marshaler. -func (s *Sample) MarshalJSON() ([]byte, error) { - sample := struct { - Metric model.Metric `json:"metric"` - Value cortexpb.Sample `json:"value"` - }{ - Metric: cortexpb.FromLabelAdaptersToMetric(s.Labels), - Value: s.Sample, - } - return json.Marshal(sample) +func encodeSample(ptr unsafe.Pointer, stream *jsoniter.Stream) { + ss := (*Sample)(ptr) + stream.WriteObjectStart() + + stream.WriteObjectField(`metric`) + lbls, err := cortexpb.FromLabelAdaptersToLabels(ss.Labels).MarshalJSON() + if err != nil { + stream.Error = err + return + } + stream.SetBuffer(append(stream.Buffer(), lbls...)) + + if ss.Sample != nil { + stream.WriteMore() + stream.WriteObjectField(`value`) + cortexpb.SampleJsoniterEncode(unsafe.Pointer(ss.Sample), stream) + } + + if ss.Histogram != nil { + stream.WriteMore() + stream.WriteObjectField(`histogram`) + tripperware.MarshalSampleHistogramPairJSON(unsafe.Pointer(ss.Histogram), stream) + } + + stream.WriteObjectEnd() } // UnmarshalJSON implements json.Unmarshaler. diff --git a/pkg/querier/tripperware/instantquery/instant_query_test.go b/pkg/querier/tripperware/instantquery/instant_query_test.go index aba71fb099e..3a19641b975 100644 --- a/pkg/querier/tripperware/instantquery/instant_query_test.go +++ b/pkg/querier/tripperware/instantquery/instant_query_test.go @@ -21,6 +21,8 @@ import ( "github.com/cortexproject/cortex/pkg/querier/tripperware" ) +const testHistogramResponse = `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"prometheus_http_request_duration_seconds","handler":"/metrics","instance":"localhost:9090","job":"prometheus"},"histogram":[1719528871.898,{"count":"6342","sum":"43.31319875499995","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"],[0,"0.0015060652591874421","0.001642375811042411","7"],[0,"0.001642375811042411","0.0017910235218841233","5"],[0,"0.0017910235218841233","0.001953125","13"],[0,"0.001953125","0.0021298979153618314","19"],[0,"0.0021298979153618314","0.0023226701464896895","13"],[0,"0.0023226701464896895","0.002532889755177753","13"],[0,"0.002532889755177753","0.002762135864009951","15"],[0,"0.002762135864009951","0.0030121305183748843","12"],[0,"0.0030121305183748843","0.003284751622084822","34"],[0,"0.003284751622084822","0.0035820470437682465","188"],[0,"0.0035820470437682465","0.00390625","372"],[0,"0.00390625","0.004259795830723663","400"],[0,"0.004259795830723663","0.004645340292979379","411"],[0,"0.004645340292979379","0.005065779510355506","425"],[0,"0.005065779510355506","0.005524271728019902","425"],[0,"0.005524271728019902","0.0060242610367497685","521"],[0,"0.0060242610367497685","0.006569503244169644","621"],[0,"0.006569503244169644","0.007164094087536493","593"],[0,"0.007164094087536493","0.0078125","506"],[0,"0.0078125","0.008519591661447326","458"],[0,"0.008519591661447326","0.009290680585958758","346"],[0,"0.009290680585958758","0.010131559020711013","285"],[0,"0.010131559020711013","0.011048543456039804","196"],[0,"0.011048543456039804","0.012048522073499537","129"],[0,"0.012048522073499537","0.013139006488339287","85"],[0,"0.013139006488339287","0.014328188175072986","65"],[0,"0.014328188175072986","0.015625","54"],[0,"0.015625","0.01703918332289465","53"],[0,"0.01703918332289465","0.018581361171917516","20"],[0,"0.018581361171917516","0.020263118041422026","21"],[0,"0.020263118041422026","0.022097086912079608","15"],[0,"0.022097086912079608","0.024097044146999074","11"],[0,"0.024097044146999074","0.026278012976678575","2"],[0,"0.026278012976678575","0.028656376350145972","3"],[0,"0.028656376350145972","0.03125","3"],[0,"0.04052623608284405","0.044194173824159216","2"]]}]}]}}` + func TestRequest(t *testing.T) { t.Parallel() codec := InstantQueryCodec @@ -185,6 +187,9 @@ func TestResponse(t *testing.T) { { body: `{"status":"success","data":{"resultType":"vector","result":[{"metric":{},"value":[1,"1266464.0146205237"]}]}}`, }, + { + body: testHistogramResponse, + }, } { tc := tc t.Run(strconv.Itoa(i), func(t *testing.T) { @@ -260,6 +265,12 @@ func TestMergeResponse(t *testing.T) { }, expectedResp: `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up"},"value":[1,"1"]}]}}`, }, + { + name: "duplicated histogram responses", + req: defaultReq, + resps: []string{testHistogramResponse, testHistogramResponse}, + expectedResp: testHistogramResponse, + }, { name: "duplicated response with stats", req: defaultReq, @@ -278,6 +289,15 @@ func TestMergeResponse(t *testing.T) { }, expectedResp: `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"bar"},"value":[2,"2"]},{"metric":{"__name__":"up","job":"foo"},"value":[1,"1"]}]}}`, }, + { + name: "merge two histogram responses", + req: defaultReq, + resps: []string{ + `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"foo"},"histogram":[1719528871.898,{"count":"6342","sum":"43.31319875499995","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]}]}}`, + `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"bar"},"histogram":[1719528800,{"count":"1","sum":"0","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]}]}}`, + }, + expectedResp: `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"bar"},"histogram":[1719528800,{"count":"1","sum":"0","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]},{"metric":{"__name__":"up","job":"foo"},"histogram":[1719528871.898,{"count":"6342","sum":"43.31319875499995","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]}]}}`, + }, { name: "merge two responses with sort", req: &PrometheusRequest{Query: "sort(sum by (job) (up))"}, @@ -287,6 +307,15 @@ func TestMergeResponse(t *testing.T) { }, expectedResp: `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"foo"},"value":[1,"1"]},{"metric":{"__name__":"up","job":"bar"},"value":[1,"2"]}]}}`, }, + { + name: "merge two histogram responses with sort", + req: &PrometheusRequest{Query: "sort(sum by (job) (up))"}, + resps: []string{ + `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"foo"},"histogram":[1719528871.898,{"count":"6342","sum":"43.31319875499995","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]}]}}`, + `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"bar"},"histogram":[1719528880,{"count":"1","sum":"0","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]}]}}`, + }, + expectedResp: `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"bar"},"histogram":[1719528880,{"count":"1","sum":"0","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]},{"metric":{"__name__":"up","job":"foo"},"histogram":[1719528871.898,{"count":"6342","sum":"43.31319875499995","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]}]}}`, + }, { name: "merge two responses with sort_desc", req: &PrometheusRequest{Query: "sort_desc(sum by (job) (up))"}, @@ -296,6 +325,15 @@ func TestMergeResponse(t *testing.T) { }, expectedResp: `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"bar"},"value":[1,"2"]},{"metric":{"__name__":"up","job":"foo"},"value":[1,"1"]}]}}`, }, + { + name: "merge two histogram responses with sort_desc", + req: &PrometheusRequest{Query: "sort_desc(sum by (job) (up))"}, + resps: []string{ + `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"foo"},"histogram":[1719528871.898,{"count":"6342","sum":"43.31319875499995","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]}]}}`, + `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"bar"},"histogram":[1719528880,{"count":"1","sum":"0","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]}]}}`, + }, + expectedResp: `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"foo"},"histogram":[1719528871.898,{"count":"6342","sum":"43.31319875499995","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]},{"metric":{"__name__":"up","job":"bar"},"histogram":[1719528880,{"count":"1","sum":"0","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]}]}}`, + }, { name: "merge two responses with topk", req: &PrometheusRequest{Query: "topk(10, up) by(job)"}, @@ -305,6 +343,15 @@ func TestMergeResponse(t *testing.T) { }, expectedResp: `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"foo"},"value":[1,"1"]},{"metric":{"__name__":"up","job":"bar"},"value":[1,"2"]}]}}`, }, + { + name: "merge two histogram responses with topk", + req: &PrometheusRequest{Query: "topk(10, up) by(job)"}, + resps: []string{ + `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"foo"},"histogram":[1719528871.898,{"count":"6342","sum":"43.31319875499995","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]}]}}`, + `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"bar"},"histogram":[1719528880,{"count":"1","sum":"0","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]}]}}`, + }, + expectedResp: `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"foo"},"histogram":[1719528871.898,{"count":"6342","sum":"43.31319875499995","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]},{"metric":{"__name__":"up","job":"bar"},"histogram":[1719528880,{"count":"1","sum":"0","buckets":[[0,"0.0013810679320049755","0.0015060652591874421","1"]]}]}]}}`, + }, { name: "merge with warnings.", req: &PrometheusRequest{Query: "topk(10, up) by(job)"}, diff --git a/pkg/querier/tripperware/instantquery/instantquery.pb.go b/pkg/querier/tripperware/instantquery/instantquery.pb.go index a401a19840a..6324739eb34 100644 --- a/pkg/querier/tripperware/instantquery/instantquery.pb.go +++ b/pkg/querier/tripperware/instantquery/instantquery.pb.go @@ -313,8 +313,9 @@ func (m *Vector) GetSamples() []*Sample { } type Sample struct { - Labels []github_com_cortexproject_cortex_pkg_cortexpb.LabelAdapter `protobuf:"bytes,1,rep,name=labels,proto3,customtype=github.com/cortexproject/cortex/pkg/cortexpb.LabelAdapter" json:"metric"` - Sample cortexpb.Sample `protobuf:"bytes,2,opt,name=sample,proto3" json:"value"` + Labels []github_com_cortexproject_cortex_pkg_cortexpb.LabelAdapter `protobuf:"bytes,1,rep,name=labels,proto3,customtype=github.com/cortexproject/cortex/pkg/cortexpb.LabelAdapter" json:"metric"` + Sample *cortexpb.Sample `protobuf:"bytes,2,opt,name=sample,proto3" json:"value"` + Histogram *tripperware.SampleHistogramPair `protobuf:"bytes,3,opt,name=histogram,proto3" json:"histogram"` } func (m *Sample) Reset() { *m = Sample{} } @@ -349,11 +350,18 @@ func (m *Sample) XXX_DiscardUnknown() { var xxx_messageInfo_Sample proto.InternalMessageInfo -func (m *Sample) GetSample() cortexpb.Sample { +func (m *Sample) GetSample() *cortexpb.Sample { if m != nil { return m.Sample } - return cortexpb.Sample{} + return nil +} + +func (m *Sample) GetHistogram() *tripperware.SampleHistogramPair { + if m != nil { + return m.Histogram + } + return nil } type Matrix struct { @@ -411,50 +419,52 @@ func init() { func init() { proto.RegisterFile("instantquery.proto", fileDescriptor_d2ce36475a368033) } var fileDescriptor_d2ce36475a368033 = []byte{ - // 679 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x54, 0x4d, 0x4f, 0x13, 0x41, - 0x18, 0xde, 0xa1, 0x74, 0x81, 0x29, 0xa0, 0x19, 0x08, 0x56, 0x42, 0x66, 0x9b, 0x46, 0x93, 0x6a, - 0x74, 0x9b, 0xd4, 0x18, 0xf5, 0xe8, 0x2a, 0x06, 0xa3, 0x06, 0x9c, 0x12, 0x4d, 0xbc, 0x4d, 0xcb, - 0xa4, 0xac, 0x76, 0xbb, 0xeb, 0xec, 0x14, 0xe8, 0xcd, 0x9f, 0xe0, 0xaf, 0x30, 0xfe, 0x07, 0xff, - 0x00, 0x47, 0x8e, 0xc4, 0xc3, 0x46, 0x96, 0x8b, 0xd9, 0x13, 0xde, 0x3c, 0x9a, 0xf9, 0x58, 0xd8, - 0xfa, 0x89, 0xb7, 0x9d, 0xe7, 0x7d, 0x9e, 0xf7, 0xe3, 0x79, 0x67, 0x07, 0x22, 0x7f, 0x10, 0x0b, - 0x3a, 0x10, 0x6f, 0x87, 0x8c, 0x8f, 0xdc, 0x88, 0x87, 0x22, 0x44, 0xb3, 0x45, 0x6c, 0x79, 0xb1, - 0x17, 0xf6, 0x42, 0x15, 0x68, 0xca, 0x2f, 0xcd, 0x59, 0xbe, 0xd7, 0xf3, 0xc5, 0xf6, 0xb0, 0xe3, - 0x76, 0xc3, 0xa0, 0xd9, 0x0d, 0xb9, 0x60, 0x7b, 0x11, 0x0f, 0x5f, 0xb3, 0xae, 0x30, 0xa7, 0x66, - 0xf4, 0xa6, 0x97, 0x07, 0x3a, 0xe6, 0xc3, 0x48, 0x1f, 0x9c, 0x47, 0x2a, 0x6b, 0xfb, 0x8c, 0x37, - 0x05, 0xf7, 0xa3, 0x88, 0xf1, 0x5d, 0xca, 0x59, 0xb3, 0xd0, 0x63, 0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06, 0xf5, 0xb9, 0xf7, 0x44, 0x7a, 0xff, 0x39, 0x71, 0xfe, 0xeb, - 0x25, 0xd0, 0xfa, 0xfb, 0x5b, 0x34, 0x12, 0x8c, 0xcb, 0xc5, 0x05, 0x4c, 0x70, 0xbf, 0x4b, 0x4c, - 0x3d, 0x74, 0x07, 0xda, 0xba, 0x1f, 0x73, 0x19, 0x2e, 0x9e, 0x55, 0xd6, 0xbd, 0x79, 0x73, 0x66, - 0xe5, 0xe5, 0x1d, 0xda, 0x1f, 0x32, 0x62, 0xe8, 0xf5, 0x75, 0x68, 0x6b, 0x57, 0xd0, 0x2a, 0x9c, - 0xd3, 0x58, 0x5b, 0x70, 0x46, 0x83, 0x7c, 0x86, 0xcb, 0x63, 0x77, 0xa0, 0x5d, 0x60, 0x78, 0x93, - 0x32, 0x25, 0x19, 0x57, 0x79, 0xde, 0xc1, 0x11, 0xb6, 0x0e, 0x8f, 0xb0, 0x75, 0x72, 0x84, 0xc1, - 0xbb, 0x14, 0x83, 0x8f, 0x29, 0x06, 0xfb, 0x29, 0x06, 0x07, 0x29, 0x06, 0x5f, 0x52, 0x0c, 0xbe, - 0xa6, 0xd8, 0x3a, 0x49, 0x31, 0x78, 0x7f, 0x8c, 0xad, 0x83, 0x63, 0x6c, 0x1d, 0x1e, 0x63, 0xeb, - 0xd5, 0xd8, 0xd3, 0xd9, 0xb1, 0xd5, 0x5b, 0x75, 0xeb, 0x47, 0x00, 0x00, 0x00, 0xff, 0xff, 0x18, - 0x40, 0x53, 0x5d, 0x65, 0x05, 0x00, 0x00, + // 713 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x54, 0xc1, 0x6e, 0xd3, 0x4a, + 0x14, 0xf5, 0x34, 0x8d, 0xdb, 0x4c, 0xda, 0xbe, 0xf7, 0xa6, 0x55, 0x5f, 0x5e, 0x55, 0x8d, 0xa3, + 0xe8, 0x21, 0x05, 0x04, 0x8e, 0x14, 0x84, 0x80, 0x25, 0x86, 0xa2, 0x20, 0x40, 0x6d, 0xa7, 0x15, + 0x48, 0xec, 0x26, 0xe9, 0x28, 0x35, 0xc4, 0xb1, 0x99, 0x99, 0xb4, 0xcd, 0x8e, 0x2f, 0x40, 0x7c, + 0x05, 0xe2, 0x53, 0xba, 0xec, 0xb2, 0x62, 0x61, 0xd1, 0x74, 0x83, 0xbc, 0x2a, 0x3b, 0x96, 0xc8, + 0x33, 0xe3, 0xc6, 0x81, 0x02, 0x65, 0xe7, 0xb9, 0xf7, 0x9c, 0x73, 0xef, 0x3d, 0x77, 0x3c, 0x10, + 0xf9, 0x7d, 0x21, 0x69, 0x5f, 0xbe, 0x1e, 0x30, 0x3e, 0x74, 0x23, 0x1e, 0xca, 0x10, 0xcd, 0xe5, + 0x63, 0x2b, 0x4b, 0xdd, 0xb0, 0x1b, 0xaa, 0x44, 0x23, 0xfd, 0xd2, 0x98, 0x95, 0xbb, 0x5d, 0x5f, + 0xee, 0x0e, 0xda, 0x6e, 0x27, 0x0c, 0x1a, 0x9d, 0x90, 0x4b, 0x76, 0x10, 0xf1, 0xf0, 0x25, 0xeb, + 0x48, 0x73, 0x6a, 0x44, 0xaf, 0xba, 0x59, 0xa2, 0x6d, 0x3e, 0x0c, 0xf5, 0xfe, 0x65, 0xa8, 0x69, + 0x6d, 0x9f, 0xf1, 0x86, 0xe4, 0x7e, 0x14, 0x31, 0xbe, 0x4f, 0x39, 0x6b, 0xe4, 0x7a, 0xac, 0x7d, + 0x9d, 0x82, 0x78, 0x83, 0x87, 0x01, 0x93, 0xbb, 0x6c, 0x20, 0x1e, 0xe9, 0x86, 0x37, 0x53, 0x00, + 0x61, 0x22, 0x0a, 0xfb, 0x82, 0xa1, 0x1a, 0xb4, 0xb7, 0x24, 0x95, 0x03, 0x51, 0x01, 0x55, 0x50, + 0x2f, 0x79, 0x30, 0x89, 0x1d, 0x5b, 0xa8, 0x08, 0x31, 0x19, 0xb4, 0x0d, 0xa7, 0x1f, 0x50, 0x49, + 0x2b, 0x53, 0x55, 0x50, 0x2f, 0x37, 0xeb, 0xee, 0x84, 0x1b, 0x17, 0xeb, 0xa7, 0x78, 0x6f, 0xf9, + 0x30, 0x76, 0xac, 0x24, 0x76, 0x16, 0x76, 0xa8, 0xa4, 0xd7, 0xc3, 0xc0, 0x97, 0x2c, 0x88, 0xe4, + 0x90, 0x28, 0x35, 0x74, 0x0b, 0x96, 0xd6, 0x38, 0x0f, 0xf9, 0xf6, 0x30, 0x62, 0x95, 0x82, 0x2a, + 0xfe, 0x6f, 0x12, 0x3b, 0x8b, 0x2c, 0x0b, 0xe6, 0x18, 0x63, 0x24, 0xba, 0x0a, 0x8b, 0xea, 0x50, + 0x99, 0x56, 0x94, 0xc5, 0x24, 0x76, 0xfe, 0x52, 0x94, 0x1c, 0x5c, 0x23, 0xd0, 0x43, 0x38, 0xd3, + 0x62, 0x74, 0x87, 0x71, 0x51, 0x29, 0x56, 0x0b, 0xf5, 0x72, 0xf3, 0x8a, 0x9b, 0x73, 0x2a, 0xd7, + 0x79, 0xe6, 0x86, 0x46, 0x7b, 0xc5, 0x24, 0x76, 0xc0, 0x0d, 0x92, 0x91, 0x51, 0x13, 0xce, 0x3e, + 0xa7, 0xbc, 0xef, 0xf7, 0xbb, 0xa2, 0x62, 0x57, 0x0b, 0xf5, 0x92, 0xb7, 0x9c, 0xc4, 0x0e, 0xda, + 0x37, 0xb1, 0x5c, 0xe1, 0x73, 0x5c, 0xed, 0x0b, 0x80, 0x2b, 0x3f, 0xb7, 0x06, 0xb9, 0x10, 0x12, + 0x26, 0x06, 0x3d, 0xa9, 0xa6, 0xd7, 0xd6, 0x2f, 0x24, 0xb1, 0x03, 0xf9, 0x79, 0x94, 0xe4, 0x10, + 0x88, 0x40, 0x5b, 0x9f, 0xcc, 0x12, 0xae, 0x5d, 0x66, 0x09, 0x9a, 0xe1, 0x2d, 0x98, 0x35, 0xd8, + 0x5a, 0x9b, 0x18, 0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, 0xb8, 0x47, 0x7b, 0x03, 0x46, 0x0c, 0x1c, 0x6d, + 0xc2, 0xd2, 0xae, 0x2f, 0x64, 0xd8, 0xe5, 0x34, 0x30, 0xa6, 0x55, 0x27, 0xb6, 0xae, 0xe9, 0xad, + 0x0c, 0xa3, 0x46, 0xf8, 0xc7, 0x68, 0x8d, 0xa9, 0x64, 0xfc, 0x59, 0x5b, 0x87, 0xb6, 0x36, 0x1a, + 0xad, 0xc1, 0x79, 0x5d, 0x66, 0x4b, 0x72, 0x46, 0x83, 0xcc, 0x96, 0xff, 0x2e, 0x28, 0xa0, 0x11, + 0xde, 0x74, 0x6a, 0x0e, 0x99, 0x64, 0x79, 0xde, 0xd1, 0x09, 0xb6, 0x8e, 0x4f, 0xb0, 0x75, 0x76, + 0x82, 0xc1, 0x9b, 0x11, 0x06, 0x1f, 0x46, 0x18, 0x1c, 0x8e, 0x30, 0x38, 0x1a, 0x61, 0xf0, 0x69, + 0x84, 0xc1, 0xe7, 0x11, 0xb6, 0xce, 0x46, 0x18, 0xbc, 0x3b, 0xc5, 0xd6, 0xd1, 0x29, 0xb6, 0x8e, + 0x4f, 0xb1, 0xf5, 0x62, 0xe2, 0x35, 0x6e, 0xdb, 0xea, 0xf9, 0xbb, 0xf9, 0x2d, 0x00, 0x00, 0xff, + 0xff, 0x3f, 0x4a, 0x4a, 0x48, 0xb8, 0x05, 0x00, 0x00, } func (this *PrometheusInstantQueryResponse) Equal(that interface{}) bool { @@ -694,7 +704,10 @@ func (this *Sample) Equal(that interface{}) bool { return false } } - if !this.Sample.Equal(&that1.Sample) { + if !this.Sample.Equal(that1.Sample) { + return false + } + if !this.Histogram.Equal(that1.Histogram) { return false } return true @@ -811,10 +824,15 @@ func (this *Sample) GoString() string { if this == nil { return "nil" } - s := make([]string, 0, 6) + s := make([]string, 0, 7) s = append(s, "&instantquery.Sample{") s = append(s, "Labels: "+fmt.Sprintf("%#v", this.Labels)+",\n") - s = append(s, "Sample: "+strings.Replace(this.Sample.GoString(), `&`, ``, 1)+",\n") + if this.Sample != nil { + s = append(s, "Sample: "+fmt.Sprintf("%#v", this.Sample)+",\n") + } + if this.Histogram != nil { + s = append(s, "Histogram: "+fmt.Sprintf("%#v", this.Histogram)+",\n") + } s = append(s, "}") return strings.Join(s, "") } @@ -1115,16 +1133,30 @@ func (m *Sample) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - { - size, err := m.Sample.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err + if m.Histogram != nil { + { + size, err := m.Histogram.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintInstantquery(dAtA, i, uint64(size)) } - i -= size - i = encodeVarintInstantquery(dAtA, i, uint64(size)) + i-- + dAtA[i] = 0x1a + } + if m.Sample != nil { + { + size, err := m.Sample.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintInstantquery(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 } - i-- - dAtA[i] = 0x12 if len(m.Labels) > 0 { for iNdEx := len(m.Labels) - 1; iNdEx >= 0; iNdEx-- { { @@ -1319,8 +1351,14 @@ func (m *Sample) Size() (n int) { n += 1 + l + sovInstantquery(uint64(l)) } } - l = m.Sample.Size() - n += 1 + l + sovInstantquery(uint64(l)) + if m.Sample != nil { + l = m.Sample.Size() + n += 1 + l + sovInstantquery(uint64(l)) + } + if m.Histogram != nil { + l = m.Histogram.Size() + n += 1 + l + sovInstantquery(uint64(l)) + } return n } @@ -1438,7 +1476,8 @@ func (this *Sample) String() string { } s := strings.Join([]string{`&Sample{`, `Labels:` + fmt.Sprintf("%v", this.Labels) + `,`, - `Sample:` + strings.Replace(strings.Replace(fmt.Sprintf("%v", this.Sample), "Sample", "cortexpb.Sample", 1), `&`, ``, 1) + `,`, + `Sample:` + strings.Replace(fmt.Sprintf("%v", this.Sample), "Sample", "cortexpb.Sample", 1) + `,`, + `Histogram:` + strings.Replace(fmt.Sprintf("%v", this.Histogram), "SampleHistogramPair", "tripperware.SampleHistogramPair", 1) + `,`, `}`, }, "") return s @@ -2203,10 +2242,49 @@ func (m *Sample) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } + if m.Sample == nil { + m.Sample = &cortexpb.Sample{} + } if err := m.Sample.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Histogram", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowInstantquery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthInstantquery + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthInstantquery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Histogram == nil { + m.Histogram = &tripperware.SampleHistogramPair{} + } + if err := m.Histogram.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipInstantquery(dAtA[iNdEx:]) diff --git a/pkg/querier/tripperware/instantquery/instantquery.proto b/pkg/querier/tripperware/instantquery/instantquery.proto index 8240e2b78db..d67b1ad0619 100644 --- a/pkg/querier/tripperware/instantquery/instantquery.proto +++ b/pkg/querier/tripperware/instantquery/instantquery.proto @@ -41,7 +41,8 @@ message Vector { message Sample { repeated cortexpb.LabelPair labels = 1 [(gogoproto.nullable) = false, (gogoproto.jsontag) = "metric", (gogoproto.customtype) = "github.com/cortexproject/cortex/pkg/cortexpb.LabelAdapter"]; - cortexpb.Sample sample = 2 [(gogoproto.nullable) = false, (gogoproto.jsontag) = "value"]; + cortexpb.Sample sample = 2 [(gogoproto.nullable) = true, (gogoproto.jsontag) = "value"]; + tripperware.SampleHistogramPair histogram = 3 [(gogoproto.nullable) = true, (gogoproto.jsontag) = "histogram"]; } message Matrix { diff --git a/pkg/querier/tripperware/query.go b/pkg/querier/tripperware/query.go index af3e8029ada..0e8237c74eb 100644 --- a/pkg/querier/tripperware/query.go +++ b/pkg/querier/tripperware/query.go @@ -107,7 +107,7 @@ func decodeSampleStream(ptr unsafe.Pointer, iter *jsoniter.Iterator) { case "histograms": for iter.ReadArray() { h := SampleHistogramPair{} - unmarshalSampleHistogramPairJSON(unsafe.Pointer(&h), iter) + UnmarshalSampleHistogramPairJSON(unsafe.Pointer(&h), iter) ss.Histograms = append(ss.Histograms, h) } default: @@ -150,7 +150,7 @@ func encodeSampleStream(ptr unsafe.Pointer, stream *jsoniter.Stream) { if i > 0 { stream.WriteMore() } - marshalSampleHistogramPairJSON(unsafe.Pointer(&h), stream) + MarshalSampleHistogramPairJSON(unsafe.Pointer(&h), stream) } stream.WriteArrayEnd() } @@ -196,8 +196,8 @@ func init() { jsoniter.RegisterTypeDecoderFunc("tripperware.PrometheusResponseQueryableSamplesStatsPerStep", PrometheusResponseQueryableSamplesStatsPerStepJsoniterDecode) jsoniter.RegisterTypeEncoderFunc("tripperware.SampleStream", encodeSampleStream, marshalJSONIsEmpty) jsoniter.RegisterTypeDecoderFunc("tripperware.SampleStream", decodeSampleStream) - jsoniter.RegisterTypeEncoderFunc("tripperware.SampleHistogramPair", marshalSampleHistogramPairJSON, marshalJSONIsEmpty) - jsoniter.RegisterTypeDecoderFunc("tripperware.SampleHistogramPair", unmarshalSampleHistogramPairJSON) + jsoniter.RegisterTypeEncoderFunc("tripperware.SampleHistogramPair", MarshalSampleHistogramPairJSON, marshalJSONIsEmpty) + jsoniter.RegisterTypeDecoderFunc("tripperware.SampleHistogramPair", UnmarshalSampleHistogramPairJSON) } func marshalJSONIsEmpty(ptr unsafe.Pointer) bool { @@ -284,7 +284,7 @@ func StatsMerge(stats map[int64]*PrometheusResponseQueryableSamplesStatsPerStep) } // Adapted from https://github.com/prometheus/client_golang/blob/4b158abea9470f75b6f07460cdc2189b91914562/api/prometheus/v1/api.go#L84. -func unmarshalSampleHistogramPairJSON(ptr unsafe.Pointer, iter *jsoniter.Iterator) { +func UnmarshalSampleHistogramPairJSON(ptr unsafe.Pointer, iter *jsoniter.Iterator) { p := (*SampleHistogramPair)(ptr) if !iter.ReadArray() { iter.ReportError("unmarshal SampleHistogramPair", "SampleHistogramPair must be [timestamp, {histogram}]") @@ -374,7 +374,7 @@ func unmarshalHistogramBucket(iter *jsoniter.Iterator) (*HistogramBucket, error) } // Adapted from https://github.com/prometheus/client_golang/blob/4b158abea9470f75b6f07460cdc2189b91914562/api/prometheus/v1/api.go#L137. -func marshalSampleHistogramPairJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) { +func MarshalSampleHistogramPairJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) { p := *((*SampleHistogramPair)(ptr)) stream.WriteArrayStart() stream.WriteFloat64(float64(p.TimestampMs) / float64(time.Second/time.Millisecond))