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

Distributor: improve wrapping of gRPC errors returned by the ingester #6426

Merged
merged 1 commit into from
Oct 18, 2023
Merged
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
6 changes: 6 additions & 0 deletions pkg/distributor/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,12 @@ func handleIngesterPushError(err error) error {
// Wrap HTTP gRPC error with more explanatory message.
return httpgrpc.Errorf(int(resp.Code), "failed pushing to ingester: %s", resp.Body)
}
stat, ok := status.FromError(err)
if ok {
st := stat.Proto()
st.Message = fmt.Sprintf("failed pushing to ingester: %s", st.Message)
return status.ErrorProto(st)
}
return errors.Wrap(err, "failed pushing to ingester")
}

Expand Down
46 changes: 35 additions & 11 deletions pkg/distributor/distributor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4785,10 +4785,10 @@ func TestHandleIngesterPushError(t *testing.T) {
outputErrorMsgPrefix := "failed pushing to ingester"
userID := "test"
errWithUserID := fmt.Errorf("user=%s: %s", userID, testErrorMsg)
unavailableErr := status.New(codes.Unavailable, testErrorMsg).Err()
test := map[string]struct {
ingesterPushError error
expectedOutputError error
checkDetails bool
}{
"no error gives no error": {
ingesterPushError: nil,
Expand All @@ -4797,32 +4797,49 @@ func TestHandleIngesterPushError(t *testing.T) {
"a 4xx HTTP gRPC error gives a 4xx HTTP gRPC error": {
ingesterPushError: httpgrpc.Errorf(http.StatusBadRequest, testErrorMsg),
expectedOutputError: httpgrpc.Errorf(http.StatusBadRequest, "%s: %s", outputErrorMsgPrefix, testErrorMsg),
checkDetails: true,
},
"a 5xx HTTP gRPC error gives a 5xx HTTP gRPC error": {
ingesterPushError: httpgrpc.Errorf(http.StatusServiceUnavailable, testErrorMsg),
expectedOutputError: httpgrpc.Errorf(http.StatusServiceUnavailable, "%s: %s", outputErrorMsgPrefix, testErrorMsg),
checkDetails: true,
},
"a random ingester error without status gives the same wrapped error": {
ingesterPushError: errWithUserID,
expectedOutputError: errors.Wrap(errWithUserID, outputErrorMsgPrefix),
},
"a gRPC unavailable error gives the same wrapped error": {
ingesterPushError: unavailableErr,
expectedOutputError: errors.Wrap(unavailableErr, outputErrorMsgPrefix),
"a gRPC unavailable error gives a gRPC unavailable error with a wrapped message": {
ingesterPushError: createGRPCErrorWithDetails(t, codes.Unavailable, testErrorMsg, mimirpb.INVALID),
expectedOutputError: createGRPCErrorWithDetails(t, codes.Unavailable, fmt.Sprintf("%s: %s", outputErrorMsgPrefix, testErrorMsg), mimirpb.INVALID),
checkDetails: true,
},
"a context cancel error gives the same wrapped error": {
ingesterPushError: context.Canceled,
expectedOutputError: errors.Wrap(context.Canceled, outputErrorMsgPrefix),
},
}

for _, testData := range test {
err := handleIngesterPushError(testData.ingesterPushError)
if testData.expectedOutputError == nil {
require.NoError(t, err)
} else {
require.ErrorContains(t, err, testData.expectedOutputError.Error())
}
for testName, testData := range test {
t.Run(testName, func(t *testing.T) {
err := handleIngesterPushError(testData.ingesterPushError)
if testData.expectedOutputError == nil {
require.NoError(t, err)
} else {
if testData.checkDetails {
expectedStat, ok := status.FromError(testData.expectedOutputError)
require.True(t, ok)

stat, ok := status.FromError(err)
require.True(t, ok)

require.Equal(t, expectedStat.Code(), stat.Code())
require.Equal(t, expectedStat.Message(), stat.Message())
require.Equal(t, expectedStat.Details(), stat.Details())
} else {
require.Errorf(t, err, testData.expectedOutputError.Error())
}
}
})
}
}

Expand Down Expand Up @@ -4924,3 +4941,10 @@ func checkGRPCError(t *testing.T, expectedStatus *status.Status, expectedDetails
require.Equal(t, expectedDetails, errorDetails)
}
}

func createGRPCErrorWithDetails(t *testing.T, code codes.Code, message string, cause mimirpb.ErrorCause) error {
stat := status.New(code, message)
statWithDetails, err := stat.WithDetails(&mimirpb.WriteErrorDetails{Cause: cause})
require.NoError(t, err)
return statWithDetails.Err()
}
Loading