diff --git a/src/main/java/io/stargate/sgv2/jsonapi/service/processor/MeteredCommandProcessor.java b/src/main/java/io/stargate/sgv2/jsonapi/service/processor/MeteredCommandProcessor.java index 27fa864b02..c14d2bb02c 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/service/processor/MeteredCommandProcessor.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/service/processor/MeteredCommandProcessor.java @@ -13,6 +13,7 @@ import io.stargate.sgv2.jsonapi.api.v1.metrics.MetricsConfig; import io.stargate.sgv2.jsonapi.config.CommandLevelLoggingConfig; import io.stargate.sgv2.jsonapi.config.constants.DocumentConstants; +import io.stargate.sgv2.jsonapi.exception.APIException; import io.stargate.sgv2.jsonapi.metrics.ExceptionMetrics; import io.stargate.sgv2.jsonapi.service.schema.SchemaObject; import io.stargate.sgv2.jsonapi.util.ClassUtils; @@ -288,7 +289,7 @@ private Tags getCustomTags( // Determine the type of sorting used (if any), primarily for FindCommand. // NOTE: This logic might need refinement or replacement when CommandFeatures is fully // integrated, especially for FindAndRerankCommand. - JsonApiMetricsConfig.SortType sortType = getVectorTypeTag(commandContext, command); + JsonApiMetricsConfig.SortType sortType = sortTypeTag(commandContext, command); tags.add(Tag.of(jsonApiMetricsConfig.sortType(), sortType.name())); // --- Command Feature Usage Tags --- @@ -297,6 +298,24 @@ private Tags getCustomTags( return Tags.of(tags); } + /** + * Returns the sort type tag, or {@link JsonApiMetricsConfig.SortType#NONE} when the command's + * filter or sort clause is invalid. + * + *

The clauses are built again here from the request. A clause that was rejected while the + * command ran throws the same {@link APIException} again, after the processor has already turned + * it into the error result. Letting it escape would replace that result with a failed Uni and + * skip the timer. + */ + private JsonApiMetricsConfig.SortType sortTypeTag( + CommandContext commandContext, Command command) { + try { + return getVectorTypeTag(commandContext, command); + } catch (APIException e) { + return JsonApiMetricsConfig.SortType.NONE; + } + } + /** * Determines the {@link JsonApiMetricsConfig.SortType} of sorting used in the command based on * the command's sort clause. Primarily intended for vector-based sorting. diff --git a/src/test/java/io/stargate/sgv2/jsonapi/service/processor/MeteredCommandProcessorTest.java b/src/test/java/io/stargate/sgv2/jsonapi/service/processor/MeteredCommandProcessorTest.java index fbda3ec592..6406fe1092 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/service/processor/MeteredCommandProcessorTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/service/processor/MeteredCommandProcessorTest.java @@ -2,6 +2,7 @@ import static io.restassured.RestAssured.given; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowableOfType; import com.fasterxml.jackson.databind.ObjectMapper; import io.quarkus.test.InjectMock; @@ -13,9 +14,11 @@ import io.stargate.sgv2.jsonapi.api.model.command.CommandResult; import io.stargate.sgv2.jsonapi.api.model.command.impl.CountDocumentsCommand; import io.stargate.sgv2.jsonapi.api.model.command.impl.FindCommand; +import io.stargate.sgv2.jsonapi.api.model.command.impl.FindOneCommand; import io.stargate.sgv2.jsonapi.api.model.command.tracing.RequestTracing; import io.stargate.sgv2.jsonapi.api.request.RequestContext; import io.stargate.sgv2.jsonapi.api.request.tenant.TenantFactory; +import io.stargate.sgv2.jsonapi.exception.FilterException; import io.stargate.sgv2.jsonapi.exception.ServerException; import io.stargate.sgv2.jsonapi.service.schema.collections.CollectionSchemaObject; import io.stargate.sgv2.jsonapi.testresource.NoGlobalResourcesTestProfile; @@ -104,12 +107,12 @@ public void metrics() throws Exception { public void errorMetricsWithNoErrorCode() throws Exception { String json = """ - { - "find": { + { + "find": { + } } - } - """; + """; FindCommand countCommand = objectMapper.readValue(json, FindCommand.class); @@ -219,5 +222,70 @@ public void errorMetrics() throws Exception { }); }); } + + /** + * The processor builds the filter and sort clauses again to choose the sort type tag. A clause + * that failed during the command fails again here, and that second failure must not replace the + * error result. + */ + @Test + public void errorMetricsWithUnsupportedFilterOperator() throws Exception { + String json = + """ + { + "findOne": { + "filter": {"year": {"$bogus": 1}} + } + } + """; + + FindOneCommand findOneCommand = objectMapper.readValue(json, FindOneCommand.class); + + // the same failure the command processor recovers into the error result + var exception = + catchThrowableOfType( + FilterException.class, () -> findOneCommand.filterClause(commandContext)); + assertThat(exception).isNotNull(); + var commandResult = + CommandResult.statusOnlyBuilder(RequestTracing.NO_OP).addThrowable(exception).build(); + + Mockito.when(commandProcessor.processCommand(commandContext, findOneCommand)) + .thenReturn(Uni.createFrom().item(commandResult)); + + var actualResult = + meteredCommandProcessor + .processCommand(commandContext, findOneCommand) + .await() + .atMost(Duration.ofMinutes(1)); + assertThat(actualResult).isSameAs(commandResult); + + // same timing problem as errorMetrics() + Thread.sleep(1000); + + String metrics = given().when().get("/metrics").then().statusCode(200).extract().asString(); + List httpMetrics = + metrics + .lines() + .filter( + line -> + line.startsWith("command_processor_process_") + && !line.startsWith("command_processor_process_seconds_bucket") + && !line.contains("quantile") + && line.contains("command=\"FindOneCommand\"")) + .toList(); + + assertThat(httpMetrics) + .satisfies( + lines -> { + assertThat(lines.size()).isEqualTo(3); + lines.forEach( + line -> { + assertThat(line).contains("error=\"true\""); + assertThat(line) + .contains("error_code=\"" + exception.fullyQualifiedCode() + "\""); + assertThat(line).contains("sort_type=\"NONE\""); + }); + }); + } } }