Skip to content

Commit

Permalink
feat(): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawven committed Oct 8, 2024
2 parents a4da395 + 8a09ec8 commit 1917cc2
Show file tree
Hide file tree
Showing 29 changed files with 586 additions and 249 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 @@ -37,17 +37,18 @@
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;

/**
* ConfigMonitorInitializer initializes the Apollo Config Monitor.
*/
public class ConfigMonitorInitializer {

protected static boolean hasInitialized = false;
private static ConfigUtil m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
private static ApolloClientMonitorContext monitorContext = ApolloInjector.getInstance(
private static final ApolloClientMonitorContext MONITOR_CONTEXT = ApolloInjector.getInstance(
ApolloClientMonitorContext.class);
protected static volatile boolean hasInitialized = false;
private static ConfigUtil m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);

public static void initialize() {
if (m_configUtil.isClientMonitorEnabled() && !hasInitialized) {
Expand All @@ -64,41 +65,49 @@ private static void doInit() {
initializeMetricsEventListener();
initializeMetricsExporter();
initializeJmxMonitoring();
hasInitialized = true;
}


private static void initializeJmxMonitoring() {
if (m_configUtil.isClientMonitorJmxEnabled()) {
monitorContext.getCollectors().forEach(metricsCollector ->
MONITOR_CONTEXT.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(m_configUtil);
DefaultApolloClientNamespaceApi defaultApolloClientNamespaceApi = new DefaultApolloClientNamespaceApi(
configManager);
DefaultApolloClientThreadPoolApi defaultApolloClientThreadPoolApi = new DefaultApolloClientThreadPoolApi(
RemoteConfigRepository.m_executorService,
AbstractConfig.m_executorService, AbstractConfigFile.m_executorService,
AbstractApolloClientMetricsExporter.m_executorService));
AbstractApolloClientMetricsExporter.m_executorService);

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

private static void initializeMetricsExporter(
) {
ApolloClientMetricsExporterFactory exporterFactory = ApolloInjector.getInstance(
ApolloClientMetricsExporterFactory.class);
ApolloClientMetricsExporter metricsReporter = exporterFactory.getMetricsReporter(
monitorContext.getCollectors());
if(metricsReporter != null) {
monitorContext.setApolloClientMetricsExporter(metricsReporter);
MONITOR_CONTEXT.getApolloClientMonitorEventListeners());
if (metricsReporter != null) {
MONITOR_CONTEXT.setApolloClientMetricsExporter(metricsReporter);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
* @author Rawven
Expand All @@ -35,11 +36,6 @@ public interface ApolloClientNamespaceMonitorApi {
*/
Integer getNamespacePropertySize(String namespace);

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

/**
* get not found namespaces
*/
Expand All @@ -53,7 +49,7 @@ public interface ApolloClientNamespaceMonitorApi {

class NamespaceMetrics {

private int usageCount;
private AtomicInteger usageCount = new AtomicInteger(0);
private long firstLoadTimeSpendInMs;
private LocalDateTime latestUpdateTime = LocalDateTime.now();
private String releaseKey = "";
Expand All @@ -67,11 +63,11 @@ public void setReleaseKey(String releaseKey) {
}

public int getUsageCount() {
return usageCount;
return usageCount.get();
}

public void incrementUsageCount() {
usageCount++;
usageCount.incrementAndGet();
}

public long getFirstLoadTimeSpendInMs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,11 @@
*/
package com.ctrip.framework.apollo.monitor.internal;

import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
* @author Rawven
*/
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 +52,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 All @@ -73,7 +64,6 @@ public class ApolloClientMonitorConstant {
*/
public static final String METRICS_NAMESPACE_LATEST_UPDATE_TIME = "apollo_client_namespace_latest_update_time";
public static final String METRICS_NAMESPACE_ITEM_NUM = "apollo_client_namespace_item_num";
public static final String METRICS_CONFIG_FILE_NUM = "apollo_client_config_file_num";
public static final String METRICS_EXCEPTION_NUM = "apollo_client_exception_num";
public static final String METRICS_NAMESPACE_FIRST_LOAD_SPEND = "apollo_client_namespace_first_load_time_spend_in_ms";
public static final String METRICS_NAMESPACE_USAGE = "apollo_client_namespace_usage";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.ctrip.framework.apollo.monitor.internal.listener.impl.NullClientNamespaceMonitorApi;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.NullClientThreadPoolMonitorApi;
import com.google.common.collect.Lists;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -40,6 +41,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 List<ApolloClientMonitorEventListener> getApolloClientMonitorEventListeners() {
return Collections.unmodifiableList(listener);
}

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

public void setApolloClientExceptionMonitorApi(
ApolloClientExceptionMonitorApi apolloClientExceptionMonitorApi) {
Expand All @@ -66,14 +81,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 @@ -30,7 +30,7 @@
*/
public class DefaultConfigMonitor implements ConfigMonitor {

private ApolloClientMonitorContext apolloClientMonitorContext = ApolloInjector.getInstance(
private final ApolloClientMonitorContext apolloClientMonitorContext = ApolloInjector.getInstance(
ApolloClientMonitorContext.class);

@Override
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(listener -> {
if (listener.isMetricsSampleUpdated()) {
log.debug("Listener {} has updated samples.", listener.getName());
listener.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,8 @@
*/
public interface ApolloClientMetricsExporterFactory {

ApolloClientMetricsExporter getMetricsReporter(List<ApolloClientMonitorEventListener> collectors);
/**
* get metrics reporter
*/
ApolloClientMetricsExporter getMetricsReporter(List<ApolloClientMonitorEventListener> listeners);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMonitorEventListener;
import com.ctrip.framework.apollo.monitor.internal.exporter.ApolloClientMetricsExporter;
Expand All @@ -40,27 +41,25 @@ public class DefaultApolloClientMetricsExporterFactory implements

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

List<ApolloClientMonitorEventListener> listeners) {
String externalSystemType = configUtil.getMonitorExternalType();
if (externalSystemType == null) {
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);
if (StringUtils.isEmpty(externalSystemType)) {
return null;
}
ApolloClientMetricsExporter reporter = exporters.stream()
.filter(metricsExporter -> metricsExporter.isSupport(externalSystemType))
.findFirst()
.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 @@ -30,10 +30,10 @@
public class NullApolloClientMetricsExporter implements ApolloClientMetricsExporter {

private static final Logger log = DeferredLoggerFactory.getLogger(
AbstractApolloClientMetricsExporter.class);
NullApolloClientMetricsExporter.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 @@ -27,7 +27,7 @@
public interface ApolloClientJmxNamespaceMBean {
// Because JMX does not support all type return values
// declare the interface separately.

/**
* NamespaceMetrics: 1.usageCount 2.firstLoadSpend 3.latestUpdateTime 4.releaseKey
*/
Expand All @@ -38,11 +38,6 @@ public interface ApolloClientJmxNamespaceMBean {
*/
Integer getNamespacePropertySize(String namespace);

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

/**
* get not found namespaces
*/
Expand Down
Loading

0 comments on commit 1917cc2

Please sign in to comment.