Skip to content

Commit

Permalink
Integrate cortexpbv2 to cortexpb
Browse files Browse the repository at this point in the history
Signed-off-by: SungJin1212 <[email protected]>
  • Loading branch information
SungJin1212 committed Nov 11, 2024
1 parent 2b5ac96 commit e739b3d
Show file tree
Hide file tree
Showing 29 changed files with 3,014 additions and 5,702 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ $(foreach exe, $(EXES), $(eval $(call dep_exe, $(exe))))

# Manually declared dependencies And what goes into each exe
pkg/cortexpb/cortex.pb.go: pkg/cortexpb/cortex.proto
pkg/cortexpbv2/cortexv2.pb.go: pkg/cortexpbv2/cortexv2.proto
pkg/ingester/client/ingester.pb.go: pkg/ingester/client/ingester.proto
pkg/distributor/distributorpb/distributor.pb.go: pkg/distributor/distributorpb/distributor.proto
pkg/ingester/wal.pb.go: pkg/ingester/wal.proto
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/cortexproject/cortex/pkg/alertmanager/alertmanagerpb"
"github.com/cortexproject/cortex/pkg/compactor"
"github.com/cortexproject/cortex/pkg/cortexpb"
"github.com/cortexproject/cortex/pkg/cortexpbv2"
"github.com/cortexproject/cortex/pkg/distributor"
"github.com/cortexproject/cortex/pkg/distributor/distributorpb"
frontendv1 "github.com/cortexproject/cortex/pkg/frontend/v1"
Expand Down Expand Up @@ -318,7 +317,7 @@ type Ingester interface {
AllUserStatsHandler(http.ResponseWriter, *http.Request)
ModeHandler(http.ResponseWriter, *http.Request)
Push(context.Context, *cortexpb.WriteRequest) (*cortexpb.WriteResponse, error)
PushV2(context.Context, *cortexpbv2.WriteRequest) (*cortexpbv2.WriteResponse, error)
PushV2(context.Context, *cortexpb.WriteRequestV2) (*cortexpb.WriteResponseV2, error)
}

// RegisterIngester registers the ingesters HTTP and GRPC service
Expand Down
34 changes: 34 additions & 0 deletions pkg/cortexpb/codecv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cortexpb

import (
"github.com/prometheus/prometheus/model/labels"
)

// ToLabels return model labels.Labels from timeseries' remote labels.
func (t TimeSeriesV2) ToLabels(b *labels.ScratchBuilder, symbols []string) labels.Labels {
return desymbolizeLabels(b, t.GetLabelsRefs(), symbols)
}

// ToLabels return model labels.Labels from exemplar remote labels.
func (e ExemplarV2) ToLabels(b *labels.ScratchBuilder, symbols []string) labels.Labels {
return desymbolizeLabels(b, e.GetLabelsRefs(), symbols)
}

func (m MetadataV2) ToV1Metadata(name string, symbols []string) *MetricMetadata {
return &MetricMetadata{
Type: m.Type,
MetricFamilyName: name,
Unit: symbols[m.UnitRef],
Help: symbols[m.HelpRef],
}
}

// desymbolizeLabels decodes label references, with given symbols to labels.
func desymbolizeLabels(b *labels.ScratchBuilder, labelRefs []uint32, symbols []string) labels.Labels {
b.Reset()
for i := 0; i < len(labelRefs); i += 2 {
b.Add(symbols[labelRefs[i]], symbols[labelRefs[i+1]])
}
b.Sort()
return b.Labels()
}
4 changes: 2 additions & 2 deletions pkg/cortexpb/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

// ToWriteRequest converts matched slices of Labels, Samples, Metadata and Histograms into a WriteRequest proto.
// It gets timeseries from the pool, so ReuseSlice() should be called when done.
func ToWriteRequest(lbls []labels.Labels, samples []Sample, metadata []*MetricMetadata, histograms []Histogram, source WriteRequest_SourceEnum) *WriteRequest {
func ToWriteRequest(lbls []labels.Labels, samples []Sample, metadata []*MetricMetadata, histograms []Histogram, source SourceEnum) *WriteRequest {
req := &WriteRequest{
Timeseries: PreallocTimeseriesSliceFromPool(),
Metadata: metadata,
Expand Down Expand Up @@ -170,7 +170,7 @@ func (s byLabel) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

// MetricMetadataMetricTypeToMetricType converts a metric type from our internal client
// to a Prometheus one.
func MetricMetadataMetricTypeToMetricType(mt MetricMetadata_MetricType) model.MetricType {
func MetricMetadataMetricTypeToMetricType(mt MetricType) model.MetricType {
switch mt {
case UNKNOWN:
return model.MetricTypeUnknown
Expand Down
4 changes: 2 additions & 2 deletions pkg/cortexpb/compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func testUnmarshalling(t *testing.T, unmarshalFn func(data []byte, v interface{}
func TestMetricMetadataToMetricTypeToMetricType(t *testing.T) {
tc := []struct {
desc string
input MetricMetadata_MetricType
input MetricType
expected model.MetricType
}{
{
Expand All @@ -89,7 +89,7 @@ func TestMetricMetadataToMetricTypeToMetricType(t *testing.T) {
},
{
desc: "with an unknown metric",
input: MetricMetadata_MetricType(100),
input: MetricType(100),
expected: model.MetricTypeUnknown,
},
}
Expand Down
12 changes: 5 additions & 7 deletions pkg/cortexpbv2/compatv2.go → pkg/cortexpb/compatv2.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package cortexpbv2
package cortexpb

import (
"github.com/prometheus/prometheus/model/labels"
writev2 "github.com/prometheus/prometheus/prompb/io/prometheus/write/v2"

"github.com/cortexproject/cortex/pkg/cortexpb"
)

// ToWriteRequestV2 converts matched slices of Labels, Samples, and Histograms into a WriteRequest proto.
func ToWriteRequestV2(lbls []labels.Labels, samples []Sample, histograms []Histogram, metadata []Metadata, source WriteRequest_SourceEnum, help ...string) *WriteRequest {
func ToWriteRequestV2(lbls []labels.Labels, samples []Sample, histograms []Histogram, metadata []MetadataV2, source SourceEnum, help ...string) *WriteRequestV2 {
st := writev2.NewSymbolTable()
labelRefs := make([][]uint32, 0, len(lbls))
for _, lbl := range lbls {
Expand All @@ -21,7 +19,7 @@ func ToWriteRequestV2(lbls []labels.Labels, samples []Sample, histograms []Histo

symbols := st.Symbols()

req := &WriteRequest{
req := &WriteRequestV2{
Timeseries: PreallocTimeseriesV2SliceFromPool(),
Symbols: symbols,
Source: source,
Expand All @@ -41,13 +39,13 @@ func ToWriteRequestV2(lbls []labels.Labels, samples []Sample, histograms []Histo
ts.Metadata = metadata[i]
}
i++
req.Timeseries = append(req.Timeseries, PreallocTimeseriesV2{TimeSeries: ts})
req.Timeseries = append(req.Timeseries, PreallocTimeseriesV2{TimeSeriesV2: ts})
}

return req
}

func GetLabelRefsFromLabelAdapters(symbols []string, las []cortexpb.LabelAdapter) []uint32 {
func GetLabelRefsFromLabelAdapters(symbols []string, las []LabelAdapter) []uint32 {
var ret []uint32

symbolMap := map[string]uint32{}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
package cortexpbv2
package cortexpb

import (
"testing"

"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/require"

"github.com/cortexproject/cortex/pkg/cortexpb"
)

func Test_GetLabelRefsFromLabelAdapters(t *testing.T) {
tests := []struct {
symbols []string
lbs []cortexpb.LabelAdapter
lbs []LabelAdapter
expectedSeriesRefs []uint32
}{
{
symbols: []string{"", "__name__", "test_metric", "foo", "bar", "baz", "qux"},
lbs: []cortexpb.LabelAdapter{{Name: "__name__", Value: "test_metric"}, {Name: "foo", Value: "bar"}},
lbs: []LabelAdapter{{Name: "__name__", Value: "test_metric"}, {Name: "foo", Value: "bar"}},
expectedSeriesRefs: []uint32{1, 2, 3, 4},
},
{
symbols: []string{"", "__name__", "test_metric", "foo", "bar", "baz", "qux"},
lbs: []cortexpb.LabelAdapter{{Name: "__name__", Value: "test_metric"}, {Name: "baz", Value: "qux"}},
lbs: []LabelAdapter{{Name: "__name__", Value: "test_metric"}, {Name: "baz", Value: "qux"}},
expectedSeriesRefs: []uint32{1, 2, 5, 6},
},
{
symbols: []string{"", "__name__", "test_metric", "foo", "bar", "baz", "qux", "1"},
lbs: []cortexpb.LabelAdapter{{Name: "__name__", Value: "test_metric"}, {Name: "baz", Value: "qux"}, {Name: "qux", Value: "1"}},
lbs: []LabelAdapter{{Name: "__name__", Value: "test_metric"}, {Name: "baz", Value: "qux"}, {Name: "qux", Value: "1"}},
expectedSeriesRefs: []uint32{1, 2, 5, 6, 6, 7},
},
}
Expand Down
Loading

0 comments on commit e739b3d

Please sign in to comment.