{"record":{"id":"ff48f5e05ea3013f","repo":"thanos-io/thanos","slug":"unmarshal-alertmanager-alert-api-response","errorCode":null,"errorMessage":"unmarshal alertmanager alert API response","messagePattern":"unmarshal alertmanager alert API response","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/promclient/promclient.go","lineNumber":653,"sourceCode":"\tu.Path = path.Join(u.Path, \"/api/v1/alerts\")\n\n\tlevel.Debug(c.logger).Log(\"msg\", \"querying instant\", \"url\", u.String())\n\n\tspan, ctx := tracing.StartSpan(ctx, \"/alertmanager_alerts HTTP[client]\")\n\tdefer span.Finish()\n\n\tbody, _, err := c.req2xx(ctx, &u, http.MethodGet, nil)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\t// Decode only ResultType and load Result only as RawJson since we don't know\n\t// structure of the Result yet.\n\tvar v struct {\n\t\tData []*model.Alert `json:\"data\"`\n\t}\n\tif err = json.Unmarshal(body, &v); err != nil {\n\t\treturn nil, errors.Wrap(err, \"unmarshal alertmanager alert API response\")\n\t}\n\tsort.Slice(v.Data, func(i, j int) bool {\n\t\treturn v.Data[i].Labels.Before(v.Data[j].Labels)\n\t})\n\treturn v.Data, nil\n}\n\n// BuildVersion returns Prometheus version from /api/v1/status/buildinfo Prometheus endpoint.\n// For Prometheus versions < 2.14.0 it returns \"0\" as Prometheus version.\nfunc (c *Client) BuildVersion(ctx context.Context, base *url.URL) (string, error) {\n\tu := *base\n\tu.Path = path.Join(u.Path, \"/api/v1/status/buildinfo\")\n\n\tlevel.Debug(c.logger).Log(\"msg\", \"build version\", \"url\", u.String())\n\n\tspan, ctx := tracing.StartSpan(ctx, \"/prom_buildversion HTTP[client]\")\n\tdefer span.Finish()\n","sourceCodeStart":635,"sourceCodeEnd":671,"githubUrl":"https://github.com/thanos-io/thanos/blob/35b8b991177def87ed52dcf10f9b6d87f07282c8/pkg/promclient/promclient.go#L635-L671","documentation":"AlertmanagerAlerts fetches /api/v1/alerts from an Alertmanager and decodes the 2xx body into a struct with a data field of []*model.Alert. This error wraps json.Unmarshal failing because the body is not valid JSON or does not match that envelope (e.g. HTML page, wrong root type, data not an array).","triggerScenarios":"Client.AlertmanagerAlerts receives a 2xx response whose body fails to decode into {\"data\": [alert...]} — non-JSON body, data as object/string, or unexpected field types inside alerts.","commonSituations":"Pointing the client at the Prometheus API instead of Alertmanager (different response shape), an authentication portal returning an HTML 200 page, wrong port or path prefix, or a proxy intercepting the request.","solutions":["Verify the base URL is the Alertmanager (default :9093), not Prometheus (:9090); log the raw body to confirm.","Check for auth redirects/SSO proxies that return HTML login pages with status 200.","Test directly with curl '<base>/api/v1/alerts' and confirm the JSON has a data array.","Alertmanager API version: ensure the endpoint exists and is not an api/v2-only setup where shapes differ (this client targets the legacy api/v1)."],"exampleFix":"// before: wrong server\nurl: http://prometheus:9090\n// after: Alertmanager serves /api/v1/alerts\nurl: http://alertmanager:9093","handlingStrategy":"try-catch","validationCode":"// pre-flight: confirm this is Alertmanager's API\nresp, _ := http.Get(base + \"/api/v1/status/buildinfo\") // AM returns 200 with version info; Prom buildinfo schema differs\n// or simply check the configured port (9093 vs 9090) before constructing the client","typeGuard":null,"tryCatchPattern":"alerts, err := client.AlertmanagerAlerts(ctx, u)\nif err != nil && strings.Contains(err.Error(), \"unmarshal alertmanager alert API response\") {\n    // wrong endpoint or HTML-200 portal page: log body/URL\n    return err\n}","preventionTips":["Use the Alertmanager address/port (default 9093), not Prometheus","Beware SSO/auth proxies returning HTML 200 pages","Verify /api/v1/alerts is available (legacy API enabled) on your Alertmanager version"],"tags":["json","alertmanager","unmarshal","http-response"],"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"}