You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/12/18 13:52:44 UTC
[shardingsphere] branch master updated: Refactor PrometheusAdvisorDefinitionService (#22955)
This is an automated email from the ASF dual-hosted git repository.
zhaojinchao 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 4b9e47a075f Refactor PrometheusAdvisorDefinitionService (#22955)
4b9e47a075f is described below
commit 4b9e47a075fe65dcab6e00075f510108da961578
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Dec 18 21:52:36 2022 +0800
Refactor PrometheusAdvisorDefinitionService (#22955)
* Refactor PrometheusAdvisorDefinitionService
* Refactor PrometheusAdvisorDefinitionService
---
.../YamlAdvisorsConfigurationLoader.java} | 12 ++---
.../swapper/YamlAdvisorConfigurationSwapper.java | 54 ++++++++++++++++++++++
.../swapper/YamlAdvisorsConfigurationSwapper.java | 23 +++++++++
.../core/fixture/FixtureWrapperFactory.java | 3 --
agent/plugins/metrics/type/prometheus/pom.xml | 7 +++
.../PrometheusAdvisorDefinitionService.java | 35 +++-----------
6 files changed, 96 insertions(+), 38 deletions(-)
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/swapper/YamlAdvisorsConfigurationSwapper.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/loader/YamlAdvisorsConfigurationLoader.java
similarity index 77%
copy from agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/swapper/YamlAdvisorsConfigurationSwapper.java
copy to agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/loader/YamlAdvisorsConfigurationLoader.java
index 560f00ab5cc..9ab26dfd64d 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/swapper/YamlAdvisorsConfigurationSwapper.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/loader/YamlAdvisorsConfigurationLoader.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.agent.core.plugin.yaml.swapper;
+package org.apache.shardingsphere.agent.core.plugin.yaml.loader;
import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlAdvisorsConfiguration;
import org.yaml.snakeyaml.Yaml;
@@ -23,17 +23,17 @@ import org.yaml.snakeyaml.Yaml;
import java.io.InputStream;
/**
- * YAML advisors configuration swapper.
+ * YAML advisors configuration loader.
*/
-public final class YamlAdvisorsConfigurationSwapper {
+public final class YamlAdvisorsConfigurationLoader {
/**
- * Unmarshal advisors configuration.
+ * Load advisors configuration.
*
* @param inputStream input stream
- * @return unmarshalled advisors configuration
+ * @return loaded advisors configuration
*/
- public YamlAdvisorsConfiguration unmarshal(final InputStream inputStream) {
+ public YamlAdvisorsConfiguration load(final InputStream inputStream) {
return new Yaml().loadAs(inputStream, YamlAdvisorsConfiguration.class);
}
}
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/swapper/YamlAdvisorConfigurationSwapper.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/swapper/YamlAdvisorConfigurationSwapper.java
new file mode 100644
index 00000000000..02982fb75fa
--- /dev/null
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/swapper/YamlAdvisorConfigurationSwapper.java
@@ -0,0 +1,54 @@
+/*
+ * 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.core.plugin.yaml.swapper;
+
+import net.bytebuddy.matcher.ElementMatchers;
+import org.apache.shardingsphere.agent.config.advisor.AdvisorConfiguration;
+import org.apache.shardingsphere.agent.config.advisor.MethodAdvisorConfiguration;
+import org.apache.shardingsphere.agent.core.plugin.advisor.AdvisorConfigurationRegistryFactory;
+import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlAdvisorConfiguration;
+import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlPointcutConfiguration;
+
+/**
+ * YAML advisor configuration swapper.
+ */
+public final class YamlAdvisorConfigurationSwapper {
+
+ /**
+ * Swap from YAML advisor configuration to advisors configuration.
+ *
+ * @param yamlAdvisorConfig YAML advisor configuration
+ * @param type type
+ * @return advisor configuration
+ */
+ public AdvisorConfiguration swapToObject(final YamlAdvisorConfiguration yamlAdvisorConfig, final String type) {
+ AdvisorConfiguration result = AdvisorConfigurationRegistryFactory.getRegistry(type).getAdvisorConfiguration(yamlAdvisorConfig.getTarget());
+ if (null != yamlAdvisorConfig.getConstructAdvice() && !("".equals(yamlAdvisorConfig.getConstructAdvice()))) {
+ result.getConstructorAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.isConstructor(), yamlAdvisorConfig.getConstructAdvice()));
+ }
+ String[] instanceMethodPointcuts = yamlAdvisorConfig.getPointcuts().stream().filter(each -> "instance".equals(each.getType())).map(YamlPointcutConfiguration::getName).toArray(String[]::new);
+ if (instanceMethodPointcuts.length > 0) {
+ result.getInstanceMethodAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.namedOneOf(instanceMethodPointcuts), yamlAdvisorConfig.getInstanceAdvice()));
+ }
+ String[] staticMethodPointcuts = yamlAdvisorConfig.getPointcuts().stream().filter(each -> "static".equals(each.getType())).map(YamlPointcutConfiguration::getName).toArray(String[]::new);
+ if (staticMethodPointcuts.length > 0) {
+ result.getStaticMethodAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.namedOneOf(staticMethodPointcuts), yamlAdvisorConfig.getStaticAdvice()));
+ }
+ return result;
+ }
+}
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/swapper/YamlAdvisorsConfigurationSwapper.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/swapper/YamlAdvisorsConfigurationSwapper.java
index 560f00ab5cc..1663d197c10 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/swapper/YamlAdvisorsConfigurationSwapper.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/swapper/YamlAdvisorsConfigurationSwapper.java
@@ -17,16 +17,22 @@
package org.apache.shardingsphere.agent.core.plugin.yaml.swapper;
+import org.apache.shardingsphere.agent.config.advisor.AdvisorConfiguration;
+import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlAdvisorConfiguration;
import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlAdvisorsConfiguration;
import org.yaml.snakeyaml.Yaml;
import java.io.InputStream;
+import java.util.Collection;
+import java.util.LinkedList;
/**
* YAML advisors configuration swapper.
*/
public final class YamlAdvisorsConfigurationSwapper {
+ private final YamlAdvisorConfigurationSwapper advisorConfigurationSwapper = new YamlAdvisorConfigurationSwapper();
+
/**
* Unmarshal advisors configuration.
*
@@ -36,4 +42,21 @@ public final class YamlAdvisorsConfigurationSwapper {
public YamlAdvisorsConfiguration unmarshal(final InputStream inputStream) {
return new Yaml().loadAs(inputStream, YamlAdvisorsConfiguration.class);
}
+
+ /**
+ * Swap from YAML advisors configuration to advisor configurations.
+ *
+ * @param yamlAdvisorsConfig YAML advisors configuration
+ * @param type type
+ * @return advisor configurations
+ */
+ public Collection<AdvisorConfiguration> swapToObject(final YamlAdvisorsConfiguration yamlAdvisorsConfig, final String type) {
+ Collection<AdvisorConfiguration> result = new LinkedList<>();
+ for (YamlAdvisorConfiguration each : yamlAdvisorsConfig.getAdvisors()) {
+ if (null != each.getTarget()) {
+ result.add(advisorConfigurationSwapper.swapToObject(each, type));
+ }
+ }
+ return result;
+ }
}
diff --git a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/metrics/core/fixture/FixtureWrapperFactory.java b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/metrics/core/fixture/FixtureWrapperFactory.java
index 5ccddabfeeb..2c710168ff3 100644
--- a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/metrics/core/fixture/FixtureWrapperFactory.java
+++ b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/metrics/core/fixture/FixtureWrapperFactory.java
@@ -22,9 +22,6 @@ import org.apache.shardingsphere.agent.metrics.core.MetricsWrapperFactory;
import java.util.Optional;
-/**
- * Fixed metric wrapper factory.
- */
public final class FixtureWrapperFactory implements MetricsWrapperFactory {
@Override
diff --git a/agent/plugins/metrics/type/prometheus/pom.xml b/agent/plugins/metrics/type/prometheus/pom.xml
index 49b6edcb8b1..5ff53433bd8 100644
--- a/agent/plugins/metrics/type/prometheus/pom.xml
+++ b/agent/plugins/metrics/type/prometheus/pom.xml
@@ -38,6 +38,13 @@
<artifactId>shardingsphere-agent-metrics-core</artifactId>
<version>${project.version}</version>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-proxy-bootstrap</artifactId>
+ <version>${project.version}</version>
+ <scope>provided</scope>
+ </dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-proxy-frontend-core</artifactId>
diff --git a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/PrometheusAdvisorDefinitionService.java b/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/PrometheusAdvisorDefinitionService.java
index 4d7ce1a0f5c..57f73d664c3 100644
--- a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/PrometheusAdvisorDefinitionService.java
+++ b/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/PrometheusAdvisorDefinitionService.java
@@ -17,49 +17,26 @@
package org.apache.shardingsphere.agent.metrics.prometheus;
-import net.bytebuddy.matcher.ElementMatchers;
import org.apache.shardingsphere.agent.config.advisor.AdvisorConfiguration;
-import org.apache.shardingsphere.agent.config.advisor.MethodAdvisorConfiguration;
-import org.apache.shardingsphere.agent.core.plugin.advisor.AdvisorConfigurationRegistryFactory;
-import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlAdvisorConfiguration;
-import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlPointcutConfiguration;
+import org.apache.shardingsphere.agent.core.plugin.yaml.loader.YamlAdvisorsConfigurationLoader;
import org.apache.shardingsphere.agent.core.plugin.yaml.swapper.YamlAdvisorsConfigurationSwapper;
import org.apache.shardingsphere.agent.spi.advisor.AdvisorDefinitionService;
import java.util.Collection;
import java.util.Collections;
-import java.util.LinkedList;
/**
* Prometheus advisor definition service.
*/
public final class PrometheusAdvisorDefinitionService implements AdvisorDefinitionService {
+ private final YamlAdvisorsConfigurationLoader loader = new YamlAdvisorsConfigurationLoader();
+
+ private final YamlAdvisorsConfigurationSwapper swapper = new YamlAdvisorsConfigurationSwapper();
+
@Override
public Collection<AdvisorConfiguration> getProxyAdvisorConfigurations() {
- Collection<AdvisorConfiguration> result = new LinkedList<>();
- for (YamlAdvisorConfiguration each : new YamlAdvisorsConfigurationSwapper().unmarshal(getClass().getResourceAsStream("/prometheus/advisors.yaml")).getAdvisors()) {
- if (null != each.getTarget()) {
- result.add(createAdvisorConfiguration(each));
- }
- }
- return result;
- }
-
- private AdvisorConfiguration createAdvisorConfiguration(final YamlAdvisorConfiguration yamlAdvisorConfig) {
- AdvisorConfiguration result = AdvisorConfigurationRegistryFactory.getRegistry(getType()).getAdvisorConfiguration(yamlAdvisorConfig.getTarget());
- if (null != yamlAdvisorConfig.getConstructAdvice() && !("".equals(yamlAdvisorConfig.getConstructAdvice()))) {
- result.getConstructorAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.isConstructor(), yamlAdvisorConfig.getConstructAdvice()));
- }
- String[] instancePointcuts = yamlAdvisorConfig.getPointcuts().stream().filter(i -> "instance".equals(i.getType())).map(YamlPointcutConfiguration::getName).toArray(String[]::new);
- if (instancePointcuts.length > 0) {
- result.getInstanceMethodAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.namedOneOf(instancePointcuts), yamlAdvisorConfig.getInstanceAdvice()));
- }
- String[] staticPointcuts = yamlAdvisorConfig.getPointcuts().stream().filter(i -> "static".equals(i.getType())).map(YamlPointcutConfiguration::getName).toArray(String[]::new);
- if (staticPointcuts.length > 0) {
- result.getStaticMethodAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.namedOneOf(staticPointcuts), yamlAdvisorConfig.getStaticAdvice()));
- }
- return result;
+ return swapper.swapToObject(loader.load(getClass().getResourceAsStream("/prometheus/advisors.yaml")), getType());
}
@Override