VictoriaMetrics/VictoriaMetrics · error
`regex` cannot be used for `action=keep_if_contains`
Error message
`regex` cannot be used for `action=keep_if_contains`
What it means
Like other *contains actions, `keep_if_contains` operates on a concrete target label and cannot combine with `regex`, since there is no regex semantics for this action. parseRelabelConfig rejects configs that set `regex` with `action=keep_if_contains`.
Source
Thrown at lib/promrelabel/config.go:299
if targetLabel == "" {
return nil, fmt.Errorf("missing `target_label` for `action=replace`")
}
case "replace_all":
if len(sourceLabels) == 0 {
return nil, fmt.Errorf("missing `source_labels` for `action=replace_all`")
}
if targetLabel == "" {
return nil, fmt.Errorf("missing `target_label` for `action=replace_all`")
}
case "keep_if_contains":
if targetLabel == "" {
return nil, fmt.Errorf("`target_label` must be set for `action=keep_if_contains`")
}
if len(sourceLabels) == 0 {
return nil, fmt.Errorf("`source_labels` must contain at least a single entry for `action=keep_if_contains`")
}
if rc.Regex != nil {
return nil, fmt.Errorf("`regex` cannot be used for `action=keep_if_contains`")
}
case "drop_if_contains":
if targetLabel == "" {
return nil, fmt.Errorf("`target_label` must be set for `action=drop_if_contains`")
}
if len(sourceLabels) == 0 {
return nil, fmt.Errorf("`source_labels` must contain at least a single entry for `action=drop_if_contains`")
}
if rc.Regex != nil {
return nil, fmt.Errorf("`regex` cannot be used for `action=drop_if_contains`")
}
case "keep_if_equal":
if len(sourceLabels) < 2 {
return nil, fmt.Errorf("`source_labels` must contain at least two entries for `action=keep_if_equal`; got %q", sourceLabels)
}
if targetLabel != "" {
return nil, fmt.Errorf("`target_label` cannot be used for `action=keep_if_equal`")
}View on GitHub (pinned to 5079fb58f1)
Solutions
- Remove the `regex` field; express the match via the source label values
- Use `action=keep` with `regex` if regex-based sample filtering is what you need
- Use `action=replace` if regex transformation is intended
Example fix
// before - action: keep_if_contains target_label: pod source_labels: [pod] regex: "prod-.*" // after - action: keep_if_contains target_label: pod source_labels: ["prod-.*"]
Defensive patterns
Strategy: validation
Validate before calling
def validate_relabel(cfg):
if cfg.get('action') == 'keep_if_contains' and cfg.get('regex') is not None:
raise ValueError('regex cannot be used with action=keep_if_contains')
return True Prevention
- Encode patterns via source_labels values, not regex
- Switch to action=keep when regex filtering is needed
- Keep rule templates per-action to avoid field bleed
When it happens
Trigger: ParseRelabelConfigs is given a config with `action: keep_if_contains` plus a non-nil `regex` field.
Common situations: Copy-pasting a `replace` rule and changing only the action; assuming contains-actions support regex like replace does.
Related errors
- `target_label` cannot be used with `action=graphite`; see ht
- `replacement` cannot be used with `action=graphite`; see htt
- `regex` cannot be used for `action=graphite`; see https://do
- missing `target_label` for `action=replace`
- missing `source_labels` for `action=replace_all`
AI-assisted analysis of VictoriaMetrics/VictoriaMetrics@5079fb58f1 (2026-09-03).
Data as JSON: /api/errors/b48cfcead86d1c3e.
Report an issue: GitHub.