From 40c278ee2d2882898b18951ed70da325bcf84f9b Mon Sep 17 00:00:00 2001 From: Tobias Schmidt Date: Fri, 22 Sep 2017 18:06:43 +0200 Subject: [PATCH] Send a HTTP Accept header when scraping --- retrieval/scrape.go | 5 ++--- retrieval/scrape_test.go | 9 ++++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/retrieval/scrape.go b/retrieval/scrape.go index beb965770..abda0c476 100644 --- a/retrieval/scrape.go +++ b/retrieval/scrape.go @@ -397,7 +397,7 @@ type targetScraper struct { buf *bufio.Reader } -const acceptHeader = `application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.7,text/plain;version=0.0.4;q=0.3,*/*;q=0.1` +const acceptHeader = `text/plain;version=0.0.4;q=1,*/*;q=0.1` var userAgentHeader = fmt.Sprintf("Prometheus/%s", version.Version) @@ -407,8 +407,7 @@ func (s *targetScraper) scrape(ctx context.Context, w io.Writer) error { if err != nil { return err } - // Disable accept header to always negotiate for text format. - // req.Header.Add("Accept", acceptHeader) + req.Header.Add("Accept", acceptHeader) req.Header.Add("Accept-Encoding", "gzip") req.Header.Set("User-Agent", userAgentHeader) req.Header.Set("X-Prometheus-Scrape-Timeout-Seconds", fmt.Sprintf("%f", s.timeout.Seconds())) diff --git a/retrieval/scrape_test.go b/retrieval/scrape_test.go index a9ca87cd2..b267bbc40 100644 --- a/retrieval/scrape_test.go +++ b/retrieval/scrape_test.go @@ -895,11 +895,14 @@ func TestTargetScraperScrapeOK(t *testing.T) { server := httptest.NewServer( http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + accept := r.Header.Get("Accept") + if !strings.HasPrefix(accept, "text/plain;") { + t.Errorf("Expected Accept header to prefer text/plain, got %q", accept) + } + timeout := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds") if timeout != expectedTimeout { - t.Errorf("Scrape timeout did not match expected timeout") - t.Errorf("Expected: %v", expectedTimeout) - t.Fatalf("Got: %v", timeout) + t.Errorf("Expected scrape timeout header %q, got %q", expectedTimeout, timeout) } w.Header().Set("Content-Type", `text/plain; version=0.0.4`)