Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Flapping addon staus due to 409 response on remote write to the hub #1777

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion collectors/metrics/pkg/forwarder/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,13 @@ func (w *Worker) forward(ctx context.Context) error {

req := &http.Request{Method: "POST", URL: w.to}
if err := w.toClient.RemoteWrite(ctx, req, families, w.interval); err != nil {
updateStatus(statuslib.ForwardFailed, "Failed to send metrics")
var httpError *metricsclient.HTTPError
// Avoid degrading the status on 409
if errors.As(err, &httpError) && httpError.StatusCode == http.StatusConflict {
updateStatus(statuslib.ForwardSuccessful, "Cluster metrics sent successfully")
} else {
updateStatus(statuslib.ForwardFailed, "Failed to send metrics")
}
return err
}

Expand Down
14 changes: 13 additions & 1 deletion collectors/metrics/pkg/metricsclient/metricsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ const (
maxSeriesLength = 10000
)

type HTTPError struct {
StatusCode int
Message string
}

func (e *HTTPError) Error() string {
return fmt.Sprintf("HTTP %d: %s", e.StatusCode, e.Message)
}

type Client struct {
client *http.Client
maxBytes int64
Expand Down Expand Up @@ -555,7 +564,10 @@ func (c *Client) sendRequest(ctx context.Context, serverURL string, body []byte)
logger.Log(c.logger, logger.Warn, err)
}

retErr := fmt.Errorf("response status code is %s, response body is %s", resp.Status, string(bodyBytes))
retErr := &HTTPError{
StatusCode: resp.StatusCode,
Message: string(bodyBytes),
}

if isTransientResponseError(resp) {
return retErr
Expand Down
4 changes: 4 additions & 0 deletions collectors/metrics/pkg/metricsclient/metricsclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ func TestClient_RemoteWrite(t *testing.T) {
expect: func(t *testing.T, err error, retryCount int) {
assert.Error(t, err)
assert.Equal(t, 1, retryCount)
// Ensure the http error is wrapped
var httpError *HTTPError
assert.ErrorAs(t, err, &httpError)
assert.Equal(t, http.StatusConflict, httpError.StatusCode)
},
},
}
Expand Down
Loading