Merge pull request #2786 from tomwilkie/report-error-in-remote-write

Remote write: read first line of response and include it in the error.
This commit is contained in:
Frederic Branczyk 2017-06-07 10:21:34 +02:00 committed by GitHub
commit 5d9ac3565e
2 changed files with 18 additions and 6 deletions

View file

@ -14,8 +14,10 @@
package remote package remote
import ( import (
"bufio"
"bytes" "bytes"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"time" "time"
@ -31,6 +33,8 @@ import (
"github.com/prometheus/prometheus/util/httputil" "github.com/prometheus/prometheus/util/httputil"
) )
const maxErrMsgLen = 256
// Client allows reading and writing from/to a remote HTTP endpoint. // Client allows reading and writing from/to a remote HTTP endpoint.
type Client struct { type Client struct {
index int // Used to differentiate metrics. index int // Used to differentiate metrics.
@ -117,7 +121,12 @@ func (c *Client) Store(samples model.Samples) error {
defer httpResp.Body.Close() defer httpResp.Body.Close()
if httpResp.StatusCode/100 != 2 { if httpResp.StatusCode/100 != 2 {
err = fmt.Errorf("server returned HTTP status %s", httpResp.Status) scanner := bufio.NewScanner(io.LimitReader(httpResp.Body, maxErrMsgLen))
line := ""
if scanner.Scan() {
line = scanner.Text()
}
err = fmt.Errorf("server returned HTTP status %s: %s", httpResp.Status, line)
} }
if httpResp.StatusCode/100 == 5 { if httpResp.StatusCode/100 == 5 {
return recoverableError{err} return recoverableError{err}

View file

@ -19,6 +19,7 @@ import (
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"reflect" "reflect"
"strings"
"testing" "testing"
"time" "time"
@ -26,6 +27,8 @@ import (
"github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/config"
) )
var longErrMessage = strings.Repeat("error message", maxErrMsgLen)
func TestStoreHTTPErrorHandling(t *testing.T) { func TestStoreHTTPErrorHandling(t *testing.T) {
tests := []struct { tests := []struct {
code int code int
@ -37,22 +40,22 @@ func TestStoreHTTPErrorHandling(t *testing.T) {
}, },
{ {
code: 300, code: 300,
err: fmt.Errorf("server returned HTTP status 300 Multiple Choices"), err: fmt.Errorf("server returned HTTP status 300 Multiple Choices: " + longErrMessage[:maxErrMsgLen]),
}, },
{ {
code: 404, code: 404,
err: fmt.Errorf("server returned HTTP status 404 Not Found"), err: fmt.Errorf("server returned HTTP status 404 Not Found: " + longErrMessage[:maxErrMsgLen]),
}, },
{ {
code: 500, code: 500,
err: recoverableError{fmt.Errorf("server returned HTTP status 500 Internal Server Error")}, err: recoverableError{fmt.Errorf("server returned HTTP status 500 Internal Server Error: " + longErrMessage[:maxErrMsgLen])},
}, },
} }
for i, test := range tests { for i, test := range tests {
server := httptest.NewServer( server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "test error", test.code) http.Error(w, longErrMessage, test.code)
}), }),
) )
@ -68,7 +71,7 @@ func TestStoreHTTPErrorHandling(t *testing.T) {
err = c.Store(nil) err = c.Store(nil)
if !reflect.DeepEqual(err, test.err) { if !reflect.DeepEqual(err, test.err) {
t.Fatalf("%d. Unexpected error; want %v, got %v", i, test.err, err) t.Errorf("%d. Unexpected error; want %v, got %v", i, test.err, err)
} }
server.Close() server.Close()