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

menghaoran 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 7304a80  agent add metrics test case (#8919)
7304a80 is described below

commit 7304a80819b970fe376d2dffa56a6b4e46b5da36
Author: xiaoyu <[email protected]>
AuthorDate: Wed Jan 6 19:54:37 2021 +0800

    agent add metrics test case (#8919)
---
 .../api/advice/ChannelHandlerAdviceTest.java       | 72 +++++++++++++++++
 .../api/advice/CommandExecutorTaskAdviceTest.java  | 45 +++++++++++
 .../metrics/api/advice/MetricsAdviceBaseTest.java  | 34 ++++++++
 .../agent/metrics/api/advice/MockTargetObject.java | 35 +++++++++
 .../metrics/api/advice/TransactionAdviceTest.java  | 63 +++++++++++++++
 .../MetricsPluginDefinitionServiceTest.java        | 41 ++++++++++
 .../api/fixture/FixtureMetricsRegister.java        | 42 ++++++++--
 .../agent/metrics/api/util/ReflectiveUtil.java     | 50 ++++++++++++
 .../collector/BuildInfoCollectorTest.java          | 37 +++++++++
 .../register/PrometheusMetricsRegisterTest.java    | 91 ++++++++++++++++++++++
 .../service/PrometheusPluginBootServiceTest.java   | 61 +++++++++++++++
 .../metrics/prometheus/util/ReflectiveUtil.java    | 79 +++++++++++++++++++
 12 files changed, 642 insertions(+), 8 deletions(-)

diff --git 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/ChannelHandlerAdviceTest.java
 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/ChannelHandlerAdviceTest.java
new file mode 100644
index 0000000..9043195
--- /dev/null
+++ 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/ChannelHandlerAdviceTest.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.api.advice;
+
+import java.lang.reflect.Method;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.DoubleAdder;
+import org.apache.shardingsphere.agent.api.result.MethodInvocationResult;
+import org.apache.shardingsphere.agent.metrics.api.constant.MethodNameConstant;
+import org.apache.shardingsphere.agent.metrics.api.util.ReflectiveUtil;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class ChannelHandlerAdviceTest extends MetricsAdviceBaseTest {
+    
+    private ChannelHandlerAdvice channelHandlerAdvice = new 
ChannelHandlerAdvice();
+    
+    @Mock
+    private Method channelRead;
+    
+    @Mock
+    private Method channelActive;
+    
+    @Mock
+    private Method channelInactive;
+    
+    @Test
+    @SuppressWarnings("unchecked")
+    public void assertMethod() {
+        
when(channelRead.getName()).thenReturn(MethodNameConstant.CHANNEL_READ);
+        
when(channelActive.getName()).thenReturn(MethodNameConstant.CHANNEL_ACTIVE);
+        
when(channelInactive.getName()).thenReturn(MethodNameConstant.CHANNEL_INACTIVE);
+        MockTargetObject targetObject = new MockTargetObject();
+        channelHandlerAdvice.beforeMethod(targetObject, channelRead, new 
Object[]{}, new MethodInvocationResult());
+        channelHandlerAdvice.beforeMethod(targetObject, channelActive, new 
Object[]{}, new MethodInvocationResult());
+        channelHandlerAdvice.beforeMethod(targetObject, channelActive, new 
Object[]{}, new MethodInvocationResult());
+        channelHandlerAdvice.beforeMethod(targetObject, channelInactive, new 
Object[]{}, new MethodInvocationResult());
+        Map<String, DoubleAdder> doubleAdderMap = (Map<String, DoubleAdder>) 
ReflectiveUtil.getFieldValue(getFixturemetricsregister(), "COUNTER_MAP");
+        DoubleAdder doubleAdder = doubleAdderMap.get("proxy_request_total");
+        assertNotNull(doubleAdder);
+        assertThat(doubleAdder.intValue(), is(1));
+        Map<String, AtomicInteger> atomicIntegerMap = (Map<String, 
AtomicInteger>) ReflectiveUtil.getFieldValue(getFixturemetricsregister(), 
"GAUGE_MAP");
+        assertThat(atomicIntegerMap.size(), is(1));
+        AtomicInteger atomicInteger = 
atomicIntegerMap.get("proxy_connection_total");
+        assertNotNull(atomicInteger);
+        assertThat(atomicInteger.intValue(), is(1));
+    }
+}
diff --git 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/CommandExecutorTaskAdviceTest.java
 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/CommandExecutorTaskAdviceTest.java
new file mode 100644
index 0000000..954a899
--- /dev/null
+++ 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/CommandExecutorTaskAdviceTest.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.api.advice;
+
+import java.util.Map;
+import java.util.concurrent.atomic.LongAdder;
+import org.apache.shardingsphere.agent.api.result.MethodInvocationResult;
+import org.apache.shardingsphere.agent.metrics.api.util.ReflectiveUtil;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+public final class CommandExecutorTaskAdviceTest extends MetricsAdviceBaseTest 
{
+    
+    private CommandExecutorTaskAdvice commandExecutorTaskAdvice = new 
CommandExecutorTaskAdvice();
+    
+    @Test
+    @SuppressWarnings("unchecked")
+    public void assertMethod() {
+        MockTargetObject targetObject = new MockTargetObject();
+        commandExecutorTaskAdvice.beforeMethod(targetObject, null, new 
Object[]{}, new MethodInvocationResult());
+        commandExecutorTaskAdvice.afterMethod(targetObject, null, new 
Object[]{}, new MethodInvocationResult());
+        Map<String, LongAdder> longAdderMap = (Map<String, LongAdder>) 
ReflectiveUtil.getFieldValue(getFixturemetricsregister(), "HISTOGRAM_MAP");
+        assertThat(longAdderMap.size(), is(1));
+        LongAdder longAdder = longAdderMap.get("proxy_execute_latency_millis");
+        assertNotNull(longAdder);
+    }
+}
diff --git 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/MetricsAdviceBaseTest.java
 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/MetricsAdviceBaseTest.java
new file mode 100644
index 0000000..748ba34
--- /dev/null
+++ 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/MetricsAdviceBaseTest.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.api.advice;
+
+import lombok.Getter;
+import 
org.apache.shardingsphere.agent.metrics.api.fixture.FixtureMetricsRegister;
+import org.apache.shardingsphere.agent.metrics.api.reporter.MetricsReporter;
+import org.junit.BeforeClass;
+
+public abstract class MetricsAdviceBaseTest {
+    
+    @Getter
+    private static FixtureMetricsRegister fixturemetricsregister = new 
FixtureMetricsRegister();
+    
+    @BeforeClass
+    public static void setup() {
+        MetricsReporter.register(fixturemetricsregister);
+    }
+}
diff --git 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/MockTargetObject.java
 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/MockTargetObject.java
new file mode 100644
index 0000000..3dd67f2
--- /dev/null
+++ 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/MockTargetObject.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.api.advice;
+
+import org.apache.shardingsphere.agent.api.advice.TargetObject;
+
+public final class MockTargetObject implements TargetObject {
+    
+    private Object object;
+
+    @Override
+    public Object getAttachment() {
+        return object;
+    }
+
+    @Override
+    public void setAttachment(final Object attachment) {
+        this.object = attachment;
+    }
+}
diff --git 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/TransactionAdviceTest.java
 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/TransactionAdviceTest.java
new file mode 100644
index 0000000..6d0cdc0
--- /dev/null
+++ 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/TransactionAdviceTest.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.api.advice;
+
+import java.lang.reflect.Method;
+import java.util.Map;
+import java.util.concurrent.atomic.DoubleAdder;
+import org.apache.shardingsphere.agent.api.result.MethodInvocationResult;
+import org.apache.shardingsphere.agent.metrics.api.constant.MethodNameConstant;
+import org.apache.shardingsphere.agent.metrics.api.util.ReflectiveUtil;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class TransactionAdviceTest extends MetricsAdviceBaseTest {
+    
+    private TransactionAdvice transactionAdvice = new TransactionAdvice();
+    
+    @Mock
+    private Method commit;
+    
+    @Mock
+    private Method rollback;
+    
+    @Test
+    @SuppressWarnings("unchecked")
+    public void assertMethod() {
+        when(commit.getName()).thenReturn(MethodNameConstant.COMMIT);
+        when(rollback.getName()).thenReturn(MethodNameConstant.ROLL_BACK);
+        MockTargetObject targetObject = new MockTargetObject();
+        transactionAdvice.beforeMethod(targetObject, commit, new Object[]{}, 
new MethodInvocationResult());
+        transactionAdvice.beforeMethod(targetObject, rollback, new Object[]{}, 
new MethodInvocationResult());
+        Map<String, DoubleAdder> doubleAdderMap = (Map<String, DoubleAdder>) 
ReflectiveUtil.getFieldValue(getFixturemetricsregister(), "COUNTER_MAP");
+        DoubleAdder commitTotal = 
doubleAdderMap.get("proxy_transaction_commit_total");
+        assertNotNull(commitTotal);
+        assertThat(commitTotal.intValue(), is(1));
+        DoubleAdder rollbackTotal = 
doubleAdderMap.get("proxy_transaction_rollback_total");
+        assertNotNull(rollbackTotal);
+        assertThat(rollbackTotal.intValue(), is(1));
+    }
+}
diff --git 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/definition/MetricsPluginDefinitionServiceTest.java
 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/definition/MetricsPluginDefinitionServiceTest.java
new file mode 100644
index 0000000..47fe891
--- /dev/null
+++ 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/definition/MetricsPluginDefinitionServiceTest.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.api.definition;
+
+import java.util.List;
+import org.apache.shardingsphere.agent.api.point.PluginInterceptorPoint;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class MetricsPluginDefinitionServiceTest {
+    
+    private final MetricsPluginDefinitionService 
metricsPluginDefinitionService = new MetricsPluginDefinitionService();
+    
+    @Test
+    public void assertDefine() {
+        List<PluginInterceptorPoint> interceptorPointList = 
metricsPluginDefinitionService.build();
+        assertThat(interceptorPointList.size(), is(4));
+    }
+    
+    @Test
+    public void assertType() {
+        assertThat(metricsPluginDefinitionService.getType(), is("Metrics"));
+    }
+}
diff --git 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/fixture/FixtureMetricsRegister.java
 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/fixture/FixtureMetricsRegister.java
index 21f89dd..cefc83b 100644
--- 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/fixture/FixtureMetricsRegister.java
+++ 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/fixture/FixtureMetricsRegister.java
@@ -17,47 +17,73 @@
 
 package org.apache.shardingsphere.agent.metrics.api.fixture;
 
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.DoubleAdder;
+import java.util.concurrent.atomic.LongAdder;
 import org.apache.shardingsphere.agent.metrics.api.MetricsRegister;
 
 public final class FixtureMetricsRegister implements MetricsRegister {
     
+    private static final Map<String, DoubleAdder> COUNTER_MAP = new 
ConcurrentHashMap<>();
+    
+    private static final Map<String, AtomicInteger> GAUGE_MAP = new 
ConcurrentHashMap<>();
+    
+    private static final Map<String, LongAdder> HISTOGRAM_MAP = new 
ConcurrentHashMap<>();
+    
     @Override
     public void registerGauge(final String name, final String[] labelNames, 
final String document) {
-    
+        GAUGE_MAP.put(name, new AtomicInteger());
     }
     
     @Override
     public void registerCounter(final String name, final String[] labelNames, 
final String document) {
-    
+        COUNTER_MAP.put(name, new DoubleAdder());
     }
     
     @Override
     public void registerHistogram(final String name, final String[] 
labelNames, final String document) {
-    
+        HISTOGRAM_MAP.put(name, new LongAdder());
     }
     
     @Override
     public void counterIncrement(final String name, final String[] 
labelValues) {
-    
+        DoubleAdder doubleAdder = COUNTER_MAP.get(name);
+        if (null != doubleAdder) {
+            doubleAdder.add(1.0d);
+        }
     }
     
     @Override
     public void counterIncrement(final String name, final String[] 
labelValues, final long count) {
-    
+        DoubleAdder doubleAdder = COUNTER_MAP.get(name);
+        if (null != doubleAdder) {
+            doubleAdder.add(count);
+        }
     }
     
     @Override
     public void gaugeIncrement(final String name, final String[] labelValues) {
-    
+        AtomicInteger atomicInteger = GAUGE_MAP.get(name);
+        if (null != atomicInteger) {
+            atomicInteger.incrementAndGet();
+        }
     }
     
     @Override
     public void gaugeDecrement(final String name, final String[] labelValues) {
-    
+        AtomicInteger atomicInteger = GAUGE_MAP.get(name);
+        if (null != atomicInteger) {
+            atomicInteger.decrementAndGet();
+        }
     }
     
     @Override
     public void recordTime(final String name, final String[] labelValues, 
final long duration) {
-    
+        LongAdder longAdder = HISTOGRAM_MAP.get(name);
+        if (null != longAdder) {
+            longAdder.add(duration);
+        }
     }
 }
diff --git 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/util/ReflectiveUtil.java
 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/util/ReflectiveUtil.java
index f7296ac..6327373 100644
--- 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/util/ReflectiveUtil.java
+++ 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/util/ReflectiveUtil.java
@@ -46,4 +46,54 @@ public final class ReflectiveUtil {
             }
         }
     }
+    
+    /**
+     * Get field.
+     *
+     * @param clazz clazz
+     * @param fieldName field name
+     * @return field
+     */
+    public static Field getField(final Class<?> clazz, final String fieldName) 
{
+        Field[] fields = clazz.getDeclaredFields();
+        if (fields.length != 0) {
+            for (Field each : fields) {
+                if (fieldName.equals(each.getName())) {
+                    return each;
+                }
+            }
+        }
+        return null;
+    }
+    
+    /**
+     * Get field value object.
+     *
+     * @param object object
+     * @param fieldName field name
+     * @return object
+     */
+    public static Object getFieldValue(final Object object, final String 
fieldName) {
+        return getFieldValue(object, getField(object.getClass(), fieldName));
+    }
+    
+    /**
+     * Get field value.
+     *
+     * @param object  object
+     * @param field field
+     * @return field value
+     */
+    public static Object getFieldValue(final Object object, final Field field) 
{
+        if (null == object || null == field) {
+            return null;
+        }
+        field.setAccessible(true);
+        Object result = null;
+        try {
+            result = field.get(object);
+        } catch (IllegalAccessException ignored) {
+        }
+        return result;
+    }
 }
diff --git 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/BuildInfoCollectorTest.java
 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/BuildInfoCollectorTest.java
new file mode 100644
index 0000000..03caf32
--- /dev/null
+++ 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/BuildInfoCollectorTest.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.prometheus.collector;
+
+import io.prometheus.client.Collector.MetricFamilySamples;
+import java.util.List;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class BuildInfoCollectorTest {
+
+    @Test
+    public void assertCollect() {
+        BuildInfoCollector buildInfoCollector = new BuildInfoCollector();
+        List<MetricFamilySamples> metricFamilySamples = 
buildInfoCollector.collect();
+        assertThat(metricFamilySamples.toString(), is("[Name: 
jmx_exporter_build_info Type: GAUGE Help: "
+                + "A metric with a constant '1' value labeled with the version 
of the JMX exporter. Samples: "
+                + "[Name: jmx_exporter_build_info LabelNames: [version, name] 
labelValues: [unknown, unknown] Value: 1.0 TimestampMs: null]]"));
+    }
+}
diff --git 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/register/PrometheusMetricsRegisterTest.java
 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/register/PrometheusMetricsRegisterTest.java
new file mode 100644
index 0000000..95a9051
--- /dev/null
+++ 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/register/PrometheusMetricsRegisterTest.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.prometheus.register;
+
+import io.prometheus.client.Counter;
+import io.prometheus.client.Gauge;
+import io.prometheus.client.Histogram;
+import java.util.Map;
+import org.apache.shardingsphere.agent.metrics.prometheus.util.ReflectiveUtil;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class PrometheusMetricsRegisterTest {
+    
+    private final PrometheusMetricsRegister prometheusMetricsRegister = 
PrometheusMetricsRegister.getInstance();
+    
+    @Test
+    @SuppressWarnings("unchecked")
+    public void assertCounter() {
+        String routeDatasource = "route_datasource";
+        String[] labelNames = new String[] {"name"};
+        prometheusMetricsRegister.registerCounter(routeDatasource, labelNames, 
"the shardingsphere proxy route datasource");
+        prometheusMetricsRegister.counterIncrement(routeDatasource, 
labelNames);
+        prometheusMetricsRegister.counterIncrement(routeDatasource, 
labelNames, 2);
+        String routeTable = "route_table";
+        prometheusMetricsRegister.registerCounter(routeTable, null, "the 
shardingsphere proxy route table");
+        prometheusMetricsRegister.counterIncrement(routeTable, null);
+        prometheusMetricsRegister.counterIncrement(routeTable, null, 2);
+        Map<String, Counter> counterMap = (Map<String, Counter>) 
ReflectiveUtil.getFieldValue(prometheusMetricsRegister, "COUNTER_MAP");
+        assertThat(counterMap.size(), is(2));
+        Counter routeDatasourceCounter = counterMap.get(routeDatasource);
+        assertThat(routeDatasourceCounter.labels(labelNames).get(), is(3.0d));
+        Counter routeTableCounter = counterMap.get(routeTable);
+        assertThat(routeTableCounter.get(), is(3.0d));
+    }
+    
+    @Test
+    @SuppressWarnings("unchecked")
+    public void assertGauge() {
+        String connectionTotal = "proxy_connection_total";
+        String[] labelNames = new String[] {"connectionTotal"};
+        prometheusMetricsRegister.registerGauge(connectionTotal, labelNames, 
"the shardingsphere proxy request total");
+        prometheusMetricsRegister.gaugeIncrement(connectionTotal, labelNames);
+        prometheusMetricsRegister.gaugeIncrement(connectionTotal, labelNames);
+        prometheusMetricsRegister.gaugeDecrement(connectionTotal, labelNames);
+        String handlerTotal = "handler_total";
+        prometheusMetricsRegister.registerGauge(handlerTotal, null, "the 
shardingsphere proxy handler total");
+        prometheusMetricsRegister.gaugeIncrement(handlerTotal, null);
+        prometheusMetricsRegister.gaugeIncrement(handlerTotal, null);
+        prometheusMetricsRegister.gaugeDecrement(handlerTotal, null);
+        Map<String, Gauge> gaugeMap = (Map<String, Gauge>) 
ReflectiveUtil.getFieldValue(prometheusMetricsRegister, "GAUGE_MAP");
+        assertThat(gaugeMap.size(), is(2));
+        Gauge connectionTotalGauge = gaugeMap.get(connectionTotal);
+        assertThat(connectionTotalGauge.labels(labelNames).get(), is(1.0d));
+        Gauge handlerTotalGauge = gaugeMap.get(handlerTotal);
+        assertThat(handlerTotalGauge.get(), is(1.0d));
+    }
+    
+    @Test
+    @SuppressWarnings("unchecked")
+    public void assertHistogram() {
+        String name = "proxy_execute_latency_millis";
+        String[] labelNames = new String[] {"name"};
+        prometheusMetricsRegister.registerHistogram(name, labelNames, "the 
shardingsphere proxy executor latency millis");
+        prometheusMetricsRegister.recordTime(name, labelNames, 1000);
+        String latencyMillis = "execute_latency_millis";
+        prometheusMetricsRegister.registerHistogram(latencyMillis, null, "the 
shardingsphere executor latency millis");
+        prometheusMetricsRegister.recordTime(latencyMillis, null, 1000);
+        Map<String, Histogram> histogramMap = (Map<String, Histogram>) 
ReflectiveUtil.getFieldValue(prometheusMetricsRegister, "HISTOGRAM_MAP");
+        assertThat(histogramMap.size(), is(2));
+        Histogram histogram = histogramMap.get(name);
+        assertThat(histogram.labels(labelNames).get().sum, is(1000.0));
+    }
+}
diff --git 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootServiceTest.java
 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootServiceTest.java
new file mode 100644
index 0000000..c290380
--- /dev/null
+++ 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootServiceTest.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.prometheus.service;
+
+import io.prometheus.client.exporter.HTTPServer;
+import java.lang.reflect.Field;
+import java.util.Properties;
+import lombok.SneakyThrows;
+import org.apache.shardingsphere.agent.config.PluginConfiguration;
+import org.junit.AfterClass;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+public final class PrometheusPluginBootServiceTest {
+    
+    private static PrometheusPluginBootService prometheusPluginBootService = 
new PrometheusPluginBootService();
+    
+    @SneakyThrows
+    @Test
+    public void assertStart() {
+        Properties props = new Properties();
+        props.setProperty("JVM_INFORMATION_COLLECTOR_ENABLED", "true");
+        PluginConfiguration configuration = new PluginConfiguration();
+        configuration.setPort(8090);
+        configuration.setProps(props);
+        prometheusPluginBootService.start(configuration);
+        Field field = 
PrometheusPluginBootService.class.getDeclaredField("httpServer");
+        field.setAccessible(true);
+        HTTPServer httpServer = (HTTPServer) 
field.get(prometheusPluginBootService);
+        assertNotNull(httpServer);
+        assertThat(httpServer.getPort(), is(8090));
+    }
+    
+    @Test
+    public void assertType() {
+        assertThat(prometheusPluginBootService.getType(), is("Prometheus"));
+    }
+    
+    @AfterClass
+    public static void close() {
+        prometheusPluginBootService.close();
+    }
+}
diff --git 
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/util/ReflectiveUtil.java
 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/util/ReflectiveUtil.java
new file mode 100644
index 0000000..de1e665
--- /dev/null
+++ 
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/util/ReflectiveUtil.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.prometheus.util;
+
+import java.lang.reflect.Field;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+/**
+ * Reflective utility.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ReflectiveUtil {
+    
+    /**
+     * Get field.
+     *
+     * @param clazz clazz
+     * @param fieldName field name
+     * @return field
+     */
+    public static Field getField(final Class<?> clazz, final String fieldName) 
{
+        Field[] fields = clazz.getDeclaredFields();
+        if (fields.length != 0) {
+            for (Field each : fields) {
+                if (fieldName.equals(each.getName())) {
+                    return each;
+                }
+            }
+        }
+        return null;
+    }
+    
+    /**
+     * Get field value object.
+     *
+     * @param object object
+     * @param fieldName field name
+     * @return object
+     */
+    public static Object getFieldValue(final Object object, final String 
fieldName) {
+        return getFieldValue(object, getField(object.getClass(), fieldName));
+    }
+    
+    /**
+     * Get field value.
+     *
+     * @param object  object
+     * @param field field
+     * @return field value
+     */
+    public static Object getFieldValue(final Object object, final Field field) 
{
+        if (null == object || null == field) {
+            return null;
+        }
+        field.setAccessible(true);
+        Object result = null;
+        try {
+            result = field.get(object);
+        } catch (IllegalAccessException ignored) {
+        }
+        return result;
+    }
+}

Reply via email to