From 47a096a0364bc5792acf829a938f945aae53c8dc Mon Sep 17 00:00:00 2001 From: Yuqi Du Date: Tue, 24 Oct 2023 19:59:10 -0700 Subject: [PATCH] test --- .../jsonapi/exception/DebugModeConfig.java | 14 +++++++++++ .../jsonapi/exception/JsonApiException.java | 14 ++++++----- .../mappers/ThrowableToErrorMapper.java | 25 ++++++++++++++++--- .../PropertyBasedEmbeddingServiceConfig.java | 3 +++ src/main/resources/application.yaml | 4 +++ .../jsonapi/testresource/DseTestResource.java | 4 +-- src/test/resources/application.yaml | 10 +++++--- 7 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 src/main/java/io/stargate/sgv2/jsonapi/exception/DebugModeConfig.java diff --git a/src/main/java/io/stargate/sgv2/jsonapi/exception/DebugModeConfig.java b/src/main/java/io/stargate/sgv2/jsonapi/exception/DebugModeConfig.java new file mode 100644 index 0000000000..3ff517cccb --- /dev/null +++ b/src/main/java/io/stargate/sgv2/jsonapi/exception/DebugModeConfig.java @@ -0,0 +1,14 @@ +package io.stargate.sgv2.jsonapi.exception; + +//import io.quarkus.runtime.annotations.StaticInitSafe; +import io.smallrye.config.ConfigMapping; +import io.smallrye.config.WithDefault; + + +//@StaticInitSafe +@ConfigMapping(prefix = "stargate.haha") +public interface DebugModeConfig { + + @WithDefault("false") + boolean enabled(); +} diff --git a/src/main/java/io/stargate/sgv2/jsonapi/exception/JsonApiException.java b/src/main/java/io/stargate/sgv2/jsonapi/exception/JsonApiException.java index 2bdc35c4c5..f137afce9d 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/exception/JsonApiException.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/exception/JsonApiException.java @@ -1,13 +1,14 @@ package io.stargate.sgv2.jsonapi.exception; +import io.quarkus.logging.Log; +import io.smallrye.config.SmallRyeConfig; import io.stargate.sgv2.jsonapi.api.model.command.CommandResult; import io.stargate.sgv2.jsonapi.exception.mappers.ThrowableToErrorMapper; import jakarta.ws.rs.core.Response; import java.util.List; import java.util.Map; import java.util.function.Supplier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.eclipse.microprofile.config.ConfigProvider; /** * Our own {@link RuntimeException} that uses {@link ErrorCode} to describe the exception cause. @@ -17,9 +18,6 @@ * directly. */ public class JsonApiException extends RuntimeException implements Supplier { - - private static final Logger LOGGER = LoggerFactory.getLogger(JsonApiException.class); - private final ErrorCode errorCode; public JsonApiException(ErrorCode errorCode) { @@ -64,7 +62,11 @@ public CommandResult get() { public CommandResult.Error getCommandResultError(String message) { Map fields = null; // only have exceptionClass in debug mode - if (LOGGER.isDebugEnabled()) { + Log.error("??2"); + SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class); + DebugModeConfig debugModeConfig = config.getConfigMapping(DebugModeConfig.class); + final boolean debugEnabled = debugModeConfig.enabled(); + if (debugEnabled) { fields = Map.of("errorCode", errorCode.name(), "exceptionClass", this.getClass().getSimpleName()); } else { diff --git a/src/main/java/io/stargate/sgv2/jsonapi/exception/mappers/ThrowableToErrorMapper.java b/src/main/java/io/stargate/sgv2/jsonapi/exception/mappers/ThrowableToErrorMapper.java index dcd5bc61aa..d6e3c58590 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/exception/mappers/ThrowableToErrorMapper.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/exception/mappers/ThrowableToErrorMapper.java @@ -2,25 +2,42 @@ import io.grpc.Status; import io.grpc.StatusRuntimeException; +import io.quarkus.logging.Log; +import io.smallrye.config.SmallRyeConfig; import io.stargate.sgv2.jsonapi.api.model.command.CommandResult; +import io.stargate.sgv2.jsonapi.api.v1.metrics.JsonApiMetricsConfig; +import io.stargate.sgv2.jsonapi.exception.DebugModeConfig; import io.stargate.sgv2.jsonapi.exception.JsonApiException; +import io.stargate.sgv2.jsonapi.service.embedding.configuration.PropertyBasedEmbeddingServiceConfig; import jakarta.ws.rs.core.Response; import java.util.Map; import java.util.function.BiFunction; import java.util.function.Function; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.eclipse.microprofile.config.ConfigProvider; /** * Simple mapper for mapping {@link Throwable}s to {@link CommandResult.Error}, with a default * implementation. */ public final class ThrowableToErrorMapper { - private static final Logger LOGGER = LoggerFactory.getLogger(ThrowableToErrorMapper.class); private static final BiFunction MAPPER_WITH_MESSAGE = (throwable, message) -> { - final boolean debugEnabled = LOGGER.isDebugEnabled(); + SmallRyeConfig c1 = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class); + PropertyBasedEmbeddingServiceConfig hah = + c1.getConfigMapping(PropertyBasedEmbeddingServiceConfig.class); + Log.error(hah.enabled()); + + SmallRyeConfig c2 = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class); + JsonApiMetricsConfig hah1 = + c2.getConfigMapping(JsonApiMetricsConfig.class); + Log.error(hah1.errorClass()); + Log.error("??1"); + + + SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class); + DebugModeConfig debugModeConfig = config.getConfigMapping(DebugModeConfig.class); + final boolean debugEnabled = debugModeConfig.enabled(); // if our own exception, shortcut if (throwable instanceof JsonApiException jae) { return jae.getCommandResultError(message); diff --git a/src/main/java/io/stargate/sgv2/jsonapi/service/embedding/configuration/PropertyBasedEmbeddingServiceConfig.java b/src/main/java/io/stargate/sgv2/jsonapi/service/embedding/configuration/PropertyBasedEmbeddingServiceConfig.java index 2f538696c0..3235e03a2b 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/service/embedding/configuration/PropertyBasedEmbeddingServiceConfig.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/service/embedding/configuration/PropertyBasedEmbeddingServiceConfig.java @@ -11,6 +11,9 @@ @ConfigMapping(prefix = "stargate.jsonapi.embedding.service") public interface PropertyBasedEmbeddingServiceConfig { + @WithDefault("false") + boolean enabled(); + @Nullable OpenaiConfig openai(); diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 685c865756..5ea191dc06 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -3,6 +3,9 @@ stargate: + haha: + enabled: false + # disable all sgv2 exception mappers, handled differently exception-mappers: enabled: false @@ -33,6 +36,7 @@ stargate: # This can be `property` or `in-memory` to store Embedding service configuration store: property service: + enabled: true # Open AI embedding service configuration openai: enabled: false diff --git a/src/test/java/io/stargate/sgv2/jsonapi/testresource/DseTestResource.java b/src/test/java/io/stargate/sgv2/jsonapi/testresource/DseTestResource.java index db90161b24..9912111e7b 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/testresource/DseTestResource.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/testresource/DseTestResource.java @@ -30,8 +30,8 @@ public DseTestResource() { } // DEBUG mode for Integration Test - if (null == System.getProperty("quarkus.log.level")) { - System.setProperty("quarkus.log.level", "DEBUG"); + if (null == System.getProperty("stargate.debug.enabled")) { + System.setProperty("stargate.debug.enabled", "true"); } } diff --git a/src/test/resources/application.yaml b/src/test/resources/application.yaml index 296bede41f..11b9024fbe 100644 --- a/src/test/resources/application.yaml +++ b/src/test/resources/application.yaml @@ -4,10 +4,14 @@ stargate: data-store: ignore-bridge: true +# DEBUG mode for unit tests + debug: + enabled: true + # change test port from 8081 (used by other SG services) quarkus: http: test-port: 9080 - log: -# DEBUG mode for unit tests - level: DEBUG \ No newline at end of file +# log: +## DEBUG mode for unit tests +# level: DEBUG \ No newline at end of file