Skip to content

Commit

Permalink
common/relabel: update validation rules from Prometheus (#5566)
Browse files Browse the repository at this point in the history
Signed-off-by: Paschalis Tsilias <[email protected]>
  • Loading branch information
tpaschalis authored Oct 23, 2023
1 parent b6f64de commit 4307695
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 22 deletions.
9 changes: 3 additions & 6 deletions component/common/relabel/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,13 @@ func (rc *Config) Validate() error {
if rc.Modulus == 0 && rc.Action == HashMod {
return fmt.Errorf("relabel configuration for hashmod requires non-zero modulus")
}
if (rc.Action == Replace || rc.Action == KeepEqual || rc.Action == DropEqual || rc.Action == HashMod || rc.Action == Lowercase || rc.Action == Uppercase) && rc.TargetLabel == "" {
if (rc.Action == Replace || rc.Action == HashMod || rc.Action == Lowercase || rc.Action == Uppercase || rc.Action == KeepEqual || rc.Action == DropEqual) && rc.TargetLabel == "" {
return fmt.Errorf("relabel configuration for %s action requires 'target_label' value", rc.Action)
}
if (rc.Action == KeepEqual || rc.Action == DropEqual) && rc.SourceLabels == nil {
return fmt.Errorf("relabel configuration for %s action requires 'source_labels' value", rc.Action)
}
if (rc.Action == Replace || rc.Action == Lowercase || rc.Action == Uppercase) && !relabelTarget.MatchString(rc.TargetLabel) {
if (rc.Action == Replace || rc.Action == Lowercase || rc.Action == Uppercase || rc.Action == KeepEqual || rc.Action == DropEqual) && !relabelTarget.MatchString(rc.TargetLabel) {
return fmt.Errorf("%q is invalid 'target_label' for %s action", rc.TargetLabel, rc.Action)
}
if (rc.Action == Lowercase || rc.Action == Uppercase) && rc.Replacement != DefaultRelabelConfig.Replacement {
if (rc.Action == Lowercase || rc.Action == Uppercase || rc.Action == KeepEqual || rc.Action == DropEqual) && rc.Replacement != DefaultRelabelConfig.Replacement {
return fmt.Errorf("'replacement' can not be set for %s action", rc.Action)
}
if rc.Action == LabelMap && !relabelTarget.MatchString(rc.Replacement) {
Expand Down
16 changes: 0 additions & 16 deletions component/common/relabel/relabel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ func TestParseConfig(t *testing.T) {
`,
expectErr: true,
},
{
name: "missing dropequal source",
cfg: `
action = "dropequal"
target_label = "foo"
`,
expectErr: true,
},
{
name: "missing keepequal target",
cfg: `
Expand All @@ -54,14 +46,6 @@ func TestParseConfig(t *testing.T) {
`,
expectErr: true,
},
{
name: "missing keepequal source",
cfg: `
action = "keepequal"
target_label = "foo"
`,
expectErr: true,
},
{
name: "unknown action",
cfg: `
Expand Down

0 comments on commit 4307695

Please sign in to comment.