Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit 9577fd0

Browse files
committed
Relax response status code policy
The remote-write specification doesn't define the expected status code. It extends the policy to all the successful status codes because it can't make assumptions. There are so many implementations and the protocol doesn't limit them to return any code. e.g. Mimir uses a 200 OK as response status code.
1 parent b72ddfb commit 9577fd0

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

pkg/remote/client.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ func (c *WriteClient) Store(ctx context.Context, series []*prompb.TimeSeries) er
9292
defer resp.Body.Close()
9393
io.Copy(io.Discard, resp.Body) //nolint:errcheck
9494

95-
if resp.StatusCode != http.StatusNoContent {
96-
return fmt.Errorf("got status code: %s instead expected: 204 No Content", resp.Status)
97-
}
98-
return nil
95+
return validateResponseStatus(resp.StatusCode)
9996
}
10097

10198
func newWriteRequestBody(series []*prompb.TimeSeries) ([]byte, error) {
@@ -111,3 +108,11 @@ func newWriteRequestBody(series []*prompb.TimeSeries) ([]byte, error) {
111108
}
112109
return snappy.Encode(nil, b), nil
113110
}
111+
112+
func validateResponseStatus(code int) error {
113+
if code >= http.StatusOK && code < 300 {
114+
return nil
115+
}
116+
117+
return fmt.Errorf("got status code: %d instead expected a 2xx successful status code", code)
118+
}

pkg/remote/client_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func TestClientStoreHTTPBasic(t *testing.T) {
128128
},
129129
},
130130
}
131-
assert.Error(t, c.Store(context.Background(), nil))
131+
assert.NoError(t, c.Store(context.Background(), nil))
132132
}
133133

134134
func TestClientStoreHeaders(t *testing.T) {
@@ -155,7 +155,7 @@ func TestClientStoreHeaders(t *testing.T) {
155155
}),
156156
},
157157
}
158-
assert.Error(t, c.Store(context.Background(), nil))
158+
assert.NoError(t, c.Store(context.Background(), nil))
159159
}
160160

161161
func TestNewWriteRequestBody(t *testing.T) {
@@ -170,3 +170,23 @@ func TestNewWriteRequestBody(t *testing.T) {
170170
require.NotEmpty(t, string(b))
171171
assert.Contains(t, string(b), `label1`)
172172
}
173+
174+
func TestValidateStatusCode(t *testing.T) {
175+
t.Parallel()
176+
tests := []struct {
177+
status int
178+
expErr bool
179+
}{
180+
{status: http.StatusOK, expErr: false}, // Mimir
181+
{status: http.StatusNoContent, expErr: false}, // Prometheus
182+
{status: http.StatusBadRequest, expErr: true},
183+
}
184+
for _, tt := range tests {
185+
err := validateResponseStatus(tt.status)
186+
if tt.expErr {
187+
assert.Error(t, err)
188+
continue
189+
}
190+
assert.NoError(t, err)
191+
}
192+
}

0 commit comments

Comments
 (0)