This is an automated email from the ASF dual-hosted git repository.

lizhimins pushed a commit to branch rocketmq-studio
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git


The following commit(s) were added to refs/heads/rocketmq-studio by this push:
     new a92c23144 fix(settings): resolve data source type aliases to their own 
backend probe (#6489)
a92c23144 is described below

commit a92c23144408cdf5729707050e9ecd9b75f1bad0
Author: 0 <[email protected]>
AuthorDate: Thu Oct 15 20:30:48 2026 +0800

    fix(settings): resolve data source type aliases to their own backend probe 
(#6489)
    
    * fix(settings): resolve data source type aliases to their own backend probe
    
    * refactor(settings): drop dead isPrometheusCompatible and cover the 
victoria alias
    
    testDataSource now resolves the backend through resolveBackendType directly,
    so isPrometheusCompatible is left with no caller anywhere in main or test.
    Checkstyle does not flag unused private methods, so it would have stayed.
    
    Also widen the VictoriaMetrics alias case into a @ParameterizedTest over the
    five spellings #6468 listed plus the bare "victoria". That last one is the
    only behaviour this change adds over #6468 and nothing failed when the
    "VICTORIA" case label was removed; assert the success message as well, not
    just isSuccess().
    
    ---------
    
    Co-authored-by: lizhimins <[email protected]>
---
 .../rocketmq/studio/settings/SettingsService.java  | 35 +++++++++++++-------
 .../studio/settings/SettingsServiceTest.java       | 38 ++++++++++++++++++++++
 2 files changed, 62 insertions(+), 11 deletions(-)

diff --git 
a/server/src/main/java/org/apache/rocketmq/studio/settings/SettingsService.java 
b/server/src/main/java/org/apache/rocketmq/studio/settings/SettingsService.java
index 2cbe056cd..a9a603ae7 100644
--- 
a/server/src/main/java/org/apache/rocketmq/studio/settings/SettingsService.java
+++ 
b/server/src/main/java/org/apache/rocketmq/studio/settings/SettingsService.java
@@ -50,7 +50,6 @@ import java.time.Duration;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Locale;
-import java.util.Set;
 import java.util.function.UnaryOperator;
 
 @Slf4j
@@ -68,8 +67,6 @@ public class SettingsService {
                 0x00, 0x00, 0x02, 0x54
             }, // AWS IMDS IPv6: fd00:ec2::254
             new byte[] {100, 100, 100, (byte) 200}); // Alibaba Cloud ECS 
metadata
-    private static final Set<String> PROMETHEUS_COMPATIBLE_TYPES = Set.of(
-            "prometheus", "victoriametrics", "thanos", "mimir", "cortex", 
"arms");
     private static final String PROMETHEUS_TEST_QUERY = "up";
     private static final String AUTH_NONE = "none";
     private static final String AUTH_BASIC = "basic auth";
@@ -317,13 +314,14 @@ public class SettingsService {
         if (request == null) {
             return failed("Data source test request is required");
         }
-        if (!isPrometheusCompatible(request.getType())) {
+        MetricsBackendType backend = resolveBackendType(request.getType());
+        if (backend == null) {
             return failed("Unsupported data source type: " + 
request.getType());
         }
 
         try {
             String body = restClient.get()
-                    .uri(prometheusQueryUri(request.getUrl(), 
request.getType()))
+                    .uri(prometheusQueryUri(request.getUrl(), backend))
                     .accept(MediaType.APPLICATION_JSON)
                     .headers(headers -> applyAuthentication(headers, request))
                     .retrieve()
@@ -346,10 +344,25 @@ public class SettingsService {
         }
     }
 
-    private boolean isPrometheusCompatible(String type) {
-        return StringUtils.hasText(type)
-                && PROMETHEUS_COMPATIBLE_TYPES.contains(
-                        type.replaceAll("\\s+", "").toLowerCase(Locale.ROOT));
+    /**
+     * Normalises a client-supplied data source type to its backend. The DTO 
accepts aliases the
+     * raw enum spelling does not carry - victoria_metrics, victoria-metrics, 
victoria metrics and
+     * VictoriaMetrics all name the same backend - and unknown types must stay 
rejected rather than
+     * silently falling back to Prometheus. Custom is not 
prometheus-compatible for this probe.
+     */
+    private MetricsBackendType resolveBackendType(String type) {
+        if (!StringUtils.hasText(type)) {
+            return null;
+        }
+        return switch (type.trim().replaceAll("[\\s_-]+", 
"").toUpperCase(Locale.ROOT)) {
+            case "PROMETHEUS" -> MetricsBackendType.PROMETHEUS;
+            case "VICTORIAMETRICS", "VICTORIA" -> 
MetricsBackendType.VICTORIA_METRICS;
+            case "THANOS" -> MetricsBackendType.THANOS;
+            case "CORTEX" -> MetricsBackendType.CORTEX;
+            case "MIMIR" -> MetricsBackendType.MIMIR;
+            case "ARMS" -> MetricsBackendType.ARMS;
+            default -> null;
+        };
     }
 
     private String normalizeDataSourceKey(String key) {
@@ -388,7 +401,7 @@ public class SettingsService {
         return auth.trim().replaceAll("\\s+", " ").toLowerCase(Locale.ROOT);
     }
 
-    private URI prometheusQueryUri(String baseUrl, String providerType) throws 
URISyntaxException {
+    private URI prometheusQueryUri(String baseUrl, MetricsBackendType backend) 
throws URISyntaxException {
         if (!StringUtils.hasText(baseUrl)) {
             throw new IllegalArgumentException("Data source URL is required");
         }
@@ -405,7 +418,7 @@ public class SettingsService {
             throw new IllegalArgumentException(
                     "Data source URL must not point to a local or private 
address");
         }
-        String queryPath = 
MetricsBackendType.fromProviderType(providerType).getInstantQueryPath();
+        String queryPath = backend.getInstantQueryPath();
         return UriComponentsBuilder.fromUriString(normalized + queryPath)
                 .queryParam("query", PROMETHEUS_TEST_QUERY)
                 .build()
diff --git 
a/server/src/test/java/org/apache/rocketmq/studio/settings/SettingsServiceTest.java
 
b/server/src/test/java/org/apache/rocketmq/studio/settings/SettingsServiceTest.java
index 60fd940e2..3344906bc 100644
--- 
a/server/src/test/java/org/apache/rocketmq/studio/settings/SettingsServiceTest.java
+++ 
b/server/src/test/java/org/apache/rocketmq/studio/settings/SettingsServiceTest.java
@@ -25,6 +25,8 @@ import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 import org.springframework.http.HttpHeaders;
@@ -62,6 +64,7 @@ class SettingsServiceTest {
 
     private static final String PROMETHEUS_BASE_URL = "http://192.0.2.1:9090";;
     private static final String PROMETHEUS_QUERY_URL = PROMETHEUS_BASE_URL + 
"/api/v1/query?query=up";
+    private static final String VICTORIA_QUERY_URL = PROMETHEUS_BASE_URL + 
"/select/0/prometheus/api/v1/query?query=up";
     private static final String VICTORIA_METRICS_QUERY_URL =
             PROMETHEUS_BASE_URL + "/select/0/prometheus/api/v1/query?query=up";
     private static final String MIMIR_QUERY_URL =
@@ -523,6 +526,41 @@ class SettingsServiceTest {
         assertThat(result.getMessage()).isEqualTo("Connection successful");
     }
 
+    @ParameterizedTest
+    @ValueSource(strings = {
+        "victoria_metrics", " VICTORIA_METRICS ", "victoria-metrics", 
"VictoriaMetrics",
+        "Victoria Metrics", "victoria"})
+    void 
connectionShouldResolveVictoriaMetricsAliasesToItsOwnQueryPathTest(String type) 
{
+        // The DTO accepts every alias spelling of VictoriaMetrics, including 
the bare "victoria"
+        // short form; the probe must reach VictoriaMetrics' own query path 
for all of them rather
+        // than fall back to Prometheus'.
+        prometheusServer.expect(requestTo(VICTORIA_QUERY_URL))
+                .andExpect(method(HttpMethod.GET))
+                .andRespond(withSuccess(PROMETHEUS_SUCCESS_BODY, 
MediaType.APPLICATION_JSON));
+        DataSourceTestDTO request = DataSourceTestDTO.builder()
+                .url(PROMETHEUS_BASE_URL)
+                .type(type)
+                .build();
+
+        DataSourceTestResultVO result = 
settingsService.testDataSource(request);
+
+        assertThat(result.isSuccess()).isTrue();
+        assertThat(result.getMessage()).isEqualTo("Connection successful");
+    }
+
+    @Test
+    void connectionShouldStillRejectUnknownDataSourceTypesTest() {
+        DataSourceTestDTO request = DataSourceTestDTO.builder()
+                .url(PROMETHEUS_BASE_URL)
+                .type("grafana-metrics")
+                .build();
+
+        DataSourceTestResultVO result = 
settingsService.testDataSource(request);
+
+        assertThat(result.isSuccess()).isFalse();
+        assertThat(result.getMessage()).contains("Unsupported data source 
type: grafana-metrics");
+    }
+
     @Test
     void connectionShouldApplyBasicAuthenticationTest() {
         String expectedAuthorization = "Basic "

Reply via email to