{"record":{"id":"381ebba2413c4794","repo":"VictoriaMetrics/VictoriaMetrics","slug":"unexpected-status-code-returned-when-scraping-q","errorCode":null,"errorMessage":"unexpected status code returned when scraping %q: %d; expecting %d; response body: %q","messagePattern":"unexpected status code returned when scraping %q: (.+?); expecting (.+?); response body: %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/promscrape/client.go","lineNumber":181,"sourceCode":"\tscrapeRequests.Inc()\n\tresp, err := c.c.Do(req)\n\tif err != nil {\n\t\tif ue, ok := err.(*url.Error); ok && ue.Timeout() {\n\t\t\tscrapesTimedout.Inc()\n\t\t}\n\t\treturn false, fmt.Errorf(\"cannot perform request to %q: %w\", c.scrapeURL, err)\n\t}\n\tdefer resp.Body.Close()\n\n\tif resp.StatusCode != http.StatusOK {\n\t\tmetrics.GetOrCreateCounter(fmt.Sprintf(`vm_promscrape_scrapes_total{status_code=\"%d\"}`, resp.StatusCode)).Inc()\n\t\tlr := ioutil.GetLimitedReader(resp.Body, c.maxScrapeSize+1)\n\t\trespBody, err := io.ReadAll(lr)\n\t\tioutil.PutLimitedReader(lr)\n\t\tif err != nil {\n\t\t\trespBody = []byte(err.Error())\n\t\t}\n\t\treturn false, fmt.Errorf(\"unexpected status code returned when scraping %q: %d; expecting %d; response body: %q\",\n\t\t\tc.scrapeURL, resp.StatusCode, http.StatusOK, respBody)\n\t}\n\tscrapesOK.Inc()\n\n\t// Read the data from resp.Body\n\tlr := ioutil.GetLimitedReader(resp.Body, c.maxScrapeSize+1)\n\t_, err = dst.ReadFrom(lr)\n\tioutil.PutLimitedReader(lr)\n\tif err != nil {\n\t\tif ue, ok := err.(*url.Error); ok && ue.Timeout() {\n\t\t\tscrapesTimedout.Inc()\n\t\t}\n\t\treturn false, fmt.Errorf(\"cannot read data from %s: %w\", c.scrapeURL, err)\n\t}\n\tif int64(dst.Len()) > c.maxScrapeSize {\n\t\tmaxScrapeSizeExceeded.Inc()\n\t\treturn false, fmt.Errorf(\"the response from %q exceeds -promscrape.maxScrapeSize or max_scrape_size in the scrape config (%d bytes). \"+\n\t\t\t\"Possible solutions are: reduce the response size for the target, increase -promscrape.maxScrapeSize command-line flag, \"+","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/VictoriaMetrics/VictoriaMetrics/blob/5079fb58f1e8e62113f90c945ad71586c797d770/lib/promscrape/client.go#L163-L199","documentation":"Raised in client.ReadData when the scrape response status is not 200: the body (truncated to max_scrape_size) is included to aid debugging, and a vm_promscrape_scrapes_total{status_code=...} counter is incremented.","triggerScenarios":"Thrown at lib/promscrape/client.go:181 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Read the response body snippet for the target's error message","Common causes: wrong metrics_path (404), auth required (401), target behind a proxy (403/502) — fix job config accordingly"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}