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 8ece70f5cc1 Optimize the label of the proxy_meta_data_info metric 
(#24193)
8ece70f5cc1 is described below

commit 8ece70f5cc17916fb9830e1ffabb74538601717d
Author: jiangML <[email protected]>
AuthorDate: Thu Feb 16 19:55:16 2023 +0800

    Optimize the label of the proxy_meta_data_info metric (#24193)
    
    * optimzie proxy_meta_data_info metric
    
    * optimize test
    
    * fix checkstyle error
---
 .../impl/proxy/ProxyMetaDataInfoExporter.java      | 46 +++-------------------
 .../impl/proxy/ProxyMetaDataInfoExporterTest.java  | 28 +++++++++++--
 .../observability/_index.cn.md                     | 28 ++++++-------
 .../observability/_index.en.md                     | 28 ++++++-------
 4 files changed, 58 insertions(+), 72 deletions(-)

diff --git 
a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporter.java
 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporter.java
index 9e401993d2f..0df0998371b 100644
--- 
a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporter.java
+++ 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporter.java
@@ -23,17 +23,10 @@ import 
org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.GaugeM
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.exporter.MetricsExporter;
-import 
org.apache.shardingsphere.infra.datasource.props.DataSourcePropertiesCreator;
-import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 
-import javax.sql.DataSource;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.Collection;
 import java.util.Collections;
-import java.util.HashSet;
 import java.util.Optional;
 
 /**
@@ -43,7 +36,7 @@ import java.util.Optional;
 public final class ProxyMetaDataInfoExporter implements MetricsExporter {
     
     private final MetricConfiguration config = new 
MetricConfiguration("proxy_meta_data_info",
-            MetricCollectorType.GAUGE_METRIC_FAMILY, "Meta data information of 
ShardingSphere-Proxy. schema_count is logic number of databases; database_count 
is actual number of databases",
+            MetricCollectorType.GAUGE_METRIC_FAMILY, "Meta data information of 
ShardingSphere-Proxy. database_count is logic number of databases; 
storage_unit_count is number of storage units",
             Collections.singletonList("name"), Collections.emptyMap());
     
     @Override
@@ -54,41 +47,12 @@ public final class ProxyMetaDataInfoExporter implements 
MetricsExporter {
         GaugeMetricFamilyMetricsCollector result = 
MetricsCollectorRegistry.get(config, pluginType);
         result.cleanMetrics();
         MetaDataContexts metaDataContexts = 
ProxyContext.getInstance().getContextManager().getMetaDataContexts();
-        result.addMetric(Collections.singletonList("schema_count"), 
metaDataContexts.getMetaData().getDatabases().size());
-        result.addMetric(Collections.singletonList("database_count"), 
getDatabaseNames(metaDataContexts).size());
+        result.addMetric(Collections.singletonList("database_count"), 
metaDataContexts.getMetaData().getDatabases().size());
+        result.addMetric(Collections.singletonList("storage_unit_count"), 
getStorageUnitCount(metaDataContexts));
         return Optional.of(result);
     }
     
-    private Collection<String> getDatabaseNames(final MetaDataContexts 
metaDataContexts) {
-        Collection<String> result = new HashSet<>();
-        for (ShardingSphereDatabase each : 
metaDataContexts.getMetaData().getDatabases().values()) {
-            result.addAll(getDatabaseNames(each));
-        }
-        return result;
-    }
-    
-    private Collection<String> getDatabaseNames(final ShardingSphereDatabase 
database) {
-        Collection<String> result = new HashSet<>();
-        for (DataSource each : 
database.getResourceMetaData().getDataSources().values()) {
-            getDatabaseName(each).ifPresent(result::add);
-        }
-        return result;
-    }
-    
-    private Optional<String> getDatabaseName(final DataSource dataSource) {
-        Object jdbcUrl = 
DataSourcePropertiesCreator.create(dataSource).getAllStandardProperties().get("url");
-        if (null == jdbcUrl) {
-            log.info("Can not get JDBC URL.");
-            return Optional.empty();
-        }
-        try {
-            URI uri = new URI(jdbcUrl.toString().substring(5));
-            if (null != uri.getPath()) {
-                return Optional.of(uri.getPath());
-            }
-        } catch (final URISyntaxException | NullPointerException ignored) {
-            log.info("Unsupported JDBC URL by URI: {}.", jdbcUrl);
-        }
-        return Optional.empty();
+    private int getStorageUnitCount(final MetaDataContexts metaDataContexts) {
+        return 
metaDataContexts.getMetaData().getDatabases().values().stream().map(each -> 
each.getResourceMetaData().getDataSources().size()).reduce(0, Integer::sum);
     }
 }
diff --git 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java
 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java
index 44f610b658a..06fdc86b7b9 100644
--- 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java
+++ 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java
@@ -23,12 +23,20 @@ import 
org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.GaugeM
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.fixture.collector.MetricsCollectorFixture;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import 
org.apache.shardingsphere.infra.metadata.database.resource.ShardingSphereResourceMetaData;
 import org.apache.shardingsphere.mode.manager.ContextManager;
+import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
+import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.junit.After;
 import org.junit.Test;
 
+import javax.sql.DataSource;
 import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
 import java.util.Optional;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -55,12 +63,26 @@ public final class ProxyMetaDataInfoExporterTest extends 
ProxyContextRestorer {
     
     @Test
     public void assertExportWithContextManager() {
+        ShardingSphereResourceMetaData resourceMetaData = 
mock(ShardingSphereResourceMetaData.class);
+        when(resourceMetaData.getDataSources()).thenReturn(mockDataSources());
+        ShardingSphereDatabase shardingSphereDatabase = 
mock(ShardingSphereDatabase.class);
+        
when(shardingSphereDatabase.getResourceMetaData()).thenReturn(resourceMetaData);
+        Map<String, ShardingSphereDatabase> databases = new LinkedHashMap<>();
+        databases.put("sharding_db", shardingSphereDatabase);
+        ShardingSphereMetaData shardingSphereMetaData = 
mock(ShardingSphereMetaData.class);
+        when(shardingSphereMetaData.getDatabases()).thenReturn(databases);
+        MetaDataContexts metaDataContexts = new 
MetaDataContexts(mock(MetaDataPersistService.class), shardingSphereMetaData);
         ContextManager contextManager = mock(ContextManager.class, 
RETURNS_DEEP_STUBS);
-        
when(contextManager.getMetaDataContexts().getMetaData().getDatabases()).thenReturn(Collections.emptyMap());
-        // TODO mock schema_count and database_count
+        
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
         ProxyContext.init(contextManager);
         Optional<GaugeMetricFamilyMetricsCollector> collector = new 
ProxyMetaDataInfoExporter().export("FIXTURE");
         assertTrue(collector.isPresent());
-        assertThat(collector.get().toString(), is("schema_count=0, 
database_count=0"));
+        assertThat(collector.get().toString(), is("database_count=1, 
storage_unit_count=1"));
+    }
+    
+    private Map<String, DataSource> mockDataSources() {
+        Map<String, DataSource> result = new LinkedHashMap<>();
+        result.put("ds_0", mock(DataSource.class));
+        return result;
     }
 }
diff --git 
a/docs/document/content/user-manual/shardingsphere-proxy/observability/_index.cn.md
 
b/docs/document/content/user-manual/shardingsphere-proxy/observability/_index.cn.md
index b1b8d326655..d7ad6ecc1c2 100644
--- 
a/docs/document/content/user-manual/shardingsphere-proxy/observability/_index.cn.md
+++ 
b/docs/document/content/user-manual/shardingsphere-proxy/observability/_index.cn.md
@@ -154,17 +154,17 @@ services:
 
 ## Metrics
 
-| 指标名称                            | 指标类型             | 指标描述                    
                                                            |
-| :-------------------------------- | :------------------ | 
:------------------------------------------------------------------------------------
 |
-| build_info                        | GAUGE_METRIC_FAMILY | 构建信息               
                                                                 |
-| parsed_sql_total                  | COUNTER             | 
按类型(INSERT、UPDATE、DELETE、SELECT、DDL、DCL、DAL、TCL、RQL、RDL、RAL、RUL)分类的解析总数 |
-| routed_sql_total                  | COUNTER             | 
按类型(INSERT、UPDATE、DELETE、SELECT)分类的路由总数                                      |
-| routed_result_total               | COUNTER             | 
路由结果总数(数据源路由结果、表路由结果)                                                    |
-| proxy_state                       | GAUGE_METRIC_FAMILY | 
ShardingSphere-Proxy 状态信息。0 表示正常状态;1 表示熔断状态;2 锁定状态                      |
-| proxy_meta_data_info              | GAUGE_METRIC_FAMILY | 
ShardingSphere-Proxy 元数据信息,schema_count:逻辑库数量, database_count:数据源数量        |
-| proxy_current_connections         | GAUGE               | 
ShardingSphere-Proxy 的当前连接数                                                     
   |
-| proxy_requests_total              | COUNTER             | 
ShardingSphere-Proxy 的接受请求总数                                                    
  |
-| proxy_commit_transactions_total   | COUNTER             | 
ShardingSphere-Proxy 的事务提交总数                                                    
  |
-| proxy_rollback_transactions_total | COUNTER             | 
ShardingSphere-Proxy 的事务回滚总数                                                    
  |
-| proxy_execute_latency_millis      | HISTOGRAM           | 
ShardingSphere-Proxy 的执行耗时毫秒直方图                                                 
|
-| proxy_execute_errors_total        | COUNTER             | 
ShardingSphere-Proxy 的执行异常总数                                                    
  |
+| 指标名称                            | 指标类型             | 指标描述                    
                                                  |
+| :-------------------------------- | :------------------ 
|:--------------------------------------------------------------------------|
+| build_info                        | GAUGE_METRIC_FAMILY | 构建信息               
                                                       |
+| parsed_sql_total                  | COUNTER             | 
按类型(INSERT、UPDATE、DELETE、SELECT、DDL、DCL、DAL、TCL、RQL、RDL、RAL、RUL)分类的解析总数   |
+| routed_sql_total                  | COUNTER             | 
按类型(INSERT、UPDATE、DELETE、SELECT)分类的路由总数                                   |
+| routed_result_total               | COUNTER             | 
路由结果总数(数据源路由结果、表路由结果)                                                     |
+| proxy_state                       | GAUGE_METRIC_FAMILY | 
ShardingSphere-Proxy 状态信息。0 表示正常状态;1 表示熔断状态;2 锁定状态                        |
+| proxy_meta_data_info              | GAUGE_METRIC_FAMILY | 
ShardingSphere-Proxy 元数据信息,database_count:逻辑库数量,storage_unit_count:存储节点数量 |
+| proxy_current_connections         | GAUGE               | 
ShardingSphere-Proxy 的当前连接数                                               |
+| proxy_requests_total              | COUNTER             | 
ShardingSphere-Proxy 的接受请求总数                                              |
+| proxy_commit_transactions_total   | COUNTER             | 
ShardingSphere-Proxy 的事务提交总数                                              |
+| proxy_rollback_transactions_total | COUNTER             | 
ShardingSphere-Proxy 的事务回滚总数                                              |
+| proxy_execute_latency_millis      | HISTOGRAM           | 
ShardingSphere-Proxy 的执行耗时毫秒直方图                                           |
+| proxy_execute_errors_total        | COUNTER             | 
ShardingSphere-Proxy 的执行异常总数                                              |
diff --git 
a/docs/document/content/user-manual/shardingsphere-proxy/observability/_index.en.md
 
b/docs/document/content/user-manual/shardingsphere-proxy/observability/_index.en.md
index 48224b093be..499a54795cc 100644
--- 
a/docs/document/content/user-manual/shardingsphere-proxy/observability/_index.en.md
+++ 
b/docs/document/content/user-manual/shardingsphere-proxy/observability/_index.en.md
@@ -157,17 +157,17 @@ services:
 
 ## Metrics
 
-| Name                              | Type                | Description        
                                                                                
                                    |
-| :-------------------------------- | :------------------ | 
:-------------------------------------------------------------------------------------------------------------------------------------
 |
-| build_info                        | GAUGE_METRIC_FAMILY | Build information  
                                                                                
                                    |
-| parsed_sql_total                  | COUNTER             | Total count of 
parsed by type (INSERT, UPDATE, DELETE, SELECT, DDL, DCL, DAL, TCL, RQL, RDL, 
RAL, RUL)                                 |
-| routed_sql_total                  | COUNTER             | Total count of 
routed by type (INSERT, UPDATE, DELETE, SELECT)                                 
                                        |
-| routed_result_total               | COUNTER             | Total count of 
routed result (data source routed, table routed)                                
                                        |
-| proxy_state                       | GAUGE_METRIC_FAMILY | Status information 
of ShardingSphere-Proxy. 0 is OK; 1 is CIRCUIT BREAK; 2 is LOCK                 
                                    |
-| proxy_meta_data_info              | GAUGE_METRIC_FAMILY | Meta data 
information of ShardingSphere-Proxy. schema_count is logic number of databases; 
database_count is actual number of databases |
-| proxy_current_connections         | GAUGE               | Current 
connections of ShardingSphere-Proxy                                             
                                               |
-| proxy_requests_total              | COUNTER             | Total requests of 
ShardingSphere-Proxy                                                            
                                     |
-| proxy_commit_transactions_total   | COUNTER             | Total commit 
transactions of ShardingSphere-Proxy                                            
                                          |
-| proxy_rollback_transactions_total | COUNTER             | Total rollback 
transactions of ShardingSphere-Proxy                                            
                                        |
-| proxy_execute_latency_millis      | HISTOGRAM           | Execute latency 
millis histogram of ShardingSphere-Proxy                                        
                                       |
-| proxy_execute_errors_total        | COUNTER             | Total executor 
errors of ShardingSphere-Proxy                                                  
                                        |
+| Name                              | Type                | Description        
                                                                                
                                      |
+| :-------------------------------- | :------------------ | 
:---------------------------------------------------------------------------------------------------------------------------------------
 |
+| build_info                        | GAUGE_METRIC_FAMILY | Build information  
                                                                                
                                      |
+| parsed_sql_total                  | COUNTER             | Total count of 
parsed by type (INSERT, UPDATE, DELETE, SELECT, DDL, DCL, DAL, TCL, RQL, RDL, 
RAL, RUL)                                   |
+| routed_sql_total                  | COUNTER             | Total count of 
routed by type (INSERT, UPDATE, DELETE, SELECT)                                 
                                          |
+| routed_result_total               | COUNTER             | Total count of 
routed result (data source routed, table routed)                                
                                          |
+| proxy_state                       | GAUGE_METRIC_FAMILY | Status information 
of ShardingSphere-Proxy. 0 is OK; 1 is CIRCUIT BREAK; 2 is LOCK                 
                                      |
+| proxy_meta_data_info              | GAUGE_METRIC_FAMILY | Meta data 
information of ShardingSphere-Proxy. database_count is logic number of 
databases; storage_unit_count is number of storage units |
+| proxy_current_connections         | GAUGE               | Current 
connections of ShardingSphere-Proxy                                             
                                                  |
+| proxy_requests_total              | COUNTER             | Total requests of 
ShardingSphere-Proxy                                                            
                                        |
+| proxy_commit_transactions_total   | COUNTER             | Total commit 
transactions of ShardingSphere-Proxy                                            
                                             |
+| proxy_rollback_transactions_total | COUNTER             | Total rollback 
transactions of ShardingSphere-Proxy                                            
                                           |
+| proxy_execute_latency_millis      | HISTOGRAM           | Execute latency 
millis histogram of ShardingSphere-Proxy                                        
                                          |
+| proxy_execute_errors_total        | COUNTER             | Total executor 
errors of ShardingSphere-Proxy                                                  
                                           |

Reply via email to