Skip to content

Commit df9b749

Browse files
committed
Use 202 Accepted for SetOverrides and buffer GetOverrides response
SetOverrides writes to the object storage bucket like the ruler API, so it should return 202 Accepted to indicate the change has been stored Also buffer the GetOverrides JSON response so http.Error works correctly if encoding fails. Signed-off-by: Friedrich Gonzalez <[email protected]>
1 parent 25e8d7b commit df9b749

3 files changed

Lines changed: 12 additions & 6 deletions

File tree

docs/guides/overrides.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ Result:
197197
}
198198
```
199199

200-
**Response (200 OK):**
200+
**Response (202 Accepted):**
201201
Returns success with no body.
202202

203203
**Response (400 Bad Request):**

pkg/overrides/api.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ func (a *API) GetOverrides(w http.ResponseWriter, r *http.Request) {
7171
http.Error(w, "not found", http.StatusNotFound)
7272
return
7373
}
74-
w.Header().Set("Content-Type", "application/json")
75-
if err := json.NewEncoder(w).Encode(overrides); err != nil {
74+
75+
// Buffer the response before writing headers so http.Error works if encoding fails
76+
var buf bytes.Buffer
77+
if err := json.NewEncoder(&buf).Encode(overrides); err != nil {
7678
level.Error(a.logger).Log("msg", "failed to encode overrides response", "err", err)
7779
http.Error(w, "Internal server error", http.StatusInternalServerError)
7880
return
7981
}
82+
w.Header().Set("Content-Type", "application/json")
83+
if _, err := w.Write(buf.Bytes()); err != nil {
84+
level.Error(a.logger).Log("msg", "failed to write overrides response", "err", err)
85+
}
8086
}
8187

8288
// SetOverrides updates overrides for a specific tenant
@@ -122,7 +128,7 @@ func (a *API) SetOverrides(w http.ResponseWriter, r *http.Request) {
122128
return
123129
}
124130

125-
w.WriteHeader(http.StatusOK)
131+
w.WriteHeader(http.StatusAccepted)
126132
}
127133

128134
// DeleteOverrides removes tenant-specific overrides

pkg/overrides/overrides_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func TestAPIEndpoints(t *testing.T) {
234234
path: "/api/v1/user-overrides",
235235
tenantID: "user789",
236236
requestBody: map[string]any{"ingestion_rate": 5000, "ruler_max_rules_per_rule_group": 10},
237-
expectedStatus: http.StatusOK,
237+
expectedStatus: http.StatusAccepted,
238238
setupMock: func(mock *bucket.ClientMock) {
239239
// Mock runtime config with allowed limits
240240
runtimeConfig := `overrides:
@@ -288,7 +288,7 @@ api_allowed_limits:
288288
path: "/api/v1/user-overrides",
289289
tenantID: "user888",
290290
requestBody: map[string]any{"ingestion_rate": 8000}, // Only update ingestion_rate
291-
expectedStatus: http.StatusOK,
291+
expectedStatus: http.StatusAccepted,
292292
setupMock: func(m *bucket.ClientMock) {
293293
// Mock runtime config with existing overrides for user888
294294
initialConfig := `overrides:

0 commit comments

Comments
 (0)