diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/loader/YamlAdvisorsConfigurationLoader.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/loader/YamlAdvisorsConfigurationLoader.java new file mode 100644 index 0000000000000..9ab26dfd64dc9 --- /dev/null +++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/loader/YamlAdvisorsConfigurationLoader.java @@ -0,0 +1,39 @@ +/* + * 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.loader; + +import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlAdvisorsConfiguration; +import org.yaml.snakeyaml.Yaml; + +import java.io.InputStream; + +/** + * YAML advisors configuration loader. + */ +public final class YamlAdvisorsConfigurationLoader { + + /** + * Load advisors configuration. + * + * @param inputStream input stream + * @return loaded advisors configuration + */ + 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 0000000000000..02982fb75fa74 --- /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 560f00ab5cc39..1663d197c10cd 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 swapToObject(final YamlAdvisorsConfiguration yamlAdvisorsConfig, final String type) { + Collection 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 5ccddabfeeb46..2c710168ff367 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 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 49b6edcb8b141..5ff53433bd872 100644 --- a/agent/plugins/metrics/type/prometheus/pom.xml +++ b/agent/plugins/metrics/type/prometheus/pom.xml @@ -38,6 +38,13 @@ shardingsphere-agent-metrics-core ${project.version} + + + org.apache.shardingsphere + shardingsphere-proxy-bootstrap + ${project.version} + provided + org.apache.shardingsphere shardingsphere-proxy-frontend-core 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 4d7ce1a0f5c6a..57f73d664c3b1 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 getProxyAdvisorConfigurations() { - Collection 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