Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -288,7 +289,7 @@ private <T extends SchemaObject> 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 ---
Expand All @@ -297,6 +298,24 @@ private <T extends SchemaObject> 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.
*
* <p>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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<String> 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\"");
});
});
}
}
}