Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor PrometheusPluginBootService #22953

Merged
merged 2 commits into from
Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,32 @@
public final class RemotePluginConfigurationValidator {

/**
* Validate remote plugin configuration.
* Validate host and port.
*
* @param type plugin type
* @param pluginConfig to be validated plugin configuration
*/
public static void validate(final String type, final PluginConfiguration pluginConfig) {
public static void validateHostAndPort(final String type, final PluginConfiguration pluginConfig) {
validateHost(type, pluginConfig);
validatePort(type, pluginConfig);
}

/**
* Validate host.
*
* @param type plugin type
* @param pluginConfig to be validated plugin configuration
*/
public static void validateHost(final String type, final PluginConfiguration pluginConfig) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(pluginConfig.getHost()), "Hostname of %s is required.", type);
}

/**
* Validate port.
* @param type plugin type
* @param pluginConfig to be validated plugin configuration
*/
public static void validatePort(final String type, final PluginConfiguration pluginConfig) {
Preconditions.checkArgument(pluginConfig.getPort() > 0, "Port `%s` of %s must be a positive number.", pluginConfig.getPort(), type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public final class AgentTransformer implements Transformer {

private final Map<String, AdvisorConfiguration> advisorConfigs;

private final boolean enhanceProxy;
private final boolean isEnhancedForProxy;

@SuppressWarnings("NullableProblems")
@Override
Expand All @@ -61,7 +61,7 @@ public Builder<?> transform(final Builder<?> builder, final TypeDescription type
}
Builder<?> result = builder.defineField(EXTRA_DATA, Object.class, Opcodes.ACC_PRIVATE | Opcodes.ACC_VOLATILE).implement(TargetAdviceObject.class).intercept(FieldAccessor.ofField(EXTRA_DATA));
AdvisorConfiguration advisorConfig = advisorConfigs.get(typeDescription.getTypeName());
AdviceFactory adviceFactory = new AdviceFactory(classLoader, pluginConfigs, pluginJars, enhanceProxy);
AdviceFactory adviceFactory = new AdviceFactory(classLoader, pluginConfigs, pluginJars, isEnhancedForProxy);
result = new MethodAdvisorBuildEngine(advisorConfig.getConstructorAdvisors(), typeDescription).create(result, new ConstructorAdvisorBuilder(adviceFactory));
result = new MethodAdvisorBuildEngine(advisorConfig.getInstanceMethodAdvisors(), typeDescription).create(result, new InstanceMethodAdvisorBuilder(adviceFactory));
result = new MethodAdvisorBuildEngine(advisorConfig.getStaticMethodAdvisors(), typeDescription).create(result, new StaticMethodAdvisorBuilder(adviceFactory));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@
public final class RemotePluginConfigurationValidatorTest {

@Test
public void assertValidateSuccess() {
RemotePluginConfigurationValidator.validate("foo_type", new PluginConfiguration("localhost", 8080, "pwd", null));
public void assertValidateHostAndPortSuccess() {
RemotePluginConfigurationValidator.validateHostAndPort("foo_type", new PluginConfiguration("localhost", 8080, "pwd", null));
}

@Test
public void assertValidateWhenHostIsEmpty() {
public void assertValidateHostAndPortWhenHostIsEmpty() {
assertThrows("Hostname of foo_type is required", IllegalArgumentException.class,
() -> RemotePluginConfigurationValidator.validate("foo_type", new PluginConfiguration("", 8080, "pwd", null)));
() -> RemotePluginConfigurationValidator.validateHostAndPort("foo_type", new PluginConfiguration("", 8080, "pwd", null)));
}

@Test
public void assertValidateWhenHostIsNull() {
public void assertValidateHostAndPortWhenHostIsNull() {
assertThrows("Hostname of foo_type is required", IllegalArgumentException.class,
() -> RemotePluginConfigurationValidator.validate("foo_type", new PluginConfiguration(null, 8080, "pwd", null)));
() -> RemotePluginConfigurationValidator.validateHostAndPort("foo_type", new PluginConfiguration(null, 8080, "pwd", null)));
}

@Test
public void assertValidateWhenPortLessThanOne() {
public void assertValidateHostAndPortWhenPortLessThanOne() {
assertThrows("Port `0` of foo_host must be a positive number", IllegalArgumentException.class,
() -> RemotePluginConfigurationValidator.validate("foo_type", new PluginConfiguration("localhost", 0, "pwd", null)));
() -> RemotePluginConfigurationValidator.validateHostAndPort("foo_type", new PluginConfiguration("localhost", 0, "pwd", null)));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.agent.metrics.prometheus;

import com.google.common.base.Strings;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.HTTPServer;
import io.prometheus.client.hotspot.DefaultExports;
Expand Down Expand Up @@ -45,35 +46,35 @@ public final class PrometheusPluginBootService implements PluginBootService {

@Override
public void start(final PluginConfiguration pluginConfig, final boolean isEnhancedForProxy) {
RemotePluginConfigurationValidator.validate(getType(), pluginConfig);
RemotePluginConfigurationValidator.validatePort(getType(), pluginConfig);
startServer(pluginConfig, isEnhancedForProxy);
MetricsPool.setMetricsFactory(new PrometheusWrapperFactory());
}

private void startServer(final PluginConfiguration pluginConfig, final boolean isEnhancedForProxy) {
registerCollector(Boolean.parseBoolean(pluginConfig.getProps().getProperty(KEY_JVM_INFORMATION_COLLECTOR_ENABLED)), isEnhancedForProxy);
InetSocketAddress socketAddress = getSocketAddress(pluginConfig.getHost(), pluginConfig.getPort());
InetSocketAddress socketAddress = getSocketAddress(pluginConfig);
try {
httpServer = new HTTPServer(socketAddress, CollectorRegistry.defaultRegistry, true);
log.info("Prometheus metrics HTTP server `{}:{}` start success", socketAddress.getHostString(), socketAddress.getPort());
log.info("Prometheus metrics HTTP server `{}:{}` start success.", socketAddress.getHostString(), socketAddress.getPort());
} catch (final IOException ex) {
log.error("Prometheus metrics HTTP server start fail", ex);
log.error("Prometheus metrics HTTP server start fail.", ex);
}
}

private void registerCollector(final boolean isCollectJVMInformation, final boolean isEnhancedForProxy) {
private void registerCollector(final boolean isJVMInformationCollection, final boolean isEnhancedForProxy) {
new BuildInfoCollector(isEnhancedForProxy).register();
if (isEnhancedForProxy) {
new ProxyInfoCollector().register();
new MetaDataInfoCollector().register();
}
if (isCollectJVMInformation) {
if (isJVMInformationCollection) {
DefaultExports.initialize();
}
}

private InetSocketAddress getSocketAddress(final String host, final int port) {
return null == host || "".equals(host) ? new InetSocketAddress(port) : new InetSocketAddress(host, port);
private InetSocketAddress getSocketAddress(final PluginConfiguration pluginConfig) {
return Strings.isNullOrEmpty(pluginConfig.getHost()) ? new InetSocketAddress(pluginConfig.getPort()) : new InetSocketAddress(pluginConfig.getHost(), pluginConfig.getPort());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.prometheus.client.Collector;
import io.prometheus.client.GaugeMetricFamily;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.SneakyThrows;
import org.apache.shardingsphere.agent.metrics.core.constant.MetricIds;
import org.apache.shardingsphere.agent.metrics.prometheus.wrapper.PrometheusWrapperFactory;

Expand All @@ -33,7 +33,6 @@
* Build information collector.
*/
@RequiredArgsConstructor
@Slf4j
public final class BuildInfoCollector extends Collector {

private static final String PROXY_BOOTSTRAP_CLASS = "org.apache.shardingsphere.proxy.Bootstrap";
Expand All @@ -42,6 +41,7 @@ public final class BuildInfoCollector extends Collector {

private final boolean isEnhancedForProxy;

@SneakyThrows(ClassNotFoundException.class)
@Override
public List<MetricFamilySamples> collect() {
Optional<GaugeMetricFamily> artifactInfo = FACTORY.createGaugeMetricFamily(MetricIds.BUILD_INFO);
Expand All @@ -50,11 +50,7 @@ public List<MetricFamilySamples> collect() {
}
addMetric(artifactInfo.get(), getClass().getPackage());
if (isEnhancedForProxy) {
try {
addMetric(artifactInfo.get(), Class.forName(PROXY_BOOTSTRAP_CLASS).getPackage());
} catch (final ClassNotFoundException ignored) {
log.warn("No proxy class found");
}
addMetric(artifactInfo.get(), Class.forName(PROXY_BOOTSTRAP_CLASS).getPackage());
}
return Collections.singletonList(artifactInfo.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.prometheus.client.GaugeMetricFamily;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.agent.metrics.core.constant.MetricIds;
import org.apache.shardingsphere.agent.metrics.core.util.MetricsUtil;
import org.apache.shardingsphere.agent.metrics.prometheus.wrapper.PrometheusWrapperFactory;
import org.apache.shardingsphere.infra.datasource.props.DataSourcePropertiesCreator;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
Expand All @@ -48,15 +47,13 @@ public final class MetaDataInfoCollector extends Collector {

private static final String ACTUAL_DB_COUNT = "database_count";

private static final String PROXY_CONTEXT_CLASS = "org.apache.shardingsphere.proxy.backend.context.ProxyContext";

private static final PrometheusWrapperFactory FACTORY = new PrometheusWrapperFactory();

@Override
public List<MetricFamilySamples> collect() {
List<MetricFamilySamples> result = new LinkedList<>();
Optional<GaugeMetricFamily> metaDataInfo = FACTORY.createGaugeMetricFamily(MetricIds.METADATA_INFO);
if (null != ProxyContext.getInstance().getContextManager() && metaDataInfo.isPresent() && MetricsUtil.isClassExisted(PROXY_CONTEXT_CLASS)) {
if (null != ProxyContext.getInstance().getContextManager() && metaDataInfo.isPresent()) {
collectProxy(metaDataInfo.get());
result.add(metaDataInfo.get());
}
Expand Down Expand Up @@ -88,7 +85,7 @@ private Collection<String> getDatabaseNames(final ShardingSphereDatabase databas
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");
log.info("Can not get JDBC URL.");
return Optional.empty();
}
try {
Expand All @@ -97,7 +94,7 @@ private Optional<String> getDatabaseName(final DataSource dataSource) {
return Optional.of(uri.getPath());
}
} catch (final URISyntaxException | NullPointerException ignored) {
log.info("Unsupported JDBC URL by URI: {}", jdbcUrl);
log.info("Unsupported JDBC URL by URI: {}.", jdbcUrl);
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.prometheus.client.Collector;
import io.prometheus.client.GaugeMetricFamily;
import org.apache.shardingsphere.agent.metrics.core.constant.MetricIds;
import org.apache.shardingsphere.agent.metrics.core.util.MetricsUtil;
import org.apache.shardingsphere.agent.metrics.prometheus.wrapper.PrometheusWrapperFactory;
import org.apache.shardingsphere.infra.state.StateContext;
import org.apache.shardingsphere.infra.state.StateType;
Expand All @@ -39,20 +38,18 @@ public final class ProxyInfoCollector extends Collector {

private static final String PROXY_STATE = "state";

private static final String PROXY_CLASS = "org.apache.shardingsphere.proxy.backend.context.ProxyContext";

private static final PrometheusWrapperFactory FACTORY = new PrometheusWrapperFactory();

private static final ConcurrentHashMap<StateType, Integer> PROXY_STATE_MAP = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<StateType, Integer> PROXY_STATES = new ConcurrentHashMap<>();

static {
PROXY_STATE_MAP.put(StateType.OK, 1);
PROXY_STATE_MAP.put(StateType.CIRCUIT_BREAK, 2);
PROXY_STATES.put(StateType.OK, 1);
PROXY_STATES.put(StateType.CIRCUIT_BREAK, 2);
}

@Override
public List<MetricFamilySamples> collect() {
if (!MetricsUtil.isClassExisted(PROXY_CLASS) || null == ProxyContext.getInstance().getContextManager()) {
if (null == ProxyContext.getInstance().getContextManager()) {
return Collections.emptyList();
}
Optional<GaugeMetricFamily> proxyInfo = FACTORY.createGaugeMetricFamily(MetricIds.PROXY_INFO);
Expand All @@ -61,7 +58,7 @@ public List<MetricFamilySamples> collect() {
return Collections.emptyList();
}
List<MetricFamilySamples> result = new LinkedList<>();
proxyInfo.get().addMetric(Collections.singletonList(PROXY_STATE), PROXY_STATE_MAP.get(stateContext.get().getCurrentState()));
proxyInfo.get().addMetric(Collections.singletonList(PROXY_STATE), PROXY_STATES.get(stateContext.get().getCurrentState()));
result.add(proxyInfo.get());
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class JaegerTracingPluginBootService implements PluginBootService {
@SuppressWarnings("AccessOfSystemProperties")
@Override
public void start(final PluginConfiguration pluginConfig, final boolean isEnhancedForProxy) {
RemotePluginConfigurationValidator.validate(getType(), pluginConfig);
RemotePluginConfigurationValidator.validateHostAndPort(getType(), pluginConfig);
pluginConfig.getProps().forEach((key, value) -> setSystemProperty(String.valueOf(key), String.valueOf(value)));
Configuration.SamplerConfiguration samplerConfig = Configuration.SamplerConfiguration.fromEnv();
Configuration.ReporterConfiguration reporterConfig = Configuration.ReporterConfiguration.fromEnv()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public final class ZipkinTracingPluginBootService implements PluginBootService {

@Override
public void start(final PluginConfiguration pluginConfig, final boolean isEnhancedForProxy) {
RemotePluginConfigurationValidator.validate(getType(), pluginConfig);
RemotePluginConfigurationValidator.validateHostAndPort(getType(), pluginConfig);
Properties props = pluginConfig.getProps();
String urlVersion = Optional.ofNullable(props.getProperty(KEY_URL_VERSION)).orElse(DEFAULT_URL_VERSION);
String serviceName = Optional.ofNullable(props.getProperty(KEY_SERVICE_NAME)).orElse(DEFAULT_SERVICE_NAME);
Expand Down