Merge pull request #3215 from prometheus/grobie/http-accept

Send a HTTP Accept header when scraping
This commit is contained in:
Tobias Schmidt 2017-09-25 15:18:31 +02:00 committed by GitHub
commit 52ea748248
2 changed files with 8 additions and 6 deletions

View file

@ -397,7 +397,7 @@ type targetScraper struct {
buf *bufio.Reader 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) 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 { if err != nil {
return err 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.Add("Accept-Encoding", "gzip")
req.Header.Set("User-Agent", userAgentHeader) req.Header.Set("User-Agent", userAgentHeader)
req.Header.Set("X-Prometheus-Scrape-Timeout-Seconds", fmt.Sprintf("%f", s.timeout.Seconds())) req.Header.Set("X-Prometheus-Scrape-Timeout-Seconds", fmt.Sprintf("%f", s.timeout.Seconds()))

View file

@ -895,11 +895,14 @@ func TestTargetScraperScrapeOK(t *testing.T) {
server := httptest.NewServer( server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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") timeout := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds")
if timeout != expectedTimeout { if timeout != expectedTimeout {
t.Errorf("Scrape timeout did not match expected timeout") t.Errorf("Expected scrape timeout header %q, got %q", expectedTimeout, timeout)
t.Errorf("Expected: %v", expectedTimeout)
t.Fatalf("Got: %v", timeout)
} }
w.Header().Set("Content-Type", `text/plain; version=0.0.4`) w.Header().Set("Content-Type", `text/plain; version=0.0.4`)