-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): Add more observability in apollo config client
- Loading branch information
Showing
64 changed files
with
3,762 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...o-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigMonitorInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2022 Apollo Authors | ||
* | ||
* Licensed 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 com.ctrip.framework.apollo.internals; | ||
|
||
import com.ctrip.framework.apollo.build.ApolloInjector; | ||
import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil; | ||
import com.ctrip.framework.apollo.monitor.api.ConfigMonitor; | ||
import com.ctrip.framework.apollo.monitor.internal.DefaultConfigMonitor; | ||
import com.ctrip.framework.apollo.monitor.internal.collector.MetricsCollector; | ||
import com.ctrip.framework.apollo.monitor.internal.collector.MetricsCollectorManager; | ||
import com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloExceptionCollector; | ||
import com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloNamespaceCollector; | ||
import com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloRunningParamsCollector; | ||
import com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloThreadPoolCollector; | ||
import com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultMetricsCollectorManager; | ||
import com.ctrip.framework.apollo.monitor.internal.exporter.MetricsExporter; | ||
import com.ctrip.framework.apollo.monitor.internal.exporter.MetricsExporterFactory; | ||
import com.ctrip.framework.apollo.monitor.internal.tracer.MessageProducerComposite; | ||
import com.ctrip.framework.apollo.monitor.internal.tracer.MonitorMessageProducer; | ||
import com.ctrip.framework.apollo.tracer.internals.NullMessageProducer; | ||
import com.ctrip.framework.apollo.tracer.internals.cat.CatMessageProducer; | ||
import com.ctrip.framework.apollo.tracer.internals.cat.CatNames; | ||
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; | ||
|
||
/** | ||
* @author Rawven | ||
*/ | ||
public class ConfigMonitorInitializer { | ||
|
||
private static ConfigUtil m_configUtil = ApolloInjector.getInstance(ConfigUtil.class); | ||
private static Boolean hasInitialized = false; | ||
|
||
public static void initialize() { | ||
if (m_configUtil.isClientMonitorEnabled() && !hasInitialized) { | ||
DefaultMetricsCollectorManager manager = initializeMetricsCollectorManager(); | ||
List<MetricsCollector> collectors = initializeCollectors(manager); | ||
MetricsExporter metricsExporter = initializeMetricsExporter(collectors); | ||
initializeConfigMonitor(collectors, metricsExporter); | ||
hasInitialized = true; | ||
} | ||
} | ||
|
||
private static DefaultMetricsCollectorManager initializeMetricsCollectorManager() { | ||
return (DefaultMetricsCollectorManager) ApolloInjector.getInstance(MetricsCollectorManager.class); | ||
} | ||
|
||
private static List<MetricsCollector> initializeCollectors(DefaultMetricsCollectorManager manager) { | ||
DefaultConfigManager configManager = (DefaultConfigManager) ApolloInjector.getInstance(ConfigManager.class); | ||
DefaultApolloExceptionCollector exceptionCollector = new DefaultApolloExceptionCollector(); | ||
DefaultApolloThreadPoolCollector threadPoolCollector = new DefaultApolloThreadPoolCollector( | ||
RemoteConfigRepository.m_executorService, AbstractConfig.m_executorService, AbstractConfigFile.m_executorService); | ||
DefaultApolloNamespaceCollector namespaceCollector = new DefaultApolloNamespaceCollector( | ||
configManager.m_configs, configManager.m_configLocks, configManager.m_configFiles, configManager.m_configFileLocks); | ||
DefaultApolloRunningParamsCollector startupCollector = new DefaultApolloRunningParamsCollector(m_configUtil); | ||
|
||
List<MetricsCollector> collectors = Lists.newArrayList(exceptionCollector, namespaceCollector, threadPoolCollector, startupCollector); | ||
manager.setCollectors(collectors); | ||
return collectors; | ||
} | ||
|
||
private static MetricsExporter initializeMetricsExporter(List<MetricsCollector> collectors) { | ||
MetricsExporterFactory reporterFactory = ApolloInjector.getInstance(MetricsExporterFactory.class); | ||
return reporterFactory.getMetricsReporter(collectors); | ||
} | ||
|
||
private static void initializeConfigMonitor(List<MetricsCollector> collectors, MetricsExporter metricsExporter) { | ||
DefaultConfigMonitor defaultConfigMonitor = (DefaultConfigMonitor) ApolloInjector.getInstance(ConfigMonitor.class); | ||
DefaultApolloExceptionCollector exceptionCollector = (DefaultApolloExceptionCollector) collectors.get(0); | ||
DefaultApolloNamespaceCollector namespaceCollector = (DefaultApolloNamespaceCollector) collectors.get(1); | ||
DefaultApolloThreadPoolCollector threadPoolCollector = (DefaultApolloThreadPoolCollector) collectors.get(2); | ||
DefaultApolloRunningParamsCollector startupCollector = (DefaultApolloRunningParamsCollector) collectors.get(3); | ||
defaultConfigMonitor.init(namespaceCollector, threadPoolCollector, exceptionCollector, startupCollector, metricsExporter); | ||
} | ||
|
||
public static MessageProducerComposite initializeMessageProducerComposite() { | ||
|
||
// Prioritize loading user-defined producers from SPI | ||
List<MessageProducer> producers = ServiceBootstrap.loadAllOrdered(MessageProducer.class); | ||
|
||
// The producer that comes with the client | ||
if (m_configUtil.isClientMonitorEnabled()) { | ||
producers.add(new MonitorMessageProducer()); | ||
} | ||
|
||
if (ClassLoaderUtil.isClassPresent(CatNames.CAT_CLASS)) { | ||
producers.add(new CatMessageProducer()); | ||
} | ||
|
||
// default logic | ||
if (producers.isEmpty()) { | ||
producers.add(new NullMessageProducer()); | ||
} | ||
return new MessageProducerComposite(producers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,35 +16,39 @@ | |
*/ | ||
package com.ctrip.framework.apollo.internals; | ||
|
||
import java.util.Map; | ||
import static com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloNamespaceCollector.NAMESPACE_MONITOR; | ||
import static com.ctrip.framework.apollo.monitor.internal.collector.internal.DefaultApolloNamespaceCollector.NAMESPACE_USAGE_COUNT; | ||
|
||
import com.ctrip.framework.apollo.Config; | ||
import com.ctrip.framework.apollo.ConfigFile; | ||
import com.ctrip.framework.apollo.build.ApolloInjector; | ||
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; | ||
import com.ctrip.framework.apollo.monitor.internal.MonitorConstant; | ||
import com.ctrip.framework.apollo.monitor.internal.model.MetricsEvent; | ||
import com.ctrip.framework.apollo.spi.ConfigFactory; | ||
import com.ctrip.framework.apollo.spi.ConfigFactoryManager; | ||
import com.google.common.collect.Maps; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Jason Song([email protected]) | ||
*/ | ||
public class DefaultConfigManager implements ConfigManager { | ||
private ConfigFactoryManager m_factoryManager; | ||
|
||
private Map<String, Config> m_configs = Maps.newConcurrentMap(); | ||
private Map<String, Object> m_configLocks = Maps.newConcurrentMap(); | ||
private Map<String, ConfigFile> m_configFiles = Maps.newConcurrentMap(); | ||
private Map<String, Object> m_configFileLocks = Maps.newConcurrentMap(); | ||
protected Map<String, Config> m_configs = Maps.newConcurrentMap(); | ||
protected Map<String, Object> m_configLocks = Maps.newConcurrentMap(); | ||
protected Map<String, ConfigFile> m_configFiles = Maps.newConcurrentMap(); | ||
protected Map<String, Object> m_configFileLocks = Maps.newConcurrentMap(); | ||
private ConfigFactoryManager m_factoryManager; | ||
|
||
public DefaultConfigManager() { | ||
m_factoryManager = ApolloInjector.getInstance(ConfigFactoryManager.class); | ||
|
||
} | ||
|
||
@Override | ||
public Config getConfig(String namespace) { | ||
Config config = m_configs.get(namespace); | ||
|
||
if (config == null) { | ||
Object lock = m_configLocks.computeIfAbsent(namespace, key -> new Object()); | ||
synchronized (lock) { | ||
|
@@ -59,11 +63,16 @@ public Config getConfig(String namespace) { | |
} | ||
} | ||
|
||
MetricsEvent.builder().withName(NAMESPACE_USAGE_COUNT) | ||
.putAttachment(MonitorConstant.NAMESPACE, namespace) | ||
.withTag(NAMESPACE_MONITOR).push(); | ||
|
||
return config; | ||
} | ||
|
||
@Override | ||
public ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat) { | ||
public ConfigFile getConfigFile(String namespace, | ||
ConfigFileFormat configFileFormat) { | ||
String namespaceFileName = String.format("%s.%s", namespace, configFileFormat.getValue()); | ||
ConfigFile configFile = m_configFiles.get(namespaceFileName); | ||
|
||
|
Oops, something went wrong.