From c1370d07caa6fcef959be9d2f5c6bab67c6ac11a Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Thu, 26 Nov 2015 19:16:53 +0100 Subject: [PATCH] Strip trailing / from push URL. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This circumvents the following problem: • The prometheus client library appends “/metrics/…” to the pushURL. • When pushURL ends in a trailing slash, the URL becomes e.g. http://pushgateway.example.com:9091//metrics/… • The pushgateway will reply with an HTTP 307 status code (temporary redirect). • While Go’s net/http client follows redirects by default, it will only follow HTTP 302 (Found) and HTTP 303 (See Other) redirects for PUT and POST requests, which the prometheus client library uses. Hence, when calling e.g.: prometheus.Push("foo", "bar", "http://pushgateway.example.com:9091/") …your metrics would not actually get pushed successfully, but rather you’d see the error message: 2015/11/26 10:59:49 main.go:209: unexpected status code 307 while pushing to http://push... --- prometheus/registry.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/prometheus/registry.go b/prometheus/registry.go index 883be83..1dc2536 100644 --- a/prometheus/registry.go +++ b/prometheus/registry.go @@ -336,6 +336,9 @@ func (r *registry) Push(job, instance, pushURL, method string) error { if !strings.Contains(pushURL, "://") { pushURL = "http://" + pushURL } + if strings.HasSuffix(pushURL, "/") { + pushURL = pushURL[:len(pushURL)-1] + } pushURL = fmt.Sprintf("%s/metrics/jobs/%s", pushURL, url.QueryEscape(job)) if instance != "" { pushURL += "/instances/" + url.QueryEscape(instance)