Skip to content

Commit 5459db2

Browse files
committed
processor: delete label in order
Order matters in Prometheus so we need to delete the tenant label and keep the original sorted order.
1 parent 10d6c2d commit 5459db2

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

processor.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ func (p *processor) dispatch(clientIP net.Addr, reqID uuid.UUID, m map[string]*p
325325
return
326326
}
327327

328+
func removeOrdered(slice []prompb.Label, s int) []prompb.Label {
329+
return append(slice[:s], slice[s+1:]...)
330+
}
331+
328332
func (p *processor) processTimeseries(ts *prompb.TimeSeries) (tenant string, err error) {
329333
idx := 0
330334
for i, l := range ts.Labels {
@@ -343,9 +347,10 @@ func (p *processor) processTimeseries(ts *prompb.TimeSeries) (tenant string, err
343347
}
344348

345349
if p.cfg.Tenant.LabelRemove {
346-
l := len(ts.Labels)
347-
ts.Labels[idx] = ts.Labels[l-1]
348-
ts.Labels = ts.Labels[:l-1]
350+
// Order is important. See:
351+
// https://github.com/thanos-io/thanos/issues/6452
352+
// https://github.com/prometheus/prometheus/issues/11505
353+
ts.Labels = removeOrdered(ts.Labels, idx)
349354
}
350355

351356
return

processor_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/google/uuid"
1313
"github.com/prometheus/prometheus/prompb"
1414
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/require"
1516

1617
fh "github.com/valyala/fasthttp"
1718
fhu "github.com/valyala/fasthttp/fasthttputil"
@@ -452,3 +453,34 @@ func Benchmark_marshal(b *testing.B) {
452453
_, _ = p.unmarshal(buf)
453454
}
454455
}
456+
457+
func TestRemoveOrdered(t *testing.T) {
458+
l := []prompb.Label{
459+
{
460+
Name: "aaa",
461+
Value: "bbb",
462+
},
463+
}
464+
465+
l = removeOrdered(l, 0)
466+
require.Equal(t, []prompb.Label{}, l)
467+
468+
l = []prompb.Label{
469+
{
470+
Name: "aaa",
471+
Value: "bbb",
472+
},
473+
{
474+
Name: "ccc",
475+
Value: "ddd",
476+
},
477+
}
478+
l = removeOrdered(l, 0)
479+
require.Equal(t, []prompb.Label{
480+
{
481+
Name: "ccc",
482+
Value: "ddd",
483+
},
484+
}, l)
485+
486+
}

0 commit comments

Comments
 (0)