{"record":{"id":"1cbe66a61c681609","repo":"thanos-io/thanos","slug":"validate-relabel-config-1cbe66","errorCode":null,"errorMessage":"validate relabel config","messagePattern":"validate relabel config","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/block/fetcher.go","lineNumber":1406,"sourceCode":"\tf.mtx.Unlock()\n\n\treturn nil\n}\n\nvar (\n\tSelectorSupportedRelabelActions = map[relabel.Action]struct{}{relabel.Keep: {}, relabel.Drop: {}, relabel.HashMod: {}}\n)\n\n// ParseRelabelConfig parses relabel configuration.\n// If supportedActions not specified, all relabel actions are valid.\nfunc ParseRelabelConfig(contentYaml []byte, supportedActions map[relabel.Action]struct{}) ([]*relabel.Config, error) {\n\tvar relabelConfig []*relabel.Config\n\tif err := yaml.Unmarshal(contentYaml, &relabelConfig); err != nil {\n\t\treturn nil, errors.Wrap(err, \"parsing relabel configuration\")\n\t}\n\tfor _, cfg := range relabelConfig {\n\t\tif err := cfg.Validate(prommodel.UTF8Validation); err != nil {\n\t\t\treturn nil, errors.Wrap(err, \"validate relabel config\")\n\t\t}\n\t}\n\n\tif supportedActions != nil {\n\t\tfor _, cfg := range relabelConfig {\n\t\t\tif _, ok := supportedActions[cfg.Action]; !ok {\n\t\t\t\treturn nil, errors.Errorf(\"unsupported relabel action: %v\", cfg.Action)\n\t\t\t}\n\t\t}\n\t}\n\n\treturn relabelConfig, nil\n}\n","sourceCodeStart":1388,"sourceCodeEnd":1420,"githubUrl":"https://github.com/thanos-io/thanos/blob/35b8b991177def87ed52dcf10f9b6d87f07282c8/pkg/block/fetcher.go#L1388-L1420","documentation":"After successfully unmarshaling, ParseRelabelConfig validates each rule with cfg.Validate(prommodel.UTF8Validation) and wraps failures with 'validate relabel config'. This means the YAML parsed but a rule violates Prometheus relabel semantics (e.g. invalid regex, empty required fields, invalid label names under UTF8 validation).","triggerScenarios":"Calling ParseRelabelConfig with a rule whose Validate() fails: invalid regex in 'regex', empty action or target_label where required, invalid UTF-8/label-name characters, replacement incompatible with the action.","commonSituations":"Hand-written relabel files with malformed regex (unclosed groups), target_label containing invalid characters, or rules copied from an older Prometheus version whose validation rules changed.","solutions":["Read the wrapped cause to see which rule and field failed validation; fix that specific field.","Run the config through 'promtool check config' or 'promtool check web-config' to validate rules offline.","Test regexes with RE2 semantics (https://regex101.com with Go flavor) — e.g. anchor with ^...$ explicitly since Prometheus does not anchor by default.","Ensure label names match allowed label-name syntax (or valid UTF-8 names when using UTF8Validation) and required fields per action are present."],"exampleFix":"// before\n// - action: drop\n//   regex: \"[a-\"   # invalid RE2\n// after\n// - action: drop\n//   regex: \"[a-z]+\"","handlingStrategy":"validation","validationCode":"// Go: validate each rule the same way the library does, before calling\nfor _, c := range cfgs {\n    if err := c.Validate(prommodel.UTF8Validation); err != nil {\n        return fmt.Errorf(\"rule %q invalid: %w\", c, err)\n    }\n}","typeGuard":null,"tryCatchPattern":"cfgs, err := block.ParseRelabelConfig(contentYaml, supportedActions)\nif err != nil && strings.Contains(err.Error(), \"validate relabel config\") {\n    return fmt.Errorf(\"invalid relabel rule (check regex/label names): %w\", err)\n}","preventionTips":["Test all regexes as RE2 (Go) — no backreferences or lookaheads.","Anchor regexes explicitly with ^ and $ when exact match is intended.","Use promtool to validate configs before deployment.","Keep label names within valid syntax; avoid empty source_labels for actions that forbid them."],"tags":["relabel","validation","configuration","regex"],"backgroundTag":"schema-validation-failed","analyzedSha":"35b8b991177def87ed52dcf10f9b6d87f07282c8","analyzedAt":"2026-09-07T01:49:59.689Z","contentChangedAt":"2026-09-07T01:49:59.689Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}