Skip to content

Commit

Permalink
Refactor PrometheusAdvisorDefinitionService (apache#22955)
Browse files Browse the repository at this point in the history
* Refactor PrometheusAdvisorDefinitionService

* Refactor PrometheusAdvisorDefinitionService
  • Loading branch information
terrymanu authored Dec 18, 2022
1 parent 21851ee commit 4b9e47a
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@

import java.util.Optional;

/**
* Fixed metric wrapper factory.
*/
public final class FixtureWrapperFactory implements MetricsWrapperFactory {

@Override
Expand Down
7 changes: 7 additions & 0 deletions agent/plugins/metrics/type/prometheus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4b9e47a

Please sign in to comment.