{"record":{"id":"b48cfcead86d1c3e","repo":"VictoriaMetrics/VictoriaMetrics","slug":"regex-cannot-be-used-for-action-keep-if-contain","errorCode":null,"errorMessage":"`regex` cannot be used for `action=keep_if_contains`","messagePattern":"`regex` cannot be used for `action=keep_if_contains`","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/promrelabel/config.go","lineNumber":299,"sourceCode":"\t\tif targetLabel == \"\" {\n\t\t\treturn nil, fmt.Errorf(\"missing `target_label` for `action=replace`\")\n\t\t}\n\tcase \"replace_all\":\n\t\tif len(sourceLabels) == 0 {\n\t\t\treturn nil, fmt.Errorf(\"missing `source_labels` for `action=replace_all`\")\n\t\t}\n\t\tif targetLabel == \"\" {\n\t\t\treturn nil, fmt.Errorf(\"missing `target_label` for `action=replace_all`\")\n\t\t}\n\tcase \"keep_if_contains\":\n\t\tif targetLabel == \"\" {\n\t\t\treturn nil, fmt.Errorf(\"`target_label` must be set for `action=keep_if_contains`\")\n\t\t}\n\t\tif len(sourceLabels) == 0 {\n\t\t\treturn nil, fmt.Errorf(\"`source_labels` must contain at least a single entry for `action=keep_if_contains`\")\n\t\t}\n\t\tif rc.Regex != nil {\n\t\t\treturn nil, fmt.Errorf(\"`regex` cannot be used for `action=keep_if_contains`\")\n\t\t}\n\tcase \"drop_if_contains\":\n\t\tif targetLabel == \"\" {\n\t\t\treturn nil, fmt.Errorf(\"`target_label` must be set for `action=drop_if_contains`\")\n\t\t}\n\t\tif len(sourceLabels) == 0 {\n\t\t\treturn nil, fmt.Errorf(\"`source_labels` must contain at least a single entry for `action=drop_if_contains`\")\n\t\t}\n\t\tif rc.Regex != nil {\n\t\t\treturn nil, fmt.Errorf(\"`regex` cannot be used for `action=drop_if_contains`\")\n\t\t}\n\tcase \"keep_if_equal\":\n\t\tif len(sourceLabels) < 2 {\n\t\t\treturn nil, fmt.Errorf(\"`source_labels` must contain at least two entries for `action=keep_if_equal`; got %q\", sourceLabels)\n\t\t}\n\t\tif targetLabel != \"\" {\n\t\t\treturn nil, fmt.Errorf(\"`target_label` cannot be used for `action=keep_if_equal`\")\n\t\t}","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/VictoriaMetrics/VictoriaMetrics/blob/5079fb58f1e8e62113f90c945ad71586c797d770/lib/promrelabel/config.go#L281-L317","documentation":"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`.","triggerScenarios":"ParseRelabelConfigs is given a config with `action: keep_if_contains` plus a non-nil `regex` field.","commonSituations":"Copy-pasting a `replace` rule and changing only the action; assuming contains-actions support regex like replace does.","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"],"exampleFix":"// before\n- action: keep_if_contains\n  target_label: pod\n  source_labels: [pod]\n  regex: \"prod-.*\"\n// after\n- action: keep_if_contains\n  target_label: pod\n  source_labels: [\"prod-.*\"]","handlingStrategy":"validation","validationCode":"def validate_relabel(cfg):\n    if cfg.get('action') == 'keep_if_contains' and cfg.get('regex') is not None:\n        raise ValueError('regex cannot be used with action=keep_if_contains')\n    return True","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["promrelabel","config-validation","relabeling","victoriametrics"],"backgroundTag":"invalid-relabel-config","analyzedSha":"5079fb58f1e8e62113f90c945ad71586c797d770","analyzedAt":"2026-09-03T18:10:26.153Z","contentChangedAt":"2026-09-03T18:10:26.153Z","schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}