Produce INTERNAL_ERROR observation outcome

Prior to this commit, the `DefaultExecutionRequestObservationConvention`
would only produce "INTERNAL_ERROR" outcomes if the response is null or
if an unresolved exception remains.

The `ExceptionResolversExceptionHandler` will catch all unresolved
exceptions and add them to the errors map with the
`ErorType.INTERNAL_ERROR` error type. This means that the
"INTERNAL_ERROR" outcome is never used.

This commit ensures that this outcome is also used if at least one
resolved error is of `ErorType.INTERNAL_ERROR`.

Fixes gh-1058
This commit is contained in:
Brian Clozel
2024-10-09 12:14:18 +02:00
parent d3cd569e94
commit d7ed85045b
3 changed files with 17 additions and 4 deletions

View File

@@ -32,7 +32,11 @@ By default, the following KeyValues are created:
|===
The `graphql.operation` KeyValue will use the custom name of the provided query, or http://spec.graphql.org/draft/#sec-Language.Operations[the standard name for the operation] if none (`"query"`, `"mutation"` or `"subscription"`).
The `graphql.outcome` KeyValue will be `"SUCCESS"` if a valid GraphQL response has been sent, `"REQUEST_ERROR"` if the request could not be parsed, or `"INTERNAL_ERROR"` if no valid GraphQL response could be produced.
The `graphql.outcome` KeyValue will be:
* `"SUCCESS"` if a valid GraphQL response has been sent and it contains no errors
* `"REQUEST_ERROR"` if the request could not be parsed, or if the response contains errors (none of them being of type `org.springframework.graphql.execution.ErrorType.INTERNAL_ERROR`)
* `"INTERNAL_ERROR"` if no valid GraphQL response could be produced, or if the response contains at least one error of type `org.springframework.graphql.execution.ErrorType.INTERNAL_ERROR`
.High cardinality Keys
[cols="a,a"]

View File

@@ -19,6 +19,7 @@ package org.springframework.graphql.observation;
import io.micrometer.common.KeyValue;
import io.micrometer.common.KeyValues;
import org.springframework.graphql.execution.ErrorType;
import org.springframework.graphql.observation.GraphQlObservationDocumentation.ExecutionRequestHighCardinalityKeyNames;
import org.springframework.graphql.observation.GraphQlObservationDocumentation.ExecutionRequestLowCardinalityKeyNames;
@@ -73,7 +74,10 @@ public class DefaultExecutionRequestObservationConvention implements ExecutionRe
if (context.getError() != null || context.getExecutionResult() == null) {
return OUTCOME_INTERNAL_ERROR;
}
else if (context.getExecutionResult().getErrors().size() > 0) {
else if (!context.getExecutionResult().getErrors().isEmpty()) {
if (context.getExecutionResult().getErrors().stream().anyMatch((error) -> ErrorType.INTERNAL_ERROR.equals(error.getErrorType()))) {
return OUTCOME_INTERNAL_ERROR;
}
return OUTCOME_REQUEST_ERROR;
}
return OUTCOME_SUCCESS;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 the original author or authors.
* Copyright 2020-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,12 +19,16 @@ package org.springframework.graphql.observation;
import java.util.function.Consumer;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.ExecutionResultImpl;
import graphql.GraphQLError;
import graphql.execution.ExecutionId;
import graphql.schema.idl.errors.QueryOperationMissingError;
import io.micrometer.common.KeyValue;
import org.junit.jupiter.api.Test;
import org.springframework.graphql.execution.ErrorType;
import static org.assertj.core.api.Assertions.assertThat;
/**
@@ -86,7 +90,8 @@ class DefaultExecutionRequestObservationConventionTests {
void hasOutcomeKeyValueWhenInternalError() {
ExecutionRequestObservationContext context = createObservationContext(this.input, builder -> {
});
context.setError(new IllegalStateException("custom internal error"));
GraphQLError graphQLError = GraphQLError.newError().errorType(ErrorType.INTERNAL_ERROR).message(ErrorType.INTERNAL_ERROR + " for [executionId]").build();
context.setExecutionResult(ExecutionResult.newExecutionResult().addError(graphQLError).build());
assertThat(this.convention.getLowCardinalityKeyValues(context)).contains(KeyValue.of("graphql.outcome", "INTERNAL_ERROR"));
}