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

sunnianjun 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 786a3f44abb Add MetricsConfiguration (#23563)
786a3f44abb is described below

commit 786a3f44abb8eff79d04b1d2b5f70dd86497ba20
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Jan 15 19:11:35 2023 +0800

    Add MetricsConfiguration (#23563)
---
 .../metrics/core/config/MetricConfiguration.java}  | 22 ++++++----
 .../metrics/core/config/MetricsConfiguration.java} | 36 ++++++++--------
 .../yaml/entity}/YamlMetricConfiguration.java      |  4 +-
 .../yaml/entity}/YamlMetricsConfiguration.java     |  2 +-
 .../loader/YamlMetricConfigurationsLoader.java     | 43 +++++++++++++++++++
 .../swapper/YamlMetricConfigurationSwapper.java    | 43 +++++++++++++++++++
 .../swapper/YamlMetricsConfigurationSwapper.java   | 48 ++++++++++++++++++++++
 .../wrapper/PrometheusWrapperFactory.java          | 44 +++++++-------------
 8 files changed, 182 insertions(+), 60 deletions(-)

diff --git 
a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/yaml/YamlMetricsConfiguration.java
 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/MetricConfiguration.java
similarity index 67%
copy from 
agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/yaml/YamlMetricsConfiguration.java
copy to 
agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/MetricConfiguration.java
index 3a316b96d73..e10333b6cfb 100644
--- 
a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/yaml/YamlMetricsConfiguration.java
+++ 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/MetricConfiguration.java
@@ -15,20 +15,28 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.agent.plugin.metrics.prometheus.wrapper.yaml;
+package org.apache.shardingsphere.agent.plugin.metrics.core.config;
 
 import lombok.Getter;
-import lombok.Setter;
+import lombok.RequiredArgsConstructor;
 
 import java.util.Collection;
-import java.util.LinkedList;
+import java.util.Map;
 
 /**
- * YAML metrics configuration.
+ * Metric configuration.
  */
+@RequiredArgsConstructor
 @Getter
-@Setter
-public final class YamlMetricsConfiguration {
+public final class MetricConfiguration {
     
-    private Collection<YamlMetricConfiguration> metrics = new LinkedList<>();
+    private final String id;
+    
+    private final String type;
+    
+    private final String help;
+    
+    private final Collection<String> labels;
+    
+    private final Map<String, Object> props;
 }
diff --git 
a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/yaml/YamlMetricConfiguration.java
 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/MetricsConfiguration.java
similarity index 60%
copy from 
agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/yaml/YamlMetricConfiguration.java
copy to 
agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/MetricsConfiguration.java
index 3b6752ab74b..42a09588886 100644
--- 
a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/yaml/YamlMetricConfiguration.java
+++ 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/MetricsConfiguration.java
@@ -15,32 +15,28 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.agent.plugin.metrics.prometheus.wrapper.yaml;
+package org.apache.shardingsphere.agent.plugin.metrics.core.config;
 
-import lombok.Getter;
-import lombok.Setter;
+import lombok.RequiredArgsConstructor;
 
 import java.util.Collection;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.Map;
+import java.util.Optional;
 
 /**
- * YAML metric configuration.
+ * Metric configuration.
  */
-@Getter
-@Setter
-public final class YamlMetricConfiguration {
+@RequiredArgsConstructor
+public final class MetricsConfiguration {
     
-    private String id;
+    private final Collection<MetricConfiguration> metrics;
     
-    private String name;
-    
-    private String type;
-    
-    private String help;
-    
-    private Collection<String> labels = new LinkedList<>();
-    
-    private Map<String, Object> props = new HashMap<>();
+    /**
+     * Find metric configuration.
+     * 
+     * @param id metric ID
+     * @return metric configuration
+     */
+    public Optional<MetricConfiguration> find(final String id) {
+        return metrics.stream().filter(each -> 
id.equals(each.getId())).findFirst();
+    }
 }
diff --git 
a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/yaml/YamlMetricConfiguration.java
 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/yaml/entity/YamlMetricConfiguration.java
similarity index 92%
rename from 
agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/yaml/YamlMetricConfiguration.java
rename to 
agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/yaml/entity/YamlMetricConfiguration.java
index 3b6752ab74b..85b3293191b 100644
--- 
a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/yaml/YamlMetricConfiguration.java
+++ 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/yaml/entity/YamlMetricConfiguration.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.agent.plugin.metrics.prometheus.wrapper.yaml;
+package org.apache.shardingsphere.agent.plugin.metrics.core.config.yaml.entity;
 
 import lombok.Getter;
 import lombok.Setter;
@@ -34,8 +34,6 @@ public final class YamlMetricConfiguration {
     
     private String id;
     
-    private String name;
-    
     private String type;
     
     private String help;
diff --git 
a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/yaml/YamlMetricsConfiguration.java
 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/yaml/entity/YamlMetricsConfiguration.java
similarity index 93%
rename from 
agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/yaml/YamlMetricsConfiguration.java
rename to 
agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/yaml/entity/YamlMetricsConfiguration.java
index 3a316b96d73..6475c6bc952 100644
--- 
a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/yaml/YamlMetricsConfiguration.java
+++ 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/yaml/entity/YamlMetricsConfiguration.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.agent.plugin.metrics.prometheus.wrapper.yaml;
+package org.apache.shardingsphere.agent.plugin.metrics.core.config.yaml.entity;
 
 import lombok.Getter;
 import lombok.Setter;
diff --git 
a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/yaml/loader/YamlMetricConfigurationsLoader.java
 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/yaml/loader/YamlMetricConfigurationsLoader.java
new file mode 100644
index 00000000000..56ae717ee2d
--- /dev/null
+++ 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/yaml/loader/YamlMetricConfigurationsLoader.java
@@ -0,0 +1,43 @@
+/*
+ * 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.plugin.metrics.core.config.yaml.loader;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.yaml.entity.YamlMetricsConfiguration;
+import org.yaml.snakeyaml.Yaml;
+
+import java.io.InputStream;
+
+/**
+ * YAML Metric configuration loader.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class YamlMetricConfigurationsLoader {
+    
+    /**
+     * Load advisors configuration.
+     * 
+     * @param inputStream input stream
+     * @return loaded advisors configuration
+     */
+    public static YamlMetricsConfiguration load(final InputStream inputStream) 
{
+        YamlMetricsConfiguration result = new Yaml().loadAs(inputStream, 
YamlMetricsConfiguration.class);
+        return null == result ? new YamlMetricsConfiguration() : result;
+    }
+}
diff --git 
a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/yaml/swapper/YamlMetricConfigurationSwapper.java
 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/yaml/swapper/YamlMetricConfigurationSwapper.java
new file mode 100644
index 00000000000..1fae712666b
--- /dev/null
+++ 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/yaml/swapper/YamlMetricConfigurationSwapper.java
@@ -0,0 +1,43 @@
+/*
+ * 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.plugin.metrics.core.config.yaml.swapper;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.yaml.entity.YamlMetricConfiguration;
+
+import java.util.Collections;
+
+/**
+ * YAML metric configuration swapper.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class YamlMetricConfigurationSwapper {
+    
+    /**
+     * Swap from YAML metric configuration to metric configuration.
+     * 
+     * @param yamlConfig YAML metric configuration
+     * @return metric configuration
+     */
+    public static MetricConfiguration swap(final YamlMetricConfiguration 
yamlConfig) {
+        return new MetricConfiguration(yamlConfig.getId(), 
yamlConfig.getType(), yamlConfig.getHelp(),
+                null == yamlConfig.getLabels() ? Collections.emptyList() : 
yamlConfig.getLabels(), null == yamlConfig.getProps() ? Collections.emptyMap() 
: yamlConfig.getProps());
+    }
+}
diff --git 
a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/yaml/swapper/YamlMetricsConfigurationSwapper.java
 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/yaml/swapper/YamlMetricsConfigurationSwapper.java
new file mode 100644
index 00000000000..9f2c54c5c6e
--- /dev/null
+++ 
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/yaml/swapper/YamlMetricsConfigurationSwapper.java
@@ -0,0 +1,48 @@
+/*
+ * 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.plugin.metrics.core.config.yaml.swapper;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricsConfiguration;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.yaml.entity.YamlMetricsConfiguration;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.stream.Collectors;
+
+/**
+ * YAML metrics configuration swapper.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class YamlMetricsConfigurationSwapper {
+    
+    /**
+     * Swap from YAML metrics configuration to metrics configuration.
+     * 
+     * @param yamlConfig YAML metrics configuration
+     * @return metrics configuration
+     */
+    public static MetricsConfiguration swap(final YamlMetricsConfiguration 
yamlConfig) {
+        Collection<MetricConfiguration> metricConfigs = null == 
yamlConfig.getMetrics()
+                ? Collections.emptyList()
+                : 
yamlConfig.getMetrics().stream().map(YamlMetricConfigurationSwapper::swap).collect(Collectors.toList());
+        return new MetricsConfiguration(metricConfigs);
+    }
+}
diff --git 
a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/PrometheusWrapperFactory.java
 
b/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/PrometheusWrapperFactory.java
index 6452ecdc5ba..8506290d6b5 100644
--- 
a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/PrometheusWrapperFactory.java
+++ 
b/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/wrapper/PrometheusWrapperFactory.java
@@ -24,15 +24,15 @@ import io.prometheus.client.Histogram;
 import io.prometheus.client.Summary;
 import org.apache.shardingsphere.agent.plugin.metrics.core.MetricsWrapper;
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.MetricsWrapperFactory;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricsConfiguration;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.yaml.loader.YamlMetricConfigurationsLoader;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.yaml.swapper.YamlMetricsConfigurationSwapper;
 import 
org.apache.shardingsphere.agent.plugin.metrics.prometheus.wrapper.type.CounterWrapper;
 import 
org.apache.shardingsphere.agent.plugin.metrics.prometheus.wrapper.type.GaugeWrapper;
 import 
org.apache.shardingsphere.agent.plugin.metrics.prometheus.wrapper.type.HistogramWrapper;
 import 
org.apache.shardingsphere.agent.plugin.metrics.prometheus.wrapper.type.SummaryWrapper;
-import 
org.apache.shardingsphere.agent.plugin.metrics.prometheus.wrapper.yaml.YamlMetricConfiguration;
-import 
org.apache.shardingsphere.agent.plugin.metrics.prometheus.wrapper.yaml.YamlMetricsConfiguration;
-import org.yaml.snakeyaml.Yaml;
 
-import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -44,24 +44,19 @@ import java.util.Optional;
  */
 public final class PrometheusWrapperFactory implements MetricsWrapperFactory {
     
-    private static YamlMetricsConfiguration metricsConfig;
+    private static final MetricsConfiguration METRICS_CONFIG;
     
     static {
-        parseMetricsYAML();
-    }
-    
-    private static void parseMetricsYAML() {
-        InputStream inputStream = 
PrometheusWrapperFactory.class.getResourceAsStream("/META-INF/conf/prometheus-metrics.yaml");
-        metricsConfig = new Yaml().loadAs(inputStream, 
YamlMetricsConfiguration.class);
+        METRICS_CONFIG = 
YamlMetricsConfigurationSwapper.swap(YamlMetricConfigurationsLoader.load(PrometheusWrapperFactory.class.getResourceAsStream("/META-INF/conf/prometheus-metrics.yaml")));
     }
     
     @Override
     public Optional<MetricsWrapper> create(final String id) {
-        Optional<YamlMetricConfiguration> metricConfig = findMetric(id);
+        Optional<MetricConfiguration> metricConfig = METRICS_CONFIG.find(id);
         return metricConfig.isPresent() ? create(metricConfig.get()) : 
Optional.empty();
     }
     
-    private Optional<MetricsWrapper> create(final YamlMetricConfiguration 
metricConfig) {
+    private Optional<MetricsWrapper> create(final MetricConfiguration 
metricConfig) {
         if (null == metricConfig.getType()) {
             return Optional.empty();
         }
@@ -79,11 +74,7 @@ public final class PrometheusWrapperFactory implements 
MetricsWrapperFactory {
         }
     }
     
-    private Optional<YamlMetricConfiguration> findMetric(final String id) {
-        return metricsConfig.getMetrics().stream().filter(each -> 
id.equals(each.getId())).findFirst();
-    }
-    
-    private MetricsWrapper createCounter(final YamlMetricConfiguration 
metricConfig) {
+    private MetricsWrapper createCounter(final MetricConfiguration 
metricConfig) {
         Counter.Builder builder = 
Counter.build().name(metricConfig.getId()).help(metricConfig.getHelp());
         List<String> metricLabels = (List<String>) metricConfig.getLabels();
         if (null != metricLabels) {
@@ -92,7 +83,7 @@ public final class PrometheusWrapperFactory implements 
MetricsWrapperFactory {
         return new CounterWrapper(builder.register());
     }
     
-    private MetricsWrapper createGauge(final YamlMetricConfiguration 
metricConfig) {
+    private MetricsWrapper createGauge(final MetricConfiguration metricConfig) 
{
         Gauge.Builder builder = 
Gauge.build().name(metricConfig.getId()).help(metricConfig.getHelp());
         Collection<String> metricLabels = metricConfig.getLabels();
         if (null != metricLabels) {
@@ -101,7 +92,7 @@ public final class PrometheusWrapperFactory implements 
MetricsWrapperFactory {
         return new GaugeWrapper(builder.register());
     }
     
-    private MetricsWrapper createHistogram(final YamlMetricConfiguration 
metricConfig) {
+    private MetricsWrapper createHistogram(final MetricConfiguration 
metricConfig) {
         Histogram.Builder builder = 
Histogram.build().name(metricConfig.getId()).help(metricConfig.getHelp());
         Collection<String> metricLabels = metricConfig.getLabels();
         if (null != metricLabels) {
@@ -133,7 +124,7 @@ public final class PrometheusWrapperFactory implements 
MetricsWrapperFactory {
         }
     }
     
-    private MetricsWrapper createSummary(final YamlMetricConfiguration 
metricConfig) {
+    private MetricsWrapper createSummary(final MetricConfiguration 
metricConfig) {
         Summary.Builder builder = 
Summary.build().name(metricConfig.getId()).help(metricConfig.getHelp());
         Collection<String> metricLabels = metricConfig.getLabels();
         if (null != metricLabels) {
@@ -145,19 +136,14 @@ public final class PrometheusWrapperFactory implements 
MetricsWrapperFactory {
     /**
      * Create gauge metric family.
      *
-     * @param id string
+     * @param id metric id
      * @return gauge metric family
      */
     public Optional<GaugeMetricFamily> createGaugeMetricFamily(final String 
id) {
-        Optional<YamlMetricConfiguration> metricMap = findMetric(id);
-        if (!metricMap.isPresent()) {
-            return Optional.empty();
-        }
-        YamlMetricConfiguration metricConfig = metricMap.get();
-        return "GAUGEMETRICFAMILY".equalsIgnoreCase(metricConfig.getType()) ? 
Optional.of(createGaugeMetricFamily(metricConfig)) : Optional.empty();
+        return METRICS_CONFIG.find(id).filter(optional -> 
"GAUGEMETRICFAMILY".equalsIgnoreCase(optional.getType())).map(this::createGaugeMetricFamily);
     }
     
-    private GaugeMetricFamily createGaugeMetricFamily(final 
YamlMetricConfiguration metricConfig) {
+    private GaugeMetricFamily createGaugeMetricFamily(final 
MetricConfiguration metricConfig) {
         Collection<String> labels = metricConfig.getLabels();
         return null == labels
                 ? new GaugeMetricFamily(metricConfig.getId(), 
metricConfig.getHelp(), 1d)

Reply via email to