{"record":{"id":"9f8e8173127d13ee","repo":"thanos-io/thanos","slug":"rule-alerting-rule-unmarshal-v","errorCode":null,"errorMessage":"rule: alerting rule unmarshal: %v","messagePattern":"rule: alerting rule unmarshal: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/rules/rulespb/custom.go","lineNumber":227,"sourceCode":"\tdecider := struct {\n\t\tType string `json:\"type\"`\n\t}{}\n\tif err := json.Unmarshal(entry, &decider); err != nil {\n\t\treturn errors.Wrapf(err, \"rule: type field unmarshal: %v\", string(entry))\n\t}\n\n\tswitch strings.ToLower(decider.Type) {\n\tcase \"recording\":\n\t\tr := &RecordingRule{}\n\t\tif err := json.Unmarshal(entry, r); err != nil {\n\t\t\treturn errors.Wrapf(err, \"rule: recording rule unmarshal: %v\", string(entry))\n\t\t}\n\n\t\tm.Result = &Rule_Recording{Recording: r}\n\tcase \"alerting\":\n\t\tr := &Alert{}\n\t\tif err := json.Unmarshal(entry, r); err != nil {\n\t\t\treturn errors.Wrapf(err, \"rule: alerting rule unmarshal: %v\", string(entry))\n\t\t}\n\n\t\tm.Result = &Rule_Alert{Alert: r}\n\tcase \"\":\n\t\treturn errors.Errorf(\"rule: no type field provided: %v\", string(entry))\n\tdefault:\n\t\treturn errors.Errorf(\"rule: unknown type field provided %s; %v\", decider.Type, string(entry))\n\t}\n\treturn nil\n}\n\nfunc (m *Rule) MarshalJSON() ([]byte, error) {\n\tif r := m.GetRecording(); r != nil {\n\t\treturn json.Marshal(struct {\n\t\t\t*RecordingRule\n\t\t\tType string `json:\"type\"`\n\t\t}{\n\t\t\tRecordingRule: r,","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/thanos-io/thanos/blob/35b8b991177def87ed52dcf10f9b6d87f07282c8/pkg/rules/rulespb/custom.go#L209-L245","documentation":"When Rule.UnmarshalJSON sees type == \"alerting\", it unmarshals the entry into an Alert; failure is wrapped as 'rule: alerting rule unmarshal' with the raw entry. The type dispatch succeeded but the alerting payload violates the Alert JSON schema (e.g. name/expr wrong types).","triggerScenarios":"A rule entry with \"type\": \"alerting\" whose fields don't match the Alert struct — mistyped fields, missing required alert fields encoded with wrong JSON types, or mismatched schema versions.","commonSituations":"Rules exported by older/newer tooling; hand-edited alert rules; converting Prometheus YAML rules to JSON with incorrect types.","solutions":["Fix the entry to match the Alert schema (string fields for name, query, etc.)","Validate the whole rules file against the rulespb Alert JSON schema","Re-export rules with a compatible tool version"],"exampleFix":"// before\n{\"type\": \"alerting\", \"alert\": {\"name\": 42}}\n// after\n{\"type\": \"alerting\", \"alert\": {\"name\": \"HighErrors\", \"expr\": \"up == 0\"}}","handlingStrategy":"validation","validationCode":"var probe struct {\n    Type string `json:\"type\"`\n    Alert struct {\n        Name string `json:\"name\"`\n        Expr string `json:\"expr\"`\n    } `json:\"alert\"`\n}\nif err := json.Unmarshal(entry, &probe); err != nil || probe.Alert.Name == \"\" {\n    return fmt.Errorf(\"not a valid alerting rule\")\n}","typeGuard":null,"tryCatchPattern":"if err := json.Unmarshal(entry, &rule); err != nil {\n    if strings.Contains(err.Error(), \"alerting rule unmarshal\") {\n        log.Printf(\"bad alerting rule entry: %s\", entry)\n    }\n    return err\n}","preventionTips":["Ensure alert fields (name, expr, for, labels) are correctly typed strings","Validate rules files in CI before upload","Pin tool versions that emit the current Alert JSON schema"],"tags":["json","unmarshal","alerting-rule"],"backgroundTag":"json-unmarshal-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"}