Skip to content

Commit d385cba

Browse files
authored
relabel: default value for source_labels should be nil and not empty slice (#5059)
* relabel: default value for source_labels should be nil and not empty slice
1 parent d2fda91 commit d385cba

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Main (unreleased)
6969

7070
- Allow configuration of `force_attempt_http2` and default it to `true` for otelcol exporters with HTTP client configurations. (@dehaansa)
7171

72+
- Fix default values for relabel rules, this caused issues in e.g. `prometheus.operator.servicemonitors` when using labeldrop. (@kalleep)
73+
7274
v1.12.0
7375
-----------------
7476

internal/component/common/relabel/relabel.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,12 @@ func doRelabel(cfg *Config, lb LabelBuilder) (keep bool) {
318318
func ComponentToPromRelabelConfigs(rcs []*Config) []*relabel.Config {
319319
res := make([]*relabel.Config, len(rcs))
320320
for i, rc := range rcs {
321-
sourceLabels := make([]model.LabelName, len(rc.SourceLabels))
322-
for i, sl := range rc.SourceLabels {
323-
sourceLabels[i] = model.LabelName(sl)
321+
var sourceLabels []model.LabelName
322+
if len(rc.SourceLabels) > 0 {
323+
sourceLabels = make([]model.LabelName, len(rc.SourceLabels))
324+
for i, sl := range rc.SourceLabels {
325+
sourceLabels[i] = model.LabelName(sl)
326+
}
324327
}
325328

326329
res[i] = &relabel.Config{

internal/component/common/relabel/relabel_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import (
1919
"fmt"
2020
"testing"
2121

22+
"github.com/grafana/alloy/syntax"
2223
"github.com/grafana/regexp"
2324
"github.com/stretchr/testify/require"
2425
"gopkg.in/yaml.v2"
2526

27+
"github.com/prometheus/common/model"
2628
"github.com/prometheus/prometheus/model/labels"
2729
"github.com/prometheus/prometheus/util/testutil"
2830
)
@@ -857,6 +859,21 @@ func BenchmarkRelabel(b *testing.B) {
857859
}
858860
}
859861

862+
func TestComponentToPromRelabelConfigs(t *testing.T) {
863+
rule := `
864+
action = "labeldrop"
865+
regex = "helm_sh_chart"
866+
`
867+
868+
var cfg Config
869+
require.NoError(t, syntax.Unmarshal([]byte(rule), &cfg))
870+
871+
converted := ComponentToPromRelabelConfigs([]*Config{&cfg})
872+
for _, r := range converted {
873+
require.NoError(t, r.Validate(model.LegacyValidation))
874+
}
875+
}
876+
860877
// MustNewRegexp works like NewRegexp, but panics if the regular expression does not compile.
861878
func MustNewRegexp(s string) Regexp {
862879
re, err := NewRegexp(s)

0 commit comments

Comments
 (0)