mirror of
https://github.com/prometheus/prometheus
synced 2024-12-26 00:23:18 +00:00
Send a HTTP Accept header when scraping
This commit is contained in:
parent
64c7932e85
commit
40c278ee2d
@ -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()))
|
||||||
|
@ -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`)
|
||||||
|
Loading…
Reference in New Issue
Block a user