Skip to content

Commit

Permalink
feat(): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawven committed Sep 21, 2024
2 parents a4da395 + 8a09ec8 commit 3245b61
Show file tree
Hide file tree
Showing 27 changed files with 558 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ public interface ConfigManager {
* @return the config file instance for the namespace
*/
ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat);


}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.ctrip.framework.apollo.monitor.internal.exporter.ApolloClientMetricsExporter;
import com.ctrip.framework.apollo.monitor.internal.jmx.ApolloClientJmxMBeanRegister;
import com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorContext;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMonitorEventListener;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientBootstrapArgsApi;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientExceptionApi;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientNamespaceApi;
Expand All @@ -37,6 +38,7 @@
import com.ctrip.framework.apollo.tracer.spi.MessageProducer;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.ctrip.framework.foundation.internals.ServiceBootstrap;
import com.google.common.collect.Lists;
import java.util.List;

/**
Expand Down Expand Up @@ -64,39 +66,45 @@ private static void doInit() {
initializeMetricsEventListener();
initializeMetricsExporter();
initializeJmxMonitoring();
hasInitialized = true;
}


private static void initializeJmxMonitoring() {
if (m_configUtil.isClientMonitorJmxEnabled()) {
monitorContext.getCollectors().forEach(metricsCollector ->
monitorContext.getApolloClientMonitorEventListeners().forEach(metricsListener ->
ApolloClientJmxMBeanRegister.register(
MBEAN_NAME + metricsCollector.getName(), metricsCollector)
MBEAN_NAME + metricsListener.getName(), metricsListener)
);
}
}

private static void initializeMetricsEventListener() {
DefaultConfigManager configManager = (DefaultConfigManager) ApolloInjector.getInstance(
ConfigManager configManager = ApolloInjector.getInstance(
ConfigManager.class);
monitorContext.setApolloClientBootstrapArgsMonitorApi(new DefaultApolloClientBootstrapArgsApi(
m_configUtil));
monitorContext.setApolloClientExceptionMonitorApi(new DefaultApolloClientExceptionApi());
monitorContext.setApolloClientNamespaceMonitorApi(new DefaultApolloClientNamespaceApi(
configManager.m_configs, configManager.m_configFiles));
monitorContext.setApolloClientThreadPoolMonitorApi(new DefaultApolloClientThreadPoolApi(
DefaultApolloClientBootstrapArgsApi defaultApolloClientBootstrapArgsApi = new DefaultApolloClientBootstrapArgsApi(
m_configUtil);
DefaultApolloClientExceptionApi defaultApolloClientExceptionApi = new DefaultApolloClientExceptionApi();
DefaultApolloClientNamespaceApi defaultApolloClientNamespaceApi = new DefaultApolloClientNamespaceApi(
configManager);
DefaultApolloClientThreadPoolApi defaultApolloClientThreadPoolApi = new DefaultApolloClientThreadPoolApi(
RemoteConfigRepository.m_executorService,
AbstractConfig.m_executorService, AbstractConfigFile.m_executorService,
AbstractApolloClientMetricsExporter.m_executorService));
AbstractApolloClientMetricsExporter.m_executorService);

monitorContext.setApolloClientBootstrapArgsMonitorApi(defaultApolloClientBootstrapArgsApi);
monitorContext.setApolloClientExceptionMonitorApi(defaultApolloClientExceptionApi);
monitorContext.setApolloClientNamespaceMonitorApi(defaultApolloClientNamespaceApi);
monitorContext.setApolloClientThreadPoolMonitorApi(defaultApolloClientThreadPoolApi);
monitorContext.setApolloClientMonitorEventListeners(Lists.newArrayList(defaultApolloClientBootstrapArgsApi,
defaultApolloClientNamespaceApi,defaultApolloClientThreadPoolApi,defaultApolloClientExceptionApi));
}

private static void initializeMetricsExporter(
) {
ApolloClientMetricsExporterFactory exporterFactory = ApolloInjector.getInstance(
ApolloClientMetricsExporterFactory.class);
ApolloClientMetricsExporter metricsReporter = exporterFactory.getMetricsReporter(
monitorContext.getCollectors());
monitorContext.getApolloClientMonitorEventListeners());
if(metricsReporter != null) {
monitorContext.setApolloClientMetricsExporter(metricsReporter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ public interface ApolloClientNamespaceMonitorApi {
*/
Integer getNamespacePropertySize(String namespace);

/**
* get ConfigFile namespaces
*/
List<String> getConfigFileNamespaces();

/**
* get not found namespaces
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
*/
public class ApolloClientMonitorConstant {

/**
* util
*/
public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(
"yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());

/**
* common
*/
Expand Down Expand Up @@ -61,7 +55,7 @@ public class ApolloClientMonitorConstant {
public static final String APOLLO_CLIENT_NAMESPACE_FIRST_LOAD_SPEND = "Apollo.Client.NamespaceFirstLoadSpendTime";

/**
* collector tag
* listener tag
*/
public static final String TAG_ERROR = "ErrorMonitor";
public static final String TAG_NAMESPACE = "NamespaceMonitor";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,20 @@ public class ApolloClientMonitorContext {
private ApolloClientBootstrapArgsMonitorApi apolloClientBootstrapArgsMonitorApi = new NullClientBootstrapArgsMonitorApi();
private ApolloClientThreadPoolMonitorApi apolloClientThreadPoolMonitorApi = new NullClientThreadPoolMonitorApi();
private ApolloClientMetricsExporter apolloClientMetricsExporter = new NullApolloClientMetricsExporter();

private List<ApolloClientMonitorEventListener> listener = Lists.newArrayList();

public void addApolloClientMonitorEventListener(ApolloClientMonitorEventListener listener){
this.listener.add(listener);
}

public void setApolloClientMonitorEventListeners(List<ApolloClientMonitorEventListener> listeners){
listener = listeners;
}

public List<ApolloClientMonitorEventListener> getApolloClientMonitorEventListeners(){
return listener;
}

public void setApolloClientExceptionMonitorApi(
ApolloClientExceptionMonitorApi apolloClientExceptionMonitorApi) {
this.apolloClientExceptionMonitorApi = apolloClientExceptionMonitorApi;
Expand All @@ -66,14 +79,6 @@ public void setApolloClientMetricsExporter(
this.apolloClientMetricsExporter = apolloClientMetricsExporter;
}

public List<ApolloClientMonitorEventListener> getCollectors() {
return Lists.newArrayList(
(ApolloClientMonitorEventListener) apolloClientBootstrapArgsMonitorApi,
(ApolloClientMonitorEventListener) apolloClientThreadPoolMonitorApi,
(ApolloClientMonitorEventListener) apolloClientExceptionMonitorApi,
(ApolloClientMonitorEventListener) apolloClientNamespaceMonitorApi);
}

public ApolloClientExceptionMonitorApi getExceptionApi() {
return apolloClientExceptionMonitorApi;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public class ApolloClientMonitorEventFactory {

public static volatile ApolloClientMonitorEventFactory INSTANCE;
private static volatile ApolloClientMonitorEventFactory INSTANCE;

private ApolloClientMonitorEventFactory() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@
*/
public class ApolloClientMonitorEventPublisher {

private static ApolloClientMonitorContext COLLECTOR_MANAGER = ApolloInjector.getInstance(
private static ApolloClientMonitorContext MONITOR_CONTEXT = ApolloInjector.getInstance(
ApolloClientMonitorContext.class);
private static ConfigUtil m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);

public static void publish(ApolloClientMonitorEvent event) {
if (m_configUtil.isClientMonitorEnabled()) {
for (ApolloClientMonitorEventListener collector : COLLECTOR_MANAGER.getCollectors()) {
if (collector.isSupported(event)) {
collector.collect(event);
for (ApolloClientMonitorEventListener listener : MONITOR_CONTEXT.getApolloClientMonitorEventListeners()) {
if (listener.isSupported(event)) {
listener.collect(event);
return;
}
}
}
}

protected static void reset() {
COLLECTOR_MANAGER = ApolloInjector.getInstance(
MONITOR_CONTEXT = ApolloInjector.getInstance(
ApolloClientMonitorContext.class);
m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public abstract class AbstractApolloClientMetricsExporter implements ApolloClien
ApolloThreadFactory.create(ApolloClientMetricsExporter.class.getName(), true));
}

protected List<ApolloClientMonitorEventListener> collectors;
protected List<ApolloClientMonitorEventListener> listeners;

@Override
public void init(List<ApolloClientMonitorEventListener> collectors, long collectPeriod) {
log.info("Initializing metrics exporter with {} collectors and collect period of {} seconds.",
collectors.size(), collectPeriod);
public void init(List<ApolloClientMonitorEventListener> listeners, long collectPeriod) {
log.info("Initializing metrics exporter with {} listeners and collect period of {} seconds.",
listeners.size(), collectPeriod);
doInit();
this.collectors = collectors;
this.listeners = listeners;
initScheduleMetricsCollectSync(collectPeriod);
log.info("Metrics collection scheduled with a period of {} seconds.", collectPeriod);
}
Expand All @@ -73,10 +73,10 @@ private void initScheduleMetricsCollectSync(long collectPeriod) {

protected void updateMetricsData() {
log.debug("Start to update metrics data job");
collectors.forEach(collector -> {
if (collector.isMetricsSampleUpdated()) {
log.debug("Collector {} has updated samples.", collector.getName());
collector.export().forEach(this::registerSample);
listeners.forEach(listeners -> {
if (listeners.isMetricsSampleUpdated()) {
log.debug("Listener {} has updated samples.", listeners.getName());
listeners.export().forEach(this::registerSample);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface ApolloClientMetricsExporter extends Ordered {
/**
* init method
*/
void init(List<ApolloClientMonitorEventListener> collectors, long collectPeriod);
void init(List<ApolloClientMonitorEventListener> listeners, long collectPeriod);

/**
* Used to access custom monitoring systems
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
*/
public interface ApolloClientMetricsExporterFactory {

ApolloClientMetricsExporter getMetricsReporter(List<ApolloClientMonitorEventListener> collectors);
ApolloClientMetricsExporter getMetricsReporter(List<ApolloClientMonitorEventListener> listeners);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ public class DefaultApolloClientMetricsExporterFactory implements

@Override
public ApolloClientMetricsExporter getMetricsReporter(
List<ApolloClientMonitorEventListener> collectors) {
List<ApolloClientMonitorEventListener> listeners) {

String externalSystemType = configUtil.getMonitorExternalType();
if (externalSystemType == null) {
if (externalSystemType == null || externalSystemType.equals("NONE")) {
return null;
}

return findAndInitializeExporter(collectors, externalSystemType);
return findAndInitializeExporter(listeners, externalSystemType);
}

private ApolloClientMetricsExporter findAndInitializeExporter(
List<ApolloClientMonitorEventListener> collectors, String externalSystemType) {
List<ApolloClientMonitorEventListener> listeners, String externalSystemType) {
List<ApolloClientMetricsExporter> exporters = ServiceBootstrap.loadAllOrdered(
ApolloClientMetricsExporter.class);
ApolloClientMetricsExporter reporter = exporters.stream()
Expand All @@ -60,7 +60,7 @@ private ApolloClientMetricsExporter findAndInitializeExporter(
.orElse(null);

if (reporter != null) {
reporter.init(collectors, configUtil.getMonitorExternalExportPeriod());
reporter.init(listeners, configUtil.getMonitorExternalExportPeriod());
} else {
String errorMessage =
"No matching exporter found with monitor-external-type " + externalSystemType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class NullApolloClientMetricsExporter implements ApolloClientMetricsExpor
AbstractApolloClientMetricsExporter.class);

@Override
public void init(List<ApolloClientMonitorEventListener> collectors, long collectPeriod) {
public void init(List<ApolloClientMonitorEventListener> listeners, long collectPeriod) {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ public interface ApolloClientJmxNamespaceMBean {
*/
Integer getNamespacePropertySize(String namespace);

/**
* get ConfigFile namespaces
*/
List<String> getConfigFileNamespaces();

/**
* get not found namespaces
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory;
import com.ctrip.framework.apollo.internals.ConfigManager;
import com.ctrip.framework.apollo.monitor.api.ApolloClientNamespaceMonitorApi;
import com.ctrip.framework.apollo.monitor.internal.jmx.mbean.ApolloClientJmxNamespaceMBean;
import com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorConstant;
Expand All @@ -46,18 +47,15 @@ public class DefaultApolloClientNamespaceApi extends

private static final Logger logger = DeferredLoggerFactory.getLogger(
DefaultApolloClientNamespaceApi.class);
private final Map<String, Config> m_configs;
private final Map<String, ConfigFile> m_configFiles;
private final ConfigManager configManager;
private final Map<String, NamespaceMetrics> namespaces = Maps.newConcurrentMap();
private final Set<String> namespace404 = Sets.newCopyOnWriteArraySet();
private final Set<String> namespaceTimeout = Sets.newCopyOnWriteArraySet();

public DefaultApolloClientNamespaceApi(Map<String, Config> m_configs,
Map<String, ConfigFile> m_configFiles
public DefaultApolloClientNamespaceApi(ConfigManager configManager
) {
super(TAG_NAMESPACE);
this.m_configs = m_configs;
this.m_configFiles = m_configFiles;
this.configManager = configManager;
}

@Override
Expand Down Expand Up @@ -152,13 +150,9 @@ public void export0() {
createOrUpdateGaugeSample(
METRICS_NAMESPACE_ITEM_NUM,
new String[]{NAMESPACE}, new String[]{namespace},
m_configs.get(namespace).getPropertyNames().size());
configManager.getConfig(namespace).getPropertyNames().size());
});

// update ConfigFile num
createOrUpdateGaugeSample(METRICS_CONFIG_FILE_NUM,
m_configFiles.size());

// update NamespaceStatus metrics
createOrUpdateGaugeSample(METRICS_NAMESPACE_NOT_FOUND,
namespace404.size());
Expand Down Expand Up @@ -198,13 +192,8 @@ public Map<String, NamespaceMetricsString> getNamespaceMetricsString() {

@Override
public Integer getNamespacePropertySize(String namespace) {
Config config = m_configs.get(namespace);
Config config = configManager.getConfig(namespace);
return (config != null) ? config.getPropertyNames().size() : 0;
}

@Override
public List<String> getConfigFileNamespaces() {
return new ArrayList<>(m_configFiles.keySet());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,4 @@ public Integer getNamespacePropertySize(String namespace) {
return 0;
}

@Override
public List<String> getConfigFileNamespaces() {
return Collections.emptyList();
}

}
Loading

0 comments on commit 3245b61

Please sign in to comment.