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

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 82fae3f068a Add test cases for agent-metrics-prometheus module (#37701)
82fae3f068a is described below

commit 82fae3f068ab358c70e0ddb8a8c7197d005f2daa
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Jan 10 17:28:51 2026 +0800

    Add test cases for agent-metrics-prometheus module (#37701)
    
    * Add SQL parser test cases for Hive
    
    * Add test cases for agent-metrics-prometheus module
---
 .../PrometheusMetricsCounterCollectorTest.java     | 17 +++++-
 .../type/PrometheusMetricsGaugeCollectorTest.java  | 31 +++++++++-
 .../PrometheusMetricsHistogramCollectorTest.java   | 66 +++++++++++++++++++++-
 .../exoprter/PrometheusMetricsExporterTest.java    | 17 +++++-
 4 files changed, 120 insertions(+), 11 deletions(-)

diff --git 
a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/collector/type/PrometheusMetricsCounterCollectorTest.java
 
b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/collector/type/PrometheusMetricsCounterCollectorTest.java
index 1bc601b0ffb..1407335f808 100644
--- 
a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/collector/type/PrometheusMetricsCounterCollectorTest.java
+++ 
b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/collector/type/PrometheusMetricsCounterCollectorTest.java
@@ -31,11 +31,22 @@ import static org.hamcrest.MatcherAssert.assertThat;
 class PrometheusMetricsCounterCollectorTest {
     
     @Test
-    void assertCreate() throws ReflectiveOperationException {
-        PrometheusMetricsCounterCollector collector = new 
PrometheusMetricsCounterCollector(new MetricConfiguration("foo_counter",
+    void assertInc() throws ReflectiveOperationException {
+        PrometheusMetricsCounterCollector collector = new 
PrometheusMetricsCounterCollector(new MetricConfiguration("counter_inc",
                 MetricCollectorType.COUNTER, "foo_help", 
Collections.emptyList(), Collections.emptyMap()));
         collector.inc();
         Counter counter = (Counter) 
Plugins.getMemberAccessor().get(PrometheusMetricsCounterCollector.class.getDeclaredField("counter"),
 collector);
-        assertThat(counter.get(), is(1D));
+        double actualValue = counter.get();
+        double expectedValue = 1D;
+        assertThat(actualValue, is(expectedValue));
+    }
+    
+    @Test
+    void assertIncWithLabels() throws ReflectiveOperationException {
+        PrometheusMetricsCounterCollector collector = new 
PrometheusMetricsCounterCollector(new MetricConfiguration("counter_inc_label",
+                MetricCollectorType.COUNTER, "foo_help", 
Collections.singletonList("type"), Collections.emptyMap()));
+        collector.inc("bar");
+        Counter counter = (Counter) 
Plugins.getMemberAccessor().get(PrometheusMetricsCounterCollector.class.getDeclaredField("counter"),
 collector);
+        assertThat(counter.labels("bar").get(), is(1D));
     }
 }
diff --git 
a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/collector/type/PrometheusMetricsGaugeCollectorTest.java
 
b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/collector/type/PrometheusMetricsGaugeCollectorTest.java
index 8eaceadef39..523ac120100 100644
--- 
a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/collector/type/PrometheusMetricsGaugeCollectorTest.java
+++ 
b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/collector/type/PrometheusMetricsGaugeCollectorTest.java
@@ -31,13 +31,38 @@ import static org.hamcrest.MatcherAssert.assertThat;
 class PrometheusMetricsGaugeCollectorTest {
     
     @Test
-    void assertCreate() throws ReflectiveOperationException {
-        PrometheusMetricsGaugeCollector collector = new 
PrometheusMetricsGaugeCollector(new MetricConfiguration("foo_gauge",
+    void assertInc() throws ReflectiveOperationException {
+        PrometheusMetricsGaugeCollector collector = new 
PrometheusMetricsGaugeCollector(new MetricConfiguration("gauge_inc",
                 MetricCollectorType.GAUGE, "foo_help", 
Collections.emptyList(), Collections.emptyMap()));
         collector.inc();
         Gauge gauge = (Gauge) 
Plugins.getMemberAccessor().get(PrometheusMetricsGaugeCollector.class.getDeclaredField("gauge"),
 collector);
         assertThat(gauge.get(), is(1D));
+    }
+    
+    @Test
+    void assertIncWithLabels() throws ReflectiveOperationException {
+        PrometheusMetricsGaugeCollector collector = new 
PrometheusMetricsGaugeCollector(new MetricConfiguration("gauge_inc_label",
+                MetricCollectorType.GAUGE, "foo_help", 
Collections.singletonList("type"), Collections.emptyMap()));
+        collector.inc("bar");
+        Gauge gauge = (Gauge) 
Plugins.getMemberAccessor().get(PrometheusMetricsGaugeCollector.class.getDeclaredField("gauge"),
 collector);
+        assertThat(gauge.labels("bar").get(), is(1D));
+    }
+    
+    @Test
+    void assertDec() throws ReflectiveOperationException {
+        PrometheusMetricsGaugeCollector collector = new 
PrometheusMetricsGaugeCollector(new MetricConfiguration("gauge_dec",
+                MetricCollectorType.GAUGE, "foo_help", 
Collections.emptyList(), Collections.emptyMap()));
         collector.dec();
-        assertThat(gauge.get(), is(0D));
+        Gauge gauge = (Gauge) 
Plugins.getMemberAccessor().get(PrometheusMetricsGaugeCollector.class.getDeclaredField("gauge"),
 collector);
+        assertThat(gauge.get(), is(-1D));
+    }
+    
+    @Test
+    void assertDecWithLabels() throws ReflectiveOperationException {
+        PrometheusMetricsGaugeCollector collector = new 
PrometheusMetricsGaugeCollector(new MetricConfiguration("gauge_dec_label",
+                MetricCollectorType.GAUGE, "foo_help", 
Collections.singletonList("type"), Collections.emptyMap()));
+        collector.dec("bar");
+        Gauge gauge = (Gauge) 
Plugins.getMemberAccessor().get(PrometheusMetricsGaugeCollector.class.getDeclaredField("gauge"),
 collector);
+        assertThat(gauge.labels("bar").get(), is(-1D));
     }
 }
diff --git 
a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/collector/type/PrometheusMetricsHistogramCollectorTest.java
 
b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/collector/type/PrometheusMetricsHistogramCollectorTest.java
index cdfede52e6c..27814c3c47c 100644
--- 
a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/collector/type/PrometheusMetricsHistogramCollectorTest.java
+++ 
b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/collector/type/PrometheusMetricsHistogramCollectorTest.java
@@ -23,19 +23,79 @@ import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfigur
 import org.junit.jupiter.api.Test;
 import org.mockito.internal.configuration.plugins.Plugins;
 
+import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 
 class PrometheusMetricsHistogramCollectorTest {
     
     @Test
-    void assertCreate() throws ReflectiveOperationException {
-        PrometheusMetricsHistogramCollector collector = new 
PrometheusMetricsHistogramCollector(new MetricConfiguration("foo_histogram",
+    void assertObserveWithoutBuckets() throws ReflectiveOperationException {
+        PrometheusMetricsHistogramCollector collector = new 
PrometheusMetricsHistogramCollector(new 
MetricConfiguration("histogram_default_observe",
                 MetricCollectorType.HISTOGRAM, "foo_help", 
Collections.emptyList(), Collections.emptyMap()));
         collector.observe(1D);
         Histogram histogram = (Histogram) 
Plugins.getMemberAccessor().get(PrometheusMetricsHistogramCollector.class.getDeclaredField("histogram"),
 collector);
-        assertThat(histogram.collect().size(), is(1));
+        assertThat(histogram.collect().get(0).samples.stream().filter(sample 
-> sample.name.endsWith("_count")).findFirst().get().value, is(1D));
+    }
+    
+    @Test
+    void assertAppendPropertiesWithExponentialBuckets() throws 
ReflectiveOperationException {
+        Map<String, Object> buckets = new HashMap<>(4, 1F);
+        buckets.put("type", "exp");
+        buckets.put("start", 2D);
+        buckets.put("factor", 3D);
+        buckets.put("count", 2);
+        PrometheusMetricsHistogramCollector collector = new 
PrometheusMetricsHistogramCollector(new 
MetricConfiguration("histogram_explicit_exp",
+                MetricCollectorType.HISTOGRAM, "foo_help", 
Collections.emptyList(), Collections.singletonMap("buckets", buckets)));
+        Histogram histogram = (Histogram) 
Plugins.getMemberAccessor().get(PrometheusMetricsHistogramCollector.class.getDeclaredField("histogram"),
 collector);
+        assertThat(getBucketLabelValues(histogram), is(Arrays.asList("2.0", 
"6.0", "+Inf")));
+    }
+    
+    @Test
+    void assertAppendPropertiesWithExponentialBucketsDefaults() throws 
ReflectiveOperationException {
+        PrometheusMetricsHistogramCollector collector = new 
PrometheusMetricsHistogramCollector(new 
MetricConfiguration("histogram_default_exp",
+                MetricCollectorType.HISTOGRAM, "foo_help", 
Collections.emptyList(), Collections.singletonMap("buckets", 
Collections.singletonMap("type", "exp"))));
+        Histogram histogram = (Histogram) 
Plugins.getMemberAccessor().get(PrometheusMetricsHistogramCollector.class.getDeclaredField("histogram"),
 collector);
+        assertThat(getBucketLabelValues(histogram), is(Arrays.asList("1.0", 
"+Inf")));
+    }
+    
+    @Test
+    void assertAppendPropertiesWithLinearBuckets() throws 
ReflectiveOperationException {
+        Map<String, Object> buckets = new HashMap<>(4, 1F);
+        buckets.put("type", "linear");
+        buckets.put("start", 2D);
+        buckets.put("width", 2D);
+        buckets.put("count", 2);
+        PrometheusMetricsHistogramCollector collector = new 
PrometheusMetricsHistogramCollector(new 
MetricConfiguration("histogram_explicit_linear",
+                MetricCollectorType.HISTOGRAM, "foo_help", 
Collections.emptyList(), Collections.singletonMap("buckets", buckets)));
+        Histogram histogram = (Histogram) 
Plugins.getMemberAccessor().get(PrometheusMetricsHistogramCollector.class.getDeclaredField("histogram"),
 collector);
+        assertThat(getBucketLabelValues(histogram), is(Arrays.asList("2.0", 
"4.0", "+Inf")));
+    }
+    
+    @Test
+    void assertAppendPropertiesWithLinearBucketsDefaults() throws 
ReflectiveOperationException {
+        PrometheusMetricsHistogramCollector collector = new 
PrometheusMetricsHistogramCollector(new 
MetricConfiguration("histogram_default_linear",
+                MetricCollectorType.HISTOGRAM, "foo_help", 
Collections.emptyList(), Collections.singletonMap("buckets", 
Collections.singletonMap("type", "linear"))));
+        Histogram histogram = (Histogram) 
Plugins.getMemberAccessor().get(PrometheusMetricsHistogramCollector.class.getDeclaredField("histogram"),
 collector);
+        assertThat(getBucketLabelValues(histogram), is(Arrays.asList("1.0", 
"+Inf")));
+    }
+    
+    @Test
+    void assertAppendPropertiesWithUnrecognizedType() throws 
ReflectiveOperationException {
+        PrometheusMetricsHistogramCollector collector = new 
PrometheusMetricsHistogramCollector(new 
MetricConfiguration("histogram_unknown_type",
+                MetricCollectorType.HISTOGRAM, "foo_help", 
Collections.emptyList(), Collections.singletonMap("buckets", 
Collections.singletonMap("type", "custom"))));
+        Histogram histogram = (Histogram) 
Plugins.getMemberAccessor().get(PrometheusMetricsHistogramCollector.class.getDeclaredField("histogram"),
 collector);
+        assertFalse(getBucketLabelValues(histogram).isEmpty());
+    }
+    
+    private List<String> getBucketLabelValues(final Histogram histogram) {
+        return histogram.collect().get(0).samples.stream().filter(each -> 
each.name.endsWith("_bucket")).map(each -> 
each.labelValues.get(each.labelNames.indexOf("le"))).collect(Collectors.toList());
     }
 }
diff --git 
a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/exoprter/PrometheusMetricsExporterTest.java
 
b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/exoprter/PrometheusMetricsExporterTest.java
index 0e2c4d30f91..e083bf820c5 100644
--- 
a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/exoprter/PrometheusMetricsExporterTest.java
+++ 
b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/exoprter/PrometheusMetricsExporterTest.java
@@ -17,10 +17,12 @@
 
 package org.apache.shardingsphere.agent.plugin.metrics.prometheus.exoprter;
 
+import io.prometheus.client.GaugeMetricFamily;
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.GaugeMetricFamilyMetricsCollector;
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.exporter.MetricsExporter;
 import org.junit.jupiter.api.Test;
 
+import java.util.Collections;
 import java.util.Optional;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -41,7 +43,18 @@ class PrometheusMetricsExporterTest {
     @Test
     void assertCollectWithPresentMetricsExporter() {
         MetricsExporter exporter = mock(MetricsExporter.class);
-        
when(exporter.export("Prometheus")).thenReturn(Optional.of(mock(GaugeMetricFamilyMetricsCollector.class)));
-        assertThat(new PrometheusMetricsExporter(exporter).collect().size(), 
is(1));
+        GaugeMetricFamilyMetricsCollector metricsCollector = 
mock(GaugeMetricFamilyMetricsCollector.class);
+        GaugeMetricFamily expectedMetricFamily = new 
GaugeMetricFamily("present_metric", "help", Collections.emptyList());
+        
when(metricsCollector.getRawMetricFamilyObject()).thenReturn(expectedMetricFamily);
+        
when(exporter.export("Prometheus")).thenReturn(Optional.of(metricsCollector));
+        GaugeMetricFamily actualMetricFamily = (GaugeMetricFamily) new 
PrometheusMetricsExporter(exporter).collect().get(0);
+        assertThat(actualMetricFamily, is(expectedMetricFamily));
+    }
+    
+    @Test
+    void assertCollectWithException() {
+        MetricsExporter exporter = mock(MetricsExporter.class);
+        
when(exporter.export("Prometheus")).thenThrow(IllegalStateException.class);
+        assertTrue(new 
PrometheusMetricsExporter(exporter).collect().isEmpty());
     }
 }

Reply via email to