From 9afbf03de8dbb7a0d2a84bf69e7cc15fb39544fb Mon Sep 17 00:00:00 2001 From: Salva Corts Date: Fri, 14 Jul 2023 09:34:14 +0200 Subject: [PATCH 1/2] Remove Prometheus dependency for push pkg --- go.mod | 4 +- go.sum | 8 +-- pkg/loghttp/query.go | 5 +- pkg/loghttp/query_test.go | 4 +- pkg/push/go.mod | 8 +-- pkg/push/go.sum | 14 ++--- pkg/push/types.go | 52 ++++++++++++++++--- pkg/util/marshal/legacy/marshal_test.go | 5 +- pkg/util/marshal/marshal_test.go | 4 +- pkg/util/unmarshal/legacy/unmarshal_test.go | 3 +- pkg/util/unmarshal/unmarshal_test.go | 3 +- .../github.com/grafana/loki/pkg/push/types.go | 52 ++++++++++++++++--- vendor/golang.org/x/exp/slices/slices.go | 52 ++++++++++++++----- vendor/golang.org/x/exp/slices/sort.go | 10 ++-- vendor/modules.txt | 6 +-- 15 files changed, 158 insertions(+), 72 deletions(-) diff --git a/go.mod b/go.mod index b956910418bfc..37395d8f04645 100644 --- a/go.mod +++ b/go.mod @@ -126,7 +126,7 @@ require ( github.com/thanos-io/objstore v0.0.0-20230201072718-11ffbc490204 github.com/willf/bloom v2.0.3+incompatible go4.org/netipx v0.0.0-20230125063823-8449b0a6169f - golang.org/x/exp v0.0.0-20230321023759-10a507213a29 + golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 golang.org/x/oauth2 v0.7.0 golang.org/x/text v0.11.0 ) @@ -300,7 +300,7 @@ require ( go.opentelemetry.io/otel/trace v1.14.0 // indirect go.uber.org/multierr v1.8.0 // indirect go.uber.org/zap v1.21.0 // indirect - golang.org/x/mod v0.9.0 // indirect + golang.org/x/mod v0.11.0 // indirect golang.org/x/term v0.10.0 // indirect golang.org/x/tools v0.7.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect diff --git a/go.sum b/go.sum index 1cc3d5e95435a..99fcd1b767283 100644 --- a/go.sum +++ b/go.sum @@ -1850,8 +1850,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= -golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug= -golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw= +golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= @@ -1881,8 +1881,8 @@ golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs= -golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU= +golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/pkg/loghttp/query.go b/pkg/loghttp/query.go index bcd6b02249ea6..a4f6424c43cc6 100644 --- a/pkg/loghttp/query.go +++ b/pkg/loghttp/query.go @@ -11,7 +11,6 @@ import ( "github.com/buger/jsonparser" json "github.com/json-iterator/go" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/model/labels" "github.com/grafana/loki/pkg/logproto" "github.com/grafana/loki/pkg/logqlmodel/stats" @@ -157,12 +156,12 @@ func unmarshalHTTPToLogProtoEntry(data []byte) (logproto.Entry, error) { } e.Line = v case 2: // nonIndexedLabels - var nonIndexedLabels labels.Labels + var nonIndexedLabels []logproto.LabelAdapter err := jsonparser.ObjectEach(value, func(key, val []byte, dataType jsonparser.ValueType, _ int) error { if dataType != jsonparser.String { return jsonparser.MalformedStringError } - nonIndexedLabels = append(nonIndexedLabels, labels.Label{ + nonIndexedLabels = append(nonIndexedLabels, logproto.LabelAdapter{ Name: string(key), Value: string(val), }) diff --git a/pkg/loghttp/query_test.go b/pkg/loghttp/query_test.go index 5e6bd5b3ea8dc..4eadac0e3cbd6 100644 --- a/pkg/loghttp/query_test.go +++ b/pkg/loghttp/query_test.go @@ -176,7 +176,7 @@ func TestStreams_ToProto(t *testing.T) { Labels: `{foo="bar"}`, Entries: []logproto.Entry{ {Timestamp: time.Unix(0, 1), Line: "1"}, - {Timestamp: time.Unix(0, 2), Line: "2", NonIndexedLabels: labels.Labels{ + {Timestamp: time.Unix(0, 2), Line: "2", NonIndexedLabels: []logproto.LabelAdapter{ {Name: "foo", Value: "a"}, {Name: "bar", Value: "b"}, }}, @@ -186,7 +186,7 @@ func TestStreams_ToProto(t *testing.T) { Labels: `{foo="bar", lvl="error"}`, Entries: []logproto.Entry{ {Timestamp: time.Unix(0, 3), Line: "3"}, - {Timestamp: time.Unix(0, 4), Line: "4", NonIndexedLabels: labels.Labels{ + {Timestamp: time.Unix(0, 4), Line: "4", NonIndexedLabels: []logproto.LabelAdapter{ {Name: "foo", Value: "a"}, {Name: "bar", Value: "b"}, }}, diff --git a/pkg/push/go.mod b/pkg/push/go.mod index cf928c3a58cac..9f6c824a016e1 100644 --- a/pkg/push/go.mod +++ b/pkg/push/go.mod @@ -4,25 +4,21 @@ go 1.19 require ( github.com/gogo/protobuf v1.3.2 - github.com/prometheus/prometheus v0.43.1-0.20230419161410-69155c6ba1e9 github.com/stretchr/testify v1.8.2 + golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 google.golang.org/grpc v1.53.0 ) require ( - github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang/protobuf v1.5.3 // indirect - github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd // indirect - github.com/kr/pretty v0.2.1 // indirect github.com/kr/text v0.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/common v0.42.0 // indirect - golang.org/x/exp v0.0.0-20230307190834-24139beb5833 // indirect golang.org/x/net v0.8.0 // indirect golang.org/x/sys v0.6.0 // indirect golang.org/x/text v0.8.0 // indirect google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect google.golang.org/protobuf v1.29.1 // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/pkg/push/go.sum b/pkg/push/go.sum index 06bf170f66191..47baafb905de9 100644 --- a/pkg/push/go.sum +++ b/pkg/push/go.sum @@ -1,5 +1,3 @@ -github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= -github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -11,8 +9,6 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd h1:PpuIBO5P3e9hpqBD0O/HjhShYuM6XE0i/lbE6J94kww= -github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd/go.mod h1:M5qHK+eWfAv8VR/265dIuEpL3fNfeC21tXXp9itM24A= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= @@ -23,10 +19,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI1YM= -github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc= -github.com/prometheus/prometheus v0.43.1-0.20230419161410-69155c6ba1e9 h1:GrpznPCSJgx8mGGj5qfKoHiou/dVx7uMce9/9rSdiuY= -github.com/prometheus/prometheus v0.43.1-0.20230419161410-69155c6ba1e9/go.mod h1:L8xLODXgpZM57D1MA7SPgsDecKj6ez4AF7mMczR1bis= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -39,8 +31,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/exp v0.0.0-20230307190834-24139beb5833 h1:SChBja7BCQewoTAU7IgvucQKMIXrEpFxNMs0spT3/5s= -golang.org/x/exp v0.0.0-20230307190834-24139beb5833/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw= +golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -79,7 +71,7 @@ google.golang.org/protobuf v1.29.1 h1:7QBf+IK2gx70Ap/hDsOmam3GE0v9HicjfEdAxE62Uo google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= -gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/push/types.go b/pkg/push/types.go index 80adf2563c582..9b41e35ed40a9 100644 --- a/pkg/push/types.go +++ b/pkg/push/types.go @@ -1,13 +1,14 @@ package push import ( + "encoding/json" "fmt" "io" "strings" "time" "unsafe" - "github.com/prometheus/prometheus/model/labels" + "golang.org/x/exp/slices" ) // Stream contains a unique labels set as a string and a set of entries for it. @@ -23,10 +24,47 @@ type Stream struct { type Entry struct { Timestamp time.Time `protobuf:"bytes,1,opt,name=timestamp,proto3,stdtime" json:"ts"` Line string `protobuf:"bytes,2,opt,name=line,proto3" json:"line"` - NonIndexedLabels labels.Labels `protobuf:"bytes,3,opt,name=nonIndexedLabels,proto3" json:"nonIndexedLabels,omitempty"` + NonIndexedLabels LabelsAdapter `protobuf:"bytes,3,opt,name=nonIndexedLabels,proto3" json:"nonIndexedLabels,omitempty"` } -type LabelAdapter labels.Label +// LabelAdapter should be a copy of the Prometheus labels.Label type. +// We cannot import Prometheus in this package because it would create many dependencies +// in other projects importing this package. Instead, we copy the definition here, which should +// be kept in sync with the original so it can be casted to the prometheus type. +type LabelAdapter struct { + Name, Value string +} + +// LabelsAdapter is equivalent to the Prometheus labels.Labels type. +type LabelsAdapter []LabelAdapter + +// MarshalJSON implements json.Marshaler. +// Should be kept in sync with Prometheus's labels.Labels implementation. +func (ls LabelsAdapter) MarshalJSON() ([]byte, error) { + labelsMap := make(map[string]string, len(ls)) + for _, l := range ls { + labelsMap[l.Name] = l.Value + } + return json.Marshal(labelsMap) +} + +// UnmarshalJSON implements json.Unmarshaler. +// Should be kept in sync with Prometheus's labels.Labels implementation. +func (ls *LabelsAdapter) UnmarshalJSON(b []byte) error { + var m map[string]string + + if err := json.Unmarshal(b, &m); err != nil { + return err + } + + *ls = make([]LabelAdapter, 0, len(m)) + for k, v := range m { + *ls = append(*ls, LabelAdapter{Name: k, Value: v}) + } + slices.SortFunc(*ls, func(a, b LabelAdapter) bool { return a.Name < b.Name }) + + return nil +} func (m *LabelAdapter) Marshal() (dAtA []byte, err error) { size := m.Size() @@ -428,8 +466,8 @@ func (m *Entry) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.NonIndexedLabels = append(m.NonIndexedLabels, labels.Label{}) - if err := (*LabelAdapter)(&m.NonIndexedLabels[len(m.NonIndexedLabels)-1]).Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + m.NonIndexedLabels = append(m.NonIndexedLabels, LabelAdapter{}) + if err := m.NonIndexedLabels[len(m.NonIndexedLabels)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex @@ -619,7 +657,7 @@ func (m *Entry) Size() (n int) { } if len(m.NonIndexedLabels) > 0 { for _, e := range m.NonIndexedLabels { - l = (*LabelAdapter)(&e).Size() + l = e.Size() n += 1 + l + sovPush(uint64(l)) } } @@ -702,7 +740,7 @@ func (m *Entry) Equal(that interface{}) bool { return false } for i := range m.NonIndexedLabels { - if !(*LabelAdapter)(&m.NonIndexedLabels[i]).Equal(LabelAdapter(that1.NonIndexedLabels[i])) { + if !m.NonIndexedLabels[i].Equal(that1.NonIndexedLabels[i]) { return false } } diff --git a/pkg/util/marshal/legacy/marshal_test.go b/pkg/util/marshal/legacy/marshal_test.go index b3a141a09e6df..8c84a5085cb63 100644 --- a/pkg/util/marshal/legacy/marshal_test.go +++ b/pkg/util/marshal/legacy/marshal_test.go @@ -7,7 +7,6 @@ import ( "time" json "github.com/json-iterator/go" - "github.com/prometheus/prometheus/model/labels" "github.com/stretchr/testify/require" loghttp "github.com/grafana/loki/pkg/loghttp/legacy" @@ -31,7 +30,7 @@ var queryTests = []struct { { Timestamp: mustParse(time.RFC3339Nano, "2019-09-13T18:32:23.380001319Z"), Line: "super line with labels", - NonIndexedLabels: labels.Labels{ + NonIndexedLabels: []logproto.LabelAdapter{ {Name: "foo", Value: "a"}, {Name: "bar", Value: "b"}, }, @@ -184,7 +183,7 @@ var tailTests = []struct { { Timestamp: mustParse(time.RFC3339Nano, "2019-09-13T18:32:23.380001319Z"), Line: "super line with labels", - NonIndexedLabels: labels.Labels{ + NonIndexedLabels: []logproto.LabelAdapter{ {Name: "foo", Value: "a"}, {Name: "bar", Value: "b"}, }, diff --git a/pkg/util/marshal/marshal_test.go b/pkg/util/marshal/marshal_test.go index d4957f6ac94e3..6a4d7d701e0d7 100644 --- a/pkg/util/marshal/marshal_test.go +++ b/pkg/util/marshal/marshal_test.go @@ -37,7 +37,7 @@ var queryTests = []struct { { Timestamp: time.Unix(0, 123456789012346), Line: "super line with labels", - NonIndexedLabels: labels.Labels{ + NonIndexedLabels: []logproto.LabelAdapter{ {Name: "foo", Value: "a"}, {Name: "bar", Value: "b"}, }, @@ -500,7 +500,7 @@ var tailTests = []struct { { Timestamp: time.Unix(0, 123456789012346), Line: "super line with labels", - NonIndexedLabels: labels.Labels{ + NonIndexedLabels: []logproto.LabelAdapter{ {Name: "foo", Value: "a"}, {Name: "bar", Value: "b"}, }, diff --git a/pkg/util/unmarshal/legacy/unmarshal_test.go b/pkg/util/unmarshal/legacy/unmarshal_test.go index 92365bb91125b..bd95ebe60754d 100644 --- a/pkg/util/unmarshal/legacy/unmarshal_test.go +++ b/pkg/util/unmarshal/legacy/unmarshal_test.go @@ -7,7 +7,6 @@ import ( "testing" "time" - "github.com/prometheus/prometheus/model/labels" "github.com/stretchr/testify/require" "github.com/grafana/loki/pkg/logproto" @@ -29,7 +28,7 @@ var pushTests = []struct { { Timestamp: mustParse(time.RFC3339Nano, "2019-09-13T18:32:23.380001319Z"), Line: "super line with labels", - NonIndexedLabels: labels.Labels{ + NonIndexedLabels: []logproto.LabelAdapter{ {Name: "a", Value: "1"}, {Name: "b", Value: "2"}, }, diff --git a/pkg/util/unmarshal/unmarshal_test.go b/pkg/util/unmarshal/unmarshal_test.go index 4b1b67ead71b9..61f3c29a138f0 100644 --- a/pkg/util/unmarshal/unmarshal_test.go +++ b/pkg/util/unmarshal/unmarshal_test.go @@ -7,7 +7,6 @@ import ( "testing" "time" - "github.com/prometheus/prometheus/model/labels" "github.com/stretchr/testify/require" "github.com/grafana/loki/pkg/loghttp" @@ -53,7 +52,7 @@ var pushTests = []struct { { Timestamp: time.Unix(0, 123456789012345), Line: "super line", - NonIndexedLabels: labels.Labels{ + NonIndexedLabels: []logproto.LabelAdapter{ {Name: "a", Value: "1"}, {Name: "b", Value: "2"}, }, diff --git a/vendor/github.com/grafana/loki/pkg/push/types.go b/vendor/github.com/grafana/loki/pkg/push/types.go index 80adf2563c582..9b41e35ed40a9 100644 --- a/vendor/github.com/grafana/loki/pkg/push/types.go +++ b/vendor/github.com/grafana/loki/pkg/push/types.go @@ -1,13 +1,14 @@ package push import ( + "encoding/json" "fmt" "io" "strings" "time" "unsafe" - "github.com/prometheus/prometheus/model/labels" + "golang.org/x/exp/slices" ) // Stream contains a unique labels set as a string and a set of entries for it. @@ -23,10 +24,47 @@ type Stream struct { type Entry struct { Timestamp time.Time `protobuf:"bytes,1,opt,name=timestamp,proto3,stdtime" json:"ts"` Line string `protobuf:"bytes,2,opt,name=line,proto3" json:"line"` - NonIndexedLabels labels.Labels `protobuf:"bytes,3,opt,name=nonIndexedLabels,proto3" json:"nonIndexedLabels,omitempty"` + NonIndexedLabels LabelsAdapter `protobuf:"bytes,3,opt,name=nonIndexedLabels,proto3" json:"nonIndexedLabels,omitempty"` } -type LabelAdapter labels.Label +// LabelAdapter should be a copy of the Prometheus labels.Label type. +// We cannot import Prometheus in this package because it would create many dependencies +// in other projects importing this package. Instead, we copy the definition here, which should +// be kept in sync with the original so it can be casted to the prometheus type. +type LabelAdapter struct { + Name, Value string +} + +// LabelsAdapter is equivalent to the Prometheus labels.Labels type. +type LabelsAdapter []LabelAdapter + +// MarshalJSON implements json.Marshaler. +// Should be kept in sync with Prometheus's labels.Labels implementation. +func (ls LabelsAdapter) MarshalJSON() ([]byte, error) { + labelsMap := make(map[string]string, len(ls)) + for _, l := range ls { + labelsMap[l.Name] = l.Value + } + return json.Marshal(labelsMap) +} + +// UnmarshalJSON implements json.Unmarshaler. +// Should be kept in sync with Prometheus's labels.Labels implementation. +func (ls *LabelsAdapter) UnmarshalJSON(b []byte) error { + var m map[string]string + + if err := json.Unmarshal(b, &m); err != nil { + return err + } + + *ls = make([]LabelAdapter, 0, len(m)) + for k, v := range m { + *ls = append(*ls, LabelAdapter{Name: k, Value: v}) + } + slices.SortFunc(*ls, func(a, b LabelAdapter) bool { return a.Name < b.Name }) + + return nil +} func (m *LabelAdapter) Marshal() (dAtA []byte, err error) { size := m.Size() @@ -428,8 +466,8 @@ func (m *Entry) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.NonIndexedLabels = append(m.NonIndexedLabels, labels.Label{}) - if err := (*LabelAdapter)(&m.NonIndexedLabels[len(m.NonIndexedLabels)-1]).Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + m.NonIndexedLabels = append(m.NonIndexedLabels, LabelAdapter{}) + if err := m.NonIndexedLabels[len(m.NonIndexedLabels)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex @@ -619,7 +657,7 @@ func (m *Entry) Size() (n int) { } if len(m.NonIndexedLabels) > 0 { for _, e := range m.NonIndexedLabels { - l = (*LabelAdapter)(&e).Size() + l = e.Size() n += 1 + l + sovPush(uint64(l)) } } @@ -702,7 +740,7 @@ func (m *Entry) Equal(that interface{}) bool { return false } for i := range m.NonIndexedLabels { - if !(*LabelAdapter)(&m.NonIndexedLabels[i]).Equal(LabelAdapter(that1.NonIndexedLabels[i])) { + if !m.NonIndexedLabels[i].Equal(that1.NonIndexedLabels[i]) { return false } } diff --git a/vendor/golang.org/x/exp/slices/slices.go b/vendor/golang.org/x/exp/slices/slices.go index cff0cd49ecfb3..8a7cf20dbd272 100644 --- a/vendor/golang.org/x/exp/slices/slices.go +++ b/vendor/golang.org/x/exp/slices/slices.go @@ -104,8 +104,8 @@ func CompareFunc[E1, E2 any](s1 []E1, s2 []E2, cmp func(E1, E2) int) int { // Index returns the index of the first occurrence of v in s, // or -1 if not present. func Index[E comparable](s []E, v E) int { - for i, vs := range s { - if v == vs { + for i := range s { + if v == s[i] { return i } } @@ -115,8 +115,8 @@ func Index[E comparable](s []E, v E) int { // IndexFunc returns the first index i satisfying f(s[i]), // or -1 if none do. func IndexFunc[E any](s []E, f func(E) bool) int { - for i, v := range s { - if f(v) { + for i := range s { + if f(s[i]) { return i } } @@ -168,6 +168,30 @@ func Delete[S ~[]E, E any](s S, i, j int) S { return append(s[:i], s[j:]...) } +// DeleteFunc removes any elements from s for which del returns true, +// returning the modified slice. +// When DeleteFunc removes m elements, it might not modify the elements +// s[len(s)-m:len(s)]. If those elements contain pointers you might consider +// zeroing those elements so that objects they reference can be garbage +// collected. +func DeleteFunc[S ~[]E, E any](s S, del func(E) bool) S { + // Don't start copying elements until we find one to delete. + for i, v := range s { + if del(v) { + j := i + for i++; i < len(s); i++ { + v = s[i] + if !del(v) { + s[j] = v + j++ + } + } + return s[:j] + } + } + return s +} + // Replace replaces the elements s[i:j] by the given v, and returns the // modified slice. Replace panics if s[i:j] is not a valid slice of s. func Replace[S ~[]E, E any](s S, i, j int, v ...E) S { @@ -207,12 +231,12 @@ func Compact[S ~[]E, E comparable](s S) S { return s } i := 1 - last := s[0] - for _, v := range s[1:] { - if v != last { - s[i] = v + for k := 1; k < len(s); k++ { + if s[k] != s[k-1] { + if i != k { + s[i] = s[k] + } i++ - last = v } } return s[:i] @@ -224,12 +248,12 @@ func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S { return s } i := 1 - last := s[0] - for _, v := range s[1:] { - if !eq(v, last) { - s[i] = v + for k := 1; k < len(s); k++ { + if !eq(s[k], s[k-1]) { + if i != k { + s[i] = s[k] + } i++ - last = v } } return s[:i] diff --git a/vendor/golang.org/x/exp/slices/sort.go b/vendor/golang.org/x/exp/slices/sort.go index f14f40da712a6..231b6448acd79 100644 --- a/vendor/golang.org/x/exp/slices/sort.go +++ b/vendor/golang.org/x/exp/slices/sort.go @@ -81,10 +81,12 @@ func BinarySearch[E constraints.Ordered](x []E, target E) (int, bool) { } // BinarySearchFunc works like BinarySearch, but uses a custom comparison -// function. The slice must be sorted in increasing order, where "increasing" is -// defined by cmp. cmp(a, b) is expected to return an integer comparing the two -// parameters: 0 if a == b, a negative number if a < b and a positive number if -// a > b. +// function. The slice must be sorted in increasing order, where "increasing" +// is defined by cmp. cmp should return 0 if the slice element matches +// the target, a negative number if the slice element precedes the target, +// or a positive number if the slice element follows the target. +// cmp must implement the same ordering as the slice, such that if +// cmp(a, t) < 0 and cmp(b, t) >= 0, then a must precede b in the slice. func BinarySearchFunc[E, T any](x []E, target T, cmp func(E, T) int) (int, bool) { n := len(x) // Define cmp(x[-1], target) < 0 and cmp(x[n], target) >= 0 . diff --git a/vendor/modules.txt b/vendor/modules.txt index c1fea51885bc7..d325a4375310c 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1499,11 +1499,11 @@ golang.org/x/crypto/pkcs12 golang.org/x/crypto/pkcs12/internal/rc2 golang.org/x/crypto/scrypt golang.org/x/crypto/sha3 -# golang.org/x/exp v0.0.0-20230321023759-10a507213a29 -## explicit; go 1.18 +# golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 +## explicit; go 1.20 golang.org/x/exp/constraints golang.org/x/exp/slices -# golang.org/x/mod v0.9.0 +# golang.org/x/mod v0.11.0 ## explicit; go 1.17 golang.org/x/mod/semver # golang.org/x/net v0.10.0 From a1c877f9ee09c18a52abfe56df296958b44e89e8 Mon Sep 17 00:00:00 2001 From: Salva Corts Date: Fri, 14 Jul 2023 09:49:44 +0200 Subject: [PATCH 2/2] Same exp pkg for loki and push --- go.sum | 8 ++-- pkg/push/go.mod | 2 +- vendor/golang.org/x/exp/slices/slices.go | 52 +++++++----------------- vendor/golang.org/x/exp/slices/sort.go | 10 ++--- vendor/modules.txt | 6 +-- 5 files changed, 26 insertions(+), 52 deletions(-) diff --git a/go.sum b/go.sum index f55e0cb887454..a965b8609bc7b 100644 --- a/go.sum +++ b/go.sum @@ -1853,8 +1853,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= -golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw= -golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= +golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug= +golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= @@ -1884,8 +1884,8 @@ golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU= -golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs= +golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/pkg/push/go.mod b/pkg/push/go.mod index 9f6c824a016e1..c188e1543c37d 100644 --- a/pkg/push/go.mod +++ b/pkg/push/go.mod @@ -5,7 +5,7 @@ go 1.19 require ( github.com/gogo/protobuf v1.3.2 github.com/stretchr/testify v1.8.2 - golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 + golang.org/x/exp v0.0.0-20230321023759-10a507213a29 google.golang.org/grpc v1.53.0 ) diff --git a/vendor/golang.org/x/exp/slices/slices.go b/vendor/golang.org/x/exp/slices/slices.go index 8a7cf20dbd272..cff0cd49ecfb3 100644 --- a/vendor/golang.org/x/exp/slices/slices.go +++ b/vendor/golang.org/x/exp/slices/slices.go @@ -104,8 +104,8 @@ func CompareFunc[E1, E2 any](s1 []E1, s2 []E2, cmp func(E1, E2) int) int { // Index returns the index of the first occurrence of v in s, // or -1 if not present. func Index[E comparable](s []E, v E) int { - for i := range s { - if v == s[i] { + for i, vs := range s { + if v == vs { return i } } @@ -115,8 +115,8 @@ func Index[E comparable](s []E, v E) int { // IndexFunc returns the first index i satisfying f(s[i]), // or -1 if none do. func IndexFunc[E any](s []E, f func(E) bool) int { - for i := range s { - if f(s[i]) { + for i, v := range s { + if f(v) { return i } } @@ -168,30 +168,6 @@ func Delete[S ~[]E, E any](s S, i, j int) S { return append(s[:i], s[j:]...) } -// DeleteFunc removes any elements from s for which del returns true, -// returning the modified slice. -// When DeleteFunc removes m elements, it might not modify the elements -// s[len(s)-m:len(s)]. If those elements contain pointers you might consider -// zeroing those elements so that objects they reference can be garbage -// collected. -func DeleteFunc[S ~[]E, E any](s S, del func(E) bool) S { - // Don't start copying elements until we find one to delete. - for i, v := range s { - if del(v) { - j := i - for i++; i < len(s); i++ { - v = s[i] - if !del(v) { - s[j] = v - j++ - } - } - return s[:j] - } - } - return s -} - // Replace replaces the elements s[i:j] by the given v, and returns the // modified slice. Replace panics if s[i:j] is not a valid slice of s. func Replace[S ~[]E, E any](s S, i, j int, v ...E) S { @@ -231,12 +207,12 @@ func Compact[S ~[]E, E comparable](s S) S { return s } i := 1 - for k := 1; k < len(s); k++ { - if s[k] != s[k-1] { - if i != k { - s[i] = s[k] - } + last := s[0] + for _, v := range s[1:] { + if v != last { + s[i] = v i++ + last = v } } return s[:i] @@ -248,12 +224,12 @@ func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S { return s } i := 1 - for k := 1; k < len(s); k++ { - if !eq(s[k], s[k-1]) { - if i != k { - s[i] = s[k] - } + last := s[0] + for _, v := range s[1:] { + if !eq(v, last) { + s[i] = v i++ + last = v } } return s[:i] diff --git a/vendor/golang.org/x/exp/slices/sort.go b/vendor/golang.org/x/exp/slices/sort.go index 231b6448acd79..f14f40da712a6 100644 --- a/vendor/golang.org/x/exp/slices/sort.go +++ b/vendor/golang.org/x/exp/slices/sort.go @@ -81,12 +81,10 @@ func BinarySearch[E constraints.Ordered](x []E, target E) (int, bool) { } // BinarySearchFunc works like BinarySearch, but uses a custom comparison -// function. The slice must be sorted in increasing order, where "increasing" -// is defined by cmp. cmp should return 0 if the slice element matches -// the target, a negative number if the slice element precedes the target, -// or a positive number if the slice element follows the target. -// cmp must implement the same ordering as the slice, such that if -// cmp(a, t) < 0 and cmp(b, t) >= 0, then a must precede b in the slice. +// function. The slice must be sorted in increasing order, where "increasing" is +// defined by cmp. cmp(a, b) is expected to return an integer comparing the two +// parameters: 0 if a == b, a negative number if a < b and a positive number if +// a > b. func BinarySearchFunc[E, T any](x []E, target T, cmp func(E, T) int) (int, bool) { n := len(x) // Define cmp(x[-1], target) < 0 and cmp(x[n], target) >= 0 . diff --git a/vendor/modules.txt b/vendor/modules.txt index 8e3b082751072..8f56c13093539 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1528,11 +1528,11 @@ golang.org/x/crypto/pkcs12 golang.org/x/crypto/pkcs12/internal/rc2 golang.org/x/crypto/scrypt golang.org/x/crypto/sha3 -# golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 -## explicit; go 1.20 +# golang.org/x/exp v0.0.0-20230321023759-10a507213a29 +## explicit; go 1.18 golang.org/x/exp/constraints golang.org/x/exp/slices -# golang.org/x/mod v0.11.0 +# golang.org/x/mod v0.9.0 ## explicit; go 1.17 golang.org/x/mod/semver # golang.org/x/net v0.12.0