Skip to content

Commit

Permalink
Merge pull request #57 from GiedriusS/labels_in_order
Browse files Browse the repository at this point in the history
processor: delete label in order
  • Loading branch information
blind-oracle authored Dec 6, 2023
2 parents d6d864d + 5459db2 commit 94043a3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
11 changes: 8 additions & 3 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ func (p *processor) dispatch(clientIP net.Addr, reqID uuid.UUID, m map[string]*p
return
}

func removeOrdered(slice []prompb.Label, s int) []prompb.Label {
return append(slice[:s], slice[s+1:]...)
}

func (p *processor) processTimeseries(ts *prompb.TimeSeries) (tenant string, err error) {
idx := 0
for i, l := range ts.Labels {
Expand All @@ -343,9 +347,10 @@ func (p *processor) processTimeseries(ts *prompb.TimeSeries) (tenant string, err
}

if p.cfg.Tenant.LabelRemove {
l := len(ts.Labels)
ts.Labels[idx] = ts.Labels[l-1]
ts.Labels = ts.Labels[:l-1]
// Order is important. See:
// https://github.com/thanos-io/thanos/issues/6452
// https://github.com/prometheus/prometheus/issues/11505
ts.Labels = removeOrdered(ts.Labels, idx)
}

return
Expand Down
32 changes: 32 additions & 0 deletions processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/google/uuid"
"github.com/prometheus/prometheus/prompb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

fh "github.com/valyala/fasthttp"
fhu "github.com/valyala/fasthttp/fasthttputil"
Expand Down Expand Up @@ -452,3 +453,34 @@ func Benchmark_marshal(b *testing.B) {
_, _ = p.unmarshal(buf)
}
}

func TestRemoveOrdered(t *testing.T) {
l := []prompb.Label{
{
Name: "aaa",
Value: "bbb",
},
}

l = removeOrdered(l, 0)
require.Equal(t, []prompb.Label{}, l)

l = []prompb.Label{
{
Name: "aaa",
Value: "bbb",
},
{
Name: "ccc",
Value: "ddd",
},
}
l = removeOrdered(l, 0)
require.Equal(t, []prompb.Label{
{
Name: "ccc",
Value: "ddd",
},
}, l)

}

0 comments on commit 94043a3

Please sign in to comment.